From 5cd03e0171dc1d336fed75b9851eedb379b7691f Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Thu, 30 Nov 2017 19:34:48 +0100 Subject: [PATCH 01/11] Add dedicated functions for registering update callbacks on settings This avoids having to duplicate the default, min, max and hint values when setting a callback function for an existing setting. --- src/synth/fluid_synth.c | 15 ++---- src/utils/fluid_settings.c | 108 +++++++++++++++++++++++++++++++++++++ src/utils/fluid_settings.h | 6 +++ 3 files changed, 119 insertions(+), 10 deletions(-) diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index 104df082..dbca05d6 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -612,18 +612,13 @@ new_fluid_synth(fluid_settings_t *settings) fluid_settings_getint(settings, "synth.cpu-cores", &synth->cores); /* register the callbacks */ - fluid_settings_register_num(settings, "synth.sample-rate", - 44100.0f, 8000.0f, 96000.0f, 0, + fluid_settings_callback_num(settings, "synth.sample-rate", (fluid_num_update_t) fluid_synth_update_sample_rate, synth); - fluid_settings_register_num(settings, "synth.gain", - 0.2f, 0.0f, 10.0f, 0, + fluid_settings_callback_num(settings, "synth.gain", (fluid_num_update_t) fluid_synth_update_gain, synth); - fluid_settings_register_int(settings, "synth.polyphony", - synth->polyphony, 1, 65535, 0, - (fluid_int_update_t) fluid_synth_update_polyphony, - synth); - fluid_settings_register_int(settings, "synth.device-id", - synth->device_id, 126, 0, 0, + fluid_settings_callback_int(settings, "synth.polyphony", + (fluid_int_update_t) fluid_synth_update_polyphony, synth); + fluid_settings_callback_int(settings, "synth.device-id", (fluid_int_update_t) fluid_synth_update_device_id, synth); fluid_synth_register_overflow(settings, diff --git a/src/utils/fluid_settings.c b/src/utils/fluid_settings.c index a9c320ed..d77a33af 100644 --- a/src/utils/fluid_settings.c +++ b/src/utils/fluid_settings.c @@ -625,6 +625,114 @@ fluid_settings_register_int(fluid_settings_t* settings, const char* name, int de return retval; } +/** + * Registers a callback for the specified string setting. + * + * @param settings a settings object + * @param name the setting's name + * @param callback an update function for the setting + * @param data user supplied data passed to the update function + * @return #FLUID_OK if the callback has been set, #FLUID_FAILED otherwise + */ +int fluid_settings_callback_str(fluid_settings_t* settings, const char* name, + fluid_str_update_t callback, void* data) +{ + fluid_setting_node_t *node; + fluid_str_setting_t *setting; + + fluid_return_val_if_fail (settings != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name[0] != '\0', FLUID_FAILED); + + fluid_rec_mutex_lock (settings->mutex); + + if ((fluid_settings_get(settings, name, &node) != FLUID_OK) + || node->type != FLUID_STR_TYPE) + { + fluid_rec_mutex_unlock(settings->mutex); + return FLUID_FAILED; + } + + setting = &node->str; + setting->update = callback; + setting->data = data; + + fluid_rec_mutex_unlock(settings->mutex); + return FLUID_OK; +} + +/** + * Registers a callback for the specified numeric setting. + * + * @param settings a settings object + * @param name the setting's name + * @param callback an update function for the setting + * @param data user supplied data passed to the update function + * @return #FLUID_OK if the callback has been set, #FLUID_FAILED otherwise + */ +int fluid_settings_callback_num(fluid_settings_t* settings, const char* name, + fluid_num_update_t callback, void* data) +{ + fluid_setting_node_t *node; + fluid_num_setting_t *setting; + + fluid_return_val_if_fail (settings != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name[0] != '\0', FLUID_FAILED); + + fluid_rec_mutex_lock (settings->mutex); + + if ((fluid_settings_get(settings, name, &node) != FLUID_OK) + || node->type != FLUID_NUM_TYPE) + { + fluid_rec_mutex_unlock(settings->mutex); + return FLUID_FAILED; + } + + setting = &node->num; + setting->update = callback; + setting->data = data; + + fluid_rec_mutex_unlock(settings->mutex); + return FLUID_OK; +} + +/** + * Registers a callback for the specified int setting. + * + * @param settings a settings object + * @param name the setting's name + * @param callback an update function for the setting + * @param data user supplied data passed to the update function + * @return #FLUID_OK if the callback has been set, #FLUID_FAILED otherwise + */ +int fluid_settings_callback_int(fluid_settings_t* settings, const char* name, + fluid_int_update_t callback, void* data) +{ + fluid_setting_node_t *node; + fluid_int_setting_t *setting; + + fluid_return_val_if_fail (settings != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name[0] != '\0', FLUID_FAILED); + + fluid_rec_mutex_lock (settings->mutex); + + if ((fluid_settings_get(settings, name, &node) != FLUID_OK) + || node->type != FLUID_INT_TYPE) + { + fluid_rec_mutex_unlock(settings->mutex); + return FLUID_FAILED; + } + + setting = &node->i; + setting->update = callback; + setting->data = data; + + fluid_rec_mutex_unlock(settings->mutex); + return FLUID_OK; +} + /** * Get the type of the setting with the given name * diff --git a/src/utils/fluid_settings.h b/src/utils/fluid_settings.h index bf7dad56..7a935bac 100644 --- a/src/utils/fluid_settings.h +++ b/src/utils/fluid_settings.h @@ -30,6 +30,8 @@ typedef int (*fluid_str_update_t)(void* data, const char* name, const char* valu int fluid_settings_register_str(fluid_settings_t* settings, const char* name, const char* def, int hints, fluid_str_update_t fun, void* data); +int fluid_settings_callback_str(fluid_settings_t* settings, const char* name, + fluid_str_update_t fun, void* data); typedef int (*fluid_num_update_t)(void* data, const char* name, double value); @@ -37,11 +39,15 @@ typedef int (*fluid_num_update_t)(void* data, const char* name, double value); int fluid_settings_register_num(fluid_settings_t* settings, const char* name, double def, double min, double max, int hints, fluid_num_update_t fun, void* data); +int fluid_settings_callback_num(fluid_settings_t* settings, const char* name, + fluid_num_update_t fun, void* data); typedef int (*fluid_int_update_t)(void* data, const char* name, int value); int fluid_settings_register_int(fluid_settings_t* settings, const char* name, int def, int min, int max, int hints, fluid_int_update_t fun, void* data); +int fluid_settings_callback_int(fluid_settings_t* settings, const char* name, + fluid_int_update_t fun, void* data); #endif /* _FLUID_SETTINGS_H */ From 9f579d99f31cf3b9c9552d90e538d9d9943cb905 Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Thu, 30 Nov 2017 19:51:21 +0100 Subject: [PATCH 02/11] Remove callback and data parameters from settings_register_* functions Callback functions and user data can be set with the previously added functions. And callbacks are only used in a single place in new_fluid_synth, all other calls to setttings_register_* set those two params to NULL, so lets remove them everywhere. --- src/bindings/fluid_cmd.c | 4 +- src/bindings/fluid_filerenderer.c | 16 +++--- src/drivers/fluid_adriver.c | 38 ++++++------- src/drivers/fluid_alsa.c | 8 +-- src/drivers/fluid_coreaudio.c | 2 +- src/drivers/fluid_coremidi.c | 2 +- src/drivers/fluid_dart.c | 2 +- src/drivers/fluid_dsound.c | 2 +- src/drivers/fluid_jack.c | 12 ++-- src/drivers/fluid_mdriver.c | 18 +++--- src/drivers/fluid_oss.c | 4 +- src/drivers/fluid_portaudio.c | 2 +- src/drivers/fluid_pulse.c | 8 +-- src/drivers/fluid_winmidi.c | 2 +- src/midi/fluid_midi.c | 6 +- src/synth/fluid_synth.c | 95 ++++++++++++------------------- src/utils/fluid_settings.c | 59 +++++++------------ src/utils/fluid_settings.h | 9 +-- 18 files changed, 122 insertions(+), 167 deletions(-) diff --git a/src/bindings/fluid_cmd.c b/src/bindings/fluid_cmd.c index 2d525d63..4e79f66b 100644 --- a/src/bindings/fluid_cmd.c +++ b/src/bindings/fluid_cmd.c @@ -80,8 +80,8 @@ static int fluid_handle_voice_count (void* data, int ac, char **av, void fluid_shell_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "shell.prompt", "", 0, NULL, NULL); - fluid_settings_register_int(settings, "shell.port", 9800, 1, 65535, 0, NULL, NULL); + fluid_settings_register_str(settings, "shell.prompt", "", 0); + fluid_settings_register_int(settings, "shell.port", 9800, 1, 65535, 0); } diff --git a/src/bindings/fluid_filerenderer.c b/src/bindings/fluid_filerenderer.c index 3a26f36f..8ef4a051 100644 --- a/src/bindings/fluid_filerenderer.c +++ b/src/bindings/fluid_filerenderer.c @@ -113,10 +113,10 @@ fluid_file_renderer_settings (fluid_settings_t* settings) unsigned int n; fluid_settings_register_str(settings, "audio.file.name", "fluidsynth.wav", - FLUID_HINT_FILENAME, NULL, NULL); - fluid_settings_register_str(settings, "audio.file.type", "auto", 0, NULL, NULL); - fluid_settings_register_str(settings, "audio.file.format", "s16", 0, NULL, NULL); - fluid_settings_register_str(settings, "audio.file.endian", "auto", 0, NULL, NULL); + FLUID_HINT_FILENAME); + fluid_settings_register_str(settings, "audio.file.type", "auto", 0); + fluid_settings_register_str(settings, "audio.file.format", "s16", 0); + fluid_settings_register_str(settings, "audio.file.endian", "auto", 0); fluid_settings_add_option (settings, "audio.file.type", "auto"); @@ -149,12 +149,12 @@ fluid_file_renderer_settings (fluid_settings_t* settings) #else - fluid_settings_register_str(settings, "audio.file.name", "fluidsynth.raw", 0, NULL, NULL); - fluid_settings_register_str(settings, "audio.file.type", "raw", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.file.name", "fluidsynth.raw", 0); + fluid_settings_register_str(settings, "audio.file.type", "raw", 0); fluid_settings_add_option (settings, "audio.file.type", "raw"); - fluid_settings_register_str(settings, "audio.file.format", "s16", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.file.format", "s16", 0); fluid_settings_add_option (settings, "audio.file.format", "s16"); - fluid_settings_register_str(settings, "audio.file.endian", "cpu", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.file.endian", "cpu", 0); fluid_settings_add_option (settings, "audio.file.endian", "cpu"); #endif } diff --git a/src/drivers/fluid_adriver.c b/src/drivers/fluid_adriver.c index 0f689519..fd4ff014 100644 --- a/src/drivers/fluid_adriver.c +++ b/src/drivers/fluid_adriver.c @@ -268,47 +268,47 @@ void fluid_audio_driver_settings(fluid_settings_t* settings) { unsigned int i; - fluid_settings_register_str(settings, "audio.sample-format", "16bits", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.sample-format", "16bits", 0); fluid_settings_add_option(settings, "audio.sample-format", "16bits"); fluid_settings_add_option(settings, "audio.sample-format", "float"); #if defined(WIN32) - fluid_settings_register_int(settings, "audio.period-size", 512, 64, 8192, 0, NULL, NULL); - fluid_settings_register_int(settings, "audio.periods", 8, 2, 64, 0, NULL, NULL); + fluid_settings_register_int(settings, "audio.period-size", 512, 64, 8192, 0); + fluid_settings_register_int(settings, "audio.periods", 8, 2, 64, 0); #elif defined(MACOS9) - fluid_settings_register_int(settings, "audio.period-size", 64, 64, 8192, 0, NULL, NULL); - fluid_settings_register_int(settings, "audio.periods", 8, 2, 64, 0, NULL, NULL); + fluid_settings_register_int(settings, "audio.period-size", 64, 64, 8192, 0); + fluid_settings_register_int(settings, "audio.periods", 8, 2, 64, 0); #else - fluid_settings_register_int(settings, "audio.period-size", 64, 64, 8192, 0, NULL, NULL); - fluid_settings_register_int(settings, "audio.periods", 16, 2, 64, 0, NULL, NULL); + fluid_settings_register_int(settings, "audio.period-size", 64, 64, 8192, 0); + fluid_settings_register_int(settings, "audio.periods", 16, 2, 64, 0); #endif fluid_settings_register_int (settings, "audio.realtime-prio", - FLUID_DEFAULT_AUDIO_RT_PRIO, 0, 99, 0, NULL, NULL); + FLUID_DEFAULT_AUDIO_RT_PRIO, 0, 99, 0); /* Set the default driver */ #if JACK_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "jack", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "jack", 0); #elif ALSA_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "alsa", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "alsa", 0); #elif PULSE_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "pulseaudio", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "pulseaudio", 0); #elif OSS_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "oss", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "oss", 0); #elif COREAUDIO_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "coreaudio", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "coreaudio", 0); #elif DSOUND_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "dsound", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "dsound", 0); #elif SNDMAN_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "sndman", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "sndman", 0); #elif PORTAUDIO_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "portaudio", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "portaudio", 0); #elif DART_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "dart", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "dart", 0); #elif AUFILE_SUPPORT - fluid_settings_register_str(settings, "audio.driver", "file", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "file", 0); #else - fluid_settings_register_str(settings, "audio.driver", "", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.driver", "", 0); #endif /* Add all drivers to the list of options */ diff --git a/src/drivers/fluid_alsa.c b/src/drivers/fluid_alsa.c index 22ec4a74..afb593fb 100644 --- a/src/drivers/fluid_alsa.c +++ b/src/drivers/fluid_alsa.c @@ -151,7 +151,7 @@ static fluid_thread_return_t fluid_alsa_seq_run(void* d); void fluid_alsa_audio_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "audio.alsa.device", "default", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.alsa.device", "default", 0); } @@ -548,7 +548,7 @@ static fluid_thread_return_t fluid_alsa_audio_run_s16 (void *d) void fluid_alsa_rawmidi_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "midi.alsa.device", "default", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.alsa.device", "default", 0); } /* @@ -715,8 +715,8 @@ fluid_alsa_midi_run(void* d) void fluid_alsa_seq_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "midi.alsa_seq.device", "default", 0, NULL, NULL); - fluid_settings_register_str(settings, "midi.alsa_seq.id", "pid", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.alsa_seq.device", "default", 0); + fluid_settings_register_str(settings, "midi.alsa_seq.id", "pid", 0); } diff --git a/src/drivers/fluid_coreaudio.c b/src/drivers/fluid_coreaudio.c index 8c4b7fae..a3a146c3 100644 --- a/src/drivers/fluid_coreaudio.c +++ b/src/drivers/fluid_coreaudio.c @@ -111,7 +111,7 @@ fluid_core_audio_driver_settings(fluid_settings_t* settings) pa.mScope = kAudioObjectPropertyScopeWildcard; pa.mElement = kAudioObjectPropertyElementMaster; - fluid_settings_register_str (settings, "audio.coreaudio.device", "default", 0, NULL, NULL); + fluid_settings_register_str (settings, "audio.coreaudio.device", "default", 0); fluid_settings_add_option (settings, "audio.coreaudio.device", "default"); if (OK (AudioObjectGetPropertyDataSize (kAudioObjectSystemObject, &pa, 0, 0, &size))) { int num = size / (int) sizeof (AudioDeviceID); diff --git a/src/drivers/fluid_coremidi.c b/src/drivers/fluid_coremidi.c index 1bbe6c1e..deac4593 100644 --- a/src/drivers/fluid_coremidi.c +++ b/src/drivers/fluid_coremidi.c @@ -66,7 +66,7 @@ void fluid_coremidi_callback(const MIDIPacketList *list, void *p, void *src); void fluid_coremidi_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "midi.coremidi.id", "pid", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.coremidi.id", "pid", 0); } /* diff --git a/src/drivers/fluid_dart.c b/src/drivers/fluid_dart.c index 0bd4f11a..1c5df476 100644 --- a/src/drivers/fluid_dart.c +++ b/src/drivers/fluid_dart.c @@ -75,7 +75,7 @@ static LONG APIENTRY fluid_dart_audio_run( ULONG ulStatus, PMCI_MIX_BUFFER pBuff void fluid_dart_audio_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "audio.dart.device", "default", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.dart.device", "default", 0); } diff --git a/src/drivers/fluid_dsound.c b/src/drivers/fluid_dsound.c index 082eadd7..24635625 100644 --- a/src/drivers/fluid_dsound.c +++ b/src/drivers/fluid_dsound.c @@ -84,7 +84,7 @@ fluid_dsound_enum_callback2(LPGUID guid, LPCTSTR description, LPCTSTR module, LP void fluid_dsound_audio_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "audio.dsound.device", "default", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.dsound.device", "default", 0); fluid_settings_add_option(settings, "audio.dsound.device", "default"); DirectSoundEnumerate((LPDSENUMCALLBACK) fluid_dsound_enum_callback, settings); } diff --git a/src/drivers/fluid_jack.c b/src/drivers/fluid_jack.c index 7bd79eaa..75fd1d37 100644 --- a/src/drivers/fluid_jack.c +++ b/src/drivers/fluid_jack.c @@ -106,10 +106,10 @@ static fluid_jack_client_t *last_client = NULL; /* Last unpaired client. F void fluid_jack_audio_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "audio.jack.id", "fluidsynth", 0, NULL, NULL); - fluid_settings_register_int(settings, "audio.jack.multi", 0, 0, 1, FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_int(settings, "audio.jack.autoconnect", 0, 0, 1, FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_str(settings, "audio.jack.server", "", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.jack.id", "fluidsynth", 0); + fluid_settings_register_int(settings, "audio.jack.multi", 0, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_int(settings, "audio.jack.autoconnect", 0, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_str(settings, "audio.jack.server", "", 0); } /* @@ -614,8 +614,8 @@ fluid_jack_driver_shutdown(void *arg) void fluid_jack_midi_driver_settings (fluid_settings_t *settings) { - fluid_settings_register_str (settings, "midi.jack.id", "fluidsynth-midi", 0, NULL, NULL); - fluid_settings_register_str (settings, "midi.jack.server", "", 0, NULL, NULL); + fluid_settings_register_str (settings, "midi.jack.id", "fluidsynth-midi", 0); + fluid_settings_register_str (settings, "midi.jack.server", "", 0); } /* diff --git a/src/drivers/fluid_mdriver.c b/src/drivers/fluid_mdriver.c index 53b76346..26e073ce 100644 --- a/src/drivers/fluid_mdriver.c +++ b/src/drivers/fluid_mdriver.c @@ -144,26 +144,26 @@ void fluid_midi_driver_settings(fluid_settings_t* settings) { unsigned int i; - fluid_settings_register_int (settings, "midi.autoconnect", 0, 0, 1, FLUID_HINT_TOGGLED, NULL, NULL); + fluid_settings_register_int (settings, "midi.autoconnect", 0, 0, 1, FLUID_HINT_TOGGLED); fluid_settings_register_int (settings, "midi.realtime-prio", - FLUID_DEFAULT_MIDI_RT_PRIO, 0, 99, 0, NULL, NULL); + FLUID_DEFAULT_MIDI_RT_PRIO, 0, 99, 0); /* Set the default driver */ #if ALSA_SUPPORT - fluid_settings_register_str(settings, "midi.driver", "alsa_seq", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.driver", "alsa_seq", 0); #elif JACK_SUPPORT - fluid_settings_register_str(settings, "midi.driver", "jack", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.driver", "jack", 0); #elif OSS_SUPPORT - fluid_settings_register_str(settings, "midi.driver", "oss", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.driver", "oss", 0); #elif WINMIDI_SUPPORT - fluid_settings_register_str(settings, "midi.driver", "winmidi", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.driver", "winmidi", 0); #elif MIDISHARE_SUPPORT - fluid_settings_register_str(settings, "midi.driver", "midishare", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.driver", "midishare", 0); #elif COREMIDI_SUPPORT - fluid_settings_register_str(settings, "midi.driver", "coremidi", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.driver", "coremidi", 0); #else - fluid_settings_register_str(settings, "midi.driver", "", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.driver", "", 0); #endif /* Add all drivers to the list of options */ diff --git a/src/drivers/fluid_oss.c b/src/drivers/fluid_oss.c index 8e00f519..0fdad174 100644 --- a/src/drivers/fluid_oss.c +++ b/src/drivers/fluid_oss.c @@ -101,7 +101,7 @@ static fluid_thread_return_t fluid_oss_midi_run(void* d); void fluid_oss_audio_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "audio.oss.device", "/dev/dsp", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.oss.device", "/dev/dsp", 0); } /* @@ -502,7 +502,7 @@ fluid_oss_audio_run2(void* d) void fluid_oss_midi_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "midi.oss.device", "/dev/midi", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.oss.device", "/dev/midi", 0); } /* diff --git a/src/drivers/fluid_portaudio.c b/src/drivers/fluid_portaudio.c index 043a2335..62385627 100644 --- a/src/drivers/fluid_portaudio.c +++ b/src/drivers/fluid_portaudio.c @@ -122,7 +122,7 @@ fluid_portaudio_driver_settings (fluid_settings_t *settings) PaError err; int i; - fluid_settings_register_str (settings, "audio.portaudio.device", PORTAUDIO_DEFAULT_DEVICE, 0, NULL, NULL); + fluid_settings_register_str (settings, "audio.portaudio.device", PORTAUDIO_DEFAULT_DEVICE, 0); fluid_settings_add_option (settings, "audio.portaudio.device", PORTAUDIO_DEFAULT_DEVICE); err = Pa_Initialize(); diff --git a/src/drivers/fluid_pulse.c b/src/drivers/fluid_pulse.c index 08e75ffc..2cdd0219 100644 --- a/src/drivers/fluid_pulse.c +++ b/src/drivers/fluid_pulse.c @@ -63,11 +63,11 @@ static fluid_thread_return_t fluid_pulse_audio_run2(void* d); void fluid_pulse_audio_driver_settings(fluid_settings_t* settings) { - fluid_settings_register_str(settings, "audio.pulseaudio.server", "default", 0, NULL, NULL); - fluid_settings_register_str(settings, "audio.pulseaudio.device", "default", 0, NULL, NULL); - fluid_settings_register_str(settings, "audio.pulseaudio.media-role", "music", 0, NULL, NULL); + fluid_settings_register_str(settings, "audio.pulseaudio.server", "default", 0); + fluid_settings_register_str(settings, "audio.pulseaudio.device", "default", 0); + fluid_settings_register_str(settings, "audio.pulseaudio.media-role", "music", 0); fluid_settings_register_int(settings, "audio.pulseaudio.adjust-latency", 1, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); + FLUID_HINT_TOGGLED); } diff --git a/src/drivers/fluid_winmidi.c b/src/drivers/fluid_winmidi.c index b6503eaf..fc2021a1 100644 --- a/src/drivers/fluid_winmidi.c +++ b/src/drivers/fluid_winmidi.c @@ -86,7 +86,7 @@ void fluid_winmidi_midi_driver_settings(fluid_settings_t* settings) MMRESULT res; MIDIINCAPS in_caps; UINT i, num; - fluid_settings_register_str(settings, "midi.winmidi.device", "default", 0, NULL, NULL); + fluid_settings_register_str(settings, "midi.winmidi.device", "default", 0); num = midiInGetNumDevs(); if (num > 0) { fluid_settings_add_option(settings, "midi.winmidi.device", "default"); diff --git a/src/midi/fluid_midi.c b/src/midi/fluid_midi.c index f73e44a2..eb84ded8 100644 --- a/src/midi/fluid_midi.c +++ b/src/midi/fluid_midi.c @@ -1293,14 +1293,12 @@ fluid_player_settings(fluid_settings_t *settings) { /* player.timing-source can be either "system" (use system timer) or "sample" (use timer based on number of written samples) */ - fluid_settings_register_str(settings, "player.timing-source", "sample", 0, - NULL, NULL); + fluid_settings_register_str(settings, "player.timing-source", "sample", 0); fluid_settings_add_option(settings, "player.timing-source", "sample"); fluid_settings_add_option(settings, "player.timing-source", "system"); /* Selects whether the player should reset the synth between songs, or not. */ - fluid_settings_register_int(settings, "player.reset-synth", 1, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); + fluid_settings_register_int(settings, "player.reset-synth", 1, 0, 1, FLUID_HINT_TOGGLED); } diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index dbca05d6..dbe7e6bc 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -155,77 +155,47 @@ static const fluid_revmodel_presets_t revmodel_preset[] = { * INITIALIZATION & UTILITIES */ -static void fluid_synth_register_overflow(fluid_settings_t* settings, - fluid_num_update_t update_func, - void* update_data) -{ - fluid_settings_register_num(settings, "synth.overflow.percussion", - 4000, -10000, 10000, 0, update_func, update_data); - fluid_settings_register_num(settings, "synth.overflow.sustained", - -1000, -10000, 10000, 0, update_func, update_data); - fluid_settings_register_num(settings, "synth.overflow.released", - -2000, -10000, 10000, 0, update_func, update_data); - fluid_settings_register_num(settings, "synth.overflow.age", - 1000, -10000, 10000, 0, update_func, update_data); - fluid_settings_register_num(settings, "synth.overflow.volume", - 500, -10000, 10000, 0, update_func, update_data); -} - void fluid_synth_settings(fluid_settings_t* settings) { - fluid_settings_register_int(settings, "synth.verbose", 0, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_int(settings, "synth.reverb.active", 1, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_int(settings, "synth.chorus.active", 1, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_int(settings, "synth.ladspa.active", 0, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_int(settings, "synth.lock-memory", 1, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_str(settings, "midi.portname", "", 0, NULL, NULL); + fluid_settings_register_int(settings, "synth.verbose", 0, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_int(settings, "synth.reverb.active", 1, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_int(settings, "synth.chorus.active", 1, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_int(settings, "synth.ladspa.active", 0, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_int(settings, "synth.lock-memory", 1, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_str(settings, "midi.portname", "", 0); #ifdef DEFAULT_SOUNDFONT - fluid_settings_register_str(settings, "synth.default-soundfont", - DEFAULT_SOUNDFONT, 0, NULL, NULL); + fluid_settings_register_str(settings, "synth.default-soundfont", DEFAULT_SOUNDFONT, 0); #endif - fluid_settings_register_int(settings, "synth.polyphony", - 256, 1, 65535, 0, NULL, NULL); - fluid_settings_register_int(settings, "synth.midi-channels", - 16, 16, 256, 0, NULL, NULL); - fluid_settings_register_num(settings, "synth.gain", - 0.2f, 0.0f, 10.0f, - 0, NULL, NULL); - fluid_settings_register_int(settings, "synth.audio-channels", - 1, 1, 128, 0, NULL, NULL); - fluid_settings_register_int(settings, "synth.audio-groups", - 1, 1, 128, 0, NULL, NULL); - fluid_settings_register_int(settings, "synth.effects-channels", - 2, 2, 2, 0, NULL, NULL); - fluid_settings_register_num(settings, "synth.sample-rate", - 44100.0f, 8000.0f, 96000.0f, - 0, NULL, NULL); - fluid_settings_register_int(settings, "synth.device-id", - 0, 0, 126, 0, NULL, NULL); - fluid_settings_register_int(settings, "synth.cpu-cores", 1, 1, 256, 0, NULL, NULL); + fluid_settings_register_int(settings, "synth.polyphony", 256, 1, 65535, 0); + fluid_settings_register_int(settings, "synth.midi-channels", 16, 16, 256, 0); + fluid_settings_register_num(settings, "synth.gain", 0.2f, 0.0f, 10.0f, 0); + fluid_settings_register_int(settings, "synth.audio-channels", 1, 1, 128, 0); + fluid_settings_register_int(settings, "synth.audio-groups", 1, 1, 128, 0); + fluid_settings_register_int(settings, "synth.effects-channels", 2, 2, 2, 0); + fluid_settings_register_num(settings, "synth.sample-rate", 44100.0f, 8000.0f, 96000.0f, 0); + fluid_settings_register_int(settings, "synth.device-id", 0, 0, 126, 0); + fluid_settings_register_int(settings, "synth.cpu-cores", 1, 1, 256, 0); - fluid_settings_register_int(settings, "synth.min-note-length", 10, 0, 65535, 0, NULL, NULL); + fluid_settings_register_int(settings, "synth.min-note-length", 10, 0, 65535, 0); - fluid_settings_register_int(settings, "synth.threadsafe-api", 1, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); - fluid_settings_register_int(settings, "synth.parallel-render", 1, 0, 1, - FLUID_HINT_TOGGLED, NULL, NULL); + fluid_settings_register_int(settings, "synth.threadsafe-api", 1, 0, 1, FLUID_HINT_TOGGLED); + fluid_settings_register_int(settings, "synth.parallel-render", 1, 0, 1, FLUID_HINT_TOGGLED); - fluid_synth_register_overflow(settings, NULL, NULL); + fluid_settings_register_num(settings, "synth.overflow.percussion", 4000, -10000, 10000, 0); + fluid_settings_register_num(settings, "synth.overflow.sustained", -1000, -10000, 10000, 0); + fluid_settings_register_num(settings, "synth.overflow.released", -2000, -10000, 10000, 0); + fluid_settings_register_num(settings, "synth.overflow.age", 1000, -10000, 10000, 0); + fluid_settings_register_num(settings, "synth.overflow.volume", 500, -10000, 10000, 0); - fluid_settings_register_str(settings, "synth.midi-bank-select", "gs", 0, NULL, NULL); + fluid_settings_register_str(settings, "synth.midi-bank-select", "gs", 0); fluid_settings_add_option(settings, "synth.midi-bank-select", "gm"); fluid_settings_add_option(settings, "synth.midi-bank-select", "gs"); fluid_settings_add_option(settings, "synth.midi-bank-select", "xg"); fluid_settings_add_option(settings, "synth.midi-bank-select", "mma"); - fluid_settings_register_str(settings, "synth.volenv", "emu", 0, NULL, NULL); + fluid_settings_register_str(settings, "synth.volenv", "emu", 0); fluid_settings_add_option(settings, "synth.volenv", "emu"); fluid_settings_add_option(settings, "synth.volenv", "compliant"); } @@ -620,10 +590,17 @@ new_fluid_synth(fluid_settings_t *settings) (fluid_int_update_t) fluid_synth_update_polyphony, synth); fluid_settings_callback_int(settings, "synth.device-id", (fluid_int_update_t) fluid_synth_update_device_id, synth); + fluid_settings_callback_num(settings, "synth.overflow.percussion", + (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_settings_callback_num(settings, "synth.overflow.sustained", + (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_settings_callback_num(settings, "synth.overflow.released", + (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_settings_callback_num(settings, "synth.overflow.age", + (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_settings_callback_num(settings, "synth.overflow.volume", + (fluid_num_update_t) fluid_synth_update_overflow, synth); - fluid_synth_register_overflow(settings, - (fluid_num_update_t) fluid_synth_update_overflow, synth); - /* do some basic sanity checking on the settings */ if (synth->midi_channels % 16 != 0) { diff --git a/src/utils/fluid_settings.c b/src/utils/fluid_settings.c index d77a33af..df4866d1 100644 --- a/src/utils/fluid_settings.c +++ b/src/utils/fluid_settings.c @@ -87,7 +87,7 @@ typedef struct { } fluid_setting_node_t; static fluid_setting_node_t* -new_fluid_str_setting(const char* value, const char* def, int hints, fluid_str_update_t fun, void* data) +new_fluid_str_setting(const char* value, const char* def, int hints) { fluid_setting_node_t* node; fluid_str_setting_t* str; @@ -101,14 +101,14 @@ new_fluid_str_setting(const char* value, const char* def, int hints, fluid_str_u } node->type = FLUID_STR_TYPE; + str = &node->str; - str->value = value? FLUID_STRDUP(value) : NULL; str->def = def? FLUID_STRDUP(def) : NULL; str->hints = hints; str->options = NULL; - str->update = fun; - str->data = data; + str->update = NULL; + str->data = NULL; return node; } @@ -138,8 +138,7 @@ delete_fluid_str_setting(fluid_setting_node_t* node) static fluid_setting_node_t* -new_fluid_num_setting(double min, double max, double def, - int hints, fluid_num_update_t fun, void* data) +new_fluid_num_setting(double min, double max, double def, int hints) { fluid_setting_node_t* node; fluid_num_setting_t* num; @@ -153,15 +152,15 @@ new_fluid_num_setting(double min, double max, double def, } node->type = FLUID_NUM_TYPE; + num = &node->num; - num->value = def; num->def = def; num->min = min; num->max = max; num->hints = hints; - num->update = fun; - num->data = data; + num->update = NULL; + num->data = NULL; return node; } @@ -176,8 +175,7 @@ delete_fluid_num_setting(fluid_setting_node_t* node) } static fluid_setting_node_t* -new_fluid_int_setting(int min, int max, int def, - int hints, fluid_int_update_t fun, void* data) +new_fluid_int_setting(int min, int max, int def, int hints) { fluid_setting_node_t* node; fluid_int_setting_t* i; @@ -191,15 +189,15 @@ new_fluid_int_setting(int min, int max, int def, } node->type = FLUID_INT_TYPE; + i = &node->i; - i->value = def; i->def = def; i->min = min; i->max = max; i->hints = hints; - i->update = fun; - i->data = data; + i->update = NULL; + i->data = NULL; return node; } @@ -471,13 +469,10 @@ fluid_settings_set(fluid_settings_t* settings, const char *name, fluid_setting_n * @param name the setting's name * @param def the default value for the setting * @param hints the hints for the setting - * @param fun an update function for the setting - * @param data user supplied data * @return #FLUID_OK if the value has been register correctly, #FLUID_FAILED otherwise */ int -fluid_settings_register_str(fluid_settings_t* settings, const char* name, const char* def, int hints, - fluid_str_update_t fun, void* data) +fluid_settings_register_str(fluid_settings_t* settings, const char* name, const char* def, int hints) { fluid_setting_node_t *node; int retval = FLUID_FAILED; @@ -489,15 +484,13 @@ fluid_settings_register_str(fluid_settings_t* settings, const char* name, const fluid_rec_mutex_lock (settings->mutex); if (fluid_settings_get(settings, name, &node) != FLUID_OK) { - node = new_fluid_str_setting(def, def, hints, fun, data); + node = new_fluid_str_setting(def, def, hints); retval = fluid_settings_set(settings, name, node); if (retval != FLUID_OK) delete_fluid_str_setting (node); } else { /* if variable already exists, don't change its value. */ if (node->type == FLUID_STR_TYPE) { fluid_str_setting_t* setting = &node->str; - setting->update = fun; - setting->data = data; setting->def = def? FLUID_STRDUP(def) : NULL; setting->hints = hints; retval = FLUID_OK; @@ -520,14 +513,11 @@ fluid_settings_register_str(fluid_settings_t* settings, const char* name, const * @param min the smallest allowed value for the setting * @param max the largest allowed value for the setting * @param hints the hints for the setting - * @param fun an update function for the setting - * @param data user supplied data * @return #FLUID_OK if the value has been register correctly, #FLUID_FAILED otherwise */ int fluid_settings_register_num(fluid_settings_t* settings, const char* name, double def, - double min, double max, int hints, - fluid_num_update_t fun, void* data) + double min, double max, int hints) { fluid_setting_node_t *node; int retval = FLUID_FAILED; @@ -543,15 +533,13 @@ fluid_settings_register_num(fluid_settings_t* settings, const char* name, double if (fluid_settings_get(settings, name, &node) != FLUID_OK) { /* insert a new setting */ - node = new_fluid_num_setting(min, max, def, hints, fun, data); + node = new_fluid_num_setting(min, max, def, hints); retval = fluid_settings_set(settings, name, node); if (retval != FLUID_OK) delete_fluid_num_setting (node); } else { if (node->type == FLUID_NUM_TYPE) { /* update the existing setting but don't change its value */ fluid_num_setting_t* setting = &node->num; - setting->update = fun; - setting->data = data; setting->min = min; setting->max = max; setting->def = def; @@ -577,14 +565,11 @@ fluid_settings_register_num(fluid_settings_t* settings, const char* name, double * @param min the smallest allowed value for the setting * @param max the largest allowed value for the setting * @param hints the hints for the setting - * @param fun an update function for the setting - * @param data user supplied data * @return #FLUID_OK if the value has been register correctly, #FLUID_FAILED otherwise */ int fluid_settings_register_int(fluid_settings_t* settings, const char* name, int def, - int min, int max, int hints, - fluid_int_update_t fun, void* data) + int min, int max, int hints) { fluid_setting_node_t *node; int retval = FLUID_FAILED; @@ -600,15 +585,13 @@ fluid_settings_register_int(fluid_settings_t* settings, const char* name, int de if (fluid_settings_get(settings, name, &node) != FLUID_OK) { /* insert a new setting */ - node = new_fluid_int_setting(min, max, def, hints, fun, data); + node = new_fluid_int_setting(min, max, def, hints); retval = fluid_settings_set(settings, name, node); if (retval != FLUID_OK) delete_fluid_int_setting (node); } else { if (node->type == FLUID_INT_TYPE) { /* update the existing setting but don't change its value */ fluid_int_setting_t* setting = &node->i; - setting->update = fun; - setting->data = data; setting->min = min; setting->max = max; setting->def = def; @@ -890,7 +873,7 @@ fluid_settings_setstr(fluid_settings_t* settings, const char *name, const char * } } else { /* insert a new setting */ - node = new_fluid_str_setting(str, NULL, 0, NULL, NULL); + node = new_fluid_str_setting(str, NULL, 0); retval = fluid_settings_set(settings, name, node); if (retval != FLUID_OK) delete_fluid_str_setting (node); } @@ -1221,7 +1204,7 @@ fluid_settings_setnum(fluid_settings_t* settings, const char *name, double val) } } else { /* insert a new setting */ - node = new_fluid_num_setting(-1e10, 1e10, 0.0f, 0, NULL, NULL); + node = new_fluid_num_setting(-1e10, 1e10, 0.0f, 0); node->num.value = val; retval = fluid_settings_set(settings, name, node); if (retval != FLUID_OK) delete_fluid_num_setting (node); @@ -1373,7 +1356,7 @@ fluid_settings_setint(fluid_settings_t* settings, const char *name, int val) } } else { /* insert a new setting */ - node = new_fluid_int_setting(INT_MIN, INT_MAX, 0, 0, NULL, NULL); + node = new_fluid_int_setting(INT_MIN, INT_MAX, 0, 0); node->i.value = val; retval = fluid_settings_set(settings, name, node); if (retval != FLUID_OK) delete_fluid_int_setting (node); diff --git a/src/utils/fluid_settings.h b/src/utils/fluid_settings.h index 7a935bac..8264b89d 100644 --- a/src/utils/fluid_settings.h +++ b/src/utils/fluid_settings.h @@ -28,8 +28,7 @@ int fluid_settings_remove_option(fluid_settings_t* settings, const char* name, c typedef int (*fluid_str_update_t)(void* data, const char* name, const char* value); -int fluid_settings_register_str(fluid_settings_t* settings, const char* name, const char* def, int hints, - fluid_str_update_t fun, void* data); +int fluid_settings_register_str(fluid_settings_t* settings, const char* name, const char* def, int hints); int fluid_settings_callback_str(fluid_settings_t* settings, const char* name, fluid_str_update_t fun, void* data); @@ -37,16 +36,14 @@ int fluid_settings_callback_str(fluid_settings_t* settings, const char* name, typedef int (*fluid_num_update_t)(void* data, const char* name, double value); int fluid_settings_register_num(fluid_settings_t* settings, const char* name, double def, - double min, double max, int hints, - fluid_num_update_t fun, void* data); + double min, double max, int hints); int fluid_settings_callback_num(fluid_settings_t* settings, const char* name, fluid_num_update_t fun, void* data); typedef int (*fluid_int_update_t)(void* data, const char* name, int value); int fluid_settings_register_int(fluid_settings_t* settings, const char* name, int def, - int min, int max, int hints, - fluid_int_update_t fun, void* data); + int min, int max, int hints); int fluid_settings_callback_int(fluid_settings_t* settings, const char* name, fluid_int_update_t fun, void* data); From 4b090c2afba49cce3c9ec00520717442f945ddb3 Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 19:29:29 +0100 Subject: [PATCH 03/11] Use case-insensitive string match for boolean settings --- src/bindings/fluid_cmd.c | 6 +++--- src/fluidsynth.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bindings/fluid_cmd.c b/src/bindings/fluid_cmd.c index 4e79f66b..4aa4caa8 100644 --- a/src/bindings/fluid_cmd.c +++ b/src/bindings/fluid_cmd.c @@ -1363,9 +1363,9 @@ fluid_handle_set(void* data, int ac, char** av, fluid_ostream_t out) if (fluid_settings_get_hints (handler->synth->settings, av[0], &hints) == FLUID_OK && hints & FLUID_HINT_TOGGLED) { - if (FLUID_STRCMP (av[1], "yes") == 0 || FLUID_STRCMP (av[1], "True") == 0 - || FLUID_STRCMP (av[1], "TRUE") == 0 || FLUID_STRCMP (av[1], "true") == 0 - || FLUID_STRCMP (av[1], "T") == 0) + if (FLUID_STRCASECMP (av[1], "yes") == 0 + || FLUID_STRCASECMP (av[1], "true") == 0 + || FLUID_STRCASECMP (av[1], "t") == 0) ival = 1; else ival = atoi (av[1]); } diff --git a/src/fluidsynth.c b/src/fluidsynth.c index bfb80af5..11d08fde 100644 --- a/src/fluidsynth.c +++ b/src/fluidsynth.c @@ -94,9 +94,9 @@ void process_o_cmd_line_option(fluid_settings_t* settings, char* optarg) if (fluid_settings_get_hints (settings, optarg, &hints) == FLUID_OK && hints & FLUID_HINT_TOGGLED) { - if (FLUID_STRCMP (val, "yes") == 0 || FLUID_STRCMP (val, "True") == 0 - || FLUID_STRCMP (val, "TRUE") == 0 || FLUID_STRCMP (val, "true") == 0 - || FLUID_STRCMP (val, "T") == 0) + if (FLUID_STRCASECMP (val, "yes") == 0 + || FLUID_STRCASECMP (val, "true") == 0 + || FLUID_STRCASECMP (val, "t") == 0) ival = 1; else ival = atoi (val); } From 6ab0cc3024a18b9691b57880b101915c6d3f764d Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 20:21:07 +0100 Subject: [PATCH 04/11] Refactor and cleanup fluid_settings_set* functions - backwards incompatible: remove "yes", "no" support for int settings via fluid_settings_setstr - backwards incompatible: remove silent setting creation of named setting does not exist - Unlock settings mutex before calling into an update callback, to avoid possible deadlock with FluidSynth API mutex --- src/utils/fluid_settings.c | 197 +++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 95 deletions(-) diff --git a/src/utils/fluid_settings.c b/src/utils/fluid_settings.c index df4866d1..0d113638 100644 --- a/src/utils/fluid_settings.c +++ b/src/utils/fluid_settings.c @@ -832,55 +832,54 @@ int fluid_settings_setstr(fluid_settings_t* settings, const char *name, const char *str) { fluid_setting_node_t *node; - int retval = FLUID_FAILED; + fluid_str_setting_t *setting; + char *new_value = NULL; + fluid_str_update_t callback = NULL; + void *data = NULL; - fluid_return_val_if_fail (settings != NULL, retval); - fluid_return_val_if_fail (name != NULL, retval); - fluid_return_val_if_fail (name[0] != '\0', retval); + fluid_return_val_if_fail (settings != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name[0] != '\0', FLUID_FAILED); fluid_rec_mutex_lock (settings->mutex); - if (fluid_settings_get (settings, name, &node) == FLUID_OK) { - if (node->type == FLUID_STR_TYPE) { - fluid_str_setting_t *setting = &node->str; - - if (setting->value) FLUID_FREE (setting->value); - setting->value = str ? FLUID_STRDUP (str) : NULL; - - /* Call under lock to keep update() synchronized with the current value */ - if (setting->update) (*setting->update)(setting->data, name, str); - retval = FLUID_OK; - } - else if (node->type == FLUID_INT_TYPE) /* Handle yes/no for boolean values for backwards compatibility */ - { - fluid_int_setting_t *setting = &node->i; - - if (setting->hints & FLUID_HINT_TOGGLED) - { - if (FLUID_STRCMP (str, "yes") == 0) - { - setting->value = TRUE; - if (setting->update) (*setting->update)(setting->data, name, TRUE); - retval = FLUID_OK; - } - else if (FLUID_STRCMP (str, "no") == 0) - { - setting->value = FALSE; - if (setting->update) (*setting->update)(setting->data, name, FALSE); - retval = FLUID_OK; - } - } - } - } else { - /* insert a new setting */ - node = new_fluid_str_setting(str, NULL, 0); - retval = fluid_settings_set(settings, name, node); - if (retval != FLUID_OK) delete_fluid_str_setting (node); + if ((fluid_settings_get (settings, name, &node) != FLUID_OK) + || (node->type != FLUID_STR_TYPE)) { + goto error_recovery; } + setting = &node->str; + + if (setting->value) { + FLUID_FREE (setting->value); + } + + if (str) { + new_value = FLUID_STRDUP (str); + if (new_value == NULL) { + FLUID_LOG (FLUID_ERR, "Out of memory"); + goto error_recovery; + } + } + + setting->value = new_value; + + callback = setting->update; + data = setting->data; + + /* Release the mutex before calling the update callback, to avoid + * possible deadlocks with FluidSynths API lock */ fluid_rec_mutex_unlock (settings->mutex); - return retval; + if (callback) { + (*callback)(data, name, new_value); + } + + return FLUID_OK; + +error_recovery: + fluid_rec_mutex_unlock (settings->mutex); + return FLUID_FAILED; } /** @@ -1178,41 +1177,45 @@ int fluid_settings_setnum(fluid_settings_t* settings, const char *name, double val) { fluid_setting_node_t *node; - int retval = FLUID_FAILED; + fluid_num_setting_t *setting; + fluid_num_update_t callback = NULL; + void *data = NULL; - fluid_return_val_if_fail (settings != NULL, retval); - fluid_return_val_if_fail (name != NULL, retval); - fluid_return_val_if_fail (name[0] != '\0', retval); + fluid_return_val_if_fail (settings != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name[0] != '\0', FLUID_FAILED); fluid_rec_mutex_lock (settings->mutex); - if (fluid_settings_get(settings, name, &node) == FLUID_OK) { - if (node->type == FLUID_NUM_TYPE) { - fluid_num_setting_t* setting = &node->num; - - if (val < setting->min || val > setting->max) - { - FLUID_LOG(FLUID_DBG, "requested set value for %s out of range", name); - return retval; - } - - setting->value = val; - - /* Call under lock to keep update() synchronized with the current value */ - if (setting->update) (*setting->update)(setting->data, name, val); - retval = FLUID_OK; - } - } else { - /* insert a new setting */ - node = new_fluid_num_setting(-1e10, 1e10, 0.0f, 0); - node->num.value = val; - retval = fluid_settings_set(settings, name, node); - if (retval != FLUID_OK) delete_fluid_num_setting (node); + if ((fluid_settings_get (settings, name, &node) != FLUID_OK) + || (node->type != FLUID_NUM_TYPE)) { + goto error_recovery; } + setting = &node->num; + if (val < setting->min || val > setting->max) { + FLUID_LOG(FLUID_DBG, "requested set value for %s out of range", name); + goto error_recovery; + } + + setting->value = val; + + callback = setting->update; + data = setting->data; + + /* Release the mutex before calling the update callback, to avoid + * possible deadlocks with FluidSynths API lock */ fluid_rec_mutex_unlock (settings->mutex); - return retval; + if (callback) { + (*callback)(data, name, val); + } + + return FLUID_OK; + +error_recovery: + fluid_rec_mutex_unlock (settings->mutex); + return FLUID_FAILED; } /** @@ -1330,41 +1333,45 @@ int fluid_settings_setint(fluid_settings_t* settings, const char *name, int val) { fluid_setting_node_t *node; - int retval = FLUID_FAILED; + fluid_int_setting_t *setting; + fluid_int_update_t callback = NULL; + void *data = NULL; - fluid_return_val_if_fail (settings != NULL, retval); - fluid_return_val_if_fail (name != NULL, retval); - fluid_return_val_if_fail (name[0] != '\0', retval); + fluid_return_val_if_fail (settings != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name != NULL, FLUID_FAILED); + fluid_return_val_if_fail (name[0] != '\0', FLUID_FAILED); fluid_rec_mutex_lock (settings->mutex); - if (fluid_settings_get(settings, name, &node) == FLUID_OK) { - if (node->type == FLUID_INT_TYPE) { - fluid_int_setting_t* setting = &node->i; - - if (val < setting->min || val > setting->max) - { - FLUID_LOG(FLUID_DBG, "requested set value for %s out of range", name); - return retval; - } - - setting->value = val; - - /* Call under lock to keep update() synchronized with the current value */ - if (setting->update) (*setting->update)(setting->data, name, val); - retval = FLUID_OK; - } - } else { - /* insert a new setting */ - node = new_fluid_int_setting(INT_MIN, INT_MAX, 0, 0); - node->i.value = val; - retval = fluid_settings_set(settings, name, node); - if (retval != FLUID_OK) delete_fluid_int_setting (node); + if ((fluid_settings_get (settings, name, &node) != FLUID_OK) + || (node->type != FLUID_INT_TYPE)) { + goto error_recovery; } + setting = &node->i; + if (val < setting->min || val > setting->max) { + FLUID_LOG(FLUID_DBG, "requested set value for %s out of range", name); + goto error_recovery; + } + + setting->value = val; + + callback = setting->update; + data = setting->data; + + /* Release the mutex before calling the update callback, to avoid + * possible deadlocks with FluidSynths API lock */ fluid_rec_mutex_unlock (settings->mutex); - return retval; + if (callback) { + (*callback)(data, name, val); + } + + return FLUID_OK; + +error_recovery: + fluid_rec_mutex_unlock (settings->mutex); + return FLUID_FAILED; } /** From 1b5cf26000fe2ed0062e8e69153d23af76dc6bb3 Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 20:51:21 +0100 Subject: [PATCH 05/11] Change settings callback to return void The callback return value is not used anywhere anyway. --- src/synth/fluid_synth.c | 26 +++++++++++--------------- src/utils/fluid_settings.h | 6 +++--- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index dbe7e6bc..47d8af3d 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -43,9 +43,9 @@ static int fluid_synth_noteon_LOCAL(fluid_synth_t* synth, int chan, int key, int vel); static int fluid_synth_noteoff_LOCAL(fluid_synth_t* synth, int chan, int key); static int fluid_synth_cc_LOCAL(fluid_synth_t* synth, int channum, int num); -static int fluid_synth_update_device_id (fluid_synth_t *synth, char *name, +static void fluid_synth_update_device_id (fluid_synth_t *synth, char *name, int value); -static int fluid_synth_update_overflow (fluid_synth_t *synth, char *name, +static void fluid_synth_update_overflow (fluid_synth_t *synth, char *name, fluid_real_t value); static int fluid_synth_sysex_midi_tuning (fluid_synth_t *synth, const char *data, int len, char *response, @@ -71,12 +71,12 @@ fluid_synth_get_preset_by_sfont_name(fluid_synth_t* synth, const char *sfontname unsigned int banknum, unsigned int prognum); static void fluid_synth_update_presets(fluid_synth_t* synth); -static int fluid_synth_update_sample_rate(fluid_synth_t* synth, +static void fluid_synth_update_sample_rate(fluid_synth_t* synth, char* name, double value); -static int fluid_synth_update_gain(fluid_synth_t* synth, +static void fluid_synth_update_gain(fluid_synth_t* synth, char* name, double value); static void fluid_synth_update_gain_LOCAL(fluid_synth_t* synth); -static int fluid_synth_update_polyphony(fluid_synth_t* synth, +static void fluid_synth_update_polyphony(fluid_synth_t* synth, char* name, int value); static int fluid_synth_update_polyphony_LOCAL(fluid_synth_t* synth, int new_polyphony); static void init_dither(void); @@ -1331,13 +1331,12 @@ fluid_synth_get_cc(fluid_synth_t* synth, int chan, int num, int* pval) /* * Handler for synth.device-id setting. */ -static int +static void fluid_synth_update_device_id (fluid_synth_t *synth, char *name, int value) { fluid_synth_api_enter(synth); synth->device_id = value; fluid_synth_api_exit(synth); - return 0; } /** @@ -2345,11 +2344,10 @@ fluid_synth_update_presets(fluid_synth_t* synth) } /* Handler for synth.sample-rate setting. */ -static int +static void fluid_synth_update_sample_rate(fluid_synth_t* synth, char* name, double value) { fluid_synth_set_sample_rate(synth, (float) value); - return 0; } /** @@ -2381,11 +2379,10 @@ fluid_synth_set_sample_rate(fluid_synth_t* synth, float sample_rate) /* Handler for synth.gain setting. */ -static int +static void fluid_synth_update_gain(fluid_synth_t* synth, char* name, double value) { fluid_synth_set_gain(synth, (float) value); - return 0; } /** @@ -2442,11 +2439,10 @@ fluid_synth_get_gain(fluid_synth_t* synth) /* * Handler for synth.polyphony setting. */ -static int +static void fluid_synth_update_polyphony(fluid_synth_t* synth, char* name, int value) { fluid_synth_set_polyphony(synth, value); - return 0; } /** @@ -3129,7 +3125,7 @@ fluid_synth_render_blocks(fluid_synth_t* synth, int blockcount) } -static int fluid_synth_update_overflow (fluid_synth_t *synth, char *name, +static void fluid_synth_update_overflow (fluid_synth_t *synth, char *name, fluid_real_t value) { double d; @@ -3146,7 +3142,7 @@ static int fluid_synth_update_overflow (fluid_synth_t *synth, char *name, fluid_settings_getnum(synth->settings, "synth.overflow.age", &d); synth->overflow.age = d; - FLUID_API_RETURN(0); + fluid_synth_api_exit(synth); } diff --git a/src/utils/fluid_settings.h b/src/utils/fluid_settings.h index 8264b89d..65f23280 100644 --- a/src/utils/fluid_settings.h +++ b/src/utils/fluid_settings.h @@ -26,14 +26,14 @@ int fluid_settings_add_option(fluid_settings_t* settings, const char* name, cons int fluid_settings_remove_option(fluid_settings_t* settings, const char* name, const char* s); -typedef int (*fluid_str_update_t)(void* data, const char* name, const char* value); +typedef void (*fluid_str_update_t)(void* data, const char* name, const char* value); int fluid_settings_register_str(fluid_settings_t* settings, const char* name, const char* def, int hints); int fluid_settings_callback_str(fluid_settings_t* settings, const char* name, fluid_str_update_t fun, void* data); -typedef int (*fluid_num_update_t)(void* data, const char* name, double value); +typedef void (*fluid_num_update_t)(void* data, const char* name, double value); int fluid_settings_register_num(fluid_settings_t* settings, const char* name, double def, double min, double max, int hints); @@ -41,7 +41,7 @@ int fluid_settings_callback_num(fluid_settings_t* settings, const char* name, fluid_num_update_t fun, void* data); -typedef int (*fluid_int_update_t)(void* data, const char* name, int value); +typedef void (*fluid_int_update_t)(void* data, const char* name, int value); int fluid_settings_register_int(fluid_settings_t* settings, const char* name, int def, int min, int max, int hints); int fluid_settings_callback_int(fluid_settings_t* settings, const char* name, From 750ffc003b3b5d6a322d8ebe8559d7d240e18df1 Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 21:02:21 +0100 Subject: [PATCH 06/11] Cleanup settings callback signatures to avoid error hiding casts Removes the need to cast the callback functions when registering them. Also makes the needed cast from (void *) to (fluid_synth_t *) explicit in the callback handlers. --- src/synth/fluid_synth.c | 53 ++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index 47d8af3d..6ef0f61f 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -43,10 +43,8 @@ static int fluid_synth_noteon_LOCAL(fluid_synth_t* synth, int chan, int key, int vel); static int fluid_synth_noteoff_LOCAL(fluid_synth_t* synth, int chan, int key); static int fluid_synth_cc_LOCAL(fluid_synth_t* synth, int channum, int num); -static void fluid_synth_update_device_id (fluid_synth_t *synth, char *name, - int value); -static void fluid_synth_update_overflow (fluid_synth_t *synth, char *name, - fluid_real_t value); +static void fluid_synth_update_device_id (void *data, const char *name, int value); +static void fluid_synth_update_overflow (void *data, const char *name, double value); static int fluid_synth_sysex_midi_tuning (fluid_synth_t *synth, const char *data, int len, char *response, int *response_len, int avail_response, @@ -71,13 +69,10 @@ fluid_synth_get_preset_by_sfont_name(fluid_synth_t* synth, const char *sfontname unsigned int banknum, unsigned int prognum); static void fluid_synth_update_presets(fluid_synth_t* synth); -static void fluid_synth_update_sample_rate(fluid_synth_t* synth, - char* name, double value); -static void fluid_synth_update_gain(fluid_synth_t* synth, - char* name, double value); +static void fluid_synth_update_sample_rate(void *data, const char* name, double value); +static void fluid_synth_update_gain(void *data, const char* name, double value); static void fluid_synth_update_gain_LOCAL(fluid_synth_t* synth); -static void fluid_synth_update_polyphony(fluid_synth_t* synth, - char* name, int value); +static void fluid_synth_update_polyphony(void *data, const char* name, int value); static int fluid_synth_update_polyphony_LOCAL(fluid_synth_t* synth, int new_polyphony); static void init_dither(void); static FLUID_INLINE int roundi (float x); @@ -583,23 +578,23 @@ new_fluid_synth(fluid_settings_t *settings) /* register the callbacks */ fluid_settings_callback_num(settings, "synth.sample-rate", - (fluid_num_update_t) fluid_synth_update_sample_rate, synth); + fluid_synth_update_sample_rate, synth); fluid_settings_callback_num(settings, "synth.gain", - (fluid_num_update_t) fluid_synth_update_gain, synth); + fluid_synth_update_gain, synth); fluid_settings_callback_int(settings, "synth.polyphony", - (fluid_int_update_t) fluid_synth_update_polyphony, synth); + fluid_synth_update_polyphony, synth); fluid_settings_callback_int(settings, "synth.device-id", - (fluid_int_update_t) fluid_synth_update_device_id, synth); + fluid_synth_update_device_id, synth); fluid_settings_callback_num(settings, "synth.overflow.percussion", - (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_synth_update_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.sustained", - (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_synth_update_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.released", - (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_synth_update_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.age", - (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_synth_update_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.volume", - (fluid_num_update_t) fluid_synth_update_overflow, synth); + fluid_synth_update_overflow, synth); /* do some basic sanity checking on the settings */ @@ -1332,8 +1327,11 @@ fluid_synth_get_cc(fluid_synth_t* synth, int chan, int num, int* pval) * Handler for synth.device-id setting. */ static void -fluid_synth_update_device_id (fluid_synth_t *synth, char *name, int value) +fluid_synth_update_device_id (void *data, const char *name, int value) { + fluid_synth_t *synth = (fluid_synth_t *)data; + fluid_return_if_fail(synth != NULL); + fluid_synth_api_enter(synth); synth->device_id = value; fluid_synth_api_exit(synth); @@ -2345,8 +2343,9 @@ fluid_synth_update_presets(fluid_synth_t* synth) /* Handler for synth.sample-rate setting. */ static void -fluid_synth_update_sample_rate(fluid_synth_t* synth, char* name, double value) +fluid_synth_update_sample_rate(void *data, const char* name, double value) { + fluid_synth_t *synth = (fluid_synth_t *)data; fluid_synth_set_sample_rate(synth, (float) value); } @@ -2380,8 +2379,9 @@ fluid_synth_set_sample_rate(fluid_synth_t* synth, float sample_rate) /* Handler for synth.gain setting. */ static void -fluid_synth_update_gain(fluid_synth_t* synth, char* name, double value) +fluid_synth_update_gain(void *data, const char* name, double value) { + fluid_synth_t *synth = (fluid_synth_t *)data; fluid_synth_set_gain(synth, (float) value); } @@ -2440,8 +2440,9 @@ fluid_synth_get_gain(fluid_synth_t* synth) * Handler for synth.polyphony setting. */ static void -fluid_synth_update_polyphony(fluid_synth_t* synth, char* name, int value) +fluid_synth_update_polyphony(void *data, const char* name, int value) { + fluid_synth_t *synth = (fluid_synth_t *)data; fluid_synth_set_polyphony(synth, value); } @@ -3125,10 +3126,12 @@ fluid_synth_render_blocks(fluid_synth_t* synth, int blockcount) } -static void fluid_synth_update_overflow (fluid_synth_t *synth, char *name, - fluid_real_t value) +static void fluid_synth_update_overflow (void *data, const char *name, double value) { double d; + fluid_synth_t *synth = (fluid_synth_t *)data; + fluid_return_if_fail(synth != NULL); + fluid_synth_api_enter(synth); fluid_settings_getnum(synth->settings, "synth.overflow.percussion", &d); From 6b79c05dc26ebac152b71039ba35e05c6ef8a34d Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 21:09:06 +0100 Subject: [PATCH 07/11] Rename setting callbacks to clarify their purpose --- src/synth/fluid_synth.c | 44 ++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index 6ef0f61f..c1df1ce3 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -43,8 +43,6 @@ static int fluid_synth_noteon_LOCAL(fluid_synth_t* synth, int chan, int key, int vel); static int fluid_synth_noteoff_LOCAL(fluid_synth_t* synth, int chan, int key); static int fluid_synth_cc_LOCAL(fluid_synth_t* synth, int channum, int num); -static void fluid_synth_update_device_id (void *data, const char *name, int value); -static void fluid_synth_update_overflow (void *data, const char *name, double value); static int fluid_synth_sysex_midi_tuning (fluid_synth_t *synth, const char *data, int len, char *response, int *response_len, int avail_response, @@ -69,10 +67,7 @@ fluid_synth_get_preset_by_sfont_name(fluid_synth_t* synth, const char *sfontname unsigned int banknum, unsigned int prognum); static void fluid_synth_update_presets(fluid_synth_t* synth); -static void fluid_synth_update_sample_rate(void *data, const char* name, double value); -static void fluid_synth_update_gain(void *data, const char* name, double value); static void fluid_synth_update_gain_LOCAL(fluid_synth_t* synth); -static void fluid_synth_update_polyphony(void *data, const char* name, int value); static int fluid_synth_update_polyphony_LOCAL(fluid_synth_t* synth, int new_polyphony); static void init_dither(void); static FLUID_INLINE int roundi (float x); @@ -103,6 +98,12 @@ static void fluid_synth_set_gen_LOCAL (fluid_synth_t* synth, int chan, int param, float value, int absolute); static void fluid_synth_stop_LOCAL (fluid_synth_t *synth, unsigned int id); +/* Callback handlers for real-time settings */ +static void fluid_synth_handle_sample_rate(void *data, const char *name, double value); +static void fluid_synth_handle_gain(void *data, const char *name, double value); +static void fluid_synth_handle_polyphony(void *data, const char *name, int value); +static void fluid_synth_handle_device_id(void *data, const char *name, int value); +static void fluid_synth_handle_overflow(void *data, const char *name, double value); /*************************************************************** @@ -578,23 +579,23 @@ new_fluid_synth(fluid_settings_t *settings) /* register the callbacks */ fluid_settings_callback_num(settings, "synth.sample-rate", - fluid_synth_update_sample_rate, synth); + fluid_synth_handle_sample_rate, synth); fluid_settings_callback_num(settings, "synth.gain", - fluid_synth_update_gain, synth); + fluid_synth_handle_gain, synth); fluid_settings_callback_int(settings, "synth.polyphony", - fluid_synth_update_polyphony, synth); + fluid_synth_handle_polyphony, synth); fluid_settings_callback_int(settings, "synth.device-id", - fluid_synth_update_device_id, synth); + fluid_synth_handle_device_id, synth); fluid_settings_callback_num(settings, "synth.overflow.percussion", - fluid_synth_update_overflow, synth); + fluid_synth_handle_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.sustained", - fluid_synth_update_overflow, synth); + fluid_synth_handle_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.released", - fluid_synth_update_overflow, synth); + fluid_synth_handle_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.age", - fluid_synth_update_overflow, synth); + fluid_synth_handle_overflow, synth); fluid_settings_callback_num(settings, "synth.overflow.volume", - fluid_synth_update_overflow, synth); + fluid_synth_handle_overflow, synth); /* do some basic sanity checking on the settings */ @@ -728,7 +729,7 @@ new_fluid_synth(fluid_settings_t *settings) fluid_synth_set_sample_rate(synth, synth->sample_rate); - fluid_synth_update_overflow(synth, "", 0.0f); + fluid_synth_handle_overflow(synth, "", 0.0f); fluid_synth_update_mixer(synth, fluid_rvoice_mixer_set_polyphony, synth->polyphony, 0.0f); fluid_synth_set_reverb_on(synth, fluid_atomic_int_get(&synth->with_reverb)); @@ -1327,7 +1328,7 @@ fluid_synth_get_cc(fluid_synth_t* synth, int chan, int num, int* pval) * Handler for synth.device-id setting. */ static void -fluid_synth_update_device_id (void *data, const char *name, int value) +fluid_synth_handle_device_id (void *data, const char *name, int value) { fluid_synth_t *synth = (fluid_synth_t *)data; fluid_return_if_fail(synth != NULL); @@ -2343,7 +2344,7 @@ fluid_synth_update_presets(fluid_synth_t* synth) /* Handler for synth.sample-rate setting. */ static void -fluid_synth_update_sample_rate(void *data, const char* name, double value) +fluid_synth_handle_sample_rate(void *data, const char* name, double value) { fluid_synth_t *synth = (fluid_synth_t *)data; fluid_synth_set_sample_rate(synth, (float) value); @@ -2379,7 +2380,7 @@ fluid_synth_set_sample_rate(fluid_synth_t* synth, float sample_rate) /* Handler for synth.gain setting. */ static void -fluid_synth_update_gain(void *data, const char* name, double value) +fluid_synth_handle_gain(void *data, const char* name, double value) { fluid_synth_t *synth = (fluid_synth_t *)data; fluid_synth_set_gain(synth, (float) value); @@ -2440,7 +2441,7 @@ fluid_synth_get_gain(fluid_synth_t* synth) * Handler for synth.polyphony setting. */ static void -fluid_synth_update_polyphony(void *data, const char* name, int value) +fluid_synth_handle_polyphony(void *data, const char* name, int value) { fluid_synth_t *synth = (fluid_synth_t *)data; fluid_synth_set_polyphony(synth, value); @@ -3126,7 +3127,10 @@ fluid_synth_render_blocks(fluid_synth_t* synth, int blockcount) } -static void fluid_synth_update_overflow (void *data, const char *name, double value) +/* + * Handler for synth.overflow.* settings. + */ +static void fluid_synth_handle_overflow (void *data, const char *name, double value) { double d; fluid_synth_t *synth = (fluid_synth_t *)data; From 69cf9e1ec223f557953e731952bc7f0e8f108cc8 Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 21:18:30 +0100 Subject: [PATCH 08/11] overflow settings callback should use passed value, not query settings --- src/synth/fluid_synth.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index c1df1ce3..7a31615c 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -3132,22 +3132,26 @@ fluid_synth_render_blocks(fluid_synth_t* synth, int blockcount) */ static void fluid_synth_handle_overflow (void *data, const char *name, double value) { - double d; fluid_synth_t *synth = (fluid_synth_t *)data; fluid_return_if_fail(synth != NULL); fluid_synth_api_enter(synth); - - fluid_settings_getnum(synth->settings, "synth.overflow.percussion", &d); - synth->overflow.percussion = d; - fluid_settings_getnum(synth->settings, "synth.overflow.released", &d); - synth->overflow.released = d; - fluid_settings_getnum(synth->settings, "synth.overflow.sustained", &d); - synth->overflow.sustained = d; - fluid_settings_getnum(synth->settings, "synth.overflow.volume", &d); - synth->overflow.volume = d; - fluid_settings_getnum(synth->settings, "synth.overflow.age", &d); - synth->overflow.age = d; + + if (FLUID_STRCMP(name, "synth.overflow.percussion") == 0) { + synth->overflow.percussion = value; + } + else if (FLUID_STRCMP(name, "synth.overflow.released") == 0) { + synth->overflow.released = value; + } + else if (FLUID_STRCMP(name, "synth.overflow.sustained") == 0) { + synth->overflow.sustained = value; + } + else if (FLUID_STRCMP(name, "synth.overflow.volume") == 0) { + synth->overflow.volume = value; + } + else if (FLUID_STRCMP(name, "synth.overflow.age") == 0) { + synth->overflow.age = value; + } fluid_synth_api_exit(synth); } From ee227986e56c9fea55ca293e39acd777cc64f5bd Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 21:24:31 +0100 Subject: [PATCH 09/11] Explicity set overflow settings in new_fluid_synth Removes the need for faked callback calls. --- src/synth/fluid_synth.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index 7a31615c..aaf01e84 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -516,6 +516,7 @@ new_fluid_synth(fluid_settings_t *settings) fluid_synth_t* synth; fluid_sfloader_t* loader; double gain; + double num_val; int i, nbuf; int with_ladspa = 0; int with_reverb = 0; @@ -577,6 +578,17 @@ new_fluid_synth(fluid_settings_t *settings) fluid_settings_getint(settings, "synth.device-id", &synth->device_id); fluid_settings_getint(settings, "synth.cpu-cores", &synth->cores); + fluid_settings_getnum(settings, "synth.overflow.percussion", &num_val); + synth->overflow.percussion = num_val; + fluid_settings_getnum(settings, "synth.overflow.released", &num_val); + synth->overflow.released = num_val; + fluid_settings_getnum(settings, "synth.overflow.sustained", &num_val); + synth->overflow.sustained = num_val; + fluid_settings_getnum(settings, "synth.overflow.volume", &num_val); + synth->overflow.volume = num_val; + fluid_settings_getnum(settings, "synth.overflow.age", &num_val); + synth->overflow.age = num_val; + /* register the callbacks */ fluid_settings_callback_num(settings, "synth.sample-rate", fluid_synth_handle_sample_rate, synth); @@ -729,7 +741,6 @@ new_fluid_synth(fluid_settings_t *settings) fluid_synth_set_sample_rate(synth, synth->sample_rate); - fluid_synth_handle_overflow(synth, "", 0.0f); fluid_synth_update_mixer(synth, fluid_rvoice_mixer_set_polyphony, synth->polyphony, 0.0f); fluid_synth_set_reverb_on(synth, fluid_atomic_int_get(&synth->with_reverb)); From ba48024f596d3682f3462dec9d5686fe842e7dc7 Mon Sep 17 00:00:00 2001 From: Marcus Weseloh Date: Fri, 1 Dec 2017 21:35:03 +0100 Subject: [PATCH 10/11] Ensure that fluid_synth_set_reverb_on is protect by api mutex The function calls fluid_synth_update_mixer, which should only be called with the mutex held. This also removes the need for fluid_synth_t::with_reverb to be an atomic. fluid_synth_t::with_chorus was already protected by the mutex and doesn't need to be an atomic either. --- src/synth/fluid_synth.c | 20 +++++++++----------- src/synth/fluid_synth.h | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c index aaf01e84..e0c16e29 100644 --- a/src/synth/fluid_synth.c +++ b/src/synth/fluid_synth.c @@ -519,8 +519,6 @@ new_fluid_synth(fluid_settings_t *settings) double num_val; int i, nbuf; int with_ladspa = 0; - int with_reverb = 0; - int with_chorus = 0; /* initialize all the conversion tables and other stuff */ if (fluid_atomic_int_compare_and_exchange(&fluid_synth_initialized, 0, 1)) @@ -561,10 +559,8 @@ new_fluid_synth(fluid_settings_t *settings) synth->settings = settings; - fluid_settings_getint(settings, "synth.reverb.active", &with_reverb); - fluid_atomic_int_set(&synth->with_reverb, with_reverb); - fluid_settings_getint(settings, "synth.chorus.active", &with_chorus); - fluid_atomic_int_set(&synth->with_chorus, with_chorus); + fluid_settings_getint(settings, "synth.reverb.active", &synth->with_reverb); + fluid_settings_getint(settings, "synth.chorus.active", &synth->with_chorus); fluid_settings_getint(settings, "synth.verbose", &synth->verbose); fluid_settings_getint(settings, "synth.polyphony", &synth->polyphony); @@ -740,11 +736,10 @@ new_fluid_synth(fluid_settings_t *settings) } fluid_synth_set_sample_rate(synth, synth->sample_rate); - fluid_synth_update_mixer(synth, fluid_rvoice_mixer_set_polyphony, synth->polyphony, 0.0f); - fluid_synth_set_reverb_on(synth, fluid_atomic_int_get(&synth->with_reverb)); - fluid_synth_set_chorus_on(synth, fluid_atomic_int_get(&synth->with_chorus)); + fluid_synth_set_reverb_on(synth, synth->with_reverb); + fluid_synth_set_chorus_on(synth, synth->with_chorus); synth->cur = FLUID_BUFSIZE; synth->curmax = 0; @@ -3900,9 +3895,12 @@ fluid_synth_set_reverb_on(fluid_synth_t* synth, int on) { fluid_return_if_fail (synth != NULL); - fluid_atomic_int_set (&synth->with_reverb, on != 0); + fluid_synth_api_enter(synth); + + synth->with_reverb = (on != 0); fluid_synth_update_mixer(synth, fluid_rvoice_mixer_set_reverb_enabled, on != 0, 0.0f); + fluid_synth_api_exit(synth); } /** @@ -4104,7 +4102,7 @@ fluid_synth_set_chorus_on(fluid_synth_t* synth, int on) fluid_return_if_fail (synth != NULL); fluid_synth_api_enter(synth); - fluid_atomic_int_set (&synth->with_chorus, on != 0); + synth->with_chorus = (on != 0); fluid_synth_update_mixer(synth, fluid_rvoice_mixer_set_chorus_enabled, on != 0, 0.0f); fluid_synth_api_exit(synth); diff --git a/src/synth/fluid_synth.h b/src/synth/fluid_synth.h index 6f8d0c63..a21902b0 100644 --- a/src/synth/fluid_synth.h +++ b/src/synth/fluid_synth.h @@ -113,8 +113,8 @@ struct _fluid_synth_t fluid_settings_t* settings; /**< the synthesizer settings */ int device_id; /**< Device ID used for SYSEX messages */ int polyphony; /**< Maximum polyphony */ - fluid_atomic_int_t with_reverb; /**< Should the synth use the built-in reverb unit? */ - fluid_atomic_int_t with_chorus; /**< Should the synth use the built-in chorus unit? */ + int with_reverb; /**< Should the synth use the built-in reverb unit? */ + int with_chorus; /**< Should the synth use the built-in chorus unit? */ int verbose; /**< Turn verbose mode on? */ double sample_rate; /**< The sample rate */ int midi_channels; /**< the number of MIDI channels (>= 16) */ From 50923b3f0e45572490bfc39772ae27651cd64f60 Mon Sep 17 00:00:00 2001 From: derselbst Date: Sat, 2 Dec 2017 11:01:31 +0100 Subject: [PATCH 11/11] remove redundant string termination already done by FLUID_STRNCPY macro --- src/utils/fluid_settings.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/utils/fluid_settings.c b/src/utils/fluid_settings.c index 0d113638..72a096c1 100644 --- a/src/utils/fluid_settings.c +++ b/src/utils/fluid_settings.c @@ -920,7 +920,6 @@ fluid_settings_copystr(fluid_settings_t* settings, const char *name, if (setting->value) { FLUID_STRNCPY (str, setting->value, len); - str[len - 1] = 0; /* Force terminate, in case of truncation */ } retval = FLUID_OK; @@ -932,7 +931,6 @@ fluid_settings_copystr(fluid_settings_t* settings, const char *name, if (setting->hints & FLUID_HINT_TOGGLED) { FLUID_STRNCPY (str, setting->value ? "yes" : "no", len); - str[len - 1] = 0; /* Force terminate, in case of truncation */ retval = FLUID_OK; }