Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/fluidsynth/src/synth/fluid_voice.c
4396 views
1
/* FluidSynth - A Software Synthesizer
2
*
3
* Copyright (C) 2003 Peter Hanappe and others.
4
*
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public License
7
* as published by the Free Software Foundation; either version 2.1 of
8
* the License, or (at your option) any later version.
9
*
10
* This library is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
14
*
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free
17
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
* 02110-1301, USA
19
*/
20
21
#include "fluid_sys.h"
22
#include "fluid_voice.h"
23
#include "fluid_mod.h"
24
#include "fluid_chan.h"
25
#include "fluid_conv.h"
26
#include "fluid_synth.h"
27
#include "fluid_sys.h"
28
#include "fluid_sfont.h"
29
#include "fluid_rvoice_event.h"
30
#include "fluid_defsfont.h"
31
32
/* used for filter turn off optimization - if filter cutoff is above the
33
specified value and filter q is below the other value, turn filter off */
34
#define FLUID_MAX_AUDIBLE_FILTER_FC 19000.0f
35
#define FLUID_MIN_AUDIBLE_FILTER_Q 1.2f
36
37
/* min vol envelope release (to stop clicks) in SoundFont timecents */
38
#define FLUID_MIN_VOLENVRELEASE -7200.0f /* ~16ms */
39
40
41
static const int32_t INT24_MAX = (1 << (16 + 8 - 1));
42
43
static int fluid_voice_calculate_runtime_synthesis_parameters(fluid_voice_t *voice);
44
static int calculate_hold_decay_buffers(fluid_voice_t *voice, int gen_base,
45
int gen_key2base, int is_decay);
46
static fluid_real_t
47
fluid_voice_get_lower_boundary_for_attenuation(fluid_voice_t *voice);
48
49
#define UPDATE_RVOICE0(proc) \
50
do { \
51
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
52
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, voice->rvoice, param); \
53
} while (0)
54
55
#define UPDATE_RVOICE_GENERIC_R1(proc, obj, rarg) \
56
do { \
57
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
58
param[0].real = rarg; \
59
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
60
} while (0)
61
62
#define UPDATE_RVOICE_GENERIC_I1(proc, obj, iarg) \
63
do { \
64
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
65
param[0].i = iarg; \
66
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
67
} while (0)
68
69
#define UPDATE_RVOICE_GENERIC_I2(proc, obj, iarg1, iarg2) \
70
do { \
71
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
72
param[0].i = iarg1; \
73
param[1].i = iarg2; \
74
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
75
} while (0)
76
77
#define UPDATE_RVOICE_GENERIC_IR(proc, obj, iarg, rarg) \
78
do { \
79
fluid_rvoice_param_t param[MAX_EVENT_PARAMS]; \
80
param[0].i = iarg; \
81
param[1].real = rarg; \
82
fluid_rvoice_eventhandler_push(voice->eventhandler, proc, obj, param); \
83
} while (0)
84
85
86
#define UPDATE_RVOICE_R1(proc, arg1) UPDATE_RVOICE_GENERIC_R1(proc, voice->rvoice, arg1)
87
#define UPDATE_RVOICE_I1(proc, arg1) UPDATE_RVOICE_GENERIC_I1(proc, voice->rvoice, arg1)
88
89
#define UPDATE_RVOICE_BUFFERS_AMP(proc, iarg, rarg) UPDATE_RVOICE_GENERIC_IR(proc, &voice->rvoice->buffers, iarg, rarg)
90
#define UPDATE_RVOICE_ENVLFO_R1(proc, envp, rarg) UPDATE_RVOICE_GENERIC_R1(proc, &voice->rvoice->envlfo.envp, rarg)
91
#define UPDATE_RVOICE_ENVLFO_I1(proc, envp, iarg) UPDATE_RVOICE_GENERIC_I1(proc, &voice->rvoice->envlfo.envp, iarg)
92
93
static FLUID_INLINE void
94
fluid_voice_update_volenv(fluid_voice_t *voice,
95
int enqueue,
96
fluid_adsr_env_section_t section,
97
unsigned int count,
98
fluid_real_t coeff,
99
fluid_real_t increment,
100
fluid_real_t min,
101
fluid_real_t max)
102
{
103
fluid_rvoice_param_t param[MAX_EVENT_PARAMS];
104
105
param[0].i = section;
106
param[1].i = count;
107
param[2].real = coeff;
108
param[3].real = increment;
109
param[4].real = min;
110
param[5].real = max;
111
112
if(enqueue)
113
{
114
fluid_rvoice_eventhandler_push(voice->eventhandler,
115
fluid_adsr_env_set_data,
116
&voice->rvoice->envlfo.volenv,
117
param);
118
}
119
else
120
{
121
fluid_adsr_env_set_data(&voice->rvoice->envlfo.volenv, param);
122
}
123
}
124
125
static FLUID_INLINE void
126
fluid_voice_update_modenv(fluid_voice_t *voice,
127
int enqueue,
128
fluid_adsr_env_section_t section,
129
unsigned int count,
130
fluid_real_t coeff,
131
fluid_real_t increment,
132
fluid_real_t min,
133
fluid_real_t max)
134
{
135
fluid_rvoice_param_t param[MAX_EVENT_PARAMS];
136
137
param[0].i = section;
138
param[1].i = count;
139
param[2].real = coeff;
140
param[3].real = increment;
141
param[4].real = min;
142
param[5].real = max;
143
144
if(enqueue)
145
{
146
fluid_rvoice_eventhandler_push(voice->eventhandler,
147
fluid_adsr_env_set_data,
148
&voice->rvoice->envlfo.modenv,
149
param);
150
}
151
else
152
{
153
fluid_adsr_env_set_data(&voice->rvoice->envlfo.modenv, param);
154
}
155
}
156
157
static FLUID_INLINE void fluid_voice_sample_unref(fluid_sample_t **sample)
158
{
159
if(*sample != NULL)
160
{
161
fluid_sample_decr_ref(*sample);
162
*sample = NULL;
163
}
164
}
165
166
/*
167
* Swaps the current rvoice with the current overflow_rvoice
168
*/
169
static void fluid_voice_swap_rvoice(fluid_voice_t *voice)
170
{
171
fluid_rvoice_t *rtemp = voice->rvoice;
172
int ctemp = voice->can_access_rvoice;
173
voice->rvoice = voice->overflow_rvoice;
174
voice->can_access_rvoice = voice->can_access_overflow_rvoice;
175
voice->overflow_rvoice = rtemp;
176
voice->can_access_overflow_rvoice = ctemp;
177
voice->overflow_sample = voice->sample;
178
}
179
180
static void fluid_voice_initialize_rvoice(fluid_voice_t *voice, fluid_real_t output_rate)
181
{
182
fluid_rvoice_param_t param[MAX_EVENT_PARAMS];
183
184
FLUID_MEMSET(voice->rvoice, 0, sizeof(fluid_rvoice_t));
185
186
/* The 'sustain' and 'finished' segments of the volume / modulation
187
* envelope are constant. They are never affected by any modulator
188
* or generator. Therefore it is enough to initialize them once
189
* during the lifetime of the synth.
190
*/
191
fluid_voice_update_volenv(voice, FALSE, FLUID_VOICE_ENVSUSTAIN,
192
0xffffffff, 1.0f, 0.0f, -1.0f, 2.0f);
193
fluid_voice_update_volenv(voice, FALSE, FLUID_VOICE_ENVFINISHED,
194
0xffffffff, 0.0f, 0.0f, -1.0f, 1.0f);
195
fluid_voice_update_modenv(voice, FALSE, FLUID_VOICE_ENVSUSTAIN,
196
0xffffffff, 1.0f, 0.0f, -1.0f, 2.0f);
197
fluid_voice_update_modenv(voice, FALSE, FLUID_VOICE_ENVFINISHED,
198
0xffffffff, 0.0f, 0.0f, -1.0f, 1.0f);
199
200
param[0].i = FLUID_IIR_LOWPASS;
201
param[1].i = 0;
202
fluid_iir_filter_init(&voice->rvoice->resonant_filter, param);
203
204
param[0].i = FLUID_IIR_DISABLED;
205
fluid_iir_filter_init(&voice->rvoice->resonant_custom_filter, param);
206
207
param[0].real = output_rate;
208
fluid_rvoice_set_output_rate(voice->rvoice, param);
209
}
210
211
/*
212
* new_fluid_voice
213
*/
214
fluid_voice_t *
215
new_fluid_voice(fluid_rvoice_eventhandler_t *handler, fluid_real_t output_rate)
216
{
217
fluid_voice_t *voice;
218
voice = FLUID_NEW(fluid_voice_t);
219
220
if(voice == NULL)
221
{
222
FLUID_LOG(FLUID_ERR, "Out of memory");
223
return NULL;
224
}
225
226
voice->can_access_rvoice = TRUE;
227
voice->can_access_overflow_rvoice = TRUE;
228
229
voice->rvoice = FLUID_NEW(fluid_rvoice_t);
230
voice->overflow_rvoice = FLUID_NEW(fluid_rvoice_t);
231
232
if(voice->rvoice == NULL || voice->overflow_rvoice == NULL)
233
{
234
FLUID_LOG(FLUID_ERR, "Out of memory");
235
delete_fluid_voice(voice);
236
return NULL;
237
}
238
239
voice->status = FLUID_VOICE_CLEAN;
240
voice->chan = NO_CHANNEL;
241
voice->key = 0;
242
voice->vel = 0;
243
voice->eventhandler = handler;
244
voice->channel = NULL;
245
voice->sample = NULL;
246
voice->overflow_sample = NULL;
247
voice->output_rate = output_rate;
248
249
/* Initialize both the rvoice and overflow_rvoice */
250
fluid_voice_initialize_rvoice(voice, output_rate);
251
fluid_voice_swap_rvoice(voice);
252
fluid_voice_initialize_rvoice(voice, output_rate);
253
254
return voice;
255
}
256
257
/*
258
* delete_fluid_voice
259
*/
260
void
261
delete_fluid_voice(fluid_voice_t *voice)
262
{
263
fluid_return_if_fail(voice != NULL);
264
265
if(!voice->can_access_rvoice || !voice->can_access_overflow_rvoice)
266
{
267
FLUID_LOG(FLUID_WARN, "Deleting voice %u which has locked rvoices!", voice->id);
268
}
269
270
FLUID_FREE(voice->overflow_rvoice);
271
FLUID_FREE(voice->rvoice);
272
FLUID_FREE(voice);
273
}
274
275
/* fluid_voice_init
276
*
277
* Initialize the synthesis process
278
* inst_zone, the Instrument Zone contains the sample, Keyrange,Velrange
279
* of the voice.
280
* When playing legato (n1,n2) in mono mode, n2 will use n1 voices
281
* as far as n2 still enters in Keyrange,Velrange of n1.
282
*/
283
int
284
fluid_voice_init(fluid_voice_t *voice, fluid_sample_t *sample,
285
fluid_zone_range_t *inst_zone_range,
286
fluid_channel_t *channel, int key, int vel, unsigned int id,
287
unsigned int start_time, fluid_real_t gain)
288
{
289
/* Note: The voice parameters will be initialized later, when the
290
* generators have been retrieved from the sound font. Here, only
291
* the 'working memory' of the voice (position in envelopes, history
292
* of IIR filters, position in sample etc) is initialized. */
293
int i;
294
295
if(!voice->can_access_rvoice)
296
{
297
if(voice->can_access_overflow_rvoice)
298
{
299
fluid_voice_swap_rvoice(voice);
300
}
301
else
302
{
303
FLUID_LOG(FLUID_ERR, "Internal error: Cannot access an rvoice in fluid_voice_init!");
304
return FLUID_FAILED;
305
}
306
}
307
308
/* We are now guaranteed to have access to the rvoice */
309
310
if(voice->sample)
311
{
312
fluid_voice_off(voice);
313
}
314
315
voice->zone_range = inst_zone_range; /* Instrument zone range for legato */
316
voice->id = id;
317
voice->chan = fluid_channel_get_num(channel);
318
voice->key = (unsigned char) key;
319
voice->vel = (unsigned char) vel;
320
voice->channel = channel;
321
voice->mod_count = 0;
322
voice->start_time = start_time;
323
voice->has_noteoff = 0;
324
UPDATE_RVOICE0(fluid_rvoice_reset);
325
326
/*
327
We increment the reference count of the sample to indicate that this
328
sample is about to be owned by the rvoice. This will prevent the
329
unloading of the soundfont while this rvoice is playing.
330
*/
331
fluid_sample_incr_ref(sample);
332
fluid_rvoice_eventhandler_push_ptr(voice->eventhandler, fluid_rvoice_set_sample, voice->rvoice, sample);
333
voice->sample = sample;
334
335
i = fluid_channel_get_interp_method(channel);
336
UPDATE_RVOICE_I1(fluid_rvoice_set_interp_method, i);
337
338
/* Set all the generators to their default value, according to SF
339
* 2.01 section 8.1.3 (page 48). The value of NRPN messages are
340
* copied from the channel to the voice's generators. The sound font
341
* loader overwrites them. The generator values are later converted
342
* into voice parameters in
343
* fluid_voice_calculate_runtime_synthesis_parameters. */
344
fluid_gen_init(&voice->gen[0], channel);
345
UPDATE_RVOICE_I1(fluid_rvoice_set_samplemode, _SAMPLEMODE(voice));
346
347
voice->synth_gain = gain;
348
349
/* avoid division by zero later*/
350
if(voice->synth_gain < 0.0000001f)
351
{
352
voice->synth_gain = 0.0000001f;
353
}
354
355
UPDATE_RVOICE_R1(fluid_rvoice_set_synth_gain, voice->synth_gain);
356
357
/* Set up buffer mapping, should be done more flexible in the future. */
358
i = 2 * channel->synth->audio_groups;
359
i += (voice->chan % channel->synth->effects_groups) * channel->synth->effects_channels;
360
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 2, i + SYNTH_REVERB_CHANNEL);
361
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 3, i + SYNTH_CHORUS_CHANNEL);
362
363
i = 2 * (voice->chan % channel->synth->audio_groups);
364
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 0, i);
365
UPDATE_RVOICE_GENERIC_I2(fluid_rvoice_buffers_set_mapping, &voice->rvoice->buffers, 1, i + 1);
366
367
return FLUID_OK;
368
}
369
370
371
/**
372
* Update sample rate.
373
*
374
* @note If the voice is active, it will be turned off.
375
*/
376
void
377
fluid_voice_set_output_rate(fluid_voice_t *voice, fluid_real_t value)
378
{
379
if(fluid_voice_is_playing(voice))
380
{
381
fluid_voice_off(voice);
382
}
383
384
voice->output_rate = value;
385
UPDATE_RVOICE_GENERIC_R1(fluid_rvoice_set_output_rate, voice->rvoice, value);
386
UPDATE_RVOICE_GENERIC_R1(fluid_rvoice_set_output_rate, voice->overflow_rvoice, value);
387
}
388
389
390
/**
391
* Set the value of a generator.
392
*
393
* @param voice Voice instance
394
* @param i Generator ID (#fluid_gen_type)
395
* @param val Generator value
396
*/
397
void
398
fluid_voice_gen_set(fluid_voice_t *voice, int i, float val)
399
{
400
voice->gen[i].val = val;
401
voice->gen[i].flags = GEN_SET;
402
403
if(i == GEN_SAMPLEMODE)
404
{
405
UPDATE_RVOICE_I1(fluid_rvoice_set_samplemode, (int) val);
406
}
407
}
408
409
/**
410
* Offset the value of a generator.
411
*
412
* @param voice Voice instance
413
* @param i Generator ID (#fluid_gen_type)
414
* @param val Value to add to the existing value
415
*/
416
void
417
fluid_voice_gen_incr(fluid_voice_t *voice, int i, float val)
418
{
419
voice->gen[i].val += val;
420
voice->gen[i].flags = GEN_SET;
421
}
422
423
/**
424
* Get the value of a generator.
425
*
426
* @param voice Voice instance
427
* @param gen Generator ID (#fluid_gen_type)
428
* @return Current generator value
429
*/
430
float
431
fluid_voice_gen_get(fluid_voice_t *voice, int gen)
432
{
433
return voice->gen[gen].val;
434
}
435
436
fluid_real_t fluid_voice_gen_value(const fluid_voice_t *voice, int num)
437
{
438
return (fluid_real_t)(voice->gen[num].val + voice->gen[num].mod + voice->gen[num].nrpn);
439
}
440
441
/*
442
* fluid_voice_start
443
*/
444
void fluid_voice_start(fluid_voice_t *voice)
445
{
446
/* The maximum volume of the loop is calculated and cached once for each
447
* sample with its nominal loop settings. This happens, when the sample is used
448
* for the first time.*/
449
450
fluid_voice_calculate_runtime_synthesis_parameters(voice);
451
452
#ifdef WITH_PROFILING
453
voice->ref = fluid_profile_ref();
454
#endif
455
456
voice->status = FLUID_VOICE_ON;
457
458
/* Increment voice count */
459
voice->channel->synth->active_voice_count++;
460
}
461
462
/**
463
* Calculate the amplitude of a voice.
464
*
465
* @param gain The gain value in the range [0.0 ; 1.0]
466
* @return An amplitude used by rvoice_mixer's buffers
467
*/
468
static FLUID_INLINE fluid_real_t
469
fluid_voice_calculate_gain_amplitude(const fluid_voice_t *voice, fluid_real_t gain)
470
{
471
/* we use 24bit samples in fluid_rvoice_dsp. in order to normalize float
472
* samples to [0.0;1.0] divide samples by the max. value of an int24 and
473
* amplify them with the gain */
474
return gain * voice->synth_gain / (INT24_MAX * 1.0f);
475
}
476
477
/* Useful to return the nominal pitch of a key */
478
/* The nominal pitch is dependent of voice->root_pitch,tuning, and
479
GEN_SCALETUNE generator.
480
This is useful to set the value of GEN_PITCH generator on noteOn.
481
This is useful to get the beginning/ending pitch for portamento.
482
*/
483
fluid_real_t fluid_voice_calculate_pitch(fluid_voice_t *voice, int key)
484
{
485
fluid_tuning_t *tuning;
486
fluid_real_t x, pitch;
487
488
/* Now the nominal pitch of the key is returned.
489
* Note about SCALETUNE: SF2.01 8.1.3 says, that this generator is a
490
* non-realtime parameter. So we don't allow modulation (as opposed
491
* to fluid_voice_gen_value(voice, GEN_SCALETUNE) When the scale tuning is varied,
492
* one key remains fixed. Here C3 (MIDI number 60) is used.
493
*/
494
if(fluid_channel_has_tuning(voice->channel))
495
{
496
tuning = fluid_channel_get_tuning(voice->channel);
497
x = fluid_tuning_get_pitch(tuning, (int)(voice->root_pitch / 100.0f));
498
pitch = voice->gen[GEN_SCALETUNE].val / 100.0f *
499
(fluid_tuning_get_pitch(tuning, key) - x) + x;
500
}
501
else
502
{
503
pitch = voice->gen[GEN_SCALETUNE].val
504
* (key - voice->root_pitch / 100.0f) + voice->root_pitch;
505
}
506
507
return pitch;
508
}
509
510
void
511
fluid_voice_calculate_gen_pitch(fluid_voice_t *voice)
512
{
513
voice->gen[GEN_PITCH].val = fluid_voice_calculate_pitch(voice, fluid_voice_get_actual_key(voice));
514
}
515
516
/*
517
* fluid_voice_calculate_runtime_synthesis_parameters
518
*
519
* in this function we calculate the values of all the parameters. the
520
* parameters are converted to their most useful unit for the DSP
521
* algorithm, for example, number of samples instead of
522
* timecents. Some parameters keep their "perceptual" unit and
523
* conversion will be done in the DSP function. This is the case, for
524
* example, for the pitch since it is modulated by the controllers in
525
* cents. */
526
static int
527
fluid_voice_calculate_runtime_synthesis_parameters(fluid_voice_t *voice)
528
{
529
int i;
530
unsigned int n;
531
532
static int const list_of_generators_to_initialize[] =
533
{
534
GEN_STARTADDROFS, /* SF2.01 page 48 #0 */
535
GEN_ENDADDROFS, /* #1 */
536
GEN_STARTLOOPADDROFS, /* #2 */
537
GEN_ENDLOOPADDROFS, /* #3 */
538
/* GEN_STARTADDRCOARSEOFS see comment below [1] #4 */
539
GEN_MODLFOTOPITCH, /* #5 */
540
GEN_VIBLFOTOPITCH, /* #6 */
541
GEN_MODENVTOPITCH, /* #7 */
542
GEN_FILTERFC, /* #8 */
543
GEN_FILTERQ, /* #9 */
544
GEN_MODLFOTOFILTERFC, /* #10 */
545
GEN_MODENVTOFILTERFC, /* #11 */
546
/* GEN_ENDADDRCOARSEOFS [1] #12 */
547
GEN_MODLFOTOVOL, /* #13 */
548
/* not defined #14 */
549
GEN_CHORUSSEND, /* #15 */
550
GEN_REVERBSEND, /* #16 */
551
GEN_PAN, /* #17 */
552
/* not defined #18 */
553
/* not defined #19 */
554
/* not defined #20 */
555
GEN_MODLFODELAY, /* #21 */
556
GEN_MODLFOFREQ, /* #22 */
557
GEN_VIBLFODELAY, /* #23 */
558
GEN_VIBLFOFREQ, /* #24 */
559
GEN_MODENVDELAY, /* #25 */
560
GEN_MODENVATTACK, /* #26 */
561
GEN_MODENVHOLD, /* #27 */
562
GEN_MODENVDECAY, /* #28 */
563
/* GEN_MODENVSUSTAIN [1] #29 */
564
GEN_MODENVRELEASE, /* #30 */
565
/* GEN_KEYTOMODENVHOLD [1] #31 */
566
/* GEN_KEYTOMODENVDECAY [1] #32 */
567
GEN_VOLENVDELAY, /* #33 */
568
GEN_VOLENVATTACK, /* #34 */
569
GEN_VOLENVHOLD, /* #35 */
570
GEN_VOLENVDECAY, /* #36 */
571
/* GEN_VOLENVSUSTAIN [1] #37 */
572
GEN_VOLENVRELEASE, /* #38 */
573
/* GEN_KEYTOVOLENVHOLD [1] #39 */
574
/* GEN_KEYTOVOLENVDECAY [1] #40 */
575
/* GEN_STARTLOOPADDRCOARSEOFS [1] #45 */
576
GEN_KEYNUM, /* #46 */
577
GEN_VELOCITY, /* #47 */
578
GEN_ATTENUATION, /* #48 */
579
/* GEN_ENDLOOPADDRCOARSEOFS [1] #50 */
580
/* GEN_COARSETUNE [1] #51 */
581
/* GEN_FINETUNE [1] #52 */
582
GEN_OVERRIDEROOTKEY, /* #58 */
583
GEN_PITCH, /* --- */
584
GEN_CUSTOM_BALANCE, /* --- */
585
GEN_CUSTOM_FILTERFC, /* --- */
586
GEN_CUSTOM_FILTERQ /* --- */
587
};
588
589
/* When the voice is made ready for the synthesis process, a lot of
590
* voice-internal parameters have to be calculated.
591
*
592
* At this point, the sound font has already set the -nominal- value
593
* for all generators (excluding GEN_PITCH). Most generators can be
594
* modulated - they include a nominal value and an offset (which
595
* changes with velocity, note number, channel parameters like
596
* aftertouch, mod wheel...) Now this offset will be calculated as
597
* follows:
598
*
599
* - Process each modulator once.
600
* - Calculate its output value.
601
* - Find the target generator.
602
* - Add the output value to the modulation value of the generator.
603
*
604
* Note: The generators have been initialized with
605
* fluid_gen_init().
606
*/
607
608
for(i = 0; i < voice->mod_count; i++)
609
{
610
fluid_mod_t *mod = &voice->mod[i];
611
fluid_real_t modval = fluid_mod_get_value(mod, voice);
612
int dest_gen_index = mod->dest;
613
fluid_gen_t *dest_gen = &voice->gen[dest_gen_index];
614
dest_gen->mod += modval;
615
/* fluid_dump_modulator(mod); */
616
}
617
618
/* Now the generators are initialized, nominal and modulation value.
619
* The voice parameters (which depend on generators) are calculated
620
* with fluid_voice_update_param. Processing the list of generator
621
* changes will calculate each voice parameter once.
622
*
623
* Note [1]: Some voice parameters depend on several generators. For
624
* example, the pitch depends on GEN_COARSETUNE, GEN_FINETUNE and
625
* GEN_PITCH. voice->pitch. Unnecessary recalculation is avoided
626
* by removing all but one generator from the list of voice
627
* parameters. Same with GEN_XXX and GEN_XXXCOARSE: the
628
* initialisation list contains only GEN_XXX.
629
*/
630
631
/* Calculate the voice parameter(s) dependent on each generator. */
632
for(n = 0; n < FLUID_N_ELEMENTS(list_of_generators_to_initialize); n++)
633
{
634
fluid_voice_update_param(voice, list_of_generators_to_initialize[n]);
635
}
636
637
/* Start portamento if enabled */
638
{
639
/* fromkey note comes from "GetFromKeyPortamentoLegato()" detector.
640
When fromkey is set to ValidNote , portamento is started */
641
/* Return fromkey portamento */
642
int fromkey = voice->channel->synth->fromkey_portamento;
643
644
if(fluid_channel_is_valid_note(fromkey))
645
{
646
/* Send portamento parameters to the voice dsp */
647
fluid_voice_update_portamento(voice, fromkey, fluid_voice_get_actual_key(voice));
648
}
649
}
650
651
/* Make an estimate on how loud this voice can get at any time (attenuation). */
652
UPDATE_RVOICE_R1(fluid_rvoice_set_min_attenuation_cB,
653
fluid_voice_get_lower_boundary_for_attenuation(voice));
654
return FLUID_OK;
655
}
656
657
/*
658
* calculate_hold_decay_buffers
659
*/
660
static int
661
calculate_hold_decay_buffers(fluid_voice_t *voice, int gen_base,
662
int gen_key2base, int is_decay)
663
{
664
/* Purpose:
665
*
666
* Returns the number of DSP loops, that correspond to the hold
667
* (is_decay=0) or decay (is_decay=1) time.
668
* gen_base=GEN_VOLENVHOLD, GEN_VOLENVDECAY, GEN_MODENVHOLD,
669
* GEN_MODENVDECAY gen_key2base=GEN_KEYTOVOLENVHOLD,
670
* GEN_KEYTOVOLENVDECAY, GEN_KEYTOMODENVHOLD, GEN_KEYTOMODENVDECAY
671
*/
672
673
fluid_real_t keysteps;
674
fluid_real_t timecents;
675
fluid_real_t seconds;
676
int buffers;
677
678
/* SF2.01 section 8.4.3 # 31, 32, 39, 40
679
* GEN_KEYTOxxxENVxxx uses key 60 as 'origin'.
680
* The unit of the generator is timecents per key number.
681
* If KEYTOxxxENVxxx is 100, a key one octave over key 60 (72)
682
* will cause (60-72)*100=-1200 timecents of time variation.
683
* The time is cut in half.
684
*/
685
686
keysteps = 60.0f - fluid_channel_get_key_pitch(voice->channel, fluid_voice_get_actual_key(voice)) / 100.0f;
687
timecents = fluid_voice_gen_value(voice, gen_base) + fluid_voice_gen_value(voice, gen_key2base) * keysteps;
688
689
/* Range checking */
690
if(is_decay)
691
{
692
/* SF 2.01 section 8.1.3 # 28, 36 */
693
if(timecents > 8000.f)
694
{
695
timecents = 8000.f;
696
}
697
}
698
else
699
{
700
/* SF 2.01 section 8.1.3 # 27, 35 */
701
if(timecents > 5000.f)
702
{
703
timecents = 5000.f;
704
}
705
706
/* SF 2.01 section 8.1.2 # 27, 35:
707
* The most negative number indicates no hold time
708
*/
709
if(timecents <= -32768.f)
710
{
711
return 0;
712
}
713
}
714
715
/* SF 2.01 section 8.1.3 # 27, 28, 35, 36 */
716
if(timecents < -12000.f)
717
{
718
timecents = -12000.f;
719
}
720
721
seconds = fluid_tc2sec(timecents);
722
/* Each DSP loop processes FLUID_BUFSIZE samples. */
723
724
/* round to next full number of buffers */
725
buffers = (int)(((fluid_real_t)voice->output_rate * seconds)
726
/ (fluid_real_t)FLUID_BUFSIZE
727
+ 0.5f);
728
729
return buffers;
730
}
731
732
/*
733
* The value of a generator (gen) has changed. (The different
734
* generators are listed in fluidsynth.h, or in SF2.01 page 48-49)
735
* Now the dependent 'voice' parameters are calculated.
736
*
737
* fluid_voice_update_param can be called during the setup of the
738
* voice (to calculate the initial value for a voice parameter), or
739
* during its operation (a generator has been changed due to
740
* real-time parameter modifications like pitch-bend).
741
*
742
* Note: The generator holds three values: The base value .val, an
743
* offset caused by modulators .mod, and an offset caused by the
744
* NRPN system. fluid_voice_gen_value(voice, generator_enumerator) returns the sum
745
* of all three.
746
*/
747
748
/**
749
* Update all the synthesis parameters which depend on generator \a gen.
750
*
751
* @param voice Voice instance
752
* @param gen Generator id (#fluid_gen_type)
753
*
754
* Calling this function is only necessary after changing a generator of an already playing voice.
755
*/
756
void
757
fluid_voice_update_param(fluid_voice_t *voice, int gen)
758
{
759
unsigned int count, z;
760
fluid_real_t x = fluid_voice_gen_value(voice, gen);
761
762
switch(gen)
763
{
764
765
case GEN_PAN:
766
case GEN_CUSTOM_BALANCE:
767
/* range checking is done in the fluid_pan and fluid_balance functions */
768
voice->pan = fluid_voice_gen_value(voice, GEN_PAN);
769
voice->balance = fluid_voice_gen_value(voice, GEN_CUSTOM_BALANCE);
770
771
/* left amp */
772
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 0,
773
fluid_voice_calculate_gain_amplitude(voice,
774
fluid_pan(voice->pan, 1) * fluid_balance(voice->balance, 1)));
775
776
/* right amp */
777
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 1,
778
fluid_voice_calculate_gain_amplitude(voice,
779
fluid_pan(voice->pan, 0) * fluid_balance(voice->balance, 0)));
780
break;
781
782
case GEN_ATTENUATION:
783
voice->attenuation = x;
784
785
/* Range: SF2.01 section 8.1.3 # 48
786
* Motivation for range checking:
787
* OHPiano.SF2 sets initial attenuation to a whooping -96 dB */
788
fluid_clip(voice->attenuation, 0.f, 1440.f);
789
UPDATE_RVOICE_R1(fluid_rvoice_set_attenuation, voice->attenuation);
790
break;
791
792
/* The pitch is calculated from three different generators.
793
* Read comment in fluidsynth.h about GEN_PITCH.
794
*/
795
case GEN_PITCH:
796
case GEN_COARSETUNE:
797
case GEN_FINETUNE:
798
/* The testing for allowed range is done in 'fluid_ct2hz' */
799
voice->pitch = (fluid_voice_gen_value(voice, GEN_PITCH)
800
+ 100.0f * fluid_voice_gen_value(voice, GEN_COARSETUNE)
801
+ fluid_voice_gen_value(voice, GEN_FINETUNE));
802
UPDATE_RVOICE_R1(fluid_rvoice_set_pitch, voice->pitch);
803
break;
804
805
case GEN_REVERBSEND:
806
/* The generator unit is 'tenths of a percent'. */
807
voice->reverb_send = x / 1000.0f;
808
fluid_clip(voice->reverb_send, 0.f, 1.f);
809
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 2, fluid_voice_calculate_gain_amplitude(voice, voice->reverb_send));
810
break;
811
812
case GEN_CHORUSSEND:
813
/* The generator unit is 'tenths of a percent'. */
814
voice->chorus_send = x / 1000.0f;
815
fluid_clip(voice->chorus_send, 0.f, 1.f);
816
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 3, fluid_voice_calculate_gain_amplitude(voice, voice->chorus_send));
817
break;
818
819
case GEN_OVERRIDEROOTKEY:
820
821
/* This is a non-realtime parameter. Therefore the .mod part of the generator
822
* can be neglected.
823
* NOTE: origpitch sets MIDI root note while pitchadj is a fine tuning amount
824
* which offsets the original rate. This means that the fine tuning is
825
* inverted with respect to the root note (so subtract it, not add).
826
*/
827
if(voice->sample != NULL)
828
{
829
if(voice->gen[GEN_OVERRIDEROOTKEY].val > -1) //FIXME: use flag instead of -1
830
{
831
voice->root_pitch = voice->gen[GEN_OVERRIDEROOTKEY].val * 100.0f
832
- voice->sample->pitchadj;
833
}
834
else
835
{
836
voice->root_pitch = voice->sample->origpitch * 100.0f - voice->sample->pitchadj;
837
}
838
839
x = (fluid_ct2hz_real(voice->root_pitch) * ((fluid_real_t) voice->output_rate / voice->sample->samplerate));
840
}
841
else
842
{
843
if(voice->gen[GEN_OVERRIDEROOTKEY].val > -1) //FIXME: use flag instead of -1
844
{
845
voice->root_pitch = voice->gen[GEN_OVERRIDEROOTKEY].val * 100.0f;
846
}
847
else
848
{
849
voice->root_pitch = 0;
850
}
851
852
x = fluid_ct2hz_real(voice->root_pitch);
853
}
854
855
/* voice->pitch depends on voice->root_pitch, so calculate voice->pitch now */
856
fluid_voice_calculate_gen_pitch(voice);
857
UPDATE_RVOICE_R1(fluid_rvoice_set_root_pitch_hz, x);
858
859
break;
860
861
case GEN_FILTERFC:
862
/* The resonance frequency is converted from absolute cents to
863
* midicents .val and .mod are both used, this permits real-time
864
* modulation. The allowed range is tested in the 'fluid_ct2hz'
865
* function [PH,20021214]
866
*/
867
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_fres, &voice->rvoice->resonant_filter, x);
868
break;
869
870
case GEN_FILTERQ:
871
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_q, &voice->rvoice->resonant_filter, x);
872
break;
873
874
/* same as the two above, only for the custom filter */
875
case GEN_CUSTOM_FILTERFC:
876
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_fres, &voice->rvoice->resonant_custom_filter, x);
877
break;
878
879
case GEN_CUSTOM_FILTERQ:
880
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_q, &voice->rvoice->resonant_custom_filter, x);
881
break;
882
883
case GEN_MODLFOTOPITCH:
884
fluid_clip(x, -12000.f, 12000.f);
885
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_pitch, x);
886
break;
887
888
case GEN_MODLFOTOVOL:
889
fluid_clip(x, -960.f, 960.f);
890
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_vol, x);
891
break;
892
893
case GEN_MODLFOTOFILTERFC:
894
fluid_clip(x, -12000.f, 12000.f);
895
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_fc, x);
896
break;
897
898
case GEN_MODLFODELAY:
899
fluid_clip(x, -12000.0f, 5000.0f);
900
z = (unsigned int)(voice->output_rate * fluid_tc2sec_delay(x));
901
UPDATE_RVOICE_ENVLFO_I1(fluid_lfo_set_delay, modlfo, z);
902
break;
903
904
case GEN_MODLFOFREQ:
905
/* - the frequency is converted into a delta value, per buffer of FLUID_BUFSIZE samples
906
* - the delay into a sample delay
907
*/
908
fluid_clip(x, -16000.0f, 4500.0f);
909
x = (4.0f * FLUID_BUFSIZE * fluid_ct2hz_real(x) / voice->output_rate);
910
UPDATE_RVOICE_ENVLFO_R1(fluid_lfo_set_incr, modlfo, x);
911
break;
912
913
case GEN_VIBLFOFREQ:
914
/* vib lfo
915
*
916
* - the frequency is converted into a delta value, per buffer of FLUID_BUFSIZE samples
917
* - the delay into a sample delay
918
*/
919
fluid_clip(x, -16000.0f, 4500.0f);
920
x = 4.0f * FLUID_BUFSIZE * fluid_ct2hz_real(x) / voice->output_rate;
921
UPDATE_RVOICE_ENVLFO_R1(fluid_lfo_set_incr, viblfo, x);
922
break;
923
924
case GEN_VIBLFODELAY:
925
fluid_clip(x, -12000.0f, 5000.0f);
926
z = (unsigned int)(voice->output_rate * fluid_tc2sec_delay(x));
927
UPDATE_RVOICE_ENVLFO_I1(fluid_lfo_set_delay, viblfo, z);
928
break;
929
930
case GEN_VIBLFOTOPITCH:
931
fluid_clip(x, -12000.f, 12000.f);
932
UPDATE_RVOICE_R1(fluid_rvoice_set_viblfo_to_pitch, x);
933
break;
934
935
case GEN_KEYNUM:
936
/* GEN_KEYNUM: SF2.01 page 46, item 46
937
*
938
* If this generator is active, it forces the key number to its
939
* value. Non-realtime controller.
940
*
941
* There is a flag, which should indicate, whether a generator is
942
* enabled or not. But here we rely on the default value of -1.
943
*/
944
945
/* 2017-09-02: do not change the voice's key here, otherwise it will
946
* never be released on a noteoff event
947
*/
948
#if 0
949
x = fluid_voice_gen_value(voice, GEN_KEYNUM);
950
951
if(x >= 0)
952
{
953
voice->key = x;
954
}
955
956
#endif
957
break;
958
959
case GEN_VELOCITY:
960
/* GEN_VELOCITY: SF2.01 page 46, item 47
961
*
962
* If this generator is active, it forces the velocity to its
963
* value. Non-realtime controller.
964
*
965
* There is a flag, which should indicate, whether a generator is
966
* enabled or not. But here we rely on the default value of -1.
967
*/
968
/* 2017-09-02: do not change the voice's velocity here, use
969
* fluid_voice_get_actual_velocity() to get the value of this generator
970
* if active.
971
*/
972
#if 0
973
x = fluid_voice_gen_value(voice, GEN_VELOCITY);
974
975
if(x > 0)
976
{
977
voice->vel = x;
978
}
979
980
#endif
981
break;
982
983
case GEN_MODENVTOPITCH:
984
fluid_clip(x, -12000.f, 12000.f);
985
UPDATE_RVOICE_R1(fluid_rvoice_set_modenv_to_pitch, x);
986
break;
987
988
case GEN_MODENVTOFILTERFC:
989
/* Range: SF2.01 section 8.1.3 # 1
990
* Motivation for range checking:
991
* Filter is reported to make funny noises now and then
992
*/
993
fluid_clip(x, -12000.f, 12000.f);
994
UPDATE_RVOICE_R1(fluid_rvoice_set_modenv_to_fc, x);
995
break;
996
997
998
/* sample start and ends points
999
*
1000
* Range checking is initiated via the
1001
* voice->check_sample_sanity flag,
1002
* because it is impossible to check here:
1003
* During the voice setup, all modulators are processed, while
1004
* the voice is inactive. Therefore, illegal settings may
1005
* occur during the setup (for example: First move the loop
1006
* end point ahead of the loop start point => invalid, then
1007
* move the loop start point forward => valid again.
1008
*/
1009
case GEN_STARTADDROFS: /* SF2.01 section 8.1.3 # 0 */
1010
case GEN_STARTADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 4 */
1011
if(voice->sample != NULL)
1012
{
1013
fluid_real_t start_fine = fluid_voice_gen_value(voice, GEN_STARTADDROFS);
1014
fluid_real_t start_coar = fluid_voice_gen_value(voice, GEN_STARTADDRCOARSEOFS);
1015
1016
z = voice->sample->start + (int)start_fine + 32768 * (int)start_coar;
1017
UPDATE_RVOICE_I1(fluid_rvoice_set_start, z);
1018
}
1019
1020
break;
1021
1022
case GEN_ENDADDROFS: /* SF2.01 section 8.1.3 # 1 */
1023
case GEN_ENDADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 12 */
1024
if(voice->sample != NULL)
1025
{
1026
fluid_real_t end_fine = fluid_voice_gen_value(voice, GEN_ENDADDROFS);
1027
fluid_real_t end_coar = fluid_voice_gen_value(voice, GEN_ENDADDRCOARSEOFS);
1028
1029
z = voice->sample->end + (int)end_fine + 32768 * (int)end_coar;
1030
UPDATE_RVOICE_I1(fluid_rvoice_set_end, z);
1031
}
1032
1033
break;
1034
1035
case GEN_STARTLOOPADDROFS: /* SF2.01 section 8.1.3 # 2 */
1036
case GEN_STARTLOOPADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 45 */
1037
if(voice->sample != NULL)
1038
{
1039
fluid_real_t lstart_fine = fluid_voice_gen_value(voice, GEN_STARTLOOPADDROFS);
1040
fluid_real_t lstart_coar = fluid_voice_gen_value(voice, GEN_STARTLOOPADDRCOARSEOFS);
1041
1042
z = voice->sample->loopstart + (int)lstart_fine + 32768 * (int)lstart_coar;
1043
UPDATE_RVOICE_I1(fluid_rvoice_set_loopstart, z);
1044
}
1045
1046
break;
1047
1048
case GEN_ENDLOOPADDROFS: /* SF2.01 section 8.1.3 # 3 */
1049
case GEN_ENDLOOPADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 50 */
1050
if(voice->sample != NULL)
1051
{
1052
fluid_real_t lend_fine = fluid_voice_gen_value(voice, GEN_ENDLOOPADDROFS);
1053
fluid_real_t lend_coar = fluid_voice_gen_value(voice, GEN_ENDLOOPADDRCOARSEOFS);
1054
1055
z = voice->sample->loopend + (int)lend_fine + 32768 * (int)lend_coar;
1056
UPDATE_RVOICE_I1(fluid_rvoice_set_loopend, z);
1057
}
1058
1059
break;
1060
1061
/* Conversion functions differ in range limit */
1062
#define NUM_BUFFERS_DELAY(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_delay(_v) / FLUID_BUFSIZE)
1063
#define NUM_BUFFERS_ATTACK(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_attack(_v) / FLUID_BUFSIZE)
1064
#define NUM_BUFFERS_RELEASE(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_release(_v) / FLUID_BUFSIZE)
1065
1066
/* volume envelope
1067
*
1068
* - delay and hold times are converted to absolute number of samples
1069
* - sustain is converted to its absolute value
1070
* - attack, decay and release are converted to their increment per sample
1071
*/
1072
case GEN_VOLENVDELAY: /* SF2.01 section 8.1.3 # 33 */
1073
fluid_clip(x, -12000.0f, 5000.0f);
1074
count = NUM_BUFFERS_DELAY(x);
1075
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVDELAY,
1076
count, 0.0f, 0.0f, -1.0f, 1.0f);
1077
break;
1078
1079
case GEN_VOLENVATTACK: /* SF2.01 section 8.1.3 # 34 */
1080
fluid_clip(x, -12000.0f, 8000.0f);
1081
count = 1 + NUM_BUFFERS_ATTACK(x);
1082
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVATTACK,
1083
count, 1.0f, 1.0f / count, -1.0f, 1.0f);
1084
break;
1085
1086
case GEN_VOLENVHOLD: /* SF2.01 section 8.1.3 # 35 */
1087
case GEN_KEYTOVOLENVHOLD: /* SF2.01 section 8.1.3 # 39 */
1088
count = calculate_hold_decay_buffers(voice, GEN_VOLENVHOLD, GEN_KEYTOVOLENVHOLD, 0); /* 0 means: hold */
1089
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVHOLD,
1090
count, 1.0f, 0.0f, -1.0f, 2.0f);
1091
break;
1092
1093
case GEN_VOLENVDECAY: /* SF2.01 section 8.1.3 # 36 */
1094
case GEN_VOLENVSUSTAIN: /* SF2.01 section 8.1.3 # 37 */
1095
case GEN_KEYTOVOLENVDECAY: /* SF2.01 section 8.1.3 # 40 */
1096
x = 1.0f - 0.001f * fluid_voice_gen_value(voice, GEN_VOLENVSUSTAIN);
1097
fluid_clip(x, 0.0f, 1.0f);
1098
count = calculate_hold_decay_buffers(voice, GEN_VOLENVDECAY, GEN_KEYTOVOLENVDECAY, 1); /* 1 for decay */
1099
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVDECAY,
1100
count, 1.0f, count ? -1.0f / count : 0.0f, x, 2.0f);
1101
break;
1102
1103
case GEN_VOLENVRELEASE: /* SF2.01 section 8.1.3 # 38 */
1104
fluid_clip(x, FLUID_MIN_VOLENVRELEASE, 8000.0f);
1105
count = 1 + NUM_BUFFERS_RELEASE(x);
1106
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVRELEASE,
1107
count, 1.0f, -1.0f / count, 0.0f, 1.0f);
1108
break;
1109
1110
/* Modulation envelope */
1111
case GEN_MODENVDELAY: /* SF2.01 section 8.1.3 # 25 */
1112
fluid_clip(x, -12000.0f, 5000.0f);
1113
count = NUM_BUFFERS_DELAY(x);
1114
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVDELAY,
1115
count, 0.0f, 0.0f, -1.0f, 1.0f);
1116
break;
1117
1118
case GEN_MODENVATTACK: /* SF2.01 section 8.1.3 # 26 */
1119
fluid_clip(x, -12000.0f, 8000.0f);
1120
count = 1 + NUM_BUFFERS_ATTACK(x);
1121
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVATTACK,
1122
count, 1.0f, 1.0f / count, -1.0f, 1.0f);
1123
break;
1124
1125
case GEN_MODENVHOLD: /* SF2.01 section 8.1.3 # 27 */
1126
case GEN_KEYTOMODENVHOLD: /* SF2.01 section 8.1.3 # 31 */
1127
count = calculate_hold_decay_buffers(voice, GEN_MODENVHOLD, GEN_KEYTOMODENVHOLD, 0); /* 1 means: hold */
1128
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVHOLD,
1129
count, 1.0f, 0.0f, -1.0f, 2.0f);
1130
break;
1131
1132
case GEN_MODENVDECAY: /* SF 2.01 section 8.1.3 # 28 */
1133
case GEN_MODENVSUSTAIN: /* SF 2.01 section 8.1.3 # 29 */
1134
case GEN_KEYTOMODENVDECAY: /* SF 2.01 section 8.1.3 # 32 */
1135
count = calculate_hold_decay_buffers(voice, GEN_MODENVDECAY, GEN_KEYTOMODENVDECAY, 1); /* 1 for decay */
1136
x = 1.0f - 0.001f * fluid_voice_gen_value(voice, GEN_MODENVSUSTAIN);
1137
fluid_clip(x, 0.0f, 1.0f);
1138
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVDECAY,
1139
count, 1.0f, count ? -1.0f / count : 0.0f, x, 2.0f);
1140
break;
1141
1142
case GEN_MODENVRELEASE: /* SF 2.01 section 8.1.3 # 30 */
1143
fluid_clip(x, -12000.0f, 8000.0f);
1144
count = 1 + NUM_BUFFERS_RELEASE(x);
1145
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVRELEASE,
1146
count, 1.0f, -1.0f / count, 0.0f, 2.0f);
1147
1148
break;
1149
1150
} /* switch gen */
1151
}
1152
1153
/**
1154
* Recalculate voice parameters for a given control.
1155
*
1156
* @param voice the synthesis voice
1157
* @param cc flag to distinguish between a continuous control and a channel control (pitch bend, ...)
1158
* @param ctrl the control number:
1159
* when >=0, only modulators's destination having ctrl as source are updated.
1160
* when -1, all modulators's destination are updated (regardless of ctrl).
1161
*
1162
* In this implementation, I want to make sure that all controllers
1163
* are event based: the parameter values of the DSP algorithm should
1164
* only be updates when a controller event arrived and not at every
1165
* iteration of the audio cycle (which would probably be feasible if
1166
* the synth was made in silicon).
1167
*
1168
* The update is done in two steps:
1169
*
1170
* - step 1: first, we look for all the modulators that have the changed
1171
* controller as a source. This will yield a generator that will be changed
1172
* because of the controller event.
1173
*
1174
* - step 2: For this generator, calculate its new value. This is the
1175
* sum of its original value plus the values of all the attached modulators.
1176
* The generator flag is set to indicate the parameters must be updated.
1177
* This avoid the risk to call 'fluid_voice_update_param' several
1178
* times for the same generator if several modulators have that generator as
1179
* destination. So every changed generators are updated only once.
1180
*/
1181
1182
/* bit table for each generator being updated. The bits are packed in variables
1183
Each variable have NBR_BIT_BY_VAR bits represented by NBR_BIT_BY_VAR_LN2.
1184
The size of the table is the number of variables: SIZE_UPDATED_GEN_BIT.
1185
1186
Note: In this implementation NBR_BIT_BY_VAR_LN2 is set to 5 (convenient for 32 bits cpu)
1187
but this could be set to 6 for 64 bits cpu.
1188
*/
1189
1190
#define NBR_BIT_BY_VAR_LN2 5 /* for 32 bits variables */
1191
#define NBR_BIT_BY_VAR (1 << NBR_BIT_BY_VAR_LN2)
1192
#define NBR_BIT_BY_VAR_ANDMASK (NBR_BIT_BY_VAR - 1)
1193
#define SIZE_UPDATED_GEN_BIT ((GEN_LAST + NBR_BIT_BY_VAR_ANDMASK) / NBR_BIT_BY_VAR)
1194
1195
#define is_gen_updated(bit,gen) (bit[gen >> NBR_BIT_BY_VAR_LN2] & (1 << (gen & NBR_BIT_BY_VAR_ANDMASK)))
1196
#define set_gen_updated(bit,gen) (bit[gen >> NBR_BIT_BY_VAR_LN2] |= (1 << (gen & NBR_BIT_BY_VAR_ANDMASK)))
1197
1198
int fluid_voice_modulate(fluid_voice_t *voice, int cc, int ctrl)
1199
{
1200
int i, k;
1201
fluid_mod_t *mod;
1202
uint32_t gen;
1203
fluid_real_t modval;
1204
1205
/* Clears registered bits table of updated generators */
1206
uint32_t updated_gen_bit[SIZE_UPDATED_GEN_BIT] = {0};
1207
1208
/* printf("Chan=%d, CC=%d, Src=%d, Val=%d\n", voice->channel->channum, cc, ctrl, val); */
1209
1210
for(i = 0; i < voice->mod_count; i++)
1211
{
1212
mod = &voice->mod[i];
1213
1214
/* step 1: find all the modulators that have the changed controller
1215
as input source. When ctrl is -1 all modulators destination
1216
are updated */
1217
if(ctrl < 0 || fluid_mod_has_source(mod, cc, ctrl))
1218
{
1219
gen = fluid_mod_get_dest(mod);
1220
1221
/* Skip if this generator has already been updated */
1222
if(!is_gen_updated(updated_gen_bit, gen))
1223
{
1224
modval = 0.0;
1225
1226
/* step 2: for every attached modulator, calculate the modulation
1227
* value for the generator gen */
1228
for(k = 0; k < voice->mod_count; k++)
1229
{
1230
if(fluid_mod_has_dest(&voice->mod[k], gen))
1231
{
1232
modval += fluid_mod_get_value(&voice->mod[k], voice);
1233
}
1234
}
1235
1236
fluid_gen_set_mod(&voice->gen[gen], modval);
1237
1238
/* now recalculate the parameter values that are derived from the
1239
generator */
1240
fluid_voice_update_param(voice, gen);
1241
1242
/* set the bit that indicates this generator is updated */
1243
set_gen_updated(updated_gen_bit, gen);
1244
}
1245
}
1246
}
1247
1248
return FLUID_OK;
1249
}
1250
1251
/**
1252
* Update all the modulators.
1253
*
1254
* This function is called after a ALL_CTRL_OFF MIDI message has been received (CC 121).
1255
* All destinations of all modulators will be updated.
1256
*/
1257
int fluid_voice_modulate_all(fluid_voice_t *voice)
1258
{
1259
return fluid_voice_modulate(voice, 0, -1);
1260
}
1261
1262
/* legato update functions --------------------------------------------------*/
1263
1264
/* Updates voice portamento parameters
1265
*
1266
* @voice voice the synthesis voice
1267
* @fromkey the beginning pitch of portamento.
1268
* @tokey the ending pitch of portamento.
1269
*
1270
* The function calculates pitch offset and increment, then these parameters
1271
* are send to the dsp.
1272
*/
1273
void fluid_voice_update_portamento(fluid_voice_t *voice, int fromkey, int tokey)
1274
1275
{
1276
fluid_channel_t *channel = voice->channel;
1277
1278
/* calculates pitch offset */
1279
fluid_real_t PitchBeg = fluid_voice_calculate_pitch(voice, fromkey);
1280
fluid_real_t PitchEnd = fluid_voice_calculate_pitch(voice, tokey);
1281
fluid_real_t pitchoffset = PitchBeg - PitchEnd;
1282
1283
/* Calculates increment countinc */
1284
/* Increment is function of PortamentoTime (ms)*/
1285
unsigned int countinc = (unsigned int)(((fluid_real_t)voice->output_rate *
1286
0.001f *
1287
(fluid_real_t)fluid_channel_portamentotime(channel)) /
1288
(fluid_real_t)FLUID_BUFSIZE + 0.5f);
1289
1290
/* Send portamento parameters to the voice dsp */
1291
UPDATE_RVOICE_GENERIC_IR(fluid_rvoice_set_portamento, voice->rvoice, countinc, pitchoffset);
1292
}
1293
1294
/*---------------------------------------------------------------*/
1295
/*legato mode 1: multi_retrigger
1296
*
1297
* Modulates all generators dependent of key,vel.
1298
* Forces the voice envelopes in the attack section (legato mode 1).
1299
*
1300
* @voice voice the synthesis voice
1301
* @tokey the new key to be applied to this voice.
1302
* @vel the new velocity to be applied to this voice.
1303
*/
1304
void fluid_voice_update_multi_retrigger_attack(fluid_voice_t *voice,
1305
int tokey, int vel)
1306
{
1307
voice->key = tokey; /* new note */
1308
voice->vel = vel; /* new velocity */
1309
/* Updates generators dependent of velocity */
1310
/* Modulates GEN_ATTENUATION (and others ) before calling
1311
fluid_rvoice_multi_retrigger_attack().*/
1312
fluid_voice_modulate(voice, FALSE, FLUID_MOD_VELOCITY);
1313
1314
/* Updates generator dependent of voice->key */
1315
fluid_voice_update_param(voice, GEN_KEYTOMODENVHOLD);
1316
fluid_voice_update_param(voice, GEN_KEYTOMODENVDECAY);
1317
fluid_voice_update_param(voice, GEN_KEYTOVOLENVHOLD);
1318
fluid_voice_update_param(voice, GEN_KEYTOVOLENVDECAY);
1319
1320
/* Updates pitch generator */
1321
fluid_voice_calculate_gen_pitch(voice);
1322
fluid_voice_update_param(voice, GEN_PITCH);
1323
1324
/* updates adsr generator */
1325
UPDATE_RVOICE0(fluid_rvoice_multi_retrigger_attack);
1326
}
1327
/** end of legato update functions */
1328
1329
/*
1330
Force the voice into release stage. Useful anywhere a voice
1331
needs to be damped even if pedals (sustain sostenuto) are depressed.
1332
See fluid_synth_damp_voices_by_sustain_LOCAL(),
1333
fluid_synth_damp_voices_by_sostenuto_LOCAL,
1334
fluid_voice_noteoff().
1335
*/
1336
void
1337
fluid_voice_release(fluid_voice_t *voice)
1338
{
1339
unsigned int at_tick = fluid_channel_get_min_note_length_ticks(voice->channel);
1340
UPDATE_RVOICE_I1(fluid_rvoice_noteoff, at_tick);
1341
voice->has_noteoff = 1; // voice is marked as noteoff occurred
1342
}
1343
1344
/*
1345
* fluid_voice_noteoff
1346
*
1347
* Sending a noteoff event will advance the envelopes to section 5 (release).
1348
* The function is convenient for polyphonic or monophonic note
1349
*/
1350
void
1351
fluid_voice_noteoff(fluid_voice_t *voice)
1352
{
1353
fluid_channel_t *channel;
1354
1355
fluid_profile(FLUID_PROF_VOICE_NOTE, voice->ref, 0, 0);
1356
1357
channel = voice->channel;
1358
1359
/* Sustain a note under Sostenuto pedal */
1360
if(fluid_channel_sostenuto(channel) &&
1361
channel->sostenuto_orderid > voice->id)
1362
{
1363
// Sostenuto depressed after note
1364
voice->status = FLUID_VOICE_HELD_BY_SOSTENUTO;
1365
}
1366
/* Or sustain a note under Sustain pedal */
1367
else if(fluid_channel_sustained(channel))
1368
{
1369
voice->status = FLUID_VOICE_SUSTAINED;
1370
}
1371
/* Or force the voice to release stage */
1372
else
1373
{
1374
fluid_voice_release(voice);
1375
}
1376
}
1377
1378
/*
1379
* fluid_voice_kill_excl
1380
*
1381
* Percussion sounds can be mutually exclusive: for example, a 'closed
1382
* hihat' sound will terminate an 'open hihat' sound ringing at the
1383
* same time. This behaviour is modeled using 'exclusive classes',
1384
* turning on a voice with an exclusive class other than 0 will kill
1385
* all other voices having that exclusive class within the same preset
1386
* or channel. fluid_voice_kill_excl gets called, when 'voice' is to
1387
* be killed for that reason.
1388
*/
1389
1390
int
1391
fluid_voice_kill_excl(fluid_voice_t *voice)
1392
{
1393
1394
unsigned int at_tick;
1395
1396
if(!fluid_voice_is_playing(voice))
1397
{
1398
return FLUID_OK;
1399
}
1400
1401
/* Turn off the exclusive class information for this voice,
1402
so that it doesn't get killed twice
1403
*/
1404
fluid_voice_gen_set(voice, GEN_EXCLUSIVECLASS, 0);
1405
1406
/* Speed up the volume envelope */
1407
/* The value was found through listening tests with hi-hat samples. */
1408
fluid_voice_gen_set(voice, GEN_VOLENVRELEASE, -200);
1409
fluid_voice_update_param(voice, GEN_VOLENVRELEASE);
1410
1411
/* Speed up the modulation envelope */
1412
fluid_voice_gen_set(voice, GEN_MODENVRELEASE, -200);
1413
fluid_voice_update_param(voice, GEN_MODENVRELEASE);
1414
1415
at_tick = fluid_channel_get_min_note_length_ticks(voice->channel);
1416
UPDATE_RVOICE_I1(fluid_rvoice_noteoff, at_tick);
1417
1418
1419
return FLUID_OK;
1420
}
1421
1422
/*
1423
* Unlock the overflow rvoice of the voice.
1424
* Decrement the reference count of the sample owned by this rvoice.
1425
*
1426
* Called by fluid_synth when the overflow rvoice has finished by itself.
1427
* Must be called also explicitly at synth destruction to ensure that
1428
* the soundfont be unloaded successfully.
1429
*/
1430
void fluid_voice_overflow_rvoice_finished(fluid_voice_t *voice)
1431
{
1432
voice->can_access_overflow_rvoice = 1;
1433
1434
/* Decrement the reference count of the sample to indicate
1435
that this sample isn't owned by the rvoice anymore */
1436
fluid_voice_sample_unref(&voice->overflow_sample);
1437
}
1438
1439
/*
1440
* fluid_voice_off
1441
*
1442
* Force the voice into finished stage. Useful anywhere a voice
1443
* needs to be cancelled from MIDI API.
1444
*/
1445
void fluid_voice_off(fluid_voice_t *voice)
1446
{
1447
UPDATE_RVOICE0(fluid_rvoice_voiceoff); /* request to finish the voice */
1448
}
1449
1450
/*
1451
* fluid_voice_stop
1452
*
1453
* Purpose:
1454
* Turns off a voice, meaning that it is not processed anymore by the
1455
* DSP loop, i.e. contrary part to fluid_voice_start().
1456
*/
1457
void
1458
fluid_voice_stop(fluid_voice_t *voice)
1459
{
1460
fluid_profile(FLUID_PROF_VOICE_RELEASE, voice->ref, 0, 0);
1461
1462
voice->chan = NO_CHANNEL;
1463
1464
/* Decrement the reference count of the sample, to indicate
1465
that this sample isn't owned by the rvoice anymore.
1466
*/
1467
fluid_voice_sample_unref(&voice->sample);
1468
1469
voice->status = FLUID_VOICE_OFF;
1470
voice->has_noteoff = 1;
1471
1472
/* Decrement voice count */
1473
voice->channel->synth->active_voice_count--;
1474
}
1475
1476
/**
1477
* Adds a modulator to the voice if the modulator has valid sources.
1478
*
1479
* @param voice Voice instance.
1480
* @param mod Modulator info (copied).
1481
* @param mode Determines how to handle an existing identical modulator.
1482
* #FLUID_VOICE_ADD to add (offset) the modulator amounts,
1483
* #FLUID_VOICE_OVERWRITE to replace the modulator,
1484
* #FLUID_VOICE_DEFAULT when adding a default modulator - no duplicate should
1485
* exist so don't check.
1486
*/
1487
void
1488
fluid_voice_add_mod(fluid_voice_t *voice, fluid_mod_t *mod, int mode)
1489
{
1490
/* Ignore the modulator if its sources inputs are invalid */
1491
if(fluid_mod_check_sources(mod, "api fluid_voice_add_mod mod"))
1492
{
1493
fluid_voice_add_mod_local(voice, mod, mode, FLUID_NUM_MOD);
1494
}
1495
}
1496
1497
/**
1498
* Adds a modulator to the voice.
1499
* local version of fluid_voice_add_mod function. Called at noteon time.
1500
* @param voice, mod, mode, same as for fluid_voice_add_mod() (see above).
1501
* @param check_limit_count is the modulator number limit to handle with existing
1502
* identical modulator(i.e mode FLUID_VOICE_OVERWRITE, FLUID_VOICE_ADD).
1503
* - When FLUID_NUM_MOD, all the voices modulators (since the previous call)
1504
* are checked for identity.
1505
* - When check_count_limit is below the actual number of voices modulators
1506
* (voice->mod_count), this will restrict identity check to this number,
1507
* This is useful when we know by advance that there is no duplicate with
1508
* modulators at index above this limit. This avoid wasting cpu cycles at noteon.
1509
*/
1510
void
1511
fluid_voice_add_mod_local(fluid_voice_t *voice, fluid_mod_t *mod, int mode, int check_limit_count)
1512
{
1513
int i;
1514
1515
/* check_limit_count cannot be above voice->mod_count */
1516
if(check_limit_count > voice->mod_count)
1517
{
1518
check_limit_count = voice->mod_count;
1519
}
1520
1521
if(mode == FLUID_VOICE_ADD)
1522
{
1523
1524
/* if identical modulator exists, add them */
1525
for(i = 0; i < check_limit_count; i++)
1526
{
1527
if(fluid_mod_test_identity(&voice->mod[i], mod))
1528
{
1529
// printf("Adding modulator...\n");
1530
voice->mod[i].amount += mod->amount;
1531
return;
1532
}
1533
}
1534
1535
}
1536
else if(mode == FLUID_VOICE_OVERWRITE)
1537
{
1538
1539
/* if identical modulator exists, replace it (only the amount has to be changed) */
1540
for(i = 0; i < check_limit_count; i++)
1541
{
1542
if(fluid_mod_test_identity(&voice->mod[i], mod))
1543
{
1544
// printf("Replacing modulator...amount is %f\n",mod->amount);
1545
voice->mod[i].amount = mod->amount;
1546
return;
1547
}
1548
}
1549
}
1550
1551
/* Add a new modulator (No existing modulator to add / overwrite).
1552
Also, default modulators (FLUID_VOICE_DEFAULT) are added without
1553
checking, if the same modulator already exists. */
1554
if(voice->mod_count < FLUID_NUM_MOD)
1555
{
1556
fluid_mod_clone(&voice->mod[voice->mod_count++], mod);
1557
}
1558
else
1559
{
1560
FLUID_LOG(FLUID_WARN, "Voice %i has more modulators than supported, ignoring.", voice->id);
1561
}
1562
}
1563
1564
/**
1565
* Get the unique ID of the noteon-event.
1566
*
1567
* @param voice Voice instance
1568
* @return Note on unique ID
1569
*
1570
* A SoundFont loader may store pointers to voices it has created for
1571
* real-time control during the operation of a voice (for example: parameter
1572
* changes in SoundFont editor). The synth uses a pool of voices internally which are
1573
* 'recycled' and never deallocated.
1574
*
1575
* However, before modifying an existing voice, check
1576
* - that its state is still 'playing'
1577
* - that the ID is still the same
1578
*
1579
* Otherwise the voice has finished playing.
1580
*/
1581
unsigned int fluid_voice_get_id(const fluid_voice_t *voice)
1582
{
1583
return voice->id;
1584
}
1585
1586
/**
1587
* Check if a voice is producing sound.
1588
*
1589
* Like fluid_voice_is_on() this will return TRUE once a call to
1590
* fluid_synth_start_voice() has been made. Contrary to fluid_voice_is_on(),
1591
* this might also return TRUE after the voice received a noteoff event, as it may
1592
* still be playing in release phase, or because it has been sustained or
1593
* sostenuto'ed.
1594
*
1595
* @param voice Voice instance
1596
* @return TRUE if playing, FALSE otherwise
1597
*/
1598
int fluid_voice_is_playing(const fluid_voice_t *voice)
1599
{
1600
return (voice->status == FLUID_VOICE_ON)
1601
|| fluid_voice_is_sustained(voice)
1602
|| fluid_voice_is_sostenuto(voice);
1603
1604
}
1605
1606
/**
1607
* Check if a voice is ON.
1608
*
1609
* A voice is in ON state as soon as a call to fluid_synth_start_voice() has been made
1610
* (which is typically done in a fluid_preset_t's noteon function).
1611
* A voice stays ON as long as it has not received a noteoff event.
1612
*
1613
* @param voice Voice instance
1614
* @return TRUE if on, FALSE otherwise
1615
*
1616
* @since 1.1.7
1617
*/
1618
int fluid_voice_is_on(const fluid_voice_t *voice)
1619
{
1620
return (voice->status == FLUID_VOICE_ON && !voice->has_noteoff);
1621
}
1622
1623
/**
1624
* Check if a voice keeps playing after it has received a noteoff due to being held by sustain.
1625
*
1626
* @param voice Voice instance
1627
* @return TRUE if sustained, FALSE otherwise
1628
*
1629
* @since 1.1.7
1630
*/
1631
int fluid_voice_is_sustained(const fluid_voice_t *voice)
1632
{
1633
return (voice->status == FLUID_VOICE_SUSTAINED);
1634
}
1635
1636
/**
1637
* Check if a voice keeps playing after it has received a noteoff due to being held by sostenuto.
1638
*
1639
* @param voice Voice instance
1640
* @return TRUE if sostenuto, FALSE otherwise
1641
*
1642
* @since 1.1.7
1643
*/
1644
int fluid_voice_is_sostenuto(const fluid_voice_t *voice)
1645
{
1646
return (voice->status == FLUID_VOICE_HELD_BY_SOSTENUTO);
1647
}
1648
1649
/**
1650
* Return the MIDI channel the voice is playing on.
1651
*
1652
* @param voice Voice instance
1653
* @return The channel assigned to this voice
1654
*
1655
* @note The result of this function is only valid if the voice is playing.
1656
*
1657
* @since 1.1.7
1658
*/
1659
int fluid_voice_get_channel(const fluid_voice_t *voice)
1660
{
1661
return voice->chan;
1662
}
1663
1664
/**
1665
* Return the effective MIDI key of the playing voice.
1666
*
1667
* @param voice Voice instance
1668
* @return The MIDI key this voice is playing at
1669
*
1670
* If the voice was started from an instrument which uses a fixed key generator, it returns that.
1671
* Otherwise returns the same value as \c fluid_voice_get_key.
1672
*
1673
* @note The result of this function is only valid if the voice is playing.
1674
*
1675
* @since 1.1.7
1676
*/
1677
int fluid_voice_get_actual_key(const fluid_voice_t *voice)
1678
{
1679
fluid_real_t x = fluid_voice_gen_value(voice, GEN_KEYNUM);
1680
1681
if(x >= 0)
1682
{
1683
return (int)x;
1684
}
1685
else
1686
{
1687
return fluid_voice_get_key(voice);
1688
}
1689
}
1690
1691
/**
1692
* Return the MIDI key from the starting noteon event.
1693
*
1694
* @param voice Voice instance
1695
* @return The MIDI key of the noteon event that originally turned on this voice
1696
*
1697
* @note The result of this function is only valid if the voice is playing.
1698
*
1699
* @since 1.1.7
1700
*/
1701
int fluid_voice_get_key(const fluid_voice_t *voice)
1702
{
1703
return voice->key;
1704
}
1705
1706
/**
1707
* Return the effective MIDI velocity of the playing voice.
1708
*
1709
* @param voice Voice instance
1710
* @return The MIDI velocity this voice is playing at
1711
*
1712
* If the voice was started from an instrument which uses a fixed velocity generator, it returns that.
1713
* Otherwise it returns the same value as \c fluid_voice_get_velocity.
1714
*
1715
* @note The result of this function is only valid if the voice is playing.
1716
*
1717
* @since 1.1.7
1718
*/
1719
int fluid_voice_get_actual_velocity(const fluid_voice_t *voice)
1720
{
1721
fluid_real_t x = fluid_voice_gen_value(voice, GEN_VELOCITY);
1722
1723
if(x > 0)
1724
{
1725
return (int)x;
1726
}
1727
else
1728
{
1729
return fluid_voice_get_velocity(voice);
1730
}
1731
}
1732
1733
/**
1734
* Return the MIDI velocity from the starting noteon event.
1735
*
1736
* @param voice Voice instance
1737
* @return The MIDI velocity which originally turned on this voice
1738
*
1739
* @note The result of this function is only valid if the voice is playing.
1740
*
1741
* @since 1.1.7
1742
*/
1743
int fluid_voice_get_velocity(const fluid_voice_t *voice)
1744
{
1745
return voice->vel;
1746
}
1747
1748
/*
1749
* fluid_voice_get_lower_boundary_for_attenuation
1750
*
1751
* Purpose:
1752
*
1753
* A lower boundary for the attenuation (as in 'the minimum
1754
* attenuation of this voice, with volume pedals, modulators
1755
* etc. resulting in minimum attenuation, cannot fall below x cB) is
1756
* calculated. This has to be called during fluid_voice_start, after
1757
* all modulators have been run on the voice once. Also,
1758
* voice->attenuation has to be initialized.
1759
* (see fluid_voice_calculate_runtime_synthesis_parameters())
1760
*/
1761
static fluid_real_t
1762
fluid_voice_get_lower_boundary_for_attenuation(fluid_voice_t *voice)
1763
{
1764
int i;
1765
fluid_mod_t *mod;
1766
fluid_real_t possible_att_reduction_cB = 0;
1767
fluid_real_t lower_bound;
1768
1769
for(i = 0; i < voice->mod_count; i++)
1770
{
1771
mod = &voice->mod[i];
1772
1773
/* Modulator has attenuation as target and can change over time? */
1774
if((mod->dest == GEN_ATTENUATION)
1775
&& ((mod->flags1 & FLUID_MOD_CC)
1776
|| (mod->flags2 & FLUID_MOD_CC)
1777
|| (mod->src1 == FLUID_MOD_CHANNELPRESSURE)
1778
|| (mod->src1 == FLUID_MOD_KEYPRESSURE)
1779
|| (mod->src1 == FLUID_MOD_PITCHWHEEL)
1780
|| (mod->src2 == FLUID_MOD_CHANNELPRESSURE)
1781
|| (mod->src2 == FLUID_MOD_KEYPRESSURE)
1782
|| (mod->src2 == FLUID_MOD_PITCHWHEEL)))
1783
{
1784
1785
fluid_real_t current_val = fluid_mod_get_value(mod, voice);
1786
/* min_val is the possible minimum value for this modulator.
1787
it depends of 3 things :
1788
1)the minimum values of src1,src2 (i.e -1 if mapping is bipolar
1789
or 0 if mapping is unipolar).
1790
2)the sign of amount.
1791
3)absolute value of amount.
1792
1793
When at least one source mapping is bipolar:
1794
min_val is -|amount| regardless the sign of amount.
1795
When both sources mapping are unipolar:
1796
min_val is -|amount|, if amount is negative.
1797
min_val is 0, if amount is positive
1798
*/
1799
fluid_real_t min_val = fabs(mod->amount);
1800
1801
/* Can this modulator produce a negative contribution? */
1802
if((mod->flags1 & FLUID_MOD_BIPOLAR)
1803
|| (mod->flags2 & FLUID_MOD_BIPOLAR)
1804
|| (mod->amount < 0))
1805
{
1806
min_val = -min_val; /* min_val = - |amount|*/
1807
}
1808
else
1809
{
1810
/* No negative value possible. But still, the minimum contribution is 0. */
1811
min_val = 0;
1812
}
1813
1814
/* For example:
1815
* - current_val=100
1816
* - min_val=-4000
1817
* - possible reduction contribution of this modulator = current_val - min_val = 4100
1818
*/
1819
if(current_val > min_val)
1820
{
1821
possible_att_reduction_cB += (current_val - min_val);
1822
}
1823
}
1824
}
1825
1826
lower_bound = voice->attenuation - possible_att_reduction_cB;
1827
1828
/* SF2.01 specs do not allow negative attenuation */
1829
if(lower_bound < 0)
1830
{
1831
lower_bound = 0;
1832
}
1833
1834
return lower_bound;
1835
}
1836
1837
int fluid_voice_set_param(fluid_voice_t *voice, int gen, fluid_real_t nrpn_value)
1838
{
1839
voice->gen[gen].nrpn = nrpn_value;
1840
voice->gen[gen].flags = GEN_SET;
1841
fluid_voice_update_param(voice, gen);
1842
return FLUID_OK;
1843
}
1844
1845
int fluid_voice_set_gain(fluid_voice_t *voice, fluid_real_t gain)
1846
{
1847
fluid_real_t left, right, reverb, chorus;
1848
1849
/* avoid division by zero*/
1850
if(gain < 0.0000001f)
1851
{
1852
gain = 0.0000001f;
1853
}
1854
1855
voice->synth_gain = gain;
1856
left = fluid_voice_calculate_gain_amplitude(voice,
1857
fluid_pan(voice->pan, 1) * fluid_balance(voice->balance, 1));
1858
right = fluid_voice_calculate_gain_amplitude(voice,
1859
fluid_pan(voice->pan, 0) * fluid_balance(voice->balance, 0));
1860
reverb = fluid_voice_calculate_gain_amplitude(voice, voice->reverb_send);
1861
chorus = fluid_voice_calculate_gain_amplitude(voice, voice->chorus_send);
1862
1863
UPDATE_RVOICE_R1(fluid_rvoice_set_synth_gain, gain);
1864
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 0, left);
1865
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 1, right);
1866
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 2, reverb);
1867
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 3, chorus);
1868
1869
return FLUID_OK;
1870
}
1871
1872
/* - Scan the loop
1873
* - determine the peak level
1874
* - Calculate, what factor will make the loop inaudible
1875
* - Store in sample
1876
*/
1877
1878
/**
1879
* Calculate the peak volume of a sample for voice off optimization.
1880
*
1881
* @param s Sample to optimize
1882
* @return #FLUID_OK on success, #FLUID_FAILED otherwise
1883
*
1884
* If the peak volume during the loop is known, then the voice can
1885
* be released earlier during the release phase. Otherwise, the
1886
* voice will operate (inaudibly), until the envelope is at the
1887
* nominal turnoff point. So it's a good idea to call
1888
* fluid_voice_optimize_sample() on each sample once.
1889
*/
1890
int
1891
fluid_voice_optimize_sample(fluid_sample_t *s)
1892
{
1893
int32_t peak_max = 0;
1894
int32_t peak_min = 0;
1895
int32_t peak;
1896
fluid_real_t normalized_amplitude_during_loop;
1897
double result;
1898
unsigned int i;
1899
1900
/* ignore disabled samples */
1901
if(s->start == s->end)
1902
{
1903
return (FLUID_OK);
1904
}
1905
1906
if(!s->amplitude_that_reaches_noise_floor_is_valid) /* Only once */
1907
{
1908
/* Scan the loop */
1909
for(i = s->loopstart; i < s->loopend; i++)
1910
{
1911
int32_t val = fluid_rvoice_get_sample(s->data, s->data24, i);
1912
1913
if(val > peak_max)
1914
{
1915
peak_max = val;
1916
}
1917
else if(val < peak_min)
1918
{
1919
peak_min = val;
1920
}
1921
}
1922
1923
/* Determine the peak level */
1924
if(peak_max > -peak_min)
1925
{
1926
peak = peak_max;
1927
}
1928
else
1929
{
1930
peak = -peak_min;
1931
}
1932
1933
if(peak == 0)
1934
{
1935
/* Avoid division by zero */
1936
peak = 1;
1937
}
1938
1939
/* Calculate what factor will make the loop inaudible
1940
* For example: Take a peak of 3277 (10 % of 32768). The
1941
* normalized amplitude is 0.1 (10 % of 32768). An amplitude
1942
* factor of 0.0001 (as opposed to the default 0.00001) will
1943
* drop this sample to the noise floor.
1944
*/
1945
1946
/* 16 bits => 96+4=100 dB dynamic range => 0.00001 */
1947
normalized_amplitude_during_loop = ((fluid_real_t)peak) / (INT24_MAX * 1.0f);
1948
result = FLUID_NOISE_FLOOR / normalized_amplitude_during_loop;
1949
1950
/* Store in sample */
1951
s->amplitude_that_reaches_noise_floor = (double)result;
1952
s->amplitude_that_reaches_noise_floor_is_valid = 1;
1953
#if 0
1954
printf("Sample peak detection: factor %f\n", (double)result);
1955
#endif
1956
}
1957
1958
return FLUID_OK;
1959
}
1960
1961
float
1962
fluid_voice_get_overflow_prio(fluid_voice_t *voice,
1963
fluid_overflow_prio_t *score,
1964
unsigned int cur_time)
1965
{
1966
float this_voice_prio = 0;
1967
int channel;
1968
1969
/* Are we already overflowing? */
1970
if(!voice->can_access_overflow_rvoice)
1971
{
1972
return OVERFLOW_PRIO_CANNOT_KILL;
1973
}
1974
1975
/* Is this voice on the drum channel?
1976
* Then it is very important.
1977
* Also skip the released and sustained scores.
1978
*/
1979
if(voice->channel->channel_type == CHANNEL_TYPE_DRUM)
1980
{
1981
this_voice_prio += score->percussion;
1982
}
1983
else if(voice->has_noteoff)
1984
{
1985
/* Noteoff has */
1986
this_voice_prio += score->released;
1987
}
1988
else if(fluid_voice_is_sustained(voice) || fluid_voice_is_sostenuto(voice))
1989
{
1990
/* This voice is still active, since the sustain pedal is held down.
1991
* Consider it less important than non-sustained channels.
1992
* This decision is somehow subjective. But usually the sustain pedal
1993
* is used to play 'more-voices-than-fingers', so it shouldn't hurt
1994
* if we kill one voice.
1995
*/
1996
this_voice_prio += score->sustained;
1997
}
1998
1999
/* We are not enthusiastic about releasing voices, which have just been started.
2000
* Otherwise hitting a chord may result in killing notes belonging to that very same
2001
* chord. So give newer voices a higher score. */
2002
if(score->age)
2003
{
2004
cur_time -= voice->start_time;
2005
2006
if(cur_time < 1)
2007
{
2008
cur_time = 1; // Avoid div by zero
2009
}
2010
2011
this_voice_prio += (score->age * voice->output_rate) / cur_time;
2012
}
2013
2014
/* take a rough estimate of loudness into account. Louder voices are more important. */
2015
if(score->volume)
2016
{
2017
fluid_real_t a = voice->attenuation;
2018
2019
if(voice->has_noteoff)
2020
{
2021
// FIXME: Should take into account where on the envelope we are...?
2022
}
2023
2024
if(a < 0.1f)
2025
{
2026
a = 0.1f; // Avoid div by zero
2027
}
2028
2029
this_voice_prio += score->volume / a;
2030
}
2031
2032
/* Check if this voice is on an important channel. If so, then add the
2033
* score for important channels */
2034
channel = fluid_voice_get_channel(voice);
2035
2036
if(channel < score->num_important_channels && score->important_channels[channel])
2037
{
2038
this_voice_prio += score->important;
2039
}
2040
2041
return this_voice_prio;
2042
}
2043
2044
2045
void fluid_voice_set_custom_filter(fluid_voice_t *voice, enum fluid_iir_filter_type type, enum fluid_iir_filter_flags flags)
2046
{
2047
UPDATE_RVOICE_GENERIC_I2(fluid_iir_filter_init, &voice->rvoice->resonant_custom_filter, type, flags);
2048
}
2049
2050