Skip to content

Commit

Permalink
fix windows related encoding problems (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingakbar authored Oct 3, 2021
1 parent db5db4b commit 7ef8652
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/drivers/fluid_wasapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ static void fluid_wasapi_register_callback(IMMDevice *dev, void *data)
int nsz;
char *name;

nsz = WideCharToMultiByte(CP_UTF8, 0, var.pwszVal, -1, 0, 0, 0, 0);
nsz = WideCharToMultiByte(CP_ACP, 0, var.pwszVal, -1, 0, 0, 0, 0);
name = FLUID_ARRAY(char, nsz + 1);
WideCharToMultiByte(CP_UTF8, 0, var.pwszVal, -1, name, nsz, 0, 0);
WideCharToMultiByte(CP_ACP, 0, var.pwszVal, -1, name, nsz, 0, 0);
fluid_settings_add_option(settings, "audio.wasapi.device", name);
FLUID_FREE(name);
}
Expand Down Expand Up @@ -827,9 +827,9 @@ static void fluid_wasapi_finddev_callback(IMMDevice *dev, void *data)
goto cleanup;
}

nsz = WideCharToMultiByte(CP_UTF8, 0, var.pwszVal, -1, 0, 0, 0, 0);
nsz = WideCharToMultiByte(CP_ACP, 0, var.pwszVal, -1, 0, 0, 0, 0);
name = FLUID_ARRAY(char, nsz + 1);
WideCharToMultiByte(CP_UTF8, 0, var.pwszVal, -1, name, nsz, 0, 0);
WideCharToMultiByte(CP_ACP, 0, var.pwszVal, -1, name, nsz, 0, 0);

if(!FLUID_STRCASECMP(name, d->name))
{
Expand Down
59 changes: 56 additions & 3 deletions src/fluidsynth.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,45 @@ print_pretty_int(int i)
}
}

#ifdef WIN32
/* Function using win32 api to convert ANSI encoding string to UTF8 encoding string */
static char*
win32_ansi_to_utf8(const char* ansi_null_terminated_string)
{
LPWSTR u16_buf = NULL;
char *u8_buf = NULL;
fluid_return_val_if_fail(ansi_null_terminated_string != NULL, NULL);
do
{
int u16_count, u8_byte_count;
u16_count = MultiByteToWideChar(CP_ACP, 0, ansi_null_terminated_string, -1, NULL, 0);
if (u16_count == 0)
{
fprintf(stderr, "Failed to convert ANSI string to wide char string\n");
break;
}
u16_buf = malloc(u16_count * sizeof(WCHAR));
if (u16_buf == NULL)
{
fprintf(stderr, "Out of memory\n");
break;
}
u16_count = MultiByteToWideChar(CP_ACP, 0, ansi_null_terminated_string, -1, u16_buf, u16_count);
u8_byte_count = WideCharToMultiByte(CP_UTF8, 0, u16_buf, u16_count, NULL, 0, NULL, NULL);

u8_buf = malloc(u8_byte_count);
if (u8_buf == NULL)
{
fprintf(stderr, "Out of memory\n");
break;
}
WideCharToMultiByte(CP_UTF8, 0, u16_buf, u16_count, u8_buf, u8_byte_count, NULL, NULL);
} while (0);
free(u16_buf);
return u8_buf;
}
#endif

typedef struct
{
int count; /* Total count of options */
Expand Down Expand Up @@ -896,14 +935,25 @@ int main(int argc, char **argv)
/* load the soundfonts (check that all non options are SoundFont or MIDI files) */
for(i = arg1; i < argc; i++)
{
if(fluid_is_midifile(argv[i]))
const char *u8_path = argv[i];
#if defined(WIN32)
/* try to convert ANSI encoding path to UTF8 encoding path */
char *u8_buf = win32_ansi_to_utf8(argv[i]);
if (u8_buf == NULL)
{
// error msg. already printed
goto cleanup;
}
u8_path = u8_buf;
#endif
if(fluid_is_midifile(u8_path))
{
continue;
}

if(fluid_is_soundfont(argv[i]))
if(fluid_is_soundfont(u8_path))
{
if(fluid_synth_sfload(synth, argv[i], 1) == -1)
if(fluid_synth_sfload(synth, u8_path, 1) == -1)
{
fprintf(stderr, "Failed to load the SoundFont %s\n", argv[i]);
}
Expand All @@ -912,6 +962,9 @@ int main(int argc, char **argv)
{
fprintf(stderr, "Parameter '%s' not a SoundFont or MIDI file or error occurred identifying it.\n", argv[i]);
}
#if defined(WIN32)
free(u8_buf);
#endif
}

/* Try to load the default soundfont, if no soundfont specified */
Expand Down

0 comments on commit 7ef8652

Please sign in to comment.