Path: blob/master/libs/fluidsynth/src/rvoice/fluid_chorus.c
4396 views
/* FluidSynth - A Software Synthesizer1*2* Copyright (C) 2003 Peter Hanappe, Markus Nentwig and others.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public License6* as published by the Free Software Foundation; either version 2.1 of7* the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful, but10* WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public15* License along with this library; if not, write to the Free16* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA17* 02110-1301, USA18*/1920/*21based on a chorus implementation made by Juergen Mueller And Sundry Contributors in 19982223CHANGES2425- Adapted for fluidsynth, Peter Hanappe, March 20022627- Variable delay line implementation using bandlimited28interpolation, code reorganization: Markus Nentwig May 20022930- Complete rewrite using lfo computed on the fly, first order all-pass31interpolator and adding stereo unit: Jean-Jacques Ceresa, Jul 201932*/333435/*36* Chorus effect.37*38* Flow diagram scheme for n delays ( 1 <= n <= MAX_CHORUS ):39*40* ________41* direct signal (not implemented) >-->| |42* _________ | |43* mono | | | |44* input ---+---->| delay 1 |-------------------------->| Stereo |---> right45* | |_________| | | output46* | /|\ | Unit |47* : | | |48* : +-----------------+ |(width) |49* : | Delay control 1 |<-+ | |50* : +-----------------+ | | |---> left51* | _________ | | | output52* | | | | | |53* +---->| delay n |-------------------------->| |54* |_________| | | |55* /|\ | |________|56* | | +--------------+ /|\57* +-----------------+ | |mod depth (ms)| |58* | Delay control n |<-*--|lfo speed (Hz)| gain-out59* +-----------------+ +--------------+60*61*62* The delay i is controlled by a sine or triangle modulation i ( 1 <= i <= n).63*64* The chorus unit process a monophonic input signal and produces stereo output65* controlled by WIDTH macro.66* Actually WIDTH is fixed to maximum value. But in the future, we could add a67* setting (e.g "synth.chorus.width") allowing the user to get a gradually stereo68* effect from minimum (monophonic) to maximum stereo effect.69*70* Delays lines are implemented using only one line for all chorus blocks.71* Each chorus block has it own lfo (sinus/triangle). Each lfo are out of phase72* to produce uncorrelated signal at the output of the delay line (this simulates73* the presence of individual line for each block). Each lfo modulates the length74* of the line using a depth modulation value and lfo frequency value common to75* all lfos.76*77* LFO modulators are computed on the fly, instead of using lfo lookup table.78* The advantages are:79* - Avoiding a lost of 608272 memory bytes when lfo speed is low (0.3Hz).80* - Allows to diminish the lfo speed lower limit to 0.1Hz instead of 0.3Hz.81* A speed of 0.1 is interesting for chorus. Using a lookuptable for 0.1Hz82* would require too much memory (1824816 bytes).83* - Interpolation make use of first order all-pass interpolator instead of84* bandlimited interpolation.85* - Although lfo modulator is computed on the fly, cpu load is lower than86* using lfo lookup table with bandlimited interpolator.87*/8889#include "fluid_chorus.h"90#include "fluid_sys.h"919293/*-------------------------------------------------------------------------------------94Private95--------------------------------------------------------------------------------------*/96// #define DEBUG_PRINT // allows message to be printed on the console.9798#define MAX_CHORUS 99 /* number maximum of block */99#define MAX_LEVEL 10 /* max output level */100#define MIN_SPEED_HZ 0.1 /* min lfo frequency (Hz) */101#define MAX_SPEED_HZ 5 /* max lfo frequency (Hz) */102103/* WIDTH [0..10] value define a stereo separation between left and right.104When 0, the output is monophonic. When > 0 , the output is stereophonic.105Actually WIDTH is fixed to maximum value. But in the future we could add a setting to106allow a gradually stereo effect from minimum (monophonic) to maximum stereo effect.107*/108#define WIDTH 10109110/* SCALE_WET_WIDTH is a compensation weight factor to get an output111amplitude (wet) rather independent of the width setting.1120: the output amplitude is fully dependent on the width setting.113>0: the output amplitude is less dependent on the width setting.114With a SCALE_WET_WIDTH of 0.2 the output amplitude is rather115independent of width setting (see fluid_chorus_set()).116*/117#define SCALE_WET_WIDTH 0.2f118#define SCALE_WET 1.0f119120#define MAX_SAMPLES 2048 /* delay length in sample (46.4 ms at sample rate: 44100Hz).*/121#define LOW_MOD_DEPTH 176 /* low mod_depth/2 in samples */122#define HIGH_MOD_DEPTH MAX_SAMPLES/2 /* high mod_depth in sample */123#define RANGE_MOD_DEPTH (HIGH_MOD_DEPTH - LOW_MOD_DEPTH)124125/* Important min max values for MOD_RATE */126/* mod rate define the rate at which the modulator is updated. Examples12750: the modulator is updated every 50 samples (less cpu cycles expensive).1281: the modulator is updated every sample (more cpu cycles expensive).129*/130/* MOD_RATE acceptable for max lfo speed (5Hz) and max modulation depth (46.6 ms) */131#define LOW_MOD_RATE 5 /* MOD_RATE acceptable for low modulation depth (8 ms) */132#define HIGH_MOD_RATE 4 /* MOD_RATE acceptable for max modulation depth (46.6 ms) */133/* and max lfo speed (5 Hz) */134#define RANGE_MOD_RATE (HIGH_MOD_RATE - LOW_MOD_RATE)135136/* some chorus cpu_load measurement dependent of modulation rate: mod_rate137(number of chorus blocks: 2)138139No stero unit:140mod_rate | chorus cpu load(%) | one voice cpu load (%)141----------------------------------------------------14250 | 0.204 |1435 | 0.256 | 0.1691441 | 0.417 |145146With stero unit:147mod_rate | chorus cpu load(%) | one voice cpu load (%)148----------------------------------------------------14950 | 0.220 |1505 | 0.274 | 0.1691511 | 0.465 |152153*/154155/*156Number of samples to add to the desired length of the delay line. This157allows to take account of rounding error interpolation when using large158modulation depth.1591 is sufficient for max modulation depth (46.6 ms) and max lfo speed (5 Hz).160*/161//#define INTERP_SAMPLES_NBR 0162#define INTERP_SAMPLES_NBR 1163164165/*-----------------------------------------------------------------------------166Sinusoidal modulator167-----------------------------------------------------------------------------*/168/* modulator */169typedef struct170{171// for sufficient precision members MUST be double! See https://github.com/FluidSynth/fluidsynth/issues/1331172double a1; /* Coefficient: a1 = 2 * cos(w) */173double buffer1; /* buffer1 */174double buffer2; /* buffer2 */175double reset_buffer2;/* reset value of buffer2 */176} sinus_modulator;177178/*-----------------------------------------------------------------------------179Triangle modulator180-----------------------------------------------------------------------------*/181typedef struct182{183fluid_real_t freq; /* Osc. Frequency (in Hertz) */184fluid_real_t val; /* internal current value */185fluid_real_t inc; /* increment value */186} triang_modulator;187188/*-----------------------------------------------------------------------------189modulator190-----------------------------------------------------------------------------*/191typedef struct192{193/*-------------*/194int line_out; /* current line out position for this modulator */195/*-------------*/196sinus_modulator sinus; /* sinus lfo */197triang_modulator triang; /* triangle lfo */198/*-------------------------*/199/* first order All-Pass interpolator members */200fluid_real_t frac_pos_mod; /* fractional position part between samples */201/* previous value used when interpolating using fractional */202fluid_real_t buffer;203} modulator;204205/* Private data for SKEL file */206struct _fluid_chorus_t207{208int type;209fluid_real_t depth_ms;210fluid_real_t level;211fluid_real_t speed_Hz;212int number_blocks;213fluid_real_t sample_rate;214215/* width control: 0 monophonic, > 0 more stereophonic */216fluid_real_t width;217fluid_real_t wet1, wet2;218219fluid_real_t *line; /* buffer line */220int size; /* effective internal size (in samples) */221222int line_in; /* line in position */223224/* center output position members */225fluid_real_t center_pos_mod; /* center output position modulated by modulator */226int mod_depth; /* modulation depth (in samples) */227228/* variable rate control of center output position */229int index_rate; /* index rate to know when to update center_pos_mod */230int mod_rate; /* rate at which center_pos_mod is updated */231232/* modulator member */233modulator mod[MAX_CHORUS]; /* sinus/triangle modulator */234};235236/*-----------------------------------------------------------------------------237Sets the frequency of sinus oscillator.238239For sufficient precision use double precision in set_sinus_frequency() computation !.240Never use: fluid_real_t , cosf(), sinf(), FLUID_COS(), FLUID_SIN(), FLUID_M_PI.241See https://github.com/FluidSynth/fluidsynth/issues/1331242243@param mod pointer on modulator structure.244@param freq frequency of the oscillator in Hz.245@param sample_rate sample rate on audio output in Hz.246@param phase initial phase of the oscillator in degree (0 to 360).247-----------------------------------------------------------------------------*/248static void set_sinus_frequency(sinus_modulator *mod,249float freq, float sample_rate, float phase)250{251double w = (2.0 * M_PI) * freq / sample_rate; /* step phase between each sinus wave sample (in radian) */252double a; /* initial phase at which the sinus wave must begin (in radian) */253254// DO NOT use potentially single precision cosf or FLUID_COS here! See https://github.com/FluidSynth/fluidsynth/issues/1331255mod->a1 = 2 * cos(w);256257a = (2.0 * M_PI / 360.0) * phase;258259mod->buffer2 = sin(a - w); /* y(n-1) = sin(-initial angle) */260mod->buffer1 = sin(a); /* y(n) = sin(initial phase) */261mod->reset_buffer2 = sin((M_PI / 2.0) - w); /* reset value for PI/2 */262}263264/*-----------------------------------------------------------------------------265Gets current value of sinus modulator:266y(n) = a1 . y(n-1) - y(n-2)267out = a1 . buffer1 - buffer2268269@param mod pointer on modulator structure.270@return current value of the modulator sine wave.271-----------------------------------------------------------------------------*/272static FLUID_INLINE double get_mod_sinus(sinus_modulator *mod)273{274double out;275out = mod->a1 * mod->buffer1 - mod->buffer2;276mod->buffer2 = mod->buffer1;277278if(out >= 1.0) /* reset in case of instability near PI/2 */279{280out = 1.0; /* forces output to the right value */281mod->buffer2 = mod->reset_buffer2;282}283284if(out <= -1.0) /* reset in case of instability near -PI/2 */285{286out = -1.0; /* forces output to the right value */287mod->buffer2 = - mod->reset_buffer2;288}289290mod->buffer1 = out;291return out;292}293294/*-----------------------------------------------------------------------------295Set the frequency of triangular oscillator296The frequency is converted in a slope value.297The initial value is set according to frac_phase which is a position298in the period relative to the beginning of the period.299For example: 0 is the beginning of the period, 1/4 is at 1/4 of the period300relative to the beginning.301302@param mod pointer on modulator structure.303@param freq frequency of the oscillator in Hz.304@param sample_rate sample rate on audio output in Hz.305@param frac_phase initial phase (see comment above).306-----------------------------------------------------------------------------*/307static void set_triangle_frequency(triang_modulator *mod, float freq,308float sample_rate, float frac_phase)309{310fluid_real_t ns_period; /* period in numbers of sample */311312if(freq <= 0.0)313{314freq = 0.5f;315}316317mod->freq = freq;318319ns_period = sample_rate / freq;320321/* the slope of a triangular osc (0 up to +1 down to -1 up to 0....) is equivalent322to the slope of a saw osc (0 -> +4) */323mod->inc = 4 / ns_period; /* positive slope */324325/* The initial value and the sign of the slope depend of initial phase:326initial value = = (ns_period * frac_phase) * slope327*/328mod->val = ns_period * frac_phase * mod->inc;329330if(1.0 <= mod->val && mod->val < 3.0)331{332mod->val = 2.0 - mod->val; /* 1.0 down to -1.0 */333mod->inc = -mod->inc; /* negative slope */334}335else if(3.0 <= mod->val)336{337mod->val = mod->val - 4.0; /* -1.0 up to +1.0. */338}339340/* else val < 1.0 */341}342343/*-----------------------------------------------------------------------------344Get current value of triangular oscillator345y(n) = y(n-1) + dy346347@param mod pointer on triang_modulator structure.348@return current value.349-----------------------------------------------------------------------------*/350static FLUID_INLINE fluid_real_t get_mod_triang(triang_modulator *mod)351{352mod->val = mod->val + mod->inc ;353354if(mod->val >= 1.0)355{356mod->inc = -mod->inc;357return 1.0;358}359360if(mod->val <= -1.0)361{362mod->inc = -mod->inc;363return -1.0;364}365366return mod->val;367}368/*-----------------------------------------------------------------------------369Reads the sample value out of the modulated delay line.370371@param chorus pointer on chorus unit.372@param mod pointer on modulator structure.373@return current value.374-----------------------------------------------------------------------------*/375static FLUID_INLINE fluid_real_t get_mod_delay(fluid_chorus_t *chorus,376modulator *mod)377{378fluid_real_t out_index; /* new modulated index position */379int int_out_index; /* integer part of out_index */380fluid_real_t out; /* value to return */381382/* Checks if the modulator must be updated (every mod_rate samples). */383/* Important: center_pos_mod must be used immediately for the384first sample. So, mdl->index_rate must be initialized385to mdl->mod_rate (new_mod_delay_line()) */386387if(chorus->index_rate >= chorus->mod_rate)388{389/* out_index = center position (center_pos_mod) + sinus waweform */390if(chorus->type == FLUID_CHORUS_MOD_SINE)391{392out_index = chorus->center_pos_mod +393get_mod_sinus(&mod->sinus) * chorus->mod_depth;394}395else396{397out_index = chorus->center_pos_mod +398get_mod_triang(&mod->triang) * chorus->mod_depth;399}400401/* extracts integer part in int_out_index */402if(out_index >= 0.0f)403{404int_out_index = (int)out_index; /* current integer part */405406/* forces read index (line_out) with integer modulation value */407/* Boundary check and circular motion as needed */408if((mod->line_out = int_out_index) >= chorus->size)409{410mod->line_out -= chorus->size;411}412}413else /* negative */414{415int_out_index = (int)(out_index - 1); /* previous integer part */416/* forces read index (line_out) with integer modulation value */417/* circular motion as needed */418mod->line_out = int_out_index + chorus->size;419}420421/* extracts fractionnal part. (it will be used when interpolating422between line_out and line_out +1) and memorize it.423Memorizing is necessary for modulation rate above 1 */424mod->frac_pos_mod = out_index - int_out_index;425}426427/* First order all-pass interpolation ----------------------------------*/428/* https://ccrma.stanford.edu/~jos/pasp/First_Order_Allpass_Interpolation.html */429/* begins interpolation: read current sample */430out = chorus->line[mod->line_out];431432/* updates line_out to the next sample.433Boundary check and circular motion as needed */434if(++mod->line_out >= chorus->size)435{436mod->line_out -= chorus->size;437}438439/* Fractional interpolation between next sample (at next position) and440previous output added to current sample.441*/442out += mod->frac_pos_mod * (chorus->line[mod->line_out] - mod->buffer);443mod->buffer = out; /* memorizes current output */444return out;445}446447/*-----------------------------------------------------------------------------448Push a sample val into the delay line449450@param dl delay line to push value into.451@param val the value to push into dl.452-----------------------------------------------------------------------------*/453#define push_in_delay_line(dl, val) \454{\455dl->line[dl->line_in] = val;\456/* Incrementation and circular motion if necessary */\457if(++dl->line_in >= dl->size) dl->line_in -= dl->size;\458}\459460/*-----------------------------------------------------------------------------461Initialize : mod_rate, center_pos_mod, and index rate462463center_pos_mod is initialized so that the delay between center_pos_mod and464line_in is: mod_depth + INTERP_SAMPLES_NBR.465466@param chorus pointer on chorus unit.467-----------------------------------------------------------------------------*/468static void set_center_position(fluid_chorus_t *chorus)469{470int center;471472/* Sets the modulation rate. This rate defines how often473the center position (center_pos_mod ) is modulated .474The value is expressed in samples. The default value is 1 that means that475center_pos_mod is updated at every sample.476For example with a value of 2, the center position position will be477updated only one time every 2 samples only.478*/479chorus->mod_rate = LOW_MOD_RATE; /* default modulation rate */480481/* compensate mod rate for high modulation depth */482if(chorus->mod_depth > LOW_MOD_DEPTH)483{484int delta_mod_depth = (chorus->mod_depth - LOW_MOD_DEPTH);485chorus->mod_rate += (delta_mod_depth * RANGE_MOD_RATE) / RANGE_MOD_DEPTH;486}487488/* Initializes the modulated center position (center_pos_mod) so that:489- the delay between center_pos_mod and line_in is:490mod_depth + INTERP_SAMPLES_NBR.491*/492center = chorus->line_in - (INTERP_SAMPLES_NBR + chorus->mod_depth);493494if(center < 0)495{496center += chorus->size;497}498499chorus->center_pos_mod = (fluid_real_t)center;500501/* index rate to control when to update center_pos_mod */502/* Important: must be set to get center_pos_mod immediately used for the503reading of first sample (see get_mod_delay()) */504chorus->index_rate = chorus->mod_rate;505}506507/*-----------------------------------------------------------------------------508Update internal parameters dependent of sample rate.509- mod_depth.510- mod_rate, center_pos_mod, and index rate.511- modulators frequency.512513@param chorus, pointer on chorus unit.514-----------------------------------------------------------------------------*/515static void update_parameters_from_sample_rate(fluid_chorus_t *chorus)516{517int i;518519/* initialize modulation depth (peak to peak) (in samples) */520/* convert modulation depth in ms to sample number */521chorus->mod_depth = (int)(chorus->depth_ms / 1000.0522* chorus->sample_rate);523524/* the delay line is fixed. So we reduce mod_depth (if necessary) */525if(chorus->mod_depth > MAX_SAMPLES)526{527FLUID_LOG(FLUID_WARN, "chorus: Too high depth. Setting it to max (%d).",528MAX_SAMPLES);529chorus->mod_depth = MAX_SAMPLES;530/* set depth_ms to maximum to avoid spamming console with above warning */531chorus->depth_ms = (chorus->mod_depth * 1000) / chorus->sample_rate;532}533534chorus->mod_depth /= 2; /* amplitude is peak to peek / 2 */535#ifdef DEBUG_PRINT536printf("depth_ms:%f, depth_samples/2:%d\n", chorus->depth_ms, chorus->mod_depth);537#endif538539/* Initializes the modulated center position:540mod_rate, center_pos_mod, and index rate.541*/542set_center_position(chorus); /* must be called before set_xxxx_frequency() */543#ifdef DEBUG_PRINT544printf("mod_rate:%d\n", chorus->mod_rate);545#endif546547/* initialize modulator frequency */548for(i = 0; i < chorus->number_blocks; i++)549{550set_sinus_frequency(&chorus->mod[i].sinus,551chorus->speed_Hz * chorus->mod_rate,552chorus->sample_rate,553/* phase offset between modulators waveform */554(float)((360.0f / (float) chorus->number_blocks) * i));555556set_triangle_frequency(&chorus->mod[i].triang,557chorus->speed_Hz * chorus->mod_rate,558chorus->sample_rate,559/* phase offset between modulators waveform */560(float)i / chorus->number_blocks);561}562}563564/*-----------------------------------------------------------------------------565Modulated delay line initialization.566567Sets the length line ( alloc delay samples).568Remark: the function sets the internal size according to the length delay_length.569The size is augmented by INTERP_SAMPLES_NBR to take account of interpolation.570571@param chorus, pointer on chorus unit.572@param delay_length the length of the delay line in samples.573@return FLUID_OK if success , FLUID_FAILED if memory error.574575Return FLUID_OK if success, FLUID_FAILED if memory error.576-----------------------------------------------------------------------------*/577static int new_mod_delay_line(fluid_chorus_t *chorus, int delay_length)578{579/*-----------------------------------------------------------------------*/580/* checks parameter */581if(delay_length < 1)582{583return FLUID_FAILED;584}585586chorus->mod_depth = 0;587/*-----------------------------------------------------------------------588allocates delay_line and initialize members: - line, size, line_in...589*/590/* total size of the line: size = INTERP_SAMPLES_NBR + delay_length */591chorus->size = delay_length + INTERP_SAMPLES_NBR;592chorus->line = FLUID_ARRAY(fluid_real_t, chorus->size);593594if(! chorus->line)595{596return FLUID_FAILED;597}598599/* clears the buffer:600- delay line601- interpolator member: buffer, frac_pos_mod602*/603fluid_chorus_reset(chorus);604605/* Initializes line_in to the start of the buffer */606chorus->line_in = 0;607/*------------------------------------------------------------------------608Initializes modulation members:609- modulation rate (the speed at which center_pos_mod is modulated: mod_rate610- modulated center position: center_pos_mod611- index rate to know when to update center_pos_mod:index_rate612-------------------------------------------------------------------------*/613/* Initializes the modulated center position:614mod_rate, center_pos_mod, and index rate615*/616set_center_position(chorus);617618return FLUID_OK;619}620621/*-----------------------------------------------------------------------------622API623------------------------------------------------------------------------------*/624/**625* Create the chorus unit. Once created the chorus have no parameters set, so626* fluid_chorus_set() must be called at least one time after calling627* new_fluid_chorus().628*629* @param sample_rate, audio sample rate in Hz.630* @return pointer on chorus unit.631*/632fluid_chorus_t *633new_fluid_chorus(fluid_real_t sample_rate)634{635fluid_chorus_t *chorus;636637chorus = FLUID_NEW(fluid_chorus_t);638639if(chorus == NULL)640{641FLUID_LOG(FLUID_PANIC, "chorus: Out of memory");642return NULL;643}644645FLUID_MEMSET(chorus, 0, sizeof(fluid_chorus_t));646647chorus->sample_rate = sample_rate;648649#ifdef DEBUG_PRINT650printf("fluid_chorus_t:%d bytes\n", sizeof(fluid_chorus_t));651printf("fluid_real_t:%d bytes\n", sizeof(fluid_real_t));652#endif653654#ifdef DEBUG_PRINT655printf("NEW_MOD\n");656#endif657658if(new_mod_delay_line(chorus, MAX_SAMPLES) == FLUID_FAILED)659{660delete_fluid_chorus(chorus);661return NULL;662}663664return chorus;665}666667/**668* Delete the chorus unit.669* @param chorus pointer on chorus unit returned by new_fluid_chorus().670*/671void672delete_fluid_chorus(fluid_chorus_t *chorus)673{674fluid_return_if_fail(chorus != NULL);675676FLUID_FREE(chorus->line);677FLUID_FREE(chorus);678}679680/**681* Clear the internal delay line and associate filter.682* @param chorus pointer on chorus unit returned by new_fluid_chorus().683*/684void685fluid_chorus_reset(fluid_chorus_t *chorus)686{687int i;688unsigned int u;689690/* reset delay line */691for(i = 0; i < chorus->size; i++)692{693chorus->line[i] = 0;694}695696/* reset modulators's allpass filter */697for(u = 0; u < FLUID_N_ELEMENTS(chorus->mod); u++)698{699/* initializes 1st order All-Pass interpolator members */700chorus->mod[u].buffer = 0; /* previous delay sample value */701chorus->mod[u].frac_pos_mod = 0; /* fractional position (between consecutives sample) */702}703}704705/**706* Set one or more chorus parameters.707*708* @param chorus Chorus instance.709* @param set Flags indicating which chorus parameters to set (#fluid_chorus_set_t).710* @param nr Chorus voice count (0-99, CPU time consumption proportional to711* this value).712* @param level Chorus level (0.0-10.0).713* @param speed Chorus speed in Hz (0.1-5.0).714* @param depth_ms Chorus depth (max value depends on synth sample rate,715* 0.0-21.0 is safe for sample rate values up to 96KHz).716* @param type Chorus waveform type (#fluid_chorus_mod).717*/718void719fluid_chorus_set(fluid_chorus_t *chorus, int set, int nr, fluid_real_t level,720fluid_real_t speed, fluid_real_t depth_ms, int type)721{722if(set & FLUID_CHORUS_SET_NR) /* number of block */723{724chorus->number_blocks = nr;725}726727if(set & FLUID_CHORUS_SET_LEVEL) /* output level */728{729chorus->level = level;730}731732if(set & FLUID_CHORUS_SET_SPEED) /* lfo frequency (in Hz) */733{734chorus->speed_Hz = speed;735}736737if(set & FLUID_CHORUS_SET_DEPTH) /* modulation depth (in ms) */738{739chorus->depth_ms = depth_ms;740}741742if(set & FLUID_CHORUS_SET_TYPE) /* lfo shape (sinus, triangle) */743{744chorus->type = type;745}746747/* check min , max parameters */748if(chorus->number_blocks < 0)749{750FLUID_LOG(FLUID_WARN, "chorus: number blocks must be >=0! Setting value to 0.");751chorus->number_blocks = 0;752}753else if(chorus->number_blocks > MAX_CHORUS)754{755FLUID_LOG(FLUID_WARN, "chorus: number blocks larger than max. allowed! Setting value to %d.",756MAX_CHORUS);757chorus->number_blocks = MAX_CHORUS;758}759760if(chorus->speed_Hz < MIN_SPEED_HZ)761{762FLUID_LOG(FLUID_WARN, "chorus: speed is too low (min %f)! Setting value to min.",763(double) MIN_SPEED_HZ);764chorus->speed_Hz = MIN_SPEED_HZ;765}766else if(chorus->speed_Hz > MAX_SPEED_HZ)767{768FLUID_LOG(FLUID_WARN, "chorus: speed must be below %f Hz! Setting value to max.",769(double) MAX_SPEED_HZ);770chorus->speed_Hz = MAX_SPEED_HZ;771}772773if(chorus->depth_ms < 0.0)774{775FLUID_LOG(FLUID_WARN, "chorus: depth must be positive! Setting value to 0.");776chorus->depth_ms = 0.0;777}778779if(chorus->level < 0.0)780{781FLUID_LOG(FLUID_WARN, "chorus: level must be positive! Setting value to 0.");782chorus->level = 0.0;783}784else if(chorus->level > MAX_LEVEL)785{786FLUID_LOG(FLUID_WARN, "chorus: level must be < 10. A reasonable level is << 1! "787"Setting it to 0.1.");788chorus->level = 0.1;789}790791/* update parameters dependent of sample rate */792update_parameters_from_sample_rate(chorus);793794#ifdef DEBUG_PRINT795printf("lfo type:%d\n", chorus->type);796printf("speed_Hz:%f\n", chorus->speed_Hz);797#endif798799/* Initialize the lfo waveform */800if((chorus->type != FLUID_CHORUS_MOD_SINE) &&801(chorus->type != FLUID_CHORUS_MOD_TRIANGLE))802{803FLUID_LOG(FLUID_WARN, "chorus: Unknown modulation type. Using sinewave.");804chorus->type = FLUID_CHORUS_MOD_SINE;805}806807#ifdef DEBUG_PRINT808809if(chorus->type == FLUID_CHORUS_MOD_SINE)810{811printf("lfo: sinus\n");812}813else814{815printf("lfo: triangle\n");816}817818printf("nr:%d\n", chorus->number_blocks);819#endif820821/* Recalculate internal values after parameters change */822823/*824Note:825Actually WIDTH is fixed to maximum value. But in the future we could add a setting826"synth.chorus.width" to allow a gradually stereo effect from minimum (monophonic) to827maximum stereo effect.828If this setting will be added, remove the following instruction.829*/830chorus->width = WIDTH;831{832/* The stereo amplitude equation (wet1 and wet2 below) have a833tendency to produce high amplitude with high width values ( 1 < width < 10).834This results in an unwanted noisy output clipped by the audio card.835To avoid this dependency, we divide by (1 + chorus->width * SCALE_WET_WIDTH)836Actually, with a SCALE_WET_WIDTH of 0.2, (regardless of level setting),837the output amplitude (wet) seems rather independent of width setting */838839fluid_real_t wet = chorus->level * SCALE_WET ;840841/* wet1 and wet2 are used by the stereo effect controlled by the width setting842for producing a stereo ouptput from a monophonic chorus signal.843Please see the note above about a side effect tendency */844845if(chorus->number_blocks > 1)846{847wet = wet / (1.0f + chorus->width * SCALE_WET_WIDTH);848chorus->wet1 = wet * (chorus->width / 2.0f + 0.5f);849chorus->wet2 = wet * ((1.0f - chorus->width) / 2.0f);850#ifdef DEBUG_PRINT851printf("width:%f\n", chorus->width);852853if(chorus->width > 0)854{855printf("nr > 1, width > 0 => out stereo\n");856}857else858{859printf("nr > 1, width:0 =>out mono\n");860}861862#endif863}864else865{866/* only one chorus block */867if(chorus->width == 0.0)868{869/* wet1 and wet2 should make stereo output monomophic */870chorus->wet1 = chorus->wet2 = wet;871}872else873{874/* for width > 0, wet1 and wet2 should make stereo output stereo875with only one block. This will only possible by inverting876the unique signal on each left and right output.877Note however that with only one block, it isn't possible to878have a graduate width effect */879chorus->wet1 = wet;880chorus->wet2 = -wet; /* inversion */881}882883#ifdef DEBUG_PRINT884printf("width:%f\n", chorus->width);885886if(chorus->width != 0)887{888printf("one block, width > 0 => out stereo\n");889}890else891{892printf("one block, width:0 => out mono\n");893}894895#endif896}897}898}899900/*901* Applies a sample rate change on the chorus.902* Note that while the chorus is used by calling any fluid_chorus_processXXX()903* function, calling fluid_chorus_samplerate_change() isn't multi task safe.904* To deal properly with this issue follow the steps:905* 1) Stop chorus processing (i.e disable calling to any fluid_chorus_processXXX().906* chorus functions.907* 2) Change sample rate by calling fluid_chorus_samplerate_change().908* 3) Restart chorus processing (i.e enabling calling any fluid_chorus_processXXX()909* chorus functions.910*911* Another solution is to substitute step (2):912* 2.1) delete the chorus by calling delete_fluid_chorus().913* 2.2) create the chorus by calling new_fluid_chorus().914*915* @param chorus pointer on the chorus.916* @param sample_rate new sample rate value.917*/918void919fluid_chorus_samplerate_change(fluid_chorus_t *chorus, fluid_real_t sample_rate)920{921chorus->sample_rate = sample_rate;922923/* update parameters dependent of sample rate */924update_parameters_from_sample_rate(chorus);925}926927/**928* Process chorus by mixing the result in output buffer.929* @param chorus pointer on chorus unit returned by new_fluid_chorus().930* @param in, pointer on monophonic input buffer of FLUID_BUFSIZE samples.931* @param left_out, right_out, pointers on stereo output buffers of932* FLUID_BUFSIZE samples.933*/934void fluid_chorus_processmix(fluid_chorus_t *chorus, const fluid_real_t *in,935fluid_real_t *left_out, fluid_real_t *right_out)936{937int sample_index;938int i;939fluid_real_t d_out[2]; /* output stereo Left and Right */940941/* foreach sample, process output sample then input sample */942for(sample_index = 0; sample_index < FLUID_BUFSIZE; sample_index++)943{944fluid_real_t out; /* block output */945946d_out[0] = d_out[1] = 0.0f; /* clear stereo unit input */947948#if 0949/* Debug: Listen to the chorus signal only */950left_out[sample_index] = 0;951right_out[sample_index] = 0;952#endif953954++chorus->index_rate; /* modulator rate */955956/* foreach chorus block, process output sample */957for(i = 0; i < chorus->number_blocks; i++)958{959/* get sample from the output of modulated delay line */960out = get_mod_delay(chorus, &chorus->mod[i]);961962/* accumulate out into stereo unit input */963d_out[i & 1] += out;964}965966/* update modulator index rate and output center position */967if(chorus->index_rate >= chorus->mod_rate)968{969chorus->index_rate = 0; /* clear modulator index rate */970971/* updates center position (center_pos_mod) to the next position972specified by modulation rate */973if((chorus->center_pos_mod += chorus->mod_rate) >= chorus->size)974{975chorus->center_pos_mod -= chorus->size;976}977}978979/* Adjust stereo input level in case of number_blocks odd:980In those case, d_out[1] level is lower than d_out[0], so we need to981add out value to d_out[1] to have d_out[0] and d_out[1] balanced.982*/983if((i & 1) && i > 2) // i = 3,5,7...984{985d_out[1] += out ;986}987988/* Write the current input sample into the circular buffer.989* Note that 'in' may be aliased with 'left_out'. Hence this must be done990* before "processing stereo unit" (below). This ensures input buffer991* not being overwritten by stereo unit output.992*/993push_in_delay_line(chorus, in[sample_index]);994995/* process stereo unit */996/* Add the chorus stereo unit d_out to left and right output */997left_out[sample_index] += d_out[0] * chorus->wet1 + d_out[1] * chorus->wet2;998right_out[sample_index] += d_out[1] * chorus->wet1 + d_out[0] * chorus->wet2;999}1000}10011002/**1003* Process chorus by putting the result in output buffer (no mixing).1004* @param chorus pointer on chorus unit returned by new_fluid_chorus().1005* @param in, pointer on monophonic input buffer of FLUID_BUFSIZE samples.1006* @param left_out, right_out, pointers on stereo output buffers of1007* FLUID_BUFSIZE samples.1008*/1009/* Duplication of code ... (replaces sample data instead of mixing) */1010void fluid_chorus_processreplace(fluid_chorus_t *chorus, const fluid_real_t *in,1011fluid_real_t *left_out, fluid_real_t *right_out)1012{1013int sample_index;1014int i;1015fluid_real_t d_out[2]; /* output stereo Left and Right */10161017/* foreach sample, process output sample then input sample */1018for(sample_index = 0; sample_index < FLUID_BUFSIZE; sample_index++)1019{1020fluid_real_t out; /* block output */10211022d_out[0] = d_out[1] = 0.0f; /* clear stereo unit input */10231024#if 01025/* Debug: Listen to the chorus signal only */1026left_out[sample_index] = 0;1027right_out[sample_index] = 0;1028#endif10291030++chorus->index_rate; /* modulator rate */10311032/* foreach chorus block, process output sample */1033for(i = 0; i < chorus->number_blocks; i++)1034{1035/* get sample from the output of modulated delay line */1036out = get_mod_delay(chorus, &chorus->mod[i]);10371038/* accumulate out into stereo unit input */1039d_out[i & 1] += out;1040}10411042/* update modulator index rate and output center position */1043if(chorus->index_rate >= chorus->mod_rate)1044{1045chorus->index_rate = 0; /* clear modulator index rate */10461047/* updates center position (center_pos_mod) to the next position1048specified by modulation rate */1049if((chorus->center_pos_mod += chorus->mod_rate) >= chorus->size)1050{1051chorus->center_pos_mod -= chorus->size;1052}1053}10541055/* Adjust stereo input level in case of number_blocks odd:1056In those case, d_out[1] level is lower than d_out[0], so we need to1057add out value to d_out[1] to have d_out[0] and d_out[1] balanced.1058*/1059if((i & 1) && i > 2) // i = 3,5,7...1060{1061d_out[1] += out ;1062}10631064/* Write the current input sample into the circular buffer.1065* Note that 'in' may be aliased with 'left_out'. Hence this must be done1066* before "processing stereo unit" (below). This ensures input buffer1067* not being overwritten by stereo unit output.1068*/1069push_in_delay_line(chorus, in[sample_index]);10701071/* process stereo unit */1072/* store the chorus stereo unit d_out to left and right output */1073left_out[sample_index] = d_out[0] * chorus->wet1 + d_out[1] * chorus->wet2;1074right_out[sample_index] = d_out[1] * chorus->wet1 + d_out[0] * chorus->wet2;1075}1076}107710781079