-
Notifications
You must be signed in to change notification settings - Fork 260
/
fluid_voice.c
2052 lines (1780 loc) · 69.9 KB
/
fluid_voice.c
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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_sys.h"
#include "fluid_voice.h"
#include "fluid_mod.h"
#include "fluid_chan.h"
#include "fluid_conv.h"
#include "fluid_synth.h"
#include "fluid_sys.h"
#include "fluid_sfont.h"
#include "fluid_rvoice_event.h"
#include "fluid_defsfont.h"
/* used for filter turn off optimization - if filter cutoff is above the
specified value and filter q is below the other value, turn filter off */
#define FLUID_MAX_AUDIBLE_FILTER_FC 19000.0f
#define FLUID_MIN_AUDIBLE_FILTER_Q 1.2f
/* min vol envelope release (to stop clicks) in SoundFont timecents */
#define FLUID_MIN_VOLENVRELEASE -7200.0f /* ~16ms */
static const int32_t INT24_MAX = (1 << (16 + 8 - 1));
static int fluid_voice_calculate_runtime_synthesis_parameters(fluid_voice_t *voice);
static int calculate_hold_decay_buffers(fluid_voice_t *voice, int gen_base,
int gen_key2base, int is_decay);
static fluid_real_t
fluid_voice_get_lower_boundary_for_attenuation(fluid_voice_t *voice);
#define UPDATE_RVOICE0(proc) \
do { \
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, voice->rvoice, param); \
} while (0)
#define UPDATE_RVOICE_GENERIC_R1(proc, obj, rarg) \
do { \
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
param[0].real = rarg; \
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
} while (0)
#define UPDATE_RVOICE_GENERIC_I1(proc, obj, iarg) \
do { \
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
param[0].i = iarg; \
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
} while (0)
#define UPDATE_RVOICE_GENERIC_I2(proc, obj, iarg1, iarg2) \
do { \
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
param[0].i = iarg1; \
param[1].i = iarg2; \
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
} while (0)
#define UPDATE_RVOICE_GENERIC_IR(proc, obj, iarg, rarg) \
do { \
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
param[0].i = iarg; \
param[1].real = rarg; \
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
} while (0)
#define UPDATE_RVOICE_R1(proc, arg1) UPDATE_RVOICE_GENERIC_R1(proc, voice->rvoice, arg1)
#define UPDATE_RVOICE_I1(proc, arg1) UPDATE_RVOICE_GENERIC_I1(proc, voice->rvoice, arg1)
#define UPDATE_RVOICE_BUFFERS_AMP(proc, iarg, rarg) UPDATE_RVOICE_GENERIC_IR(proc, &voice->rvoice->buffers, iarg, rarg)
#define UPDATE_RVOICE_ENVLFO_R1(proc, envp, rarg) UPDATE_RVOICE_GENERIC_R1(proc, &voice->rvoice->envlfo.envp, rarg)
#define UPDATE_RVOICE_ENVLFO_I1(proc, envp, iarg) UPDATE_RVOICE_GENERIC_I1(proc, &voice->rvoice->envlfo.envp, iarg)
static FLUID_INLINE void
fluid_voice_update_volenv(fluid_voice_t *voice,
int enqueue,
fluid_adsr_env_section_t section,
unsigned int count,
fluid_real_t coeff,
fluid_real_t increment,
fluid_real_t min,
fluid_real_t max)
{
fluid_rvoice_param_t param[MAX_EVENT_PARAMS];
param[0].i = section;
param[1].i = count;
param[2].real = coeff;
param[3].real = increment;
param[4].real = min;
param[5].real = max;
if(enqueue)
{
fluid_rvoice_eventhandler_push(voice->eventhandler,
fluid_adsr_env_set_data,
&voice->rvoice->envlfo.volenv,
param);
}
else
{
fluid_adsr_env_set_data(&voice->rvoice->envlfo.volenv, param);
}
}
static FLUID_INLINE void
fluid_voice_update_modenv(fluid_voice_t *voice,
int enqueue,
fluid_adsr_env_section_t section,
unsigned int count,
fluid_real_t coeff,
fluid_real_t increment,
fluid_real_t min,
fluid_real_t max)
{
fluid_rvoice_param_t param[MAX_EVENT_PARAMS];
param[0].i = section;
param[1].i = count;
param[2].real = coeff;
param[3].real = increment;
param[4].real = min;
param[5].real = max;
if(enqueue)
{
fluid_rvoice_eventhandler_push(voice->eventhandler,
fluid_adsr_env_set_data,
&voice->rvoice->envlfo.modenv,
param);
}
else
{
fluid_adsr_env_set_data(&voice->rvoice->envlfo.modenv, param);
}
}
static FLUID_INLINE void fluid_voice_sample_unref(fluid_sample_t **sample)
{
if(*sample != NULL)
{
fluid_sample_decr_ref(*sample);
*sample = NULL;
}
}
/*
* Swaps the current rvoice with the current overflow_rvoice
*/
static void fluid_voice_swap_rvoice(fluid_voice_t *voice)
{
fluid_rvoice_t *rtemp = voice->rvoice;
int ctemp = voice->can_access_rvoice;
voice->rvoice = voice->overflow_rvoice;
voice->can_access_rvoice = voice->can_access_overflow_rvoice;
voice->overflow_rvoice = rtemp;
voice->can_access_overflow_rvoice = ctemp;
voice->overflow_sample = voice->sample;
}
static void fluid_voice_initialize_rvoice(fluid_voice_t *voice, fluid_real_t output_rate)
{
fluid_rvoice_param_t param[MAX_EVENT_PARAMS];
FLUID_MEMSET(voice->rvoice, 0, sizeof(fluid_rvoice_t));
/* The 'sustain' and 'finished' segments of the volume / modulation
* envelope are constant. They are never affected by any modulator
* or generator. Therefore it is enough to initialize them once
* during the lifetime of the synth.
*/
fluid_voice_update_volenv(voice, FALSE, FLUID_VOICE_ENVSUSTAIN,
0xffffffff, 1.0f, 0.0f, -1.0f, 2.0f);
fluid_voice_update_volenv(voice, FALSE, FLUID_VOICE_ENVFINISHED,
0xffffffff, 0.0f, 0.0f, -1.0f, 1.0f);
fluid_voice_update_modenv(voice, FALSE, FLUID_VOICE_ENVSUSTAIN,
0xffffffff, 1.0f, 0.0f, -1.0f, 2.0f);
fluid_voice_update_modenv(voice, FALSE, FLUID_VOICE_ENVFINISHED,
0xffffffff, 0.0f, 0.0f, -1.0f, 1.0f);
param[0].i = FLUID_IIR_LOWPASS;
param[1].i = 0;
fluid_iir_filter_init(&voice->rvoice->resonant_filter, param);
param[0].i = FLUID_IIR_DISABLED;
fluid_iir_filter_init(&voice->rvoice->resonant_custom_filter, param);
param[0].real = output_rate;
fluid_rvoice_set_output_rate(voice->rvoice, param);
}
/*
* new_fluid_voice
*/
fluid_voice_t *
new_fluid_voice(fluid_rvoice_eventhandler_t *handler, fluid_real_t output_rate)
{
fluid_voice_t *voice;
voice = FLUID_NEW(fluid_voice_t);
if(voice == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
return NULL;
}
voice->can_access_rvoice = TRUE;
voice->can_access_overflow_rvoice = TRUE;
voice->rvoice = FLUID_NEW(fluid_rvoice_t);
voice->overflow_rvoice = FLUID_NEW(fluid_rvoice_t);
if(voice->rvoice == NULL || voice->overflow_rvoice == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
delete_fluid_voice(voice);
return NULL;
}
voice->status = FLUID_VOICE_CLEAN;
voice->chan = NO_CHANNEL;
voice->key = 0;
voice->vel = 0;
voice->eventhandler = handler;
voice->channel = NULL;
voice->sample = NULL;
voice->overflow_sample = NULL;
voice->output_rate = output_rate;
/* Initialize both the rvoice and overflow_rvoice */
fluid_voice_initialize_rvoice(voice, output_rate);
fluid_voice_swap_rvoice(voice);
fluid_voice_initialize_rvoice(voice, output_rate);
return voice;
}
/*
* delete_fluid_voice
*/
void
delete_fluid_voice(fluid_voice_t *voice)
{
fluid_return_if_fail(voice != NULL);
if(!voice->can_access_rvoice || !voice->can_access_overflow_rvoice)
{
FLUID_LOG(FLUID_WARN, "Deleting voice %u which has locked rvoices!", voice->id);
}
FLUID_FREE(voice->overflow_rvoice);
FLUID_FREE(voice->rvoice);
FLUID_FREE(voice);
}
/* fluid_voice_init
*
* Initialize the synthesis process
* inst_zone, the Instrument Zone contains the sample, Keyrange,Velrange
* of the voice.
* When playing legato (n1,n2) in mono mode, n2 will use n1 voices
* as far as n2 still enters in Keyrange,Velrange of n1.
*/
int
fluid_voice_init(fluid_voice_t *voice, fluid_sample_t *sample,
fluid_zone_range_t *inst_zone_range,
fluid_channel_t *channel, int key, int vel, unsigned int id,
unsigned int start_time, fluid_real_t gain)
{
/* Note: The voice parameters will be initialized later, when the
* generators have been retrieved from the sound font. Here, only
* the 'working memory' of the voice (position in envelopes, history
* of IIR filters, position in sample etc) is initialized. */
int i;
if(!voice->can_access_rvoice)
{
if(voice->can_access_overflow_rvoice)
{
fluid_voice_swap_rvoice(voice);
}
else
{
FLUID_LOG(FLUID_ERR, "Internal error: Cannot access an rvoice in fluid_voice_init!");
return FLUID_FAILED;
}
}
/* We are now guaranteed to have access to the rvoice */
if(voice->sample)
{
fluid_voice_off(voice);
}
voice->zone_range = inst_zone_range; /* Instrument zone range for legato */
voice->id = id;
voice->chan = fluid_channel_get_num(channel);
voice->key = (unsigned char) key;
voice->vel = (unsigned char) vel;
voice->channel = channel;
voice->mod_count = 0;
voice->start_time = start_time;
voice->has_noteoff = 0;
UPDATE_RVOICE0(fluid_rvoice_reset);
/*
We increment the reference count of the sample to indicate that this
sample is about to be owned by the rvoice. This will prevent the
unloading of the soundfont while this rvoice is playing.
*/
fluid_sample_incr_ref(sample);
fluid_rvoice_eventhandler_push_ptr(voice->eventhandler, fluid_rvoice_set_sample, voice->rvoice, sample);
voice->sample = sample;
i = fluid_channel_get_interp_method(channel);
UPDATE_RVOICE_I1(fluid_rvoice_set_interp_method, i);
/* Set all the generators to their default value, according to SF
* 2.01 section 8.1.3 (page 48). The value of NRPN messages are
* copied from the channel to the voice's generators. The sound font
* loader overwrites them. The generator values are later converted
* into voice parameters in
* fluid_voice_calculate_runtime_synthesis_parameters. */
fluid_gen_init(&voice->gen[0], channel);
UPDATE_RVOICE_I1(fluid_rvoice_set_samplemode, _SAMPLEMODE(voice));
voice->synth_gain = gain;
/* avoid division by zero later*/
if(voice->synth_gain < 0.0000001f)
{
voice->synth_gain = 0.0000001f;
}
UPDATE_RVOICE_R1(fluid_rvoice_set_synth_gain, voice->synth_gain);
/* Set up buffer mapping, should be done more flexible in the future. */
i = 2 * channel->synth->audio_groups;
i += (voice->chan % channel->synth->effects_groups) * channel->synth->effects_channels;
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 2, i + SYNTH_REVERB_CHANNEL);
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 3, i + SYNTH_CHORUS_CHANNEL);
i = 2 * (voice->chan % channel->synth->audio_groups);
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 0, i);
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 1, i + 1);
return FLUID_OK;
}
/**
* Update sample rate.
*
* @note If the voice is active, it will be turned off.
*/
void
fluid_voice_set_output_rate(fluid_voice_t *voice, fluid_real_t value)
{
if(fluid_voice_is_playing(voice))
{
fluid_voice_off(voice);
}
voice->output_rate = value;
UPDATE_RVOICE_GENERIC_R1(fluid_rvoice_set_output_rate, voice->rvoice, value);
UPDATE_RVOICE_GENERIC_R1(fluid_rvoice_set_output_rate, voice->overflow_rvoice, value);
}
/**
* Set the value of a generator.
*
* @param voice Voice instance
* @param i Generator ID (#fluid_gen_type)
* @param val Generator value
*/
void
fluid_voice_gen_set(fluid_voice_t *voice, int i, float val)
{
voice->gen[i].val = val;
voice->gen[i].flags = GEN_SET;
if(i == GEN_SAMPLEMODE)
{
UPDATE_RVOICE_I1(fluid_rvoice_set_samplemode, (int) val);
}
}
/**
* Offset the value of a generator.
*
* @param voice Voice instance
* @param i Generator ID (#fluid_gen_type)
* @param val Value to add to the existing value
*/
void
fluid_voice_gen_incr(fluid_voice_t *voice, int i, float val)
{
voice->gen[i].val += val;
voice->gen[i].flags = GEN_SET;
}
/**
* Get the value of a generator.
*
* @param voice Voice instance
* @param gen Generator ID (#fluid_gen_type)
* @return Current generator value
*/
float
fluid_voice_gen_get(fluid_voice_t *voice, int gen)
{
return voice->gen[gen].val;
}
fluid_real_t fluid_voice_gen_value(const fluid_voice_t *voice, int num)
{
return (fluid_real_t)(voice->gen[num].val + voice->gen[num].mod + voice->gen[num].nrpn);
}
/*
* fluid_voice_start
*/
void fluid_voice_start(fluid_voice_t *voice)
{
/* The maximum volume of the loop is calculated and cached once for each
* sample with its nominal loop settings. This happens, when the sample is used
* for the first time.*/
fluid_voice_calculate_runtime_synthesis_parameters(voice);
#ifdef WITH_PROFILING
voice->ref = fluid_profile_ref();
#endif
voice->status = FLUID_VOICE_ON;
/* Increment voice count */
voice->channel->synth->active_voice_count++;
}
/**
* Calculate the amplitude of a voice.
*
* @param gain The gain value in the range [0.0 ; 1.0]
* @return An amplitude used by rvoice_mixer's buffers
*/
static FLUID_INLINE fluid_real_t
fluid_voice_calculate_gain_amplitude(const fluid_voice_t *voice, fluid_real_t gain)
{
/* we use 24bit samples in fluid_rvoice_dsp. in order to normalize float
* samples to [0.0;1.0] divide samples by the max. value of an int24 and
* amplify them with the gain */
return gain * voice->synth_gain / (INT24_MAX * 1.0f);
}
/* Useful to return the nominal pitch of a key */
/* The nominal pitch is dependent of voice->root_pitch,tuning, and
GEN_SCALETUNE generator.
This is useful to set the value of GEN_PITCH generator on noteOn.
This is useful to get the beginning/ending pitch for portamento.
*/
fluid_real_t fluid_voice_calculate_pitch(fluid_voice_t *voice, int key)
{
fluid_tuning_t *tuning;
fluid_real_t x, pitch;
/* Now the nominal pitch of the key is returned.
* Note about SCALETUNE: SF2.01 8.1.3 says, that this generator is a
* non-realtime parameter. So we don't allow modulation (as opposed
* to fluid_voice_gen_value(voice, GEN_SCALETUNE) When the scale tuning is varied,
* one key remains fixed. Here C3 (MIDI number 60) is used.
*/
if(fluid_channel_has_tuning(voice->channel))
{
tuning = fluid_channel_get_tuning(voice->channel);
x = fluid_tuning_get_pitch(tuning, (int)(voice->root_pitch / 100.0f));
pitch = voice->gen[GEN_SCALETUNE].val / 100.0f *
(fluid_tuning_get_pitch(tuning, key) - x) + x;
}
else
{
pitch = voice->gen[GEN_SCALETUNE].val
* (key - voice->root_pitch / 100.0f) + voice->root_pitch;
}
return pitch;
}
void
fluid_voice_calculate_gen_pitch(fluid_voice_t *voice)
{
voice->gen[GEN_PITCH].val = fluid_voice_calculate_pitch(voice, fluid_voice_get_actual_key(voice));
}
/*
* fluid_voice_calculate_runtime_synthesis_parameters
*
* in this function we calculate the values of all the parameters. the
* parameters are converted to their most useful unit for the DSP
* algorithm, for example, number of samples instead of
* timecents. Some parameters keep their "perceptual" unit and
* conversion will be done in the DSP function. This is the case, for
* example, for the pitch since it is modulated by the controllers in
* cents. */
static int
fluid_voice_calculate_runtime_synthesis_parameters(fluid_voice_t *voice)
{
int i;
unsigned int n;
static int const list_of_generators_to_initialize[] =
{
GEN_STARTADDROFS, /* SF2.01 page 48 #0 */
GEN_ENDADDROFS, /* #1 */
GEN_STARTLOOPADDROFS, /* #2 */
GEN_ENDLOOPADDROFS, /* #3 */
/* GEN_STARTADDRCOARSEOFS see comment below [1] #4 */
GEN_MODLFOTOPITCH, /* #5 */
GEN_VIBLFOTOPITCH, /* #6 */
GEN_MODENVTOPITCH, /* #7 */
GEN_FILTERFC, /* #8 */
GEN_FILTERQ, /* #9 */
GEN_MODLFOTOFILTERFC, /* #10 */
GEN_MODENVTOFILTERFC, /* #11 */
/* GEN_ENDADDRCOARSEOFS [1] #12 */
GEN_MODLFOTOVOL, /* #13 */
/* not defined #14 */
GEN_CHORUSSEND, /* #15 */
GEN_REVERBSEND, /* #16 */
GEN_PAN, /* #17 */
/* not defined #18 */
/* not defined #19 */
/* not defined #20 */
GEN_MODLFODELAY, /* #21 */
GEN_MODLFOFREQ, /* #22 */
GEN_VIBLFODELAY, /* #23 */
GEN_VIBLFOFREQ, /* #24 */
GEN_MODENVDELAY, /* #25 */
GEN_MODENVATTACK, /* #26 */
GEN_MODENVHOLD, /* #27 */
GEN_MODENVDECAY, /* #28 */
/* GEN_MODENVSUSTAIN [1] #29 */
GEN_MODENVRELEASE, /* #30 */
/* GEN_KEYTOMODENVHOLD [1] #31 */
/* GEN_KEYTOMODENVDECAY [1] #32 */
GEN_VOLENVDELAY, /* #33 */
GEN_VOLENVATTACK, /* #34 */
GEN_VOLENVHOLD, /* #35 */
GEN_VOLENVDECAY, /* #36 */
/* GEN_VOLENVSUSTAIN [1] #37 */
GEN_VOLENVRELEASE, /* #38 */
/* GEN_KEYTOVOLENVHOLD [1] #39 */
/* GEN_KEYTOVOLENVDECAY [1] #40 */
/* GEN_STARTLOOPADDRCOARSEOFS [1] #45 */
GEN_KEYNUM, /* #46 */
GEN_VELOCITY, /* #47 */
GEN_ATTENUATION, /* #48 */
/* GEN_ENDLOOPADDRCOARSEOFS [1] #50 */
/* GEN_COARSETUNE [1] #51 */
/* GEN_FINETUNE [1] #52 */
GEN_OVERRIDEROOTKEY, /* #58 */
GEN_PITCH, /* --- */
GEN_CUSTOM_BALANCE, /* --- */
GEN_CUSTOM_FILTERFC, /* --- */
GEN_CUSTOM_FILTERQ /* --- */
};
/* When the voice is made ready for the synthesis process, a lot of
* voice-internal parameters have to be calculated.
*
* At this point, the sound font has already set the -nominal- value
* for all generators (excluding GEN_PITCH). Most generators can be
* modulated - they include a nominal value and an offset (which
* changes with velocity, note number, channel parameters like
* aftertouch, mod wheel...) Now this offset will be calculated as
* follows:
*
* - Process each modulator once.
* - Calculate its output value.
* - Find the target generator.
* - Add the output value to the modulation value of the generator.
*
* Note: The generators have been initialized with
* fluid_gen_init().
*/
for(i = 0; i < voice->mod_count; i++)
{
fluid_mod_t *mod = &voice->mod[i];
fluid_real_t modval = fluid_mod_get_value(mod, voice);
int dest_gen_index = mod->dest;
fluid_gen_t *dest_gen = &voice->gen[dest_gen_index];
dest_gen->mod += modval;
/* fluid_dump_modulator(mod); */
}
/* Now the generators are initialized, nominal and modulation value.
* The voice parameters (which depend on generators) are calculated
* with fluid_voice_update_param. Processing the list of generator
* changes will calculate each voice parameter once.
*
* Note [1]: Some voice parameters depend on several generators. For
* example, the pitch depends on GEN_COARSETUNE, GEN_FINETUNE and
* GEN_PITCH. voice->pitch. Unnecessary recalculation is avoided
* by removing all but one generator from the list of voice
* parameters. Same with GEN_XXX and GEN_XXXCOARSE: the
* initialisation list contains only GEN_XXX.
*/
/* Calculate the voice parameter(s) dependent on each generator. */
for(n = 0; n < FLUID_N_ELEMENTS(list_of_generators_to_initialize); n++)
{
fluid_voice_update_param(voice, list_of_generators_to_initialize[n]);
}
/* Start portamento if enabled */
{
/* fromkey note comes from "GetFromKeyPortamentoLegato()" detector.
When fromkey is set to ValidNote , portamento is started */
/* Return fromkey portamento */
int fromkey = voice->channel->synth->fromkey_portamento;
if(fluid_channel_is_valid_note(fromkey))
{
/* Send portamento parameters to the voice dsp */
fluid_voice_update_portamento(voice, fromkey, fluid_voice_get_actual_key(voice));
}
}
/* Make an estimate on how loud this voice can get at any time (attenuation). */
UPDATE_RVOICE_R1(fluid_rvoice_set_min_attenuation_cB,
fluid_voice_get_lower_boundary_for_attenuation(voice));
return FLUID_OK;
}
/*
* calculate_hold_decay_buffers
*/
static int
calculate_hold_decay_buffers(fluid_voice_t *voice, int gen_base,
int gen_key2base, int is_decay)
{
/* Purpose:
*
* Returns the number of DSP loops, that correspond to the hold
* (is_decay=0) or decay (is_decay=1) time.
* gen_base=GEN_VOLENVHOLD, GEN_VOLENVDECAY, GEN_MODENVHOLD,
* GEN_MODENVDECAY gen_key2base=GEN_KEYTOVOLENVHOLD,
* GEN_KEYTOVOLENVDECAY, GEN_KEYTOMODENVHOLD, GEN_KEYTOMODENVDECAY
*/
fluid_real_t keysteps;
fluid_real_t timecents;
fluid_real_t seconds;
int buffers;
/* SF2.01 section 8.4.3 # 31, 32, 39, 40
* GEN_KEYTOxxxENVxxx uses key 60 as 'origin'.
* The unit of the generator is timecents per key number.
* If KEYTOxxxENVxxx is 100, a key one octave over key 60 (72)
* will cause (60-72)*100=-1200 timecents of time variation.
* The time is cut in half.
*/
keysteps = 60.0f - fluid_channel_get_key_pitch(voice->channel, fluid_voice_get_actual_key(voice)) / 100.0f;
timecents = fluid_voice_gen_value(voice, gen_base) + fluid_voice_gen_value(voice, gen_key2base) * keysteps;
/* Range checking */
if(is_decay)
{
/* SF 2.01 section 8.1.3 # 28, 36 */
if(timecents > 8000.f)
{
timecents = 8000.f;
}
}
else
{
/* SF 2.01 section 8.1.3 # 27, 35 */
if(timecents > 5000.f)
{
timecents = 5000.f;
}
/* SF 2.01 section 8.1.2 # 27, 35:
* The most negative number indicates no hold time
*/
if(timecents <= -32768.f)
{
return 0;
}
}
/* SF 2.01 section 8.1.3 # 27, 28, 35, 36 */
if(timecents < -12000.f)
{
timecents = -12000.f;
}
seconds = fluid_tc2sec(timecents);
/* Each DSP loop processes FLUID_BUFSIZE samples. */
/* round to next full number of buffers */
buffers = (int)(((fluid_real_t)voice->output_rate * seconds)
/ (fluid_real_t)FLUID_BUFSIZE
+ 0.5f);
return buffers;
}
/*
* The value of a generator (gen) has changed. (The different
* generators are listed in fluidsynth.h, or in SF2.01 page 48-49)
* Now the dependent 'voice' parameters are calculated.
*
* fluid_voice_update_param can be called during the setup of the
* voice (to calculate the initial value for a voice parameter), or
* during its operation (a generator has been changed due to
* real-time parameter modifications like pitch-bend).
*
* Note: The generator holds three values: The base value .val, an
* offset caused by modulators .mod, and an offset caused by the
* NRPN system. fluid_voice_gen_value(voice, generator_enumerator) returns the sum
* of all three.
*/
/**
* Update all the synthesis parameters which depend on generator \a gen.
*
* @param voice Voice instance
* @param gen Generator id (#fluid_gen_type)
*
* Calling this function is only necessary after changing a generator of an already playing voice.
*/
void
fluid_voice_update_param(fluid_voice_t *voice, int gen)
{
unsigned int count, z;
fluid_real_t x = fluid_voice_gen_value(voice, gen);
switch(gen)
{
case GEN_PAN:
case GEN_CUSTOM_BALANCE:
/* range checking is done in the fluid_pan and fluid_balance functions */
voice->pan = fluid_voice_gen_value(voice, GEN_PAN);
voice->balance = fluid_voice_gen_value(voice, GEN_CUSTOM_BALANCE);
/* left amp */
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 0,
fluid_voice_calculate_gain_amplitude(voice,
fluid_pan(voice->pan, 1) * fluid_balance(voice->balance, 1)));
/* right amp */
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 1,
fluid_voice_calculate_gain_amplitude(voice,
fluid_pan(voice->pan, 0) * fluid_balance(voice->balance, 0)));
break;
case GEN_ATTENUATION:
voice->attenuation = x;
/* Range: SF2.01 section 8.1.3 # 48
* Motivation for range checking:
* OHPiano.SF2 sets initial attenuation to a whooping -96 dB */
fluid_clip(voice->attenuation, 0.f, 1440.f);
UPDATE_RVOICE_R1(fluid_rvoice_set_attenuation, voice->attenuation);
break;
/* The pitch is calculated from three different generators.
* Read comment in fluidsynth.h about GEN_PITCH.
*/
case GEN_PITCH:
case GEN_COARSETUNE:
case GEN_FINETUNE:
/* The testing for allowed range is done in 'fluid_ct2hz' */
voice->pitch = (fluid_voice_gen_value(voice, GEN_PITCH)
+ 100.0f * fluid_voice_gen_value(voice, GEN_COARSETUNE)
+ fluid_voice_gen_value(voice, GEN_FINETUNE));
UPDATE_RVOICE_R1(fluid_rvoice_set_pitch, voice->pitch);
break;
case GEN_REVERBSEND:
/* The generator unit is 'tenths of a percent'. */
voice->reverb_send = x / 1000.0f;
fluid_clip(voice->reverb_send, 0.f, 1.f);
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 2, fluid_voice_calculate_gain_amplitude(voice, voice->reverb_send));
break;
case GEN_CHORUSSEND:
/* The generator unit is 'tenths of a percent'. */
voice->chorus_send = x / 1000.0f;
fluid_clip(voice->chorus_send, 0.f, 1.f);
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 3, fluid_voice_calculate_gain_amplitude(voice, voice->chorus_send));
break;
case GEN_OVERRIDEROOTKEY:
/* This is a non-realtime parameter. Therefore the .mod part of the generator
* can be neglected.
* NOTE: origpitch sets MIDI root note while pitchadj is a fine tuning amount
* which offsets the original rate. This means that the fine tuning is
* inverted with respect to the root note (so subtract it, not add).
*/
if(voice->sample != NULL)
{
if(voice->gen[GEN_OVERRIDEROOTKEY].val > -1) //FIXME: use flag instead of -1
{
voice->root_pitch = voice->gen[GEN_OVERRIDEROOTKEY].val * 100.0f
- voice->sample->pitchadj;
}
else
{
voice->root_pitch = voice->sample->origpitch * 100.0f - voice->sample->pitchadj;
}
x = (fluid_ct2hz_real(voice->root_pitch) * ((fluid_real_t) voice->output_rate / voice->sample->samplerate));
}
else
{
if(voice->gen[GEN_OVERRIDEROOTKEY].val > -1) //FIXME: use flag instead of -1
{
voice->root_pitch = voice->gen[GEN_OVERRIDEROOTKEY].val * 100.0f;
}
else
{
voice->root_pitch = 0;
}
x = fluid_ct2hz_real(voice->root_pitch);
}
/* voice->pitch depends on voice->root_pitch, so calculate voice->pitch now */
fluid_voice_calculate_gen_pitch(voice);
UPDATE_RVOICE_R1(fluid_rvoice_set_root_pitch_hz, x);
break;
case GEN_FILTERFC:
/* The resonance frequency is converted from absolute cents to
* midicents .val and .mod are both used, this permits real-time
* modulation. The allowed range is tested in the 'fluid_ct2hz'
* function [PH,20021214]
*/
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_fres, &voice->rvoice->resonant_filter, x);
break;
case GEN_FILTERQ:
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_q, &voice->rvoice->resonant_filter, x);
break;
/* same as the two above, only for the custom filter */
case GEN_CUSTOM_FILTERFC:
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_fres, &voice->rvoice->resonant_custom_filter, x);
break;
case GEN_CUSTOM_FILTERQ:
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_q, &voice->rvoice->resonant_custom_filter, x);
break;
case GEN_MODLFOTOPITCH:
fluid_clip(x, -12000.f, 12000.f);
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_pitch, x);
break;
case GEN_MODLFOTOVOL:
fluid_clip(x, -960.f, 960.f);
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_vol, x);
break;
case GEN_MODLFOTOFILTERFC:
fluid_clip(x, -12000.f, 12000.f);
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_fc, x);
break;
case GEN_MODLFODELAY:
fluid_clip(x, -12000.0f, 5000.0f);
z = (unsigned int)(voice->output_rate * fluid_tc2sec_delay(x));
UPDATE_RVOICE_ENVLFO_I1(fluid_lfo_set_delay, modlfo, z);
break;
case GEN_MODLFOFREQ:
/* - the frequency is converted into a delta value, per buffer of FLUID_BUFSIZE samples
* - the delay into a sample delay
*/
fluid_clip(x, -16000.0f, 4500.0f);
x = (4.0f * FLUID_BUFSIZE * fluid_ct2hz_real(x) / voice->output_rate);
UPDATE_RVOICE_ENVLFO_R1(fluid_lfo_set_incr, modlfo, x);
break;
case GEN_VIBLFOFREQ:
/* vib lfo
*
* - the frequency is converted into a delta value, per buffer of FLUID_BUFSIZE samples
* - the delay into a sample delay
*/
fluid_clip(x, -16000.0f, 4500.0f);
x = 4.0f * FLUID_BUFSIZE * fluid_ct2hz_real(x) / voice->output_rate;
UPDATE_RVOICE_ENVLFO_R1(fluid_lfo_set_incr, viblfo, x);
break;
case GEN_VIBLFODELAY:
fluid_clip(x, -12000.0f, 5000.0f);
z = (unsigned int)(voice->output_rate * fluid_tc2sec_delay(x));
UPDATE_RVOICE_ENVLFO_I1(fluid_lfo_set_delay, viblfo, z);
break;
case GEN_VIBLFOTOPITCH:
fluid_clip(x, -12000.f, 12000.f);
UPDATE_RVOICE_R1(fluid_rvoice_set_viblfo_to_pitch, x);
break;
case GEN_KEYNUM:
/* GEN_KEYNUM: SF2.01 page 46, item 46
*
* If this generator is active, it forces the key number to its
* value. Non-realtime controller.
*
* There is a flag, which should indicate, whether a generator is
* enabled or not. But here we rely on the default value of -1.
*/
/* 2017-09-02: do not change the voice's key here, otherwise it will
* never be released on a noteoff event
*/
#if 0
x = fluid_voice_gen_value(voice, GEN_KEYNUM);
if(x >= 0)
{
voice->key = x;
}
#endif
break;
case GEN_VELOCITY:
/* GEN_VELOCITY: SF2.01 page 46, item 47
*
* If this generator is active, it forces the velocity to its
* value. Non-realtime controller.
*
* There is a flag, which should indicate, whether a generator is
* enabled or not. But here we rely on the default value of -1.
*/
/* 2017-09-02: do not change the voice's velocity here, use
* fluid_voice_get_actual_velocity() to get the value of this generator
* if active.
*/
#if 0
x = fluid_voice_gen_value(voice, GEN_VELOCITY);
if(x > 0)
{
voice->vel = x;
}
#endif
break;
case GEN_MODENVTOPITCH:
fluid_clip(x, -12000.f, 12000.f);
UPDATE_RVOICE_R1(fluid_rvoice_set_modenv_to_pitch, x);
break;
case GEN_MODENVTOFILTERFC:
/* Range: SF2.01 section 8.1.3 # 1
* Motivation for range checking:
* Filter is reported to make funny noises now and then
*/
fluid_clip(x, -12000.f, 12000.f);
UPDATE_RVOICE_R1(fluid_rvoice_set_modenv_to_fc, x);
break;
/* sample start and ends points
*
* Range checking is initiated via the