3 * Copyright (C) 1998 Juergen Mueller And Sundry Contributors
4 * This source code is freely redistributable and may be used for
5 * any purpose. This copyright notice must be maintained.
6 * Juergen Mueller And Sundry Contributors are not responsible for
7 * the consequences of using this software.
14 - Adapted for fluidsynth, Peter Hanappe, March 2002
16 - Variable delay line implementation using bandlimited
17 interpolation, code reorganization: Markus Nentwig May 2002
25 * Flow diagram scheme for n delays ( 1 <= n <= MAX_CHORUS ):
28 * ibuff -----+--------------------------------------------->| |
31 * +---->| delay 1 |----------------------------->| |
35 * : +-----------------+ +--------------+ | + |
36 * : | Delay control 1 |<--| mod. speed 1 | | |
37 * : +-----------------+ +--------------+ | |
40 * +---->| delay n |----------------------------->| |
44 * +-----------------+ +--------------+ | * gain-out
45 * | Delay control n |<--| mod. speed n | |
46 * +-----------------+ +--------------+ +----->obuff
49 * The delay i is controlled by a sine or triangle modulation i ( 1 <= i <= n).
51 * The delay of each block is modulated between 0..depth ms
56 /* Variable delay line implementation
57 * ==================================
59 * The modulated delay needs the value of the delayed signal between
60 * samples. A lowpass filter is used to obtain intermediate values
61 * between samples (bandlimited interpolation). The sample pulse
62 * train is convoluted with the impulse response of the low pass
63 * filter (sinc function). To make it work with a small number of
64 * samples, the sinc function is windowed (Hamming window).
68 #include "fluid_chorus.h"
69 #include "fluid_sys.h"
74 #define MIN_SPEED_HZ 0.29
75 #define MAX_SPEED_HZ 5
77 /* Length of one delay line in samples:
78 * Set through MAX_SAMPLES_LN2.
81 * => MAX_SAMPLES=pow(2,12)=4096
82 * => MAX_SAMPLES_ANDMASK=4095
84 #define MAX_SAMPLES_LN2 12
86 #define MAX_SAMPLES (1 << (MAX_SAMPLES_LN2-1))
87 #define MAX_SAMPLES_ANDMASK (MAX_SAMPLES-1)
90 /* Interpolate how many steps between samples? Must be power of two
91 For example: 8 => use a resolution of 256 steps between any two
94 #define INTERPOLATION_SUBSAMPLES_LN2 8
95 #define INTERPOLATION_SUBSAMPLES (1 << (INTERPOLATION_SUBSAMPLES_LN2-1))
96 #define INTERPOLATION_SUBSAMPLES_ANDMASK (INTERPOLATION_SUBSAMPLES-1)
98 /* Use how many samples for interpolation? Must be odd. '7' sounds
99 relatively clean, when listening to the modulated delay signal
100 alone. For a demo on aliasing try '1' With '3', the aliasing is
101 still quite pronounced for some input frequencies
103 #define INTERPOLATION_SAMPLES 5
105 /* Private data for SKEL file */
106 struct _fluid_chorus_t {
108 fluid_real_t depth_ms;
110 fluid_real_t speed_Hz;
113 fluid_real_t *chorusbuf;
115 long phase[MAX_CHORUS];
116 long modulation_period_samples;
118 fluid_real_t sample_rate;
120 /* sinc lookup table */
121 fluid_real_t sinc_table[INTERPOLATION_SAMPLES][INTERPOLATION_SUBSAMPLES];
124 static void fluid_chorus_triangle(int *buf, int len, int depth);
125 static void fluid_chorus_sine(int *buf, int len, int depth);
129 new_fluid_chorus(fluid_real_t sample_rate)
132 fluid_chorus_t* chorus;
134 chorus = FLUID_NEW(fluid_chorus_t);
135 if (chorus == NULL) {
136 fluid_log(FLUID_PANIC, "chorus: Out of memory");
140 FLUID_MEMSET(chorus, 0, sizeof(fluid_chorus_t));
142 chorus->sample_rate = sample_rate;
144 /* Lookup table for the SI function (impulse response of an ideal low pass) */
146 /* i: Offset in terms of whole samples */
147 for (i = 0; i < INTERPOLATION_SAMPLES; i++){
149 /* ii: Offset in terms of fractional samples ('subsamples') */
150 for (ii = 0; ii < INTERPOLATION_SUBSAMPLES; ii++){
151 /* Move the origin into the center of the table */
152 double i_shifted = ((double) i- ((double) INTERPOLATION_SAMPLES) / 2.
153 + (double) ii / (double) INTERPOLATION_SUBSAMPLES);
154 if (fabs(i_shifted) < 0.000001) {
155 /* sinc(0) cannot be calculated straightforward (limit needed
157 chorus->sinc_table[i][ii] = (fluid_real_t)1.;
160 chorus->sinc_table[i][ii] = (fluid_real_t)sin(i_shifted * M_PI) / (M_PI * i_shifted);
162 chorus->sinc_table[i][ii] *= (fluid_real_t)0.5 * (1.0 + cos(2.0 * M_PI * i_shifted / (fluid_real_t)INTERPOLATION_SAMPLES));
167 /* allocate lookup tables */
168 chorus->lookup_tab = FLUID_ARRAY(int, (int) (chorus->sample_rate / MIN_SPEED_HZ));
169 if (chorus->lookup_tab == NULL) {
170 fluid_log(FLUID_PANIC, "chorus: Out of memory");
174 /* allocate sample buffer */
176 chorus->chorusbuf = FLUID_ARRAY(fluid_real_t, MAX_SAMPLES);
177 if (chorus->chorusbuf == NULL) {
178 fluid_log(FLUID_PANIC, "chorus: Out of memory");
182 if (fluid_chorus_init(chorus) != FLUID_OK){
189 delete_fluid_chorus(chorus);
194 delete_fluid_chorus(fluid_chorus_t* chorus)
196 if (chorus == NULL) {
200 if (chorus->chorusbuf != NULL) {
201 FLUID_FREE(chorus->chorusbuf);
204 if (chorus->lookup_tab != NULL) {
205 FLUID_FREE(chorus->lookup_tab);
212 fluid_chorus_init(fluid_chorus_t* chorus)
216 for (i = 0; i < MAX_SAMPLES; i++) {
217 chorus->chorusbuf[i] = 0.0;
220 /* initialize the chorus with the default settings */
221 fluid_chorus_set (chorus, FLUID_CHORUS_SET_ALL, FLUID_CHORUS_DEFAULT_N,
222 FLUID_CHORUS_DEFAULT_LEVEL, FLUID_CHORUS_DEFAULT_SPEED,
223 FLUID_CHORUS_DEFAULT_DEPTH, FLUID_CHORUS_MOD_SINE);
228 fluid_chorus_reset(fluid_chorus_t* chorus)
230 fluid_chorus_init(chorus);
234 * Set one or more chorus parameters.
235 * @param chorus Chorus instance
236 * @param set Flags indicating which chorus parameters to set (#fluid_chorus_set_t)
237 * @param nr Chorus voice count (0-99, CPU time consumption proportional to
239 * @param level Chorus level (0.0-10.0)
240 * @param speed Chorus speed in Hz (0.29-5.0)
241 * @param depth_ms Chorus depth (max value depends on synth sample rate,
242 * 0.0-21.0 is safe for sample rate values up to 96KHz)
243 * @param type Chorus waveform type (#fluid_chorus_mod)
246 fluid_chorus_set(fluid_chorus_t* chorus, int set, int nr, float level,
247 float speed, float depth_ms, int type)
249 int modulation_depth_samples;
252 if (set & FLUID_CHORUS_SET_NR) chorus->number_blocks = nr;
253 if (set & FLUID_CHORUS_SET_LEVEL) chorus->level = level;
254 if (set & FLUID_CHORUS_SET_SPEED) chorus->speed_Hz = speed;
255 if (set & FLUID_CHORUS_SET_DEPTH) chorus->depth_ms = depth_ms;
256 if (set & FLUID_CHORUS_SET_TYPE) chorus->type = type;
258 if (chorus->number_blocks < 0) {
259 fluid_log(FLUID_WARN, "chorus: number blocks must be >=0! Setting value to 0.");
260 chorus->number_blocks = 0;
261 } else if (chorus->number_blocks > MAX_CHORUS) {
262 fluid_log(FLUID_WARN, "chorus: number blocks larger than max. allowed! Setting value to %d.",
264 chorus->number_blocks = MAX_CHORUS;
267 if (chorus->speed_Hz < MIN_SPEED_HZ) {
268 fluid_log(FLUID_WARN, "chorus: speed is too low (min %f)! Setting value to min.",
269 (double) MIN_SPEED_HZ);
270 chorus->speed_Hz = MIN_SPEED_HZ;
271 } else if (chorus->speed_Hz > MAX_SPEED_HZ) {
272 fluid_log(FLUID_WARN, "chorus: speed must be below %f Hz! Setting value to max.",
273 (double) MAX_SPEED_HZ);
274 chorus->speed_Hz = MAX_SPEED_HZ;
277 if (chorus->depth_ms < 0.0) {
278 fluid_log(FLUID_WARN, "chorus: depth must be positive! Setting value to 0.");
279 chorus->depth_ms = 0.0;
281 /* Depth: Check for too high value through modulation_depth_samples. */
283 if (chorus->level < 0.0) {
284 fluid_log(FLUID_WARN, "chorus: level must be positive! Setting value to 0.");
286 } else if (chorus->level > 10) {
287 fluid_log(FLUID_WARN, "chorus: level must be < 10. A reasonable level is << 1! "
288 "Setting it to 0.1.");
292 /* The modulating LFO goes through a full period every x samples: */
293 chorus->modulation_period_samples = chorus->sample_rate / chorus->speed_Hz;
295 /* The variation in delay time is x: */
296 modulation_depth_samples = (int)
297 (chorus->depth_ms / 1000.0 /* convert modulation depth in ms to s*/
298 * chorus->sample_rate);
300 if (modulation_depth_samples > MAX_SAMPLES) {
301 fluid_log(FLUID_WARN, "chorus: Too high depth. Setting it to max (%d).", MAX_SAMPLES);
302 modulation_depth_samples = MAX_SAMPLES;
305 /* initialize LFO table */
306 if (chorus->type == FLUID_CHORUS_MOD_SINE) {
307 fluid_chorus_sine(chorus->lookup_tab, chorus->modulation_period_samples,
308 modulation_depth_samples);
309 } else if (chorus->type == FLUID_CHORUS_MOD_TRIANGLE) {
310 fluid_chorus_triangle(chorus->lookup_tab, chorus->modulation_period_samples,
311 modulation_depth_samples);
313 fluid_log(FLUID_WARN, "chorus: Unknown modulation type. Using sinewave.");
314 chorus->type = FLUID_CHORUS_MOD_SINE;
315 fluid_chorus_sine(chorus->lookup_tab, chorus->modulation_period_samples,
316 modulation_depth_samples);
319 for (i = 0; i < chorus->number_blocks; i++) {
320 /* Set the phase of the chorus blocks equally spaced */
321 chorus->phase[i] = (int) ((double) chorus->modulation_period_samples
322 * (double) i / (double) chorus->number_blocks);
325 /* Start of the circular buffer */
330 void fluid_chorus_processmix(fluid_chorus_t* chorus, fluid_real_t *in,
331 fluid_real_t *left_out, fluid_real_t *right_out)
335 fluid_real_t d_in, d_out;
337 for (sample_index = 0; sample_index < FLUID_BUFSIZE; sample_index++) {
339 d_in = in[sample_index];
343 /* Debug: Listen to the chorus signal only */
344 left_out[sample_index]=0;
345 right_out[sample_index]=0;
348 /* Write the current sample into the circular buffer */
349 chorus->chorusbuf[chorus->counter] = d_in;
351 for (i = 0; i < chorus->number_blocks; i++) {
353 /* Calculate the delay in subsamples for the delay line of chorus block nr. */
355 /* The value in the lookup table is so, that this expression
356 * will always be positive. It will always include a number of
357 * full periods of MAX_SAMPLES*INTERPOLATION_SUBSAMPLES to
358 * remain positive at all times. */
359 int pos_subsamples = (INTERPOLATION_SUBSAMPLES * chorus->counter
360 - chorus->lookup_tab[chorus->phase[i]]);
362 int pos_samples = pos_subsamples/INTERPOLATION_SUBSAMPLES;
364 /* modulo divide by INTERPOLATION_SUBSAMPLES */
365 pos_subsamples &= INTERPOLATION_SUBSAMPLES_ANDMASK;
367 for (ii = 0; ii < INTERPOLATION_SAMPLES; ii++){
368 /* Add the delayed signal to the chorus sum d_out Note: The
369 * delay in the delay line moves backwards for increasing
372 /* The & in chorusbuf[...] is equivalent to a division modulo
373 MAX_SAMPLES, only faster. */
374 d_out += chorus->chorusbuf[pos_samples & MAX_SAMPLES_ANDMASK]
375 * chorus->sinc_table[ii][pos_subsamples];
379 /* Cycle the phase of the modulating LFO */
381 chorus->phase[i] %= (chorus->modulation_period_samples);
382 } /* foreach chorus block */
384 d_out *= chorus->level;
386 /* Add the chorus sum d_out to output */
387 left_out[sample_index] += d_out;
388 right_out[sample_index] += d_out;
390 /* Move forward in circular buffer */
392 chorus->counter %= MAX_SAMPLES;
394 } /* foreach sample */
397 /* Duplication of code ... (replaces sample data instead of mixing) */
398 void fluid_chorus_processreplace(fluid_chorus_t* chorus, fluid_real_t *in,
399 fluid_real_t *left_out, fluid_real_t *right_out)
403 fluid_real_t d_in, d_out;
405 for (sample_index = 0; sample_index < FLUID_BUFSIZE; sample_index++) {
407 d_in = in[sample_index];
411 /* Debug: Listen to the chorus signal only */
412 left_out[sample_index]=0;
413 right_out[sample_index]=0;
416 /* Write the current sample into the circular buffer */
417 chorus->chorusbuf[chorus->counter] = d_in;
419 for (i = 0; i < chorus->number_blocks; i++) {
421 /* Calculate the delay in subsamples for the delay line of chorus block nr. */
423 /* The value in the lookup table is so, that this expression
424 * will always be positive. It will always include a number of
425 * full periods of MAX_SAMPLES*INTERPOLATION_SUBSAMPLES to
426 * remain positive at all times. */
427 int pos_subsamples = (INTERPOLATION_SUBSAMPLES * chorus->counter
428 - chorus->lookup_tab[chorus->phase[i]]);
430 int pos_samples = pos_subsamples / INTERPOLATION_SUBSAMPLES;
432 /* modulo divide by INTERPOLATION_SUBSAMPLES */
433 pos_subsamples &= INTERPOLATION_SUBSAMPLES_ANDMASK;
435 for (ii = 0; ii < INTERPOLATION_SAMPLES; ii++){
436 /* Add the delayed signal to the chorus sum d_out Note: The
437 * delay in the delay line moves backwards for increasing
440 /* The & in chorusbuf[...] is equivalent to a division modulo
441 MAX_SAMPLES, only faster. */
442 d_out += chorus->chorusbuf[pos_samples & MAX_SAMPLES_ANDMASK]
443 * chorus->sinc_table[ii][pos_subsamples];
447 /* Cycle the phase of the modulating LFO */
449 chorus->phase[i] %= (chorus->modulation_period_samples);
450 } /* foreach chorus block */
452 d_out *= chorus->level;
454 /* Store the chorus sum d_out to output */
455 left_out[sample_index] = d_out;
456 right_out[sample_index] = d_out;
458 /* Move forward in circular buffer */
460 chorus->counter %= MAX_SAMPLES;
462 } /* foreach sample */
467 * Calculates a modulation waveform (sine) Its value ( modulo
468 * MAXSAMPLES) varies between 0 and depth*INTERPOLATION_SUBSAMPLES.
469 * Its period length is len. The waveform data will be used modulo
470 * MAXSAMPLES only. Since MAXSAMPLES is substracted from the waveform
471 * a couple of times here, the resulting (current position in
472 * buffer)-(waveform sample) will always be positive.
475 fluid_chorus_sine(int *buf, int len, int depth)
480 for (i = 0; i < len; i++) {
481 val = sin((double) i / (double)len * 2.0 * M_PI);
482 buf[i] = (int) ((1.0 + val) * (double) depth / 2.0 * (double) INTERPOLATION_SUBSAMPLES);
483 buf[i] -= 3* MAX_SAMPLES * INTERPOLATION_SUBSAMPLES;
484 // printf("%i %i\n",i,buf[i]);
489 * Calculates a modulation waveform (triangle)
490 * See fluid_chorus_sine for comments.
493 fluid_chorus_triangle(int *buf, int len, int depth)
501 val = i * 2.0 / len * (double)depth * (double) INTERPOLATION_SUBSAMPLES;
502 val2= (int) (val + 0.5) - 3 * MAX_SAMPLES * INTERPOLATION_SUBSAMPLES;
503 buf[i++] = (int) val2;
504 buf[ii--] = (int) val2;