summaryrefslogtreecommitdiff
path: root/libs/fluidsynth/src/fluid_rvoice.c
blob: e76871163785ecaebf401034aa7875709d9cf498 (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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/* FluidSynth - A Software Synthesizer
 *
 * Copyright (C) 2003  Peter Hanappe 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
 */

#include "fluid_rvoice.h"
#include "fluid_conv.h"
#include "fluid_sys.h"


static void fluid_rvoice_noteoff_LOCAL(fluid_rvoice_t *voice, unsigned int min_ticks);

/**
 * @return -1 if voice has finished, 0 if it's currently quiet, 1 otherwise
 */
static FLUID_INLINE int
fluid_rvoice_calc_amp(fluid_rvoice_t *voice)
{
    fluid_real_t target_amp;	/* target amplitude */

    if(fluid_adsr_env_get_section(&voice->envlfo.volenv) == FLUID_VOICE_ENVDELAY)
    {
        return -1;    /* The volume amplitude is in hold phase. No sound is produced. */
    }

    if(fluid_adsr_env_get_section(&voice->envlfo.volenv) == FLUID_VOICE_ENVATTACK)
    {
        /* the envelope is in the attack section: ramp linearly to max value.
         * A positive modlfo_to_vol should increase volume (negative attenuation).
         */
        target_amp = fluid_cb2amp(voice->dsp.attenuation)
                     * fluid_cb2amp(fluid_lfo_get_val(&voice->envlfo.modlfo) * -voice->envlfo.modlfo_to_vol)
                     * fluid_adsr_env_get_val(&voice->envlfo.volenv);
    }
    else
    {
        fluid_real_t amplitude_that_reaches_noise_floor;
        fluid_real_t amp_max;

        target_amp = fluid_cb2amp(voice->dsp.attenuation)
                     * fluid_cb2amp(FLUID_PEAK_ATTENUATION * (1.0f - fluid_adsr_env_get_val(&voice->envlfo.volenv))
                                    + fluid_lfo_get_val(&voice->envlfo.modlfo) * -voice->envlfo.modlfo_to_vol);

        /* We turn off a voice, if the volume has dropped low enough. */

        /* A voice can be turned off, when an estimate for the volume
         * (upper bound) falls below that volume, that will drop the
         * sample below the noise floor.
         */

        /* If the loop amplitude is known, we can use it if the voice loop is within
         * the sample loop
         */

        /* Is the playing pointer already in the loop? */
        if(voice->dsp.has_looped)
        {
            amplitude_that_reaches_noise_floor = voice->dsp.amplitude_that_reaches_noise_floor_loop;
        }
        else
        {
            amplitude_that_reaches_noise_floor = voice->dsp.amplitude_that_reaches_noise_floor_nonloop;
        }

        /* voice->attenuation_min is a lower boundary for the attenuation
         * now and in the future (possibly 0 in the worst case).  Now the
         * amplitude of sample and volenv cannot exceed amp_max (since
         * volenv_val can only drop):
         */

        amp_max = fluid_cb2amp(voice->dsp.min_attenuation_cB) *
                  fluid_adsr_env_get_val(&voice->envlfo.volenv);

        /* And if amp_max is already smaller than the known amplitude,
         * which will attenuate the sample below the noise floor, then we
         * can safely turn off the voice. Duh. */
        if(amp_max < amplitude_that_reaches_noise_floor)
        {
            return 0;
        }
    }

    /* Volume increment to go from voice->amp to target_amp in FLUID_BUFSIZE steps */
    voice->dsp.amp_incr = (target_amp - voice->dsp.amp) / FLUID_BUFSIZE;

    fluid_check_fpe("voice_write amplitude calculation");

    /* no volume and not changing? - No need to process */
    if((voice->dsp.amp == 0.0f) && (voice->dsp.amp_incr == 0.0f))
    {
        return -1;
    }

    return 1;
}


/* these should be the absolute minimum that FluidSynth can deal with */
#define FLUID_MIN_LOOP_SIZE 2
#define FLUID_MIN_LOOP_PAD 0

#define FLUID_SAMPLESANITY_CHECK (1 << 0)
#define FLUID_SAMPLESANITY_STARTUP (1 << 1)

/* Purpose:
 *
 * Make sure, that sample start / end point and loop points are in
 * proper order. When starting up, calculate the initial phase.
 * TODO: Investigate whether this can be moved from rvoice to voice.
 */
static void
fluid_rvoice_check_sample_sanity(fluid_rvoice_t *voice)
{
    int min_index_nonloop = (int) voice->dsp.sample->start;
    int max_index_nonloop = (int) voice->dsp.sample->end;

    /* make sure we have enough samples surrounding the loop */
    int min_index_loop = (int) voice->dsp.sample->start + FLUID_MIN_LOOP_PAD;
    int max_index_loop = (int) voice->dsp.sample->end - FLUID_MIN_LOOP_PAD + 1;	/* 'end' is last valid sample, loopend can be + 1 */
    fluid_check_fpe("voice_check_sample_sanity start");

#if 0
    printf("Sample from %i to %i\n", voice->dsp.sample->start, voice->dsp.sample->end);
    printf("Sample loop from %i %i\n", voice->dsp.sample->loopstart, voice->dsp.sample->loopend);
    printf("Playback from %i to %i\n", voice->dsp.start, voice->dsp.end);
    printf("Playback loop from %i to %i\n", voice->dsp.loopstart, voice->dsp.loopend);
#endif

    /* Keep the start point within the sample data */
    if(voice->dsp.start < min_index_nonloop)
    {
        voice->dsp.start = min_index_nonloop;
    }
    else if(voice->dsp.start > max_index_nonloop)
    {
        voice->dsp.start = max_index_nonloop;
    }

    /* Keep the end point within the sample data */
    if(voice->dsp.end < min_index_nonloop)
    {
        voice->dsp.end = min_index_nonloop;
    }
    else if(voice->dsp.end > max_index_nonloop)
    {
        voice->dsp.end = max_index_nonloop;
    }

    /* Keep start and end point in the right order */
    if(voice->dsp.start > voice->dsp.end)
    {
        int temp = voice->dsp.start;
        voice->dsp.start = voice->dsp.end;
        voice->dsp.end = temp;
        /*FLUID_LOG(FLUID_DBG, "Loop / sample sanity check: Changing order of start / end points!"); */
    }

    /* Zero length? */
    if(voice->dsp.start == voice->dsp.end)
    {
        fluid_rvoice_voiceoff(voice, NULL);
        return;
    }

    if((voice->dsp.samplemode == FLUID_LOOP_UNTIL_RELEASE)
            || (voice->dsp.samplemode == FLUID_LOOP_DURING_RELEASE))
    {
        /* Keep the loop start point within the sample data */
        if(voice->dsp.loopstart < min_index_loop)
        {
            voice->dsp.loopstart = min_index_loop;
        }
        else if(voice->dsp.loopstart > max_index_loop)
        {
            voice->dsp.loopstart = max_index_loop;
        }

        /* Keep the loop end point within the sample data */
        if(voice->dsp.loopend < min_index_loop)
        {
            voice->dsp.loopend = min_index_loop;
        }
        else if(voice->dsp.loopend > max_index_loop)
        {
            voice->dsp.loopend = max_index_loop;
        }

        /* Keep loop start and end point in the right order */
        if(voice->dsp.loopstart > voice->dsp.loopend)
        {
            int temp = voice->dsp.loopstart;
            voice->dsp.loopstart = voice->dsp.loopend;
            voice->dsp.loopend = temp;
            /*FLUID_LOG(FLUID_DBG, "Loop / sample sanity check: Changing order of loop points!"); */
        }

        /* Loop too short? Then don't loop. */
        if(voice->dsp.loopend < voice->dsp.loopstart + FLUID_MIN_LOOP_SIZE)
        {
            voice->dsp.samplemode = FLUID_UNLOOPED;
        }

        /* The loop points may have changed. Obtain a new estimate for the loop volume. */
        /* Is the voice loop within the sample loop? */
        if((int)voice->dsp.loopstart >= (int)voice->dsp.sample->loopstart
                && (int)voice->dsp.loopend <= (int)voice->dsp.sample->loopend)
        {
            /* Is there a valid peak amplitude available for the loop, and can we use it? */
            if(voice->dsp.sample->amplitude_that_reaches_noise_floor_is_valid && voice->dsp.samplemode == FLUID_LOOP_DURING_RELEASE)
            {
                voice->dsp.amplitude_that_reaches_noise_floor_loop = voice->dsp.sample->amplitude_that_reaches_noise_floor / voice->dsp.synth_gain;
            }
            else
            {
                /* Worst case */
                voice->dsp.amplitude_that_reaches_noise_floor_loop = voice->dsp.amplitude_that_reaches_noise_floor_nonloop;
            };
        };

    } /* if sample mode is looped */

    /* Run startup specific code (only once, when the voice is started) */
    if(voice->dsp.check_sample_sanity_flag & FLUID_SAMPLESANITY_STARTUP)
    {
        if(max_index_loop - min_index_loop < FLUID_MIN_LOOP_SIZE)
        {
            if((voice->dsp.samplemode == FLUID_LOOP_UNTIL_RELEASE)
                    || (voice->dsp.samplemode == FLUID_LOOP_DURING_RELEASE))
            {
                voice->dsp.samplemode = FLUID_UNLOOPED;
            }
        }

        /* Set the initial phase of the voice (using the result from the
        start offset modulators). */
        fluid_phase_set_int(voice->dsp.phase, voice->dsp.start);
    } /* if startup */

    /* Is this voice run in loop mode, or does it run straight to the
       end of the waveform data? */
    if(((voice->dsp.samplemode == FLUID_LOOP_UNTIL_RELEASE) &&
            (fluid_adsr_env_get_section(&voice->envlfo.volenv) < FLUID_VOICE_ENVRELEASE))
            || (voice->dsp.samplemode == FLUID_LOOP_DURING_RELEASE))
    {
        /* Yes, it will loop as soon as it reaches the loop point.  In
         * this case we must prevent, that the playback pointer (phase)
         * happens to end up beyond the 2nd loop point, because the
         * point has moved.  The DSP algorithm is unable to cope with
         * that situation.  So if the phase is beyond the 2nd loop
         * point, set it to the start of the loop. No way to avoid some
         * noise here.  Note: If the sample pointer ends up -before the
         * first loop point- instead, then the DSP loop will just play
         * the sample, enter the loop and proceed as expected => no
         * actions required.
         */
        int index_in_sample = fluid_phase_index(voice->dsp.phase);

        if(index_in_sample >= voice->dsp.loopend)
        {
            /* FLUID_LOG(FLUID_DBG, "Loop / sample sanity check: Phase after 2nd loop point!"); */
            fluid_phase_set_int(voice->dsp.phase, voice->dsp.loopstart);
        }
    }

    /*    FLUID_LOG(FLUID_DBG, "Loop / sample sanity check: Sample from %i to %i, loop from %i to %i", voice->dsp.start, voice->dsp.end, voice->dsp.loopstart, voice->dsp.loopend); */

    /* Sample sanity has been assured. Don't check again, until some
       sample parameter is changed by modulation. */
    voice->dsp.check_sample_sanity_flag = 0;
#if 0
    printf("Sane? playback loop from %i to %i\n", voice->dsp.loopstart, voice->dsp.loopend);
#endif
    fluid_check_fpe("voice_check_sample_sanity");
}


/**
 * Synthesize a voice to a buffer.
 *
 * @param voice rvoice to synthesize
 * @param dsp_buf Audio buffer to synthesize to (#FLUID_BUFSIZE in length)
 * @return Count of samples written to dsp_buf. (-1 means voice is currently
 * quiet, 0 .. #FLUID_BUFSIZE-1 means voice finished.)
 *
 * Panning, reverb and chorus are processed separately. The dsp interpolation
 * routine is in (fluid_rvoice_dsp.c).
 */
int
fluid_rvoice_write(fluid_rvoice_t *voice, fluid_real_t *dsp_buf)
{
    int ticks = voice->envlfo.ticks;
    int count, is_looping;

    /******************* sample sanity check **********/

    if(!voice->dsp.sample)
    {
        return 0;
    }

    if(voice->dsp.check_sample_sanity_flag)
    {
        fluid_rvoice_check_sample_sanity(voice);
    }

    /******************* noteoff check ****************/

    if(voice->envlfo.noteoff_ticks != 0 &&
            voice->envlfo.ticks >= voice->envlfo.noteoff_ticks)
    {
        fluid_rvoice_noteoff_LOCAL(voice, 0);
    }

    voice->envlfo.ticks += FLUID_BUFSIZE;

    /******************* vol env **********************/

    fluid_adsr_env_calc(&voice->envlfo.volenv, 1);
    fluid_check_fpe("voice_write vol env");

    if(fluid_adsr_env_get_section(&voice->envlfo.volenv) == FLUID_VOICE_ENVFINISHED)
    {
        return 0;
    }

    /******************* mod env **********************/

    fluid_adsr_env_calc(&voice->envlfo.modenv, 0);
    fluid_check_fpe("voice_write mod env");

    /******************* lfo **********************/

    fluid_lfo_calc(&voice->envlfo.modlfo, ticks);
    fluid_check_fpe("voice_write mod LFO");
    fluid_lfo_calc(&voice->envlfo.viblfo, ticks);
    fluid_check_fpe("voice_write vib LFO");

    /******************* amplitude **********************/

    count = fluid_rvoice_calc_amp(voice);

    if(count <= 0)
    {
        return count;
    }

    /******************* phase **********************/

    /* Calculate the number of samples, that the DSP loop advances
     * through the original waveform with each step in the output
     * buffer. It is the ratio between the frequencies of original
     * waveform and output waveform.*/
    voice->dsp.phase_incr = fluid_ct2hz_real(voice->dsp.pitch +
                            voice->dsp.pitchoffset +
                            fluid_lfo_get_val(&voice->envlfo.modlfo) * voice->envlfo.modlfo_to_pitch
                            + fluid_lfo_get_val(&voice->envlfo.viblfo) * voice->envlfo.viblfo_to_pitch
                            + fluid_adsr_env_get_val(&voice->envlfo.modenv) * voice->envlfo.modenv_to_pitch)
                            / voice->dsp.root_pitch_hz;

    /******************* portamento ****************/
    /* pitchoffset is updated if enabled.
       Pitchoffset will be added to dsp pitch at next phase calculation time */

    /* In most cases portamento will be disabled. Thus first verify that portamento is
     * enabled before updating pitchoffset and before disabling portamento when necessary,
     * in order to keep the performance loss at minimum.
     * If the algorithm would first update pitchoffset and then verify if portamento
     * needs to be disabled, there would be a significant performance drop on a x87 FPU
     */
    if(voice->dsp.pitchinc > 0.0f)
    {
        /* portamento is enabled, so update pitchoffset */
        voice->dsp.pitchoffset += voice->dsp.pitchinc;

        /* when pitchoffset reaches 0.0f, portamento is disabled */
        if(voice->dsp.pitchoffset > 0.0f)
        {
            voice->dsp.pitchoffset = voice->dsp.pitchinc = 0.0f;
        }
    }
    else if(voice->dsp.pitchinc < 0.0f)
    {
        /* portamento is enabled, so update pitchoffset */
        voice->dsp.pitchoffset += voice->dsp.pitchinc;

        /* when pitchoffset reaches 0.0f, portamento is disabled */
        if(voice->dsp.pitchoffset < 0.0f)
        {
            voice->dsp.pitchoffset = voice->dsp.pitchinc = 0.0f;
        }
    }

    fluid_check_fpe("voice_write phase calculation");

    /* if phase_incr is not advancing, set it to the minimum fraction value (prevent stuckage) */
    if(voice->dsp.phase_incr == 0)
    {
        voice->dsp.phase_incr = 1;
    }

    /* voice is currently looping? */
    is_looping = voice->dsp.samplemode == FLUID_LOOP_DURING_RELEASE
                 || (voice->dsp.samplemode == FLUID_LOOP_UNTIL_RELEASE
                     && fluid_adsr_env_get_section(&voice->envlfo.volenv) < FLUID_VOICE_ENVRELEASE);

    /*********************** run the dsp chain ************************
     * The sample is mixed with the output buffer.
     * The buffer has to be filled from 0 to FLUID_BUFSIZE-1.
     * Depending on the position in the loop and the loop size, this
     * may require several runs. */

    switch(voice->dsp.interp_method)
    {
    case FLUID_INTERP_NONE:
        count = fluid_rvoice_dsp_interpolate_none(&voice->dsp, dsp_buf, is_looping);
        break;

    case FLUID_INTERP_LINEAR:
        count = fluid_rvoice_dsp_interpolate_linear(&voice->dsp, dsp_buf, is_looping);
        break;

    case FLUID_INTERP_4THORDER:
    default:
        count = fluid_rvoice_dsp_interpolate_4th_order(&voice->dsp, dsp_buf, is_looping);
        break;

    case FLUID_INTERP_7THORDER:
        count = fluid_rvoice_dsp_interpolate_7th_order(&voice->dsp, dsp_buf, is_looping);
        break;
    }

    fluid_check_fpe("voice_write interpolation");

    if(count == 0)
    {
        return count;
    }

    /*************** resonant filter ******************/

    fluid_iir_filter_calc(&voice->resonant_filter, voice->dsp.output_rate,
                          fluid_lfo_get_val(&voice->envlfo.modlfo) * voice->envlfo.modlfo_to_fc +
                          fluid_adsr_env_get_val(&voice->envlfo.modenv) * voice->envlfo.modenv_to_fc);

    fluid_iir_filter_apply(&voice->resonant_filter, dsp_buf, count);

    /* additional custom filter - only uses the fixed modulator, no lfos... */
    fluid_iir_filter_calc(&voice->resonant_custom_filter, voice->dsp.output_rate, 0);
    fluid_iir_filter_apply(&voice->resonant_custom_filter, dsp_buf, count);

    return count;
}

/**
 * Initialize buffers up to (and including) bufnum
 */
static int
fluid_rvoice_buffers_check_bufnum(fluid_rvoice_buffers_t *buffers, unsigned int bufnum)
{
    unsigned int i;

    if(bufnum < buffers->count)
    {
        return FLUID_OK;
    }

    if(bufnum >= FLUID_RVOICE_MAX_BUFS)
    {
        return FLUID_FAILED;
    }

    for(i = buffers->count; i <= bufnum; i++)
    {
        buffers->bufs[i].amp = 0.0f;
    }

    buffers->count = bufnum + 1;
    return FLUID_OK;
}


DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_buffers_set_amp)
{
    fluid_rvoice_buffers_t *buffers = obj;
    unsigned int bufnum = param[0].i;
    fluid_real_t value = param[1].real;

    if(fluid_rvoice_buffers_check_bufnum(buffers, bufnum) != FLUID_OK)
    {
        return;
    }

    buffers->bufs[bufnum].amp = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_buffers_set_mapping)
{
    fluid_rvoice_buffers_t *buffers = obj;
    unsigned int bufnum = param[0].i;
    int mapping = param[1].i;

    if(fluid_rvoice_buffers_check_bufnum(buffers, bufnum) != FLUID_OK)
    {
        return;
    }

    buffers->bufs[bufnum].mapping = mapping;
}


DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_reset)
{
    fluid_rvoice_t *voice = obj;

    voice->dsp.has_looped = 0;
    voice->envlfo.ticks = 0;
    voice->envlfo.noteoff_ticks = 0;
    voice->dsp.amp = 0.0f; /* The last value of the volume envelope, used to
                            calculate the volume increment during
                            processing */

    /* legato initialization */
    voice->dsp.pitchoffset = 0.0;   /* portamento initialization */
    voice->dsp.pitchinc = 0.0;

    /* mod env initialization*/
    fluid_adsr_env_reset(&voice->envlfo.modenv);

    /* vol env initialization */
    fluid_adsr_env_reset(&voice->envlfo.volenv);

    /* Fixme: Retrieve from any other existing
       voice on this channel to keep LFOs in
       unison? */
    fluid_lfo_reset(&voice->envlfo.viblfo);
    fluid_lfo_reset(&voice->envlfo.modlfo);

    /* Clear sample history in filter */
    fluid_iir_filter_reset(&voice->resonant_filter);
    fluid_iir_filter_reset(&voice->resonant_custom_filter);

    /* Force setting of the phase at the first DSP loop run
     * This cannot be done earlier, because it depends on modulators.
       [DH] Is that comment really true? */
    voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_STARTUP;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_noteoff)
{
    fluid_rvoice_t *rvoice = obj;
    unsigned int min_ticks = param[0].i;

    fluid_rvoice_noteoff_LOCAL(rvoice, min_ticks);
}

static void
fluid_rvoice_noteoff_LOCAL(fluid_rvoice_t *voice, unsigned int min_ticks)
{
    if(min_ticks > voice->envlfo.ticks)
    {
        /* Delay noteoff */
        voice->envlfo.noteoff_ticks = min_ticks;
        return;
    }

    voice->envlfo.noteoff_ticks = 0;

    if(fluid_adsr_env_get_section(&voice->envlfo.volenv) == FLUID_VOICE_ENVATTACK)
    {
        /* A voice is turned off during the attack section of the volume
         * envelope.  The attack section ramps up linearly with
         * amplitude. The other sections use logarithmic scaling. Calculate new
         * volenv_val to achieve equievalent amplitude during the release phase
         * for seamless volume transition.
         */
        if(fluid_adsr_env_get_val(&voice->envlfo.volenv) > 0)
        {
            fluid_real_t lfo = fluid_lfo_get_val(&voice->envlfo.modlfo) * -voice->envlfo.modlfo_to_vol;
            fluid_real_t amp = fluid_adsr_env_get_val(&voice->envlfo.volenv) * fluid_cb2amp(lfo);
            fluid_real_t env_value = - ((-200 * log(amp) / log(10.0) - lfo) / FLUID_PEAK_ATTENUATION - 1);
            fluid_clip(env_value, 0.0, 1.0);
            fluid_adsr_env_set_val(&voice->envlfo.volenv, env_value);
        }
    }

    fluid_adsr_env_set_section(&voice->envlfo.volenv, FLUID_VOICE_ENVRELEASE);
    fluid_adsr_env_set_section(&voice->envlfo.modenv, FLUID_VOICE_ENVRELEASE);
}

/**
 * skips to Attack section
 *
 * Updates vol and attack data
 * Correction on volume val to achieve equivalent amplitude at noteOn legato
 *
 * @param voice the synthesis voice to be updated
*/
static FLUID_INLINE void fluid_rvoice_local_retrigger_attack(fluid_rvoice_t *voice)
{
    /* skips to Attack section */
    /* Once in Attack section, current count must be reset, to be sure
    that the section will be not be prematurely finished. */
    fluid_adsr_env_set_section(&voice->envlfo.volenv, FLUID_VOICE_ENVATTACK);
    {
        /* Correction on volume val to achieve equivalent amplitude at noteOn legato */
        fluid_env_data_t *env_data;
        fluid_real_t peak = fluid_cb2amp(voice->dsp.attenuation);
        fluid_real_t prev_peak = fluid_cb2amp(voice->dsp.prev_attenuation);
        voice->envlfo.volenv.val = (voice->envlfo.volenv.val  * prev_peak) / peak;
        /* Correction on slope direction for Attack section */
        env_data = &voice->envlfo.volenv.data[FLUID_VOICE_ENVATTACK];

        if(voice->envlfo.volenv.val <= 1.0f)
        {
            /* slope attack for legato note needs to be positive from val  up to 1 */
            env_data->increment = 1.0f / env_data->count;
            env_data->min = -1.0f;
            env_data->max =  1.0f;
        }
        else
        {
            /* slope attack for legato note needs to be negative: from val  down to 1 */
            env_data->increment = -voice->envlfo.volenv.val / env_data->count;
            env_data->min = 1.0f;
            env_data->max = voice->envlfo.volenv.val;
        }
    }
}

/**
 * Used by legato Mode : multi_retrigger
 *  see fluid_synth_noteon_mono_legato_multi_retrigger()
 * @param voice the synthesis voice to be updated
*/
DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_multi_retrigger_attack)
{
    fluid_rvoice_t *voice = obj;
    int section = fluid_adsr_env_get_section(&voice->envlfo.volenv);

    /*-------------------------------------------------------------------------
     Section skip for volume envelope
    --------------------------------------------------------------------------*/
    if(section >= FLUID_VOICE_ENVHOLD)
    {
        /* DECAY, SUSTAIN,RELEASE section use logarithmic scaling. Calculates new
        volenv_val to achieve equivalent amplitude during the attack phase
        for seamless volume transition. */
        fluid_real_t amp_cb, env_value;
        amp_cb = FLUID_PEAK_ATTENUATION *
                 (1.0f - fluid_adsr_env_get_val(&voice->envlfo.volenv));
        env_value = fluid_cb2amp(amp_cb); /* a bit of optimization */
        fluid_clip(env_value, 0.0, 1.0);
        fluid_adsr_env_set_val(&voice->envlfo.volenv, env_value);
        /* next, skips to Attack section */
    }

    /* skips to Attack section from any section */
    /* Update vol and  attack data */
    fluid_rvoice_local_retrigger_attack(voice);
    /*-------------------------------------------------------------------------
     Section skip for modulation envelope
    --------------------------------------------------------------------------*/
    /* Skips from any section to ATTACK section */
    fluid_adsr_env_set_section(&voice->envlfo.modenv, FLUID_VOICE_ENVATTACK);
    /* Actually (v 1.1.6) all sections are linear, so there is no need to
     correct val value. However soundfont 2.01/2.4 spec. says that Attack should
     be convex (see issue #153  from Christian Collins). In the case Attack
     section would be changed to a non linear shape it will be necessary to do
     a correction for seamless val transition. Here is the place to do this */
}

/**
 * sets the portamento dsp parameters: dsp.pitchoffset, dsp.pitchinc
 * @param voice rvoice to set portamento.
 * @param countinc increment count number.
 * @param pitchoffset pitch offset to apply to voice dsp.pitch.
 *
 * Notes:
 * 1) To get continuous portamento between consecutive noteOn (n1,n2,n3...),
 *   pitchoffset is accumulated in current dsp pitchoffset.
 * 2) And to get constant portamento duration, dsp pitch increment is updated.
*/
DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_portamento)
{
    fluid_rvoice_t *voice = obj;
    unsigned int countinc = param[0].i;
    fluid_real_t pitchoffset = param[1].real;

    if(countinc)
    {
        voice->dsp.pitchoffset += pitchoffset;
        voice->dsp.pitchinc = - voice->dsp.pitchoffset / countinc;
    }

    /* Then during the voice processing (in fluid_rvoice_write()),
    dsp.pitchoffset will be incremented by dsp pitchinc. */
}


DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_output_rate)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->dsp.output_rate = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_interp_method)
{
    fluid_rvoice_t *voice = obj;
    int value = param[0].i;

    voice->dsp.interp_method = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_root_pitch_hz)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->dsp.root_pitch_hz = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_pitch)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->dsp.pitch = value;
}


DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_attenuation)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->dsp.prev_attenuation = voice->dsp.attenuation;
    voice->dsp.attenuation = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_min_attenuation_cB)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->dsp.min_attenuation_cB = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_viblfo_to_pitch)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->envlfo.viblfo_to_pitch = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modlfo_to_pitch)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->envlfo.modlfo_to_pitch = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modlfo_to_vol)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->envlfo.modlfo_to_vol = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modlfo_to_fc)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->envlfo.modlfo_to_fc = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modenv_to_fc)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->envlfo.modenv_to_fc = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modenv_to_pitch)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->envlfo.modenv_to_pitch = value;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_synth_gain)
{
    fluid_rvoice_t *voice = obj;
    fluid_real_t value = param[0].real;

    voice->dsp.synth_gain = value;

    /* For a looped sample, this value will be overwritten as soon as the
     * loop parameters are initialized (they may depend on modulators).
     * This value can be kept, it is a worst-case estimate.
     */
    voice->dsp.amplitude_that_reaches_noise_floor_nonloop = FLUID_NOISE_FLOOR / value;
    voice->dsp.amplitude_that_reaches_noise_floor_loop = FLUID_NOISE_FLOOR / value;
    voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_CHECK;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_start)
{
    fluid_rvoice_t *voice = obj;
    int value = param[0].i;

    voice->dsp.start = value;
    voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_CHECK;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_end)
{
    fluid_rvoice_t *voice = obj;
    int value = param[0].i;

    voice->dsp.end = value;
    voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_CHECK;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_loopstart)
{
    fluid_rvoice_t *voice = obj;
    int value = param[0].i;

    voice->dsp.loopstart = value;
    voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_CHECK;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_loopend)
{
    fluid_rvoice_t *voice = obj;
    int value = param[0].i;

    voice->dsp.loopend = value;
    voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_CHECK;
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_samplemode)
{
    fluid_rvoice_t *voice = obj;
    enum fluid_loop value = param[0].i;

    voice->dsp.samplemode = value;
    voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_CHECK;
}


DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_sample)
{
    fluid_rvoice_t *voice = obj;
    fluid_sample_t *value = param[0].ptr;

    voice->dsp.sample = value;

    if(value)
    {
        voice->dsp.check_sample_sanity_flag |= FLUID_SAMPLESANITY_STARTUP;
    }
}

DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_voiceoff)
{
    fluid_rvoice_t *voice = obj;

    fluid_adsr_env_set_section(&voice->envlfo.volenv, FLUID_VOICE_ENVFINISHED);
    fluid_adsr_env_set_section(&voice->envlfo.modenv, FLUID_VOICE_ENVFINISHED);
}