summaryrefslogtreecommitdiff
path: root/libs/fluidsynth/src/fluid_chorus.c
blob: abcd4bc0c44499e20a9638d65c1bb0de582017e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* FluidSynth - A Software Synthesizer
 *
 * Copyright (C) 2003  Peter Hanappe, Markus Nentwig and others.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

/*
  based on a chrous implementation made by Juergen Mueller And Sundry Contributors in 1998

  CHANGES

  - Adapted for fluidsynth, Peter Hanappe, March 2002

  - Variable delay line implementation using bandlimited
    interpolation, code reorganization: Markus Nentwig May 2002

 */


/*
 * 	Chorus effect.
 *
 * Flow diagram scheme for n delays ( 1 <= n <= MAX_CHORUS ):
 *
 *        * gain-in                                           ___
 * ibuff -----+--------------------------------------------->|   |
 *            |      _________                               |   |
 *            |     |         |                   * level 1  |   |
 *            +---->| delay 1 |----------------------------->|   |
 *            |     |_________|                              |   |
 *            |        /|\                                   |   |
 *            :         |                                    |   |
 *            : +-----------------+   +--------------+       | + |
 *            : | Delay control 1 |<--| mod. speed 1 |       |   |
 *            : +-----------------+   +--------------+       |   |
 *            |      _________                               |   |
 *            |     |         |                   * level n  |   |
 *            +---->| delay n |----------------------------->|   |
 *                  |_________|                              |   |
 *                     /|\                                   |___|
 *                      |                                      |
 *              +-----------------+   +--------------+         | * gain-out
 *              | Delay control n |<--| mod. speed n |         |
 *              +-----------------+   +--------------+         +----->obuff
 *
 *
 * The delay i is controlled by a sine or triangle modulation i ( 1 <= i <= n).
 *
 * The delay of each block is modulated between 0..depth ms
 *
 */


/* Variable delay line implementation
 * ==================================
 *
 * The modulated delay needs the value of the delayed signal between
 * samples.  A lowpass filter is used to obtain intermediate values
 * between samples (bandlimited interpolation).  The sample pulse
 * train is convoluted with the impulse response of the low pass
 * filter (sinc function).  To make it work with a small number of
 * samples, the sinc function is windowed (Hamming window).
 *
 */

#include "fluid_chorus.h"
#include "fluid_sys.h"

#define MAX_CHORUS	99
#define MAX_DELAY	100
#define MAX_DEPTH	10
#define MIN_SPEED_HZ	0.29
#define MAX_SPEED_HZ    5

/* Length of one delay line in samples:
 * Set through MAX_SAMPLES_LN2.
 * For example:
 * MAX_SAMPLES_LN2=12
 * => MAX_SAMPLES=pow(2,12-1)=2048
 * => MAX_SAMPLES_ANDMASK=2047
 */
#define MAX_SAMPLES_LN2 12

#define MAX_SAMPLES (1 << (MAX_SAMPLES_LN2-1))
#define MAX_SAMPLES_ANDMASK (MAX_SAMPLES-1)


/* Interpolate how many steps between samples? Must be power of two
   For example: 8 => use a resolution of 256 steps between any two
   samples
*/
#define INTERPOLATION_SUBSAMPLES_LN2 8
#define INTERPOLATION_SUBSAMPLES (1 << (INTERPOLATION_SUBSAMPLES_LN2-1))
#define INTERPOLATION_SUBSAMPLES_ANDMASK (INTERPOLATION_SUBSAMPLES-1)

/* Use how many samples for interpolation? Must be odd.  '7' sounds
   relatively clean, when listening to the modulated delay signal
   alone.  For a demo on aliasing try '1' With '3', the aliasing is
   still quite pronounced for some input frequencies
*/
#define INTERPOLATION_SAMPLES 5

/* Private data for SKEL file */
struct _fluid_chorus_t
{
    int type;
    fluid_real_t depth_ms;
    fluid_real_t level;
    fluid_real_t speed_Hz;
    int number_blocks;

    fluid_real_t *chorusbuf;
    int counter;
    long phase[MAX_CHORUS];
    long modulation_period_samples;
    int *lookup_tab;
    fluid_real_t sample_rate;

    /* sinc lookup table */
    fluid_real_t sinc_table[INTERPOLATION_SAMPLES][INTERPOLATION_SUBSAMPLES];
};

static void fluid_chorus_triangle(int *buf, int len, int depth);
static void fluid_chorus_sine(int *buf, int len, int depth);


fluid_chorus_t *
new_fluid_chorus(fluid_real_t sample_rate)
{
    int i;
    int ii;
    fluid_chorus_t *chorus;

    chorus = FLUID_NEW(fluid_chorus_t);

    if(chorus == NULL)
    {
        FLUID_LOG(FLUID_PANIC, "chorus: Out of memory");
        return NULL;
    }

    FLUID_MEMSET(chorus, 0, sizeof(fluid_chorus_t));

    chorus->sample_rate = sample_rate;

    /* Lookup table for the SI function (impulse response of an ideal low pass) */

    /* i: Offset in terms of whole samples */
    for(i = 0; i < INTERPOLATION_SAMPLES; i++)
    {

        /* ii: Offset in terms of fractional samples ('subsamples') */
        for(ii = 0; ii < INTERPOLATION_SUBSAMPLES; ii++)
        {
            /* Move the origin into the center of the table */
            double i_shifted = ((double) i - ((double) INTERPOLATION_SAMPLES) / 2.
                                + (double) ii / (double) INTERPOLATION_SUBSAMPLES);

            if(fabs(i_shifted) < 0.000001)
            {
                /* sinc(0) cannot be calculated straightforward (limit needed
                   for 0/0) */
                chorus->sinc_table[i][ii] = (fluid_real_t)1.;

            }
            else
            {
                chorus->sinc_table[i][ii] = (fluid_real_t)sin(i_shifted * M_PI) / (M_PI * i_shifted);
                /* Hamming window */
                chorus->sinc_table[i][ii] *= (fluid_real_t)0.5 * (1.0 + cos(2.0 * M_PI * i_shifted / (fluid_real_t)INTERPOLATION_SAMPLES));
            };
        };
    };

    /* allocate lookup tables */
    chorus->lookup_tab = FLUID_ARRAY(int, (int)(chorus->sample_rate / MIN_SPEED_HZ));

    if(chorus->lookup_tab == NULL)
    {
        FLUID_LOG(FLUID_PANIC, "chorus: Out of memory");
        goto error_recovery;
    }

    /* allocate sample buffer */

    chorus->chorusbuf = FLUID_ARRAY(fluid_real_t, MAX_SAMPLES);

    if(chorus->chorusbuf == NULL)
    {
        FLUID_LOG(FLUID_PANIC, "chorus: Out of memory");
        goto error_recovery;
    }

    if(fluid_chorus_init(chorus) != FLUID_OK)
    {
        goto error_recovery;
    };

    return chorus;

error_recovery:
    delete_fluid_chorus(chorus);

    return NULL;
}

void
delete_fluid_chorus(fluid_chorus_t *chorus)
{
    fluid_return_if_fail(chorus != NULL);

    FLUID_FREE(chorus->chorusbuf);
    FLUID_FREE(chorus->lookup_tab);
    FLUID_FREE(chorus);
}

int
fluid_chorus_init(fluid_chorus_t *chorus)
{
    int i;

    for(i = 0; i < MAX_SAMPLES; i++)
    {
        chorus->chorusbuf[i] = 0.0;
    }

    return FLUID_OK;
}

void
fluid_chorus_reset(fluid_chorus_t *chorus)
{
    fluid_chorus_init(chorus);
}

/**
 * Set one or more chorus parameters.
 * @param chorus Chorus instance
 * @param set Flags indicating which chorus parameters to set (#fluid_chorus_set_t)
 * @param nr Chorus voice count (0-99, CPU time consumption proportional to
 *   this value)
 * @param level Chorus level (0.0-10.0)
 * @param speed Chorus speed in Hz (0.29-5.0)
 * @param depth_ms Chorus depth (max value depends on synth sample rate,
 *   0.0-21.0 is safe for sample rate values up to 96KHz)
 * @param type Chorus waveform type (#fluid_chorus_mod)
 */
void
fluid_chorus_set(fluid_chorus_t *chorus, int set, int nr, fluid_real_t level,
                 fluid_real_t speed, fluid_real_t depth_ms, int type)
{
    int modulation_depth_samples;
    int i;

    if(set & FLUID_CHORUS_SET_NR)
    {
        chorus->number_blocks = nr;
    }

    if(set & FLUID_CHORUS_SET_LEVEL)
    {
        chorus->level = level;
    }

    if(set & FLUID_CHORUS_SET_SPEED)
    {
        chorus->speed_Hz = speed;
    }

    if(set & FLUID_CHORUS_SET_DEPTH)
    {
        chorus->depth_ms = depth_ms;
    }

    if(set & FLUID_CHORUS_SET_TYPE)
    {
        chorus->type = type;
    }

    if(chorus->number_blocks < 0)
    {
        FLUID_LOG(FLUID_WARN, "chorus: number blocks must be >=0! Setting value to 0.");
        chorus->number_blocks = 0;
    }
    else if(chorus->number_blocks > MAX_CHORUS)
    {
        FLUID_LOG(FLUID_WARN, "chorus: number blocks larger than max. allowed! Setting value to %d.",
                  MAX_CHORUS);
        chorus->number_blocks = MAX_CHORUS;
    }

    if(chorus->speed_Hz < MIN_SPEED_HZ)
    {
        FLUID_LOG(FLUID_WARN, "chorus: speed is too low (min %f)! Setting value to min.",
                  (double) MIN_SPEED_HZ);
        chorus->speed_Hz = MIN_SPEED_HZ;
    }
    else if(chorus->speed_Hz > MAX_SPEED_HZ)
    {
        FLUID_LOG(FLUID_WARN, "chorus: speed must be below %f Hz! Setting value to max.",
                  (double) MAX_SPEED_HZ);
        chorus->speed_Hz = MAX_SPEED_HZ;
    }

    if(chorus->depth_ms < 0.0)
    {
        FLUID_LOG(FLUID_WARN, "chorus: depth must be positive! Setting value to 0.");
        chorus->depth_ms = 0.0;
    }

    /* Depth: Check for too high value through modulation_depth_samples. */

    if(chorus->level < 0.0)
    {
        FLUID_LOG(FLUID_WARN, "chorus: level must be positive! Setting value to 0.");
        chorus->level = 0.0;
    }
    else if(chorus->level > 10)
    {
        FLUID_LOG(FLUID_WARN, "chorus: level must be < 10. A reasonable level is << 1! "
                  "Setting it to 0.1.");
        chorus->level = 0.1;
    }

    /* The modulating LFO goes through a full period every x samples: */
    chorus->modulation_period_samples = chorus->sample_rate / chorus->speed_Hz;

    /* The variation in delay time is x: */
    modulation_depth_samples = (int)
                               (chorus->depth_ms / 1000.0  /* convert modulation depth in ms to s*/
                                * chorus->sample_rate);

    if(modulation_depth_samples > MAX_SAMPLES)
    {
        FLUID_LOG(FLUID_WARN, "chorus: Too high depth. Setting it to max (%d).", MAX_SAMPLES);
        modulation_depth_samples = MAX_SAMPLES;
        // set depth to maximum to avoid spamming console with above warning
        chorus->depth_ms = (modulation_depth_samples * 1000) / chorus->sample_rate;
    }

    /* initialize LFO table */
    switch(chorus->type)
    {
    default:
        FLUID_LOG(FLUID_WARN, "chorus: Unknown modulation type. Using sinewave.");
        chorus->type = FLUID_CHORUS_MOD_SINE;
        /* fall-through */
        
    case FLUID_CHORUS_MOD_SINE:
        fluid_chorus_sine(chorus->lookup_tab, chorus->modulation_period_samples,
                          modulation_depth_samples);
        break;

    case FLUID_CHORUS_MOD_TRIANGLE:
        fluid_chorus_triangle(chorus->lookup_tab, chorus->modulation_period_samples,
                              modulation_depth_samples);
        break;
    }

    for(i = 0; i < chorus->number_blocks; i++)
    {
        /* Set the phase of the chorus blocks equally spaced */
        chorus->phase[i] = (int)((double) chorus->modulation_period_samples
                                 * (double) i / (double) chorus->number_blocks);
    }

    /* Start of the circular buffer */
    chorus->counter = 0;
}


void fluid_chorus_processmix(fluid_chorus_t *chorus, const fluid_real_t *in,
                             fluid_real_t *left_out, fluid_real_t *right_out)
{
    int sample_index;
    int i;
    fluid_real_t d_in, d_out;

    for(sample_index = 0; sample_index < FLUID_BUFSIZE; sample_index++)
    {

        d_in = in[sample_index];
        d_out = 0.0f;

# if 0
        /* Debug: Listen to the chorus signal only */
        left_out[sample_index] = 0;
        right_out[sample_index] = 0;
#endif

        /* Write the current sample into the circular buffer */
        chorus->chorusbuf[chorus->counter] = d_in;

        for(i = 0; i < chorus->number_blocks; i++)
        {
            int ii;
            /* Calculate the delay in subsamples for the delay line of chorus block nr. */

            /* The value in the lookup table is so, that this expression
             * will always be positive.  It will always include a number of
             * full periods of MAX_SAMPLES*INTERPOLATION_SUBSAMPLES to
             * remain positive at all times. */
            int pos_subsamples = (INTERPOLATION_SUBSAMPLES * chorus->counter
                                  - chorus->lookup_tab[chorus->phase[i]]);

            int pos_samples = pos_subsamples / INTERPOLATION_SUBSAMPLES;

            /* modulo divide by INTERPOLATION_SUBSAMPLES */
            pos_subsamples &= INTERPOLATION_SUBSAMPLES_ANDMASK;

            for(ii = 0; ii < INTERPOLATION_SAMPLES; ii++)
            {
                /* Add the delayed signal to the chorus sum d_out Note: The
                 * delay in the delay line moves backwards for increasing
                 * delay!*/

                /* The & in chorusbuf[...] is equivalent to a division modulo
                   MAX_SAMPLES, only faster. */
                d_out += chorus->chorusbuf[pos_samples & MAX_SAMPLES_ANDMASK]
                         * chorus->sinc_table[ii][pos_subsamples];

                pos_samples--;
            };

            /* Cycle the phase of the modulating LFO */
            chorus->phase[i]++;

            chorus->phase[i] %= (chorus->modulation_period_samples);
        } /* foreach chorus block */

        d_out *= chorus->level;

        /* Add the chorus sum d_out to output */
        left_out[sample_index] += d_out;
        right_out[sample_index] += d_out;

        /* Move forward in circular buffer */
        chorus->counter++;
        chorus->counter %= MAX_SAMPLES;

    } /* foreach sample */
}

/* Duplication of code ... (replaces sample data instead of mixing) */
void fluid_chorus_processreplace(fluid_chorus_t *chorus, const fluid_real_t *in,
                                 fluid_real_t *left_out, fluid_real_t *right_out)
{
    int sample_index;
    int i;
    fluid_real_t d_in, d_out;

    for(sample_index = 0; sample_index < FLUID_BUFSIZE; sample_index++)
    {

        d_in = in[sample_index];
        d_out = 0.0f;

# if 0
        /* Debug: Listen to the chorus signal only */
        left_out[sample_index] = 0;
        right_out[sample_index] = 0;
#endif

        /* Write the current sample into the circular buffer */
        chorus->chorusbuf[chorus->counter] = d_in;

        for(i = 0; i < chorus->number_blocks; i++)
        {
            int ii;
            /* Calculate the delay in subsamples for the delay line of chorus block nr. */

            /* The value in the lookup table is so, that this expression
             * will always be positive.  It will always include a number of
             * full periods of MAX_SAMPLES*INTERPOLATION_SUBSAMPLES to
             * remain positive at all times. */
            int pos_subsamples = (INTERPOLATION_SUBSAMPLES * chorus->counter
                                  - chorus->lookup_tab[chorus->phase[i]]);

            int pos_samples = pos_subsamples / INTERPOLATION_SUBSAMPLES;

            /* modulo divide by INTERPOLATION_SUBSAMPLES */
            pos_subsamples &= INTERPOLATION_SUBSAMPLES_ANDMASK;

            for(ii = 0; ii < INTERPOLATION_SAMPLES; ii++)
            {
                /* Add the delayed signal to the chorus sum d_out Note: The
                 * delay in the delay line moves backwards for increasing
                 * delay!*/

                /* The & in chorusbuf[...] is equivalent to a division modulo
                   MAX_SAMPLES, only faster. */
                d_out += chorus->chorusbuf[pos_samples & MAX_SAMPLES_ANDMASK]
                         * chorus->sinc_table[ii][pos_subsamples];

                pos_samples--;
            };

            /* Cycle the phase of the modulating LFO */
            chorus->phase[i]++;

            chorus->phase[i] %= (chorus->modulation_period_samples);
        } /* foreach chorus block */

        d_out *= chorus->level;

        /* Store the chorus sum d_out to output */
        left_out[sample_index] = d_out;
        right_out[sample_index] = d_out;

        /* Move forward in circular buffer */
        chorus->counter++;
        chorus->counter %= MAX_SAMPLES;

    } /* foreach sample */
}

/* Purpose:
 *
 * Calculates a modulation waveform (sine) Its value ( modulo
 * MAXSAMPLES) varies between 0 and depth*INTERPOLATION_SUBSAMPLES.
 * Its period length is len.  The waveform data will be used modulo
 * MAXSAMPLES only.  Since MAXSAMPLES is substracted from the waveform
 * a couple of times here, the resulting (current position in
 * buffer)-(waveform sample) will always be positive.
 */
static void
fluid_chorus_sine(int *buf, int len, int depth)
{
    int i;
    double angle, incr, mult;

    /* Pre-calculate increment between angles. */
    incr = (2. * M_PI) / (double)len;

    /* Pre-calculate 'depth' multiplier. */
    mult = (double) depth / 2.0 * (double) INTERPOLATION_SUBSAMPLES;

    /* Initialize to zero degrees. */
    angle = 0.;

    /* Build sine modulation waveform */
    for(i = 0; i < len; i++)
    {
        buf[i] = (int)((1. + sin(angle)) * mult) - 3 * MAX_SAMPLES * INTERPOLATION_SUBSAMPLES;

        angle += incr;
    }
}

/* Purpose:
 * Calculates a modulation waveform (triangle)
 * See fluid_chorus_sine for comments.
 */
static void
fluid_chorus_triangle(int *buf, int len, int depth)
{
    int *il = buf;
    int *ir = buf + len - 1;
    int ival;
    double val, incr;

    /* Pre-calculate increment for the ramp. */
    incr = 2.0 / len * (double)depth * (double) INTERPOLATION_SUBSAMPLES;

    /* Initialize first value */
    val = 0. - 3. * MAX_SAMPLES * INTERPOLATION_SUBSAMPLES;

    /* Build triangular modulation waveform */
    while(il <= ir)
    {
        /* Assume 'val' to be always negative for rounding mode */
        ival = (int)(val - 0.5);

        *il++ = ival;
        *ir-- = ival;

        val += incr;
    }
}