From 8ed168b7d07cb9596ba65ad6dd95988d3f1cc95a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez-Cabanillas?= Date: Sun, 3 Nov 2024 14:10:11 +0100 Subject: [PATCH] workaround for libsndfile not handling utf8 file names on Windows --- src/bindings/fluid_filerenderer.c | 38 +++++++++++++++++++++++++++++-- test/test_fast_render.c | 2 -- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/bindings/fluid_filerenderer.c b/src/bindings/fluid_filerenderer.c index 401daadb4..d17373996 100644 --- a/src/bindings/fluid_filerenderer.c +++ b/src/bindings/fluid_filerenderer.c @@ -288,9 +288,43 @@ new_fluid_file_renderer(fluid_synth_t *synth) FLUID_LOG(FLUID_ERR, "Invalid or unsupported audio file format settings"); goto error_recovery; } - + +#ifdef _WIN32 + if (0 == FLUID_STRCMP("-", filename)) + { + dev->sndfile = sf_open(filename, SFM_WRITE, &info); + } + else + { + int u16_count; + LPWSTR wc_filename; + dev->sndfile = NULL; + + // utf-8 filename to utf-16 wc_filename + if (1 > (u16_count = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, NULL, 0))) + { + FLUID_LOG(FLUID_ERR, "Failed to convert UTF8 string to wide char string"); + } + else if (NULL == (wc_filename = (LPWSTR)FLUID_ARRAY(WCHAR, u16_count))) + { + FLUID_LOG(FLUID_ERR, "Out of memory"); + } + else + { + if (u16_count != MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wc_filename, u16_count)) + { + FLUID_LOG(FLUID_ERR, "Failed to convert UTF8 string to wide char string"); + } + else + { + dev->sndfile = sf_wchar_open(wc_filename, SFM_WRITE, &info); + } + FLUID_FREE(wc_filename); + } + } +#else dev->sndfile = sf_open(filename, SFM_WRITE, &info); - +#endif if(!dev->sndfile) { FLUID_LOG(FLUID_ERR, "Failed to open audio file '%s' for writing", filename); diff --git a/test/test_fast_render.c b/test/test_fast_render.c index c6b0ecaca..9772f12f0 100644 --- a/test/test_fast_render.c +++ b/test/test_fast_render.c @@ -51,12 +51,10 @@ int main(void) delete_fluid_synth(synth); delete_fluid_settings(settings); -#if 0 FILE *file; file = FLUID_FOPEN(TEST_WAV_UTF8, "rb"); TEST_ASSERT(file != NULL); TEST_ASSERT(FLUID_FCLOSE(file) == 0); -#endif return EXIT_SUCCESS; }