From 98d395bab0e8b24d1baa70b83c9013ca4440f8a8 Mon Sep 17 00:00:00 2001 From: derselbst Date: Sun, 16 Feb 2020 10:03:45 +0100 Subject: [PATCH] Update fluidsynth_fx example to explain effects mixing --- doc/fluidsynth_fx.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/doc/fluidsynth_fx.c b/doc/fluidsynth_fx.c index f5a41598..ab18f9d5 100644 --- a/doc/fluidsynth_fx.c +++ b/doc/fluidsynth_fx.c @@ -44,21 +44,36 @@ int fx_function(void *data, int len, { struct fx_data_t *fx_data = (struct fx_data_t *) data; int i, k; - float *out_i; - /* Call the synthesizer to fill the output buffers with its - * audio output. */ - if(fluid_synth_process(fx_data->synth, len, nfx, fx, nout, out) != FLUID_OK) + if(fx == 0) { - /* Some error occurred. Very unlikely to happen, though. */ - return FLUID_FAILED; + /* Note that some audio drivers may not provide buffers for effects like + * reverb and chorus. In this case it's your decision what to do. If you + * had called fluid_synth_process() like in the else branch below, no + * effects would have been rendered. Instead, you may mix the effects + * directly into the out buffers. */ + if(fluid_synth_process(fx_data->synth, len, nout, out, nout, out) != FLUID_OK) + { + /* Some error occurred. Very unlikely to happen, though. */ + return FLUID_FAILED; + } + } + else + { + /* Call the synthesizer to fill the output buffers with its + * audio output. */ + if(fluid_synth_process(fx_data->synth, len, nfx, fx, nout, out) != FLUID_OK) + { + /* Some error occurred. Very unlikely to happen, though. */ + return FLUID_FAILED; + } } /* Apply your effects here. In this example, the gain is * applied to all the output buffers. */ for(i = 0; i < nout; i++) { - out_i = out[i]; + float *out_i = out[i]; for(k = 0; k < len; k++) { @@ -66,6 +81,16 @@ int fx_function(void *data, int len, } } + for(i = 0; i < nfx; i++) + { + float *fx_i = fx[i]; + + for(k = 0; k < len; k++) + { + fx_i[k] *= fx_data->gain; + } + } + return FLUID_OK; }