Skip to content

Commit bd31a85

Browse files
authored
Merge pull request #1756 from FluidSynth/investigate-ftell-fseek
Fix SoundFonts failing to load with old MinGW versions
2 parents 06fb3bd + 7ca4bc7 commit bd31a85

15 files changed

+480
-65
lines changed

.azure/azure-pipelines-alpine.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ trigger:
1010
- '.azure/azure-pipelines-vcpkg.yml'
1111
- '.azure/azure-pipelines-win.yml'
1212
- '.circleci/config.yml'
13-
- '.github/workflows/linux.yml'
14-
- '.github/workflows/sonarcloud.yml'
13+
- '.github/workflows/*'
1514
- '.cirrus.yml'
1615
- 'README.md'
1716

.azure/azure-pipelines-android.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ trigger:
1010
- '.azure/azure-pipelines-win.yml'
1111
- '.azure/Dockerfile.azure-alpine'
1212
- '.circleci/config.yml'
13-
- '.github/workflows/linux.yml'
14-
- '.github/workflows/sonarcloud.yml'
13+
- '.github/workflows/*'
1514
- '.cirrus.yml'
1615
- 'README.md'
1716

.azure/azure-pipelines-mac.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ trigger:
1111
- '.azure/azure-pipelines-win.yml'
1212
- '.azure/Dockerfile.azure-alpine'
1313
- '.circleci/config.yml'
14-
- '.github/workflows/linux.yml'
15-
- '.github/workflows/sonarcloud.yml'
14+
- '.github/workflows/*'
1615
- '.cirrus.yml'
1716
- 'README.md'
1817

.azure/azure-pipelines-vcpkg.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ trigger:
1111
- '.azure/azure-pipelines-win.yml'
1212
- '.azure/Dockerfile.azure-alpine'
1313
- '.circleci/config.yml'
14-
- '.github/workflows/linux.yml'
15-
- '.github/workflows/sonarcloud.yml'
14+
- '.github/workflows/*'
1615
- '.cirrus.yml'
1716
- 'README.md'
1817

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Ensure binary files are not modified by line ending conversion
2+
*.sf2 binary
3+
*.sf3 binary
4+
*.dls binary
5+
*.mid binary
6+
*.midi binary
7+
*.wav binary

.github/workflows/windows.yml

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ env:
2424
LIBSNDFILE_URL_X86: 'https://github.com/libsndfile/libsndfile/releases/download/1.2.2/libsndfile-1.2.2-win32.zip'
2525
LIBSNDFILE_URL_X64: 'https://github.com/libsndfile/libsndfile/releases/download/1.2.2/libsndfile-1.2.2-win64.zip'
2626
MINGW_URL_X64: 'https://sourceforge.net/projects/mingw-w64/files/Toolchains%%20targetting%%20Win64/Personal%%20Builds/mingw-builds/8.1.0/threads-win32/sjlj/x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0.7z'
27+
MINGW_URL_X86: 'https://sourceforge.net/projects/mingw-w64/files/Toolchains%%20targetting%%20Win32/Personal%%20Builds/mingw-builds/8.1.0/threads-win32/sjlj/i686-8.1.0-release-win32-sjlj-rt_v6-rev0.7z'
2728
SDL3_URL: 'https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-VC.zip'
2829

2930
jobs:
@@ -118,7 +119,7 @@ jobs:
118119
mkdir build && cd build || exit /b 1
119120
cmake -Werror=dev -A ${{ matrix.platform }} -T v143 ^
120121
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}\${{ env.INSTALL_LOCATION }} ^
121-
-Denable-readline=0 -Denable-floats=1 -Denable-jack=0 -Denable-sdl2=0 ^
122+
-Denable-readline=0 -Denable-floats=1 -Denable-jack=0 -Denable-sdl3=0 ^
122123
-Dosal=${{ matrix.osal }} -Denable-libinstpatch=0 -Denable-dls=1 ^
123124
${{ matrix.CMAKEFLAGS }} -DCMAKE_BUILD_TYPE=Release ^
124125
-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded ^
@@ -269,7 +270,7 @@ jobs:
269270
-DCMAKE_VERBOSE_MAKEFILE=1 ${{ matrix.CMAKE_FLAGS }} ^
270271
-DNO_GUI=1 -Dwindows-version=0x0A00 -Denable-jack=0 ^
271272
-Denable-pulseaudio=0 -Denable-ladspa=0 -Denable-dbus=0 ^
272-
-Denable-readline=0 -Denable-sdl2=0 -Denable-libinstpatch=0 .. || exit /b 1
273+
-Denable-readline=0 -Denable-sdl3=0 -Denable-libinstpatch=0 .. || exit /b 1
273274
274275
- name: Build
275276
shell: cmd
@@ -302,12 +303,32 @@ jobs:
302303
fail-fast: false
303304
matrix:
304305
include:
305-
- CMAKE_FLAGS: ""
306+
- CMAKE_FLAGS: '-Denable-sdl3=0 -Denable-openmp=0 -Denable-libsndfile=0'
307+
artifactPrefix: "fluidsynth-mingw1"
308+
platform: x86
309+
sdl3-url: 'https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz'
310+
sdl3-subdir: 'i686-w64-mingw32'
311+
- CMAKE_FLAGS: '-Denable-sdl3=0 -Denable-openmp=0 -Denable-libsndfile=0 -DCMAKE_C_FLAGS="-mfpmath=387" -DCMAKE_CXX_FLAGS="-mfpmath=387"'
312+
artifactPrefix: "fluidsynth-mingw2"
313+
platform: x86
314+
sdl3-url: 'https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz'
315+
sdl3-subdir: 'i686-w64-mingw32'
316+
- CMAKE_FLAGS: '-Denable-sdl3=0 -Denable-openmp=0 -Denable-libsndfile=0 -DCMAKE_RC_FLAGS="-F pe-i386" -DCMAKE_C_FLAGS="-m32 -mfpmath=387" -DCMAKE_CXX_FLAGS="-m32 -mfpmath=387"'
317+
artifactPrefix: "fluidsynth-mingw"
318+
platform: x86
319+
sdl3-url: 'https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz'
320+
sdl3-subdir: 'i686-w64-mingw32'
321+
- CMAKE_FLAGS: '-Denable-sdl3=0 -Denable-openmp=0 -Denable-libsndfile=0 -DCMAKE_RC_FLAGS="-F pe-i386" -DCMAKE_C_FLAGS="-m32 -mfpmath=387 -ffloat-store" -DCMAKE_CXX_FLAGS="-m32 -mfpmath=387 -ffloat-store"'
322+
artifactPrefix: "fluidsynth-mingw-ffloat-store"
323+
platform: x86
324+
sdl3-url: 'https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz'
325+
sdl3-subdir: 'i686-w64-mingw32'
326+
- CMAKE_FLAGS: "-Denable-openmp=0"
306327
artifactPrefix: "fluidsynth-mingw"
307328
platform: x64
308329
sdl3-url: 'https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz'
309330
sdl3-subdir: 'x86_64-w64-mingw32'
310-
- CMAKE_FLAGS: '-DBUILD_SHARED_LIBS=0'
331+
- CMAKE_FLAGS: '-DBUILD_SHARED_LIBS=0 -Denable-openmp=0'
311332
artifactPrefix: "fluidsynth-mingw-static"
312333
platform: x64
313334
sdl3-url: 'https://github.com/libsdl-org/SDL/releases/download/release-3.2.10/SDL3-devel-3.2.10-mingw.tar.gz'
@@ -327,16 +348,25 @@ jobs:
327348
@ECHO ON
328349
mkdir d:\deps || exit /b 1
329350
cd d:\deps || exit /b 1
330-
if not "${{ env.GTK_BUNDLE_X64 }}"=="" (
331-
curl -LfsS -o gtk-bundle-dev.zip "${{ env.GTK_BUNDLE_X64 }}" || exit /b 1
351+
if "${{ matrix.platform }}"=="x86" (
352+
set GTK_URL=${{ env.GTK_BUNDLE_X86 }}
353+
set LIBSNDFILE_URL=${{ env.LIBSNDFILE_URL_X86 }}
354+
set MINGW_URL=${{ env.MINGW_URL_X86 }}
355+
) else (
356+
set GTK_URL=${{ env.GTK_BUNDLE_X64 }}
357+
set LIBSNDFILE_URL=${{ env.LIBSNDFILE_URL_X64 }}
358+
set MINGW_URL=${{ env.MINGW_URL_X64 }}
359+
)
360+
if not "%GTK_URL%"=="" (
361+
curl -LfsS -o gtk-bundle-dev.zip "%GTK_URL%" || exit /b 1
332362
7z x -aos -- gtk-bundle-dev.zip > NUL || exit /b 1
333363
)
334-
if not "${{ env.LIBSNDFILE_URL_X64 }}"=="" (
335-
curl -LfsS -o libsndfile-dev.zip "${{ env.LIBSNDFILE_URL_X64 }}" || exit /b 1
364+
if not "%LIBSNDFILE_URL%"=="" (
365+
curl -LfsS -o libsndfile-dev.zip "%LIBSNDFILE_URL%" || exit /b 1
336366
7z x -aos -- libsndfile-dev.zip > NUL || exit /b 1
337367
)
338-
if not "${{ env.MINGW_URL_X64 }}"=="" (
339-
curl -LfsS -o mingw.zip "${{ env.MINGW_URL_X64 }}" || exit /b 1
368+
if not "%MINGW_URL%"=="" (
369+
curl -LfsS -o mingw.zip "%MINGW_URL%" || exit /b 1
340370
7z x -aos -- mingw.zip > NUL || exit /b 1
341371
)
342372
curl -LfsS -o sdl3.tar.gz "${{ matrix.sdl3-url }}" || exit /b 1
@@ -376,7 +406,7 @@ jobs:
376406
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}\${{ env.INSTALL_LOCATION }} ^
377407
${{ matrix.CMAKE_FLAGS }} -Denable-readline=0 -Denable-floats=1 ^
378408
-Denable-jack=0 -Denable-pulseaudio=0 -Denable-ladspa=0 ^
379-
-Denable-dbus=0 -Denable-sdl2=0 -DCMAKE_BUILD_TYPE=Release ^
409+
-Denable-dbus=0 -DCMAKE_BUILD_TYPE=Release ^
380410
-DCMAKE_VERBOSE_MAKEFILE=1 -DNO_GUI=1 .. || exit /b 1
381411
382412
- name: Build

src/midi/fluid_midi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ fluid_file_read_full(fluid_file fp, size_t *length)
165165
size_t n;
166166

167167
/* Work out the length of the file in advance */
168-
if(FLUID_FSEEK(fp, 0, SEEK_END) != 0)
168+
if(fluid_file_seek(fp, 0, SEEK_END) != 0)
169169
{
170170
FLUID_LOG(FLUID_ERR, "File load: Could not seek within file");
171171
return NULL;
172172
}
173173

174174
buflen = ftell(fp);
175175

176-
if(FLUID_FSEEK(fp, 0, SEEK_SET) != 0)
176+
if(fluid_file_seek(fp, 0, SEEK_SET) != 0)
177177
{
178178
FLUID_LOG(FLUID_ERR, "File load: Could not seek within file");
179179
return NULL;

src/sfloader/fluid_sffile.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,11 @@ static const unsigned short invalid_preset_gen[] =
158158
#define READCHUNK(sf, var) \
159159
do \
160160
{ \
161-
if (sf->fcbs->fread(var, 8, sf->sffd) == FLUID_FAILED) \
161+
if (sf->fcbs->fread(&(var)->id, 4, sf->sffd) == FLUID_FAILED) \
162162
return FALSE; \
163-
((SFChunk *)(var))->size = FLUID_LE32TOH(((SFChunk *)(var))->size); \
163+
if (sf->fcbs->fread(&(var)->size, 4, sf->sffd) == FLUID_FAILED) \
164+
return FALSE; \
165+
(var)->size = FLUID_LE32TOH((var)->size); \
164166
} while (0)
165167

166168
#define READD(sf, var) \
@@ -291,7 +293,7 @@ int fluid_is_soundfont(const char *filename)
291293
break;
292294
}
293295

294-
if(FLUID_FSEEK(fp, 4, SEEK_CUR))
296+
if(fluid_file_seek(fp, 4, SEEK_CUR))
295297
{
296298
FLUID_LOG(FLUID_ERR, "fluid_is_soundfont(): cannot seek +4 bytes.");
297299
break;
@@ -663,7 +665,8 @@ static int read_listchunk(SFData *sf, SFChunk *chunk)
663665

664666
if(chunk->id != LIST_FCC) /* error if ! list chunk */
665667
{
666-
FLUID_LOG(FLUID_ERR, "Invalid chunk id in level 0 parse");
668+
unsigned char *p = (unsigned char *)&chunk->id;
669+
FLUID_LOG(FLUID_ERR, "Invalid chunk id '0x%X 0x%X 0x%X 0x%X' (%d bytes) in level 0 parse", (int)p[0], (int)p[1], (int)p[2], (int)p[3], chunk->size);
667670
return FALSE;
668671
}
669672

@@ -2447,8 +2450,6 @@ static int fluid_sffile_read_wav(SFData *sf, unsigned int start, unsigned int en
24472450
goto error_exit_unlock;
24482451
}
24492452

2450-
FLUID_LOG(FLUID_DBG, "ftell(): %llu, fread(): %ld bytes", sf->fcbs->ftell(sf->sffd), num_samples * sizeof(short));
2451-
24522453
if(sf->fcbs->fread(loaded_data, num_samples * sizeof(short), sf->sffd) == FLUID_FAILED)
24532454
{
24542455
FLUID_LOG(FLUID_ERR, "Failed to read sample data");

src/sfloader/fluid_sfont.c

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,16 @@ fluid_long_long_t default_ftell(void *handle)
4545
return FLUID_FTELL((FILE *)handle);
4646
}
4747

48-
#ifdef _WIN32
49-
#define FLUID_PRIi64 "I64d"
50-
#else
51-
#define FLUID_PRIi64 "lld"
52-
#endif
53-
54-
int safe_fread(void *buf, fluid_long_long_t count, void *fd)
48+
int safe_fread(void *buf, fluid_long_long_t count, void *handle)
5549
{
56-
if(FLUID_FREAD(buf, (size_t)count, 1, (FILE *)fd) != 1)
57-
{
58-
if(feof((FILE *)fd))
59-
{
60-
FLUID_LOG(FLUID_ERR, "EOF while attempting to read %" FLUID_PRIi64 " bytes", count);
61-
}
62-
else
63-
{
64-
FLUID_LOG(FLUID_ERR, "File read failed");
65-
}
66-
67-
return FLUID_FAILED;
68-
}
69-
70-
return FLUID_OK;
50+
return fluid_file_read(buf, count, handle);
7151
}
7252

73-
int safe_fseek(void *fd, fluid_long_long_t ofs, int whence)
53+
int safe_fseek(void *handle, fluid_long_long_t ofs, int whence)
7454
{
75-
if(FLUID_FSEEK((FILE *)fd, ofs, whence) != 0)
76-
{
77-
FLUID_LOG(FLUID_ERR, "File seek failed with offset = %" FLUID_PRIi64 " and whence = %d", ofs, whence);
78-
return FLUID_FAILED;
79-
}
80-
81-
return FLUID_OK;
55+
return fluid_file_seek(handle, ofs, whence);
8256
}
8357

84-
#undef FLUID_PRIi64
85-
8658
/**
8759
* Creates a new SoundFont loader.
8860
*

src/utils/fluid_sys.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,64 @@ fluid_long_long_t fluid_file_tell(FILE* f)
16411641
#endif
16421642
}
16431643

1644+
#ifdef _WIN32
1645+
#define FLUID_PRIi64 "I64d"
1646+
#else
1647+
#define FLUID_PRIi64 "lld"
1648+
#endif
1649+
1650+
int fluid_file_read(void *buf, fluid_long_long_t count, FILE *fd)
1651+
{
1652+
if(FLUID_FREAD(buf, (size_t)count, 1, (FILE *)fd) != 1)
1653+
{
1654+
if(feof((FILE *)fd))
1655+
{
1656+
FLUID_LOG(FLUID_ERR, "EOF while attempting to read %" FLUID_PRIi64 " bytes", count);
1657+
}
1658+
else
1659+
{
1660+
FLUID_LOG(FLUID_ERR, "File read failed");
1661+
}
1662+
1663+
return FLUID_FAILED;
1664+
}
1665+
1666+
return FLUID_OK;
1667+
}
1668+
1669+
int fluid_file_seek(FILE *fd, fluid_long_long_t ofs, int whence)
1670+
{
1671+
#if (defined(__MINGW32__) || defined(__MINGW64__)) && defined(__GNUC__) && (__GNUC__ < 15)
1672+
// Some older versions of MinGW report incorrect values for _ftelli64(). This is problematic,
1673+
// because _fseeki64() below would use these incorrect values when seeking with SEEK_CUR,
1674+
// resulting in incorrect file positions. So we need to work around this by doing the SEEK_CUR
1675+
// calculation ourselves.
1676+
// See https://sourceforge.net/p/mingw-w64/bugs/4897/ for more details.
1677+
if(whence == SEEK_CUR)
1678+
{
1679+
whence = SEEK_SET;
1680+
ofs += fluid_file_tell((FILE *)fd);
1681+
}
1682+
#endif
1683+
1684+
#ifdef _WIN32
1685+
#define FLUID_FSEEK(_f,_n,_set) _fseeki64(_f,_n,_set)
1686+
#else
1687+
#define FLUID_FSEEK(_f,_n,_set) fseek(_f,_n,_set)
1688+
#endif
1689+
1690+
if(FLUID_FSEEK((FILE *)fd, ofs, whence) != 0)
1691+
{
1692+
FLUID_LOG(FLUID_ERR, "File seek failed with offset = %" FLUID_PRIi64 " and whence = %d", ofs, whence);
1693+
return FLUID_FAILED;
1694+
}
1695+
#undef FLUID_FSEEK
1696+
1697+
return FLUID_OK;
1698+
}
1699+
1700+
#undef FLUID_PRIi64
1701+
16441702
#if defined(_WIN32) || defined(__CYGWIN__)
16451703
// not thread-safe!
16461704
#define FLUID_WINDOWS_MEX_ERROR_LEN 1024

0 commit comments

Comments
 (0)