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 */