Skip to content

Commit d032535

Browse files
committed
fluidsynth: Allow app to set custom FluidSynth settings per MIX_Audio object.
Reference Issue #284.
1 parent 4dd0ea7 commit d032535

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/SDL_mixer_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ struct MIX_Mixer
221221
#define MIX_PROP_DECODER_WAVPACK_WVC_PATH_STRING "SDL_mixer.decoder.wavpack.wvc_path"
222222
#define MIX_PROP_DECODER_FLUIDSYNTH_SOUNDFONT_IOSTREAM_POINTER "SDL_mixer.decoder.fluidsynth.soundfont_iostream"
223223
#define MIX_PROP_DECODER_FLUIDSYNTH_SOUNDFONT_PATH_STRING "SDL_mixer.decoder.fluidsynth.soundfont_path"
224+
#define MIX_PROP_DECODER_FLUIDSYNTH_PROPS_NUMBER "SDL_mixer.decoder.fluidsynth.props"
224225
#define MIX_PROP_AUDIO_LOAD_PATH_STRING "SDL_mixer.audio.load.path"
225226
#define MIX_PROP_AUDIO_LOAD_ONDEMAND_BOOLEAN "SDL_mixer.audio.load.ondemand"
226227

src/decoder_fluidsynth.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ typedef struct FLUIDSYNTH_AudioData
7272
{
7373
const Uint8 *sfdata;
7474
size_t sfdatalen;
75+
SDL_PropertiesID fluidsynth_props;
7576
} FLUIDSYNTH_AudioData;
7677

7778
typedef struct FLUIDSYNTH_TrackData
@@ -170,6 +171,19 @@ static bool SDLCALL FLUIDSYNTH_init_audio(SDL_IOStream *io, SDL_AudioSpec *spec,
170171
*duration_frames = -1; // !!! FIXME: fluid_player_get_total_ticks can give us a time duration, but we don't have a player until we set up the track later.
171172
*audio_userdata = adata;
172173

174+
const SDL_PropertiesID fluidsynth_props = (SDL_PropertiesID) SDL_GetNumberProperty(props, MIX_PROP_DECODER_FLUIDSYNTH_PROPS_NUMBER, 0);
175+
if (fluidsynth_props) {
176+
adata->fluidsynth_props = SDL_CreateProperties();
177+
if (!adata->fluidsynth_props || !SDL_CopyProperties(fluidsynth_props, adata->fluidsynth_props)) {
178+
if (adata->fluidsynth_props) {
179+
SDL_DestroyProperties(adata->fluidsynth_props);
180+
}
181+
SDL_free(sfdata);
182+
SDL_free(adata);
183+
return false;
184+
}
185+
}
186+
173187
return true;
174188
}
175189

@@ -211,6 +225,19 @@ static fluid_long_long_t SoundFontTell(void *handle)
211225
return SDL_TellIO((SDL_IOStream *) handle);
212226
}
213227

228+
static void SDLCALL SetCustomFluidsynthProperties(void *userdata, SDL_PropertiesID props, const char *name)
229+
{
230+
FLUIDSYNTH_TrackData *tdata = (FLUIDSYNTH_TrackData *) userdata;
231+
switch (SDL_GetPropertyType(props, name)) {
232+
case SDL_PROPERTY_TYPE_NUMBER:
233+
fluidsynth.fluid_settings_setint(tdata->settings, name, (int) SDL_GetNumberProperty(props, name, 0));
234+
break;
235+
case SDL_PROPERTY_TYPE_FLOAT:
236+
fluidsynth.fluid_settings_setnum(tdata->settings, name, (double) SDL_GetFloatProperty(props, name, 0.0f));
237+
break;
238+
default: break; // oh well.
239+
}
240+
}
214241

215242
static bool SDLCALL FLUIDSYNTH_init_track(void *audio_userdata, SDL_IOStream *io, const SDL_AudioSpec *spec, SDL_PropertiesID props, void **track_userdata)
216243
{
@@ -236,6 +263,9 @@ static bool SDLCALL FLUIDSYNTH_init_track(void *audio_userdata, SDL_IOStream *io
236263
fluidsynth.fluid_settings_setnum(tdata->settings, "synth.sample-rate", (double) spec->freq);
237264
fluidsynth.fluid_settings_getnum(tdata->settings, "synth.sample-rate", &samplerate);
238265

266+
// let custom properties override anything we already set internally. You break it, you buy it!
267+
SDL_EnumerateProperties(adata->fluidsynth_props, SetCustomFluidsynthProperties, tdata);
268+
239269
tdata->synth = fluidsynth.new_fluid_synth(tdata->settings);
240270
if (!tdata->synth) {
241271
SDL_SetError("Failed to create FluidSynth synthesizer");
@@ -361,6 +391,7 @@ static void SDLCALL FLUIDSYNTH_quit_track(void *track_userdata)
361391
static void SDLCALL FLUIDSYNTH_quit_audio(void *audio_userdata)
362392
{
363393
FLUIDSYNTH_AudioData *adata = (FLUIDSYNTH_AudioData *) audio_userdata;
394+
SDL_DestroyProperties(adata->fluidsynth_props);
364395
SDL_free((void *) adata->sfdata);
365396
SDL_free(adata);
366397
}

0 commit comments

Comments
 (0)