From f75ed6243bb0e3bff820e5eed8007712bee41e73 Mon Sep 17 00:00:00 2001 From: David Henningsson Date: Mon, 7 Feb 2011 07:30:50 +0000 Subject: [PATCH] Empty event queue from non-realtime context on startup Some time extensive events were queued to be carried out on the first audio block rendering, when in fact they could be carried out before rendering has even started, from a non-RT context. Reported-by: Krysztof Foltman --- fluidsynth/src/drivers/fluid_jack.c | 37 +++++++++++++++-------------- fluidsynth/src/synth/fluid_synth.c | 13 ++++++++++ fluidsynth/src/synth/fluid_synth.h | 2 ++ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/fluidsynth/src/drivers/fluid_jack.c b/fluidsynth/src/drivers/fluid_jack.c index 15ddb527..78c6441c 100644 --- a/fluidsynth/src/drivers/fluid_jack.c +++ b/fluidsynth/src/drivers/fluid_jack.c @@ -246,6 +246,8 @@ fluid_jack_client_register_ports (void *driver, int isaudio, jack_client_t *clie char name[64]; int multi; int i; + int jack_srate; + double sample_rate; if (!isaudio) { @@ -323,6 +325,23 @@ fluid_jack_client_register_ports (void *driver, int isaudio, jack_client_t *clie } } + + /* Adjust sample rate to match JACK's */ + jack_srate = jack_get_sample_rate (client); + FLUID_LOG (FLUID_DBG, "Jack engine sample rate: %lu", jack_srate); + + fluid_settings_getnum (settings, "synth.sample-rate", &sample_rate); + + if ((int)sample_rate != jack_srate) { + FLUID_LOG(FLUID_INFO, "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); + } + + /* Changing sample rate is non RT, so make sure we process it and/or other things now */ + if (dev->callback == NULL) + fluid_synth_process_event_queue(dev->data); + return FLUID_OK; } @@ -370,8 +389,6 @@ new_fluid_jack_audio_driver2(fluid_settings_t* settings, fluid_audio_func_t func jack_client_t *client; const char ** jack_ports; /* for looking up ports */ int autoconnect = 0; - int jack_srate; - double sample_rate; int i; dev = FLUID_NEW(fluid_jack_audio_driver_t); @@ -394,22 +411,6 @@ new_fluid_jack_audio_driver2(fluid_settings_t* settings, fluid_audio_func_t func client = dev->client_ref->client; - /* display the current sample rate. once the client is activated - (see below), you should rely on your own sample rate - callback (see above) for this value. - */ - jack_srate = jack_get_sample_rate (client); - FLUID_LOG (FLUID_DBG, "Jack engine sample rate: %lu", jack_srate); - - fluid_settings_getnum (settings, "synth.sample-rate", &sample_rate); - - if ((int)sample_rate != jack_srate) { - FLUID_LOG(FLUID_INFO, "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); - } - - /* connect the ports. */ diff --git a/fluidsynth/src/synth/fluid_synth.c b/fluidsynth/src/synth/fluid_synth.c index 5682acbe..2aadb675 100644 --- a/fluidsynth/src/synth/fluid_synth.c +++ b/fluidsynth/src/synth/fluid_synth.c @@ -753,6 +753,8 @@ new_fluid_synth(fluid_settings_t *settings) else if (fluid_settings_str_equal (settings, "synth.midi-bank-select", "mma") == 1) synth->bank_select = FLUID_BANK_STYLE_MMA; + fluid_synth_process_event_queue(synth); + /* FIXME */ synth->start = fluid_curtime(); @@ -2729,6 +2731,17 @@ fluid_synth_check_finished_voices(fluid_synth_t* synth) } } +/** + * Process all waiting events in the rvoice queue. + * Make sure no (other) rendering is running in parallel when + * you call this function! + */ +void fluid_synth_process_event_queue(fluid_synth_t* synth) +{ + fluid_rvoice_eventhandler_dispatch_all(synth->eventhandler); +} + + /** * Process blocks (FLUID_BUFSIZE) of audio. * Must be called from renderer thread only! diff --git a/fluidsynth/src/synth/fluid_synth.h b/fluidsynth/src/synth/fluid_synth.h index fae03f55..d88d71a5 100644 --- a/fluidsynth/src/synth/fluid_synth.h +++ b/fluidsynth/src/synth/fluid_synth.h @@ -224,6 +224,8 @@ int delete_fluid_sample_timer(fluid_synth_t* synth, fluid_sample_timer_t* timer) void fluid_synth_api_enter(fluid_synth_t* synth); void fluid_synth_api_exit(fluid_synth_t* synth); +void fluid_synth_process_event_queue(fluid_synth_t* synth); + /* * misc */