From f62306d14218354606a3b1fae8ac2c3fa8280f58 Mon Sep 17 00:00:00 2001 From: David Henningsson Date: Wed, 14 Jul 2010 08:45:17 +0000 Subject: [PATCH] Experimental support for changing sample-rate --- fluidsynth/include/fluidsynth/synth.h | 1 + fluidsynth/src/drivers/fluid_jack.c | 5 ++- fluidsynth/src/synth/fluid_synth.c | 49 ++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/fluidsynth/include/fluidsynth/synth.h b/fluidsynth/include/fluidsynth/synth.h index 04bfe8e0..56a3c7d1 100644 --- a/fluidsynth/include/fluidsynth/synth.h +++ b/fluidsynth/include/fluidsynth/synth.h @@ -177,6 +177,7 @@ FLUIDSYNTH_API int fluid_synth_count_effects_channels(fluid_synth_t* synth); /* Synthesis parameters */ +FLUIDSYNTH_API void fluid_synth_set_sample_rate(fluid_synth_t* synth, float sample_rate); FLUIDSYNTH_API void fluid_synth_set_gain(fluid_synth_t* synth, float gain); FLUIDSYNTH_API float fluid_synth_get_gain(fluid_synth_t* synth); FLUIDSYNTH_API int fluid_synth_set_polyphony(fluid_synth_t* synth, int polyphony); diff --git a/fluidsynth/src/drivers/fluid_jack.c b/fluidsynth/src/drivers/fluid_jack.c index a99752c5..b788c36b 100644 --- a/fluidsynth/src/drivers/fluid_jack.c +++ b/fluidsynth/src/drivers/fluid_jack.c @@ -404,10 +404,9 @@ new_fluid_jack_audio_driver2(fluid_settings_t* settings, fluid_audio_func_t func fluid_settings_getnum (settings, "synth.sample-rate", &sample_rate); if ((int)sample_rate != jack_srate) { - /* There's currently no way to change the sampling rate of the - synthesizer after it's been created. */ - FLUID_LOG(FLUID_WARN, "Jack sample rate mismatch, expect tuning issues" + FLUID_LOG(FLUID_WARN, "Jack sample rate mismatch, adjusting." " (synth.sample-rate=%lu, jackd=%lu)", (int)sample_rate, jack_srate); + fluid_settings_setnum (settings, "synth.sample-rate", jack_srate); } diff --git a/fluidsynth/src/synth/fluid_synth.c b/fluidsynth/src/synth/fluid_synth.c index e1130669..71e52f82 100644 --- a/fluidsynth/src/synth/fluid_synth.c +++ b/fluidsynth/src/synth/fluid_synth.c @@ -88,6 +88,8 @@ 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, + char* name, double value); static int fluid_synth_update_gain(fluid_synth_t* synth, char* name, double value); static void fluid_synth_update_gain_LOCAL(fluid_synth_t* synth); @@ -565,10 +567,10 @@ 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_getint(settings, "synth.min-note-length", &i); - synth->min_note_length_ticks = (unsigned int) (i*synth->sample_rate/1000.0f); - /* register the callbacks */ + fluid_settings_register_num(settings, "synth.sample-rate", + 44100.0f, 22050.0f, 96000.0f, 0, + (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_num_update_t) fluid_synth_update_gain, synth); @@ -646,8 +648,7 @@ new_fluid_synth(fluid_settings_t *settings) nbuf, synth->effects_channels); if (synth->eventhandler == NULL) goto error_recovery; - - + /* allocate and add the default sfont loader */ loader = new_fluid_defsfloader(); @@ -683,6 +684,8 @@ new_fluid_synth(fluid_settings_t *settings) } } + fluid_synth_set_sample_rate(synth, synth->sample_rate); + fluid_rvoice_eventhandler_push(synth->eventhandler, fluid_rvoice_mixer_set_polyphony, synth->eventhandler->mixer, synth->polyphony, 0.0f); @@ -2577,6 +2580,42 @@ fluid_synth_update_presets(fluid_synth_t* synth) } } +/* Handler for synth.gain setting. */ +static int +fluid_synth_update_sample_rate(fluid_synth_t* synth, char* name, double value) +{ + fluid_synth_set_sample_rate(synth, (float) value); + return 0; +} + +/** + * Set sample rate of the synth. + * NOTE: This function is currently experimental and should only be + * used when no voices or notes are active, and before any rendering calls. + * @param synth FluidSynth instance + * @param sample_rate New sample rate (Hz) + * @since 1.1.2 + */ +void +fluid_synth_set_sample_rate(fluid_synth_t* synth, float sample_rate) +{ + int i; + fluid_return_if_fail (synth != NULL); + fluid_synth_api_enter(synth); + fluid_clip (sample_rate, 22500.0f, 96000.0f); + synth->sample_rate = sample_rate; + + fluid_settings_getint(synth->settings, "synth.min-note-length", &i); + synth->min_note_length_ticks = (unsigned int) (i*synth->sample_rate/1000.0f); + + fluid_rvoice_eventhandler_push(synth->eventhandler, + fluid_rvoice_mixer_set_samplerate, + synth->eventhandler->mixer, + 0, sample_rate); + fluid_synth_api_exit(synth); +} + + /* Handler for synth.gain setting. */ static int fluid_synth_update_gain(fluid_synth_t* synth, char* name, double value)