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
8725 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
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_fres, &voice->rvoice->resonant_filter, x);
863
break;
864
865
case GEN_FILTERQ:
866
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_q, &voice->rvoice->resonant_filter, x);
867
break;
868
869
/* same as the two above, only for the custom filter */
870
case GEN_CUSTOM_FILTERFC:
871
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_fres, &voice->rvoice->resonant_custom_filter, x);
872
break;
873
874
case GEN_CUSTOM_FILTERQ:
875
UPDATE_RVOICE_GENERIC_R1(fluid_iir_filter_set_q, &voice->rvoice->resonant_custom_filter, x);
876
break;
877
878
case GEN_MODLFOTOPITCH:
879
fluid_clip(x, -12000.f, 12000.f);
880
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_pitch, x);
881
break;
882
883
case GEN_MODLFOTOVOL:
884
fluid_clip(x, -960.f, 960.f);
885
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_vol, x);
886
break;
887
888
case GEN_MODLFOTOFILTERFC:
889
fluid_clip(x, -12000.f, 12000.f);
890
UPDATE_RVOICE_R1(fluid_rvoice_set_modlfo_to_fc, x);
891
break;
892
893
case GEN_MODLFODELAY:
894
fluid_clip(x, -12000.0f, 5000.0f);
895
z = (unsigned int)(voice->output_rate * fluid_tc2sec_delay(x));
896
UPDATE_RVOICE_ENVLFO_I1(fluid_lfo_set_delay, modlfo, z);
897
break;
898
899
case GEN_MODLFOFREQ:
900
/* - the frequency is converted into a delta value, per buffer of FLUID_BUFSIZE samples
901
* - the delay into a sample delay
902
*/
903
fluid_clip(x, -16000.0f, 4500.0f);
904
x = (4.0f * FLUID_BUFSIZE * fluid_ct2hz_real(x) / voice->output_rate);
905
UPDATE_RVOICE_ENVLFO_R1(fluid_lfo_set_incr, modlfo, x);
906
break;
907
908
case GEN_VIBLFOFREQ:
909
/* vib lfo
910
*
911
* - the frequency is converted into a delta value, per buffer of FLUID_BUFSIZE samples
912
* - the delay into a sample delay
913
*/
914
fluid_clip(x, -16000.0f, 4500.0f);
915
x = 4.0f * FLUID_BUFSIZE * fluid_ct2hz_real(x) / voice->output_rate;
916
UPDATE_RVOICE_ENVLFO_R1(fluid_lfo_set_incr, viblfo, x);
917
break;
918
919
case GEN_VIBLFODELAY:
920
fluid_clip(x, -12000.0f, 5000.0f);
921
z = (unsigned int)(voice->output_rate * fluid_tc2sec_delay(x));
922
UPDATE_RVOICE_ENVLFO_I1(fluid_lfo_set_delay, viblfo, z);
923
break;
924
925
case GEN_VIBLFOTOPITCH:
926
fluid_clip(x, -12000.f, 12000.f);
927
UPDATE_RVOICE_R1(fluid_rvoice_set_viblfo_to_pitch, x);
928
break;
929
930
case GEN_KEYNUM:
931
/* GEN_KEYNUM: SF2.01 page 46, item 46
932
*
933
* If this generator is active, it forces the key number to its
934
* value. Non-realtime controller.
935
*
936
* There is a flag, which should indicate, whether a generator is
937
* enabled or not. But here we rely on the default value of -1.
938
*/
939
940
/* 2017-09-02: do not change the voice's key here, otherwise it will
941
* never be released on a noteoff event
942
*/
943
#if 0
944
x = fluid_voice_gen_value(voice, GEN_KEYNUM);
945
946
if(x >= 0)
947
{
948
voice->key = x;
949
}
950
951
#endif
952
break;
953
954
case GEN_VELOCITY:
955
/* GEN_VELOCITY: SF2.01 page 46, item 47
956
*
957
* If this generator is active, it forces the velocity to its
958
* value. Non-realtime controller.
959
*
960
* There is a flag, which should indicate, whether a generator is
961
* enabled or not. But here we rely on the default value of -1.
962
*/
963
/* 2017-09-02: do not change the voice's velocity here, use
964
* fluid_voice_get_actual_velocity() to get the value of this generator
965
* if active.
966
*/
967
#if 0
968
x = fluid_voice_gen_value(voice, GEN_VELOCITY);
969
970
if(x > 0)
971
{
972
voice->vel = x;
973
}
974
975
#endif
976
break;
977
978
case GEN_MODENVTOPITCH:
979
fluid_clip(x, -12000.f, 12000.f);
980
UPDATE_RVOICE_R1(fluid_rvoice_set_modenv_to_pitch, x);
981
break;
982
983
case GEN_MODENVTOFILTERFC:
984
/* Range: SF2.01 section 8.1.3 # 1
985
* Motivation for range checking:
986
* Filter is reported to make funny noises now and then
987
*/
988
fluid_clip(x, -12000.f, 12000.f);
989
UPDATE_RVOICE_R1(fluid_rvoice_set_modenv_to_fc, x);
990
break;
991
992
993
/* sample start and ends points
994
*
995
* Range checking is initiated via the
996
* voice->check_sample_sanity flag,
997
* because it is impossible to check here:
998
* During the voice setup, all modulators are processed, while
999
* the voice is inactive. Therefore, illegal settings may
1000
* occur during the setup (for example: First move the loop
1001
* end point ahead of the loop start point => invalid, then
1002
* move the loop start point forward => valid again.
1003
*/
1004
case GEN_STARTADDROFS: /* SF2.01 section 8.1.3 # 0 */
1005
case GEN_STARTADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 4 */
1006
if(voice->sample != NULL)
1007
{
1008
fluid_real_t start_fine = fluid_voice_gen_value(voice, GEN_STARTADDROFS);
1009
fluid_real_t start_coar = fluid_voice_gen_value(voice, GEN_STARTADDRCOARSEOFS);
1010
1011
z = voice->sample->start + (int)start_fine + 32768 * (int)start_coar;
1012
UPDATE_RVOICE_I1(fluid_rvoice_set_start, z);
1013
}
1014
1015
break;
1016
1017
case GEN_ENDADDROFS: /* SF2.01 section 8.1.3 # 1 */
1018
case GEN_ENDADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 12 */
1019
if(voice->sample != NULL)
1020
{
1021
fluid_real_t end_fine = fluid_voice_gen_value(voice, GEN_ENDADDROFS);
1022
fluid_real_t end_coar = fluid_voice_gen_value(voice, GEN_ENDADDRCOARSEOFS);
1023
1024
z = voice->sample->end + (int)end_fine + 32768 * (int)end_coar;
1025
UPDATE_RVOICE_I1(fluid_rvoice_set_end, z);
1026
}
1027
1028
break;
1029
1030
case GEN_STARTLOOPADDROFS: /* SF2.01 section 8.1.3 # 2 */
1031
case GEN_STARTLOOPADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 45 */
1032
if(voice->sample != NULL)
1033
{
1034
fluid_real_t lstart_fine = fluid_voice_gen_value(voice, GEN_STARTLOOPADDROFS);
1035
fluid_real_t lstart_coar = fluid_voice_gen_value(voice, GEN_STARTLOOPADDRCOARSEOFS);
1036
1037
z = voice->sample->loopstart + (int)lstart_fine + 32768 * (int)lstart_coar;
1038
UPDATE_RVOICE_I1(fluid_rvoice_set_loopstart, z);
1039
}
1040
1041
break;
1042
1043
case GEN_ENDLOOPADDROFS: /* SF2.01 section 8.1.3 # 3 */
1044
case GEN_ENDLOOPADDRCOARSEOFS: /* SF2.01 section 8.1.3 # 50 */
1045
if(voice->sample != NULL)
1046
{
1047
fluid_real_t lend_fine = fluid_voice_gen_value(voice, GEN_ENDLOOPADDROFS);
1048
fluid_real_t lend_coar = fluid_voice_gen_value(voice, GEN_ENDLOOPADDRCOARSEOFS);
1049
1050
z = voice->sample->loopend + (int)lend_fine + 32768 * (int)lend_coar;
1051
UPDATE_RVOICE_I1(fluid_rvoice_set_loopend, z);
1052
}
1053
1054
break;
1055
1056
/* Conversion functions differ in range limit */
1057
#define NUM_BUFFERS_DELAY(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_delay(_v) / FLUID_BUFSIZE)
1058
#define NUM_BUFFERS_ATTACK(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_attack(_v) / FLUID_BUFSIZE)
1059
#define NUM_BUFFERS_RELEASE(_v) (unsigned int) (voice->output_rate * fluid_tc2sec_release(_v) / FLUID_BUFSIZE)
1060
1061
/* volume envelope
1062
*
1063
* - delay and hold times are converted to absolute number of samples
1064
* - sustain is converted to its absolute value
1065
* - attack, decay and release are converted to their increment per sample
1066
*/
1067
case GEN_VOLENVDELAY: /* SF2.01 section 8.1.3 # 33 */
1068
fluid_clip(x, -12000.0f, 5000.0f);
1069
count = NUM_BUFFERS_DELAY(x);
1070
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVDELAY,
1071
count, 0.0f, 0.0f, -1.0f, 1.0f);
1072
break;
1073
1074
case GEN_VOLENVATTACK: /* SF2.01 section 8.1.3 # 34 */
1075
fluid_clip(x, -12000.0f, 8000.0f);
1076
count = 1 + NUM_BUFFERS_ATTACK(x);
1077
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVATTACK,
1078
count, 1.0f, 1.0f / count, -1.0f, 1.0f);
1079
break;
1080
1081
case GEN_VOLENVHOLD: /* SF2.01 section 8.1.3 # 35 */
1082
case GEN_KEYTOVOLENVHOLD: /* SF2.01 section 8.1.3 # 39 */
1083
count = calculate_hold_decay_buffers(voice, GEN_VOLENVHOLD, GEN_KEYTOVOLENVHOLD, 0); /* 0 means: hold */
1084
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVHOLD,
1085
count, 1.0f, 0.0f, -1.0f, 2.0f);
1086
break;
1087
1088
case GEN_VOLENVDECAY: /* SF2.01 section 8.1.3 # 36 */
1089
case GEN_VOLENVSUSTAIN: /* SF2.01 section 8.1.3 # 37 */
1090
case GEN_KEYTOVOLENVDECAY: /* SF2.01 section 8.1.3 # 40 */
1091
x = 1.0f - 0.001f * fluid_voice_gen_value(voice, GEN_VOLENVSUSTAIN);
1092
fluid_clip(x, 0.0f, 1.0f);
1093
count = calculate_hold_decay_buffers(voice, GEN_VOLENVDECAY, GEN_KEYTOVOLENVDECAY, 1); /* 1 for decay */
1094
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVDECAY,
1095
count, 1.0f, count ? -1.0f / count : 0.0f, x, 2.0f);
1096
break;
1097
1098
case GEN_VOLENVRELEASE: /* SF2.01 section 8.1.3 # 38 */
1099
fluid_clip(x, FLUID_MIN_VOLENVRELEASE, 8000.0f);
1100
count = 1 + NUM_BUFFERS_RELEASE(x);
1101
fluid_voice_update_volenv(voice, TRUE, FLUID_VOICE_ENVRELEASE,
1102
count, 1.0f, -1.0f / count, 0.0f, 1.0f);
1103
break;
1104
1105
/* Modulation envelope */
1106
case GEN_MODENVDELAY: /* SF2.01 section 8.1.3 # 25 */
1107
fluid_clip(x, -12000.0f, 5000.0f);
1108
count = NUM_BUFFERS_DELAY(x);
1109
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVDELAY,
1110
count, 0.0f, 0.0f, -1.0f, 1.0f);
1111
break;
1112
1113
case GEN_MODENVATTACK: /* SF2.01 section 8.1.3 # 26 */
1114
fluid_clip(x, -12000.0f, 8000.0f);
1115
count = 1 + NUM_BUFFERS_ATTACK(x);
1116
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVATTACK,
1117
count, 1.0f, 1.0f / count, -1.0f, 1.0f);
1118
break;
1119
1120
case GEN_MODENVHOLD: /* SF2.01 section 8.1.3 # 27 */
1121
case GEN_KEYTOMODENVHOLD: /* SF2.01 section 8.1.3 # 31 */
1122
count = calculate_hold_decay_buffers(voice, GEN_MODENVHOLD, GEN_KEYTOMODENVHOLD, 0); /* 1 means: hold */
1123
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVHOLD,
1124
count, 1.0f, 0.0f, -1.0f, 2.0f);
1125
break;
1126
1127
case GEN_MODENVDECAY: /* SF 2.01 section 8.1.3 # 28 */
1128
case GEN_MODENVSUSTAIN: /* SF 2.01 section 8.1.3 # 29 */
1129
case GEN_KEYTOMODENVDECAY: /* SF 2.01 section 8.1.3 # 32 */
1130
count = calculate_hold_decay_buffers(voice, GEN_MODENVDECAY, GEN_KEYTOMODENVDECAY, 1); /* 1 for decay */
1131
x = 1.0f - 0.001f * fluid_voice_gen_value(voice, GEN_MODENVSUSTAIN);
1132
fluid_clip(x, 0.0f, 1.0f);
1133
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVDECAY,
1134
count, 1.0f, count ? -1.0f / count : 0.0f, x, 2.0f);
1135
break;
1136
1137
case GEN_MODENVRELEASE: /* SF 2.01 section 8.1.3 # 30 */
1138
fluid_clip(x, -12000.0f, 8000.0f);
1139
count = 1 + NUM_BUFFERS_RELEASE(x);
1140
fluid_voice_update_modenv(voice, TRUE, FLUID_VOICE_ENVRELEASE,
1141
count, 1.0f, -1.0f / count, 0.0f, 2.0f);
1142
1143
break;
1144
1145
} /* switch gen */
1146
}
1147
1148
/**
1149
* Recalculate voice parameters for a given control.
1150
*
1151
* @param voice the synthesis voice
1152
* @param cc flag to distinguish between a continuous control and a channel control (pitch bend, ...)
1153
* @param ctrl the control number:
1154
* when >=0, only modulators's destination having ctrl as source are updated.
1155
* when -1, all modulators's destination are updated (regardless of ctrl).
1156
*
1157
* In this implementation, I want to make sure that all controllers
1158
* are event based: the parameter values of the DSP algorithm should
1159
* only be updates when a controller event arrived and not at every
1160
* iteration of the audio cycle (which would probably be feasible if
1161
* the synth was made in silicon).
1162
*
1163
* The update is done in two steps:
1164
*
1165
* - step 1: first, we look for all the modulators that have the changed
1166
* controller as a source. This will yield a generator that will be changed
1167
* because of the controller event.
1168
*
1169
* - step 2: For this generator, calculate its new value. This is the
1170
* sum of its original value plus the values of all the attached modulators.
1171
* The generator flag is set to indicate the parameters must be updated.
1172
* This avoid the risk to call 'fluid_voice_update_param' several
1173
* times for the same generator if several modulators have that generator as
1174
* destination. So every changed generators are updated only once.
1175
*/
1176
1177
/* bit table for each generator being updated. The bits are packed in variables
1178
Each variable have NBR_BIT_BY_VAR bits represented by NBR_BIT_BY_VAR_LN2.
1179
The size of the table is the number of variables: SIZE_UPDATED_GEN_BIT.
1180
1181
Note: In this implementation NBR_BIT_BY_VAR_LN2 is set to 5 (convenient for 32 bits cpu)
1182
but this could be set to 6 for 64 bits cpu.
1183
*/
1184
1185
#define NBR_BIT_BY_VAR_LN2 5 /* for 32 bits variables */
1186
#define NBR_BIT_BY_VAR (1 << NBR_BIT_BY_VAR_LN2)
1187
#define NBR_BIT_BY_VAR_ANDMASK (NBR_BIT_BY_VAR - 1)
1188
#define SIZE_UPDATED_GEN_BIT ((GEN_LAST + NBR_BIT_BY_VAR_ANDMASK) / NBR_BIT_BY_VAR)
1189
1190
#define is_gen_updated(bit,gen) (bit[gen >> NBR_BIT_BY_VAR_LN2] & (1 << (gen & NBR_BIT_BY_VAR_ANDMASK)))
1191
#define set_gen_updated(bit,gen) (bit[gen >> NBR_BIT_BY_VAR_LN2] |= (1 << (gen & NBR_BIT_BY_VAR_ANDMASK)))
1192
1193
int fluid_voice_modulate(fluid_voice_t *voice, int cc, int ctrl)
1194
{
1195
int i, k;
1196
fluid_mod_t *mod;
1197
uint32_t gen;
1198
fluid_real_t modval;
1199
1200
/* Clears registered bits table of updated generators */
1201
uint32_t updated_gen_bit[SIZE_UPDATED_GEN_BIT] = {0};
1202
1203
/* printf("Chan=%d, CC=%d, Src=%d, Val=%d\n", voice->channel->channum, cc, ctrl, val); */
1204
1205
for(i = 0; i < voice->mod_count; i++)
1206
{
1207
mod = &voice->mod[i];
1208
1209
/* step 1: find all the modulators that have the changed controller
1210
as input source. When ctrl is -1 all modulators destination
1211
are updated */
1212
if(ctrl < 0 || fluid_mod_has_source(mod, cc, ctrl))
1213
{
1214
gen = fluid_mod_get_dest(mod);
1215
1216
/* Skip if this generator has already been updated */
1217
if(!is_gen_updated(updated_gen_bit, gen))
1218
{
1219
modval = 0.0;
1220
1221
/* step 2: for every attached modulator, calculate the modulation
1222
* value for the generator gen */
1223
for(k = 0; k < voice->mod_count; k++)
1224
{
1225
if(fluid_mod_has_dest(&voice->mod[k], gen))
1226
{
1227
modval += fluid_mod_get_value(&voice->mod[k], voice);
1228
}
1229
}
1230
1231
fluid_gen_set_mod(&voice->gen[gen], modval);
1232
1233
/* now recalculate the parameter values that are derived from the
1234
generator */
1235
fluid_voice_update_param(voice, gen);
1236
1237
/* set the bit that indicates this generator is updated */
1238
set_gen_updated(updated_gen_bit, gen);
1239
}
1240
}
1241
}
1242
1243
return FLUID_OK;
1244
}
1245
1246
/**
1247
* Update all the modulators.
1248
*
1249
* This function is called after a ALL_CTRL_OFF MIDI message has been received (CC 121).
1250
* All destinations of all modulators will be updated.
1251
*/
1252
int fluid_voice_modulate_all(fluid_voice_t *voice)
1253
{
1254
return fluid_voice_modulate(voice, 0, -1);
1255
}
1256
1257
/* legato update functions --------------------------------------------------*/
1258
1259
/* Updates voice portamento parameters
1260
*
1261
* @voice voice the synthesis voice
1262
* @fromkey the beginning pitch of portamento.
1263
* @tokey the ending pitch of portamento.
1264
*
1265
* The function calculates pitch offset and increment, then these parameters
1266
* are send to the dsp.
1267
*/
1268
void fluid_voice_update_portamento(fluid_voice_t *voice, int fromkey, int tokey)
1269
1270
{
1271
fluid_channel_t *channel = voice->channel;
1272
1273
/* calculates pitch offset */
1274
fluid_real_t PitchBeg = fluid_voice_calculate_pitch(voice, fromkey);
1275
fluid_real_t PitchEnd = fluid_voice_calculate_pitch(voice, tokey);
1276
fluid_real_t pitchoffset = PitchBeg - PitchEnd;
1277
1278
/* Calculates increment countinc */
1279
/* Increment is function of PortamentoTime (ms)*/
1280
unsigned int countinc = (unsigned int)(((fluid_real_t)voice->output_rate *
1281
0.001f *
1282
(fluid_real_t)fluid_channel_portamentotime(channel)) /
1283
(fluid_real_t)FLUID_BUFSIZE + 0.5f);
1284
1285
/* Send portamento parameters to the voice dsp */
1286
UPDATE_RVOICE_GENERIC_IR(fluid_rvoice_set_portamento, voice->rvoice, countinc, pitchoffset);
1287
}
1288
1289
/*---------------------------------------------------------------*/
1290
/*legato mode 1: multi_retrigger
1291
*
1292
* Modulates all generators dependent of key,vel.
1293
* Forces the voice envelopes in the attack section (legato mode 1).
1294
*
1295
* @voice voice the synthesis voice
1296
* @tokey the new key to be applied to this voice.
1297
* @vel the new velocity to be applied to this voice.
1298
*/
1299
void fluid_voice_update_multi_retrigger_attack(fluid_voice_t *voice,
1300
int tokey, int vel)
1301
{
1302
voice->key = tokey; /* new note */
1303
voice->vel = vel; /* new velocity */
1304
/* Updates generators dependent of velocity */
1305
/* Modulates GEN_ATTENUATION (and others ) before calling
1306
fluid_rvoice_multi_retrigger_attack().*/
1307
fluid_voice_modulate(voice, FALSE, FLUID_MOD_VELOCITY);
1308
1309
/* Updates generator dependent of voice->key */
1310
fluid_voice_update_param(voice, GEN_KEYTOMODENVHOLD);
1311
fluid_voice_update_param(voice, GEN_KEYTOMODENVDECAY);
1312
fluid_voice_update_param(voice, GEN_KEYTOVOLENVHOLD);
1313
fluid_voice_update_param(voice, GEN_KEYTOVOLENVDECAY);
1314
1315
/* Updates pitch generator */
1316
fluid_voice_calculate_gen_pitch(voice);
1317
fluid_voice_update_param(voice, GEN_PITCH);
1318
1319
/* updates adsr generator */
1320
UPDATE_RVOICE0(fluid_rvoice_multi_retrigger_attack);
1321
}
1322
/** end of legato update functions */
1323
1324
/*
1325
Force the voice into release stage. Useful anywhere a voice
1326
needs to be damped even if pedals (sustain sostenuto) are depressed.
1327
See fluid_synth_damp_voices_by_sustain_LOCAL(),
1328
fluid_synth_damp_voices_by_sostenuto_LOCAL,
1329
fluid_voice_noteoff().
1330
*/
1331
void
1332
fluid_voice_release(fluid_voice_t *voice)
1333
{
1334
unsigned int at_tick = fluid_channel_get_min_note_length_ticks(voice->channel);
1335
UPDATE_RVOICE_I1(fluid_rvoice_noteoff, at_tick);
1336
voice->has_noteoff = 1; // voice is marked as noteoff occurred
1337
}
1338
1339
/*
1340
* fluid_voice_noteoff
1341
*
1342
* Sending a noteoff event will advance the envelopes to section 5 (release).
1343
* The function is convenient for polyphonic or monophonic note
1344
*/
1345
void
1346
fluid_voice_noteoff(fluid_voice_t *voice)
1347
{
1348
fluid_channel_t *channel;
1349
1350
fluid_profile(FLUID_PROF_VOICE_NOTE, voice->ref, 0, 0);
1351
1352
channel = voice->channel;
1353
1354
/* Sustain a note under Sostenuto pedal */
1355
if(fluid_channel_sostenuto(channel) &&
1356
channel->sostenuto_orderid > voice->id)
1357
{
1358
// Sostenuto depressed after note
1359
voice->status = FLUID_VOICE_HELD_BY_SOSTENUTO;
1360
}
1361
/* Or sustain a note under Sustain pedal */
1362
else if(fluid_channel_sustained(channel))
1363
{
1364
voice->status = FLUID_VOICE_SUSTAINED;
1365
}
1366
/* Or force the voice to release stage */
1367
else
1368
{
1369
fluid_voice_release(voice);
1370
}
1371
}
1372
1373
/*
1374
* fluid_voice_kill_excl
1375
*
1376
* Percussion sounds can be mutually exclusive: for example, a 'closed
1377
* hihat' sound will terminate an 'open hihat' sound ringing at the
1378
* same time. This behaviour is modeled using 'exclusive classes',
1379
* turning on a voice with an exclusive class other than 0 will kill
1380
* all other voices having that exclusive class within the same preset
1381
* or channel. fluid_voice_kill_excl gets called, when 'voice' is to
1382
* be killed for that reason.
1383
*/
1384
1385
int
1386
fluid_voice_kill_excl(fluid_voice_t *voice)
1387
{
1388
1389
unsigned int at_tick;
1390
1391
if(!fluid_voice_is_playing(voice))
1392
{
1393
return FLUID_OK;
1394
}
1395
1396
/* Turn off the exclusive class information for this voice,
1397
so that it doesn't get killed twice
1398
*/
1399
fluid_voice_gen_set(voice, GEN_EXCLUSIVECLASS, 0);
1400
1401
#if 0 /* unused in Wine */
1402
/* Speed up the volume envelope */
1403
/* The value was found through listening tests with hi-hat samples. */
1404
fluid_voice_gen_set(voice, GEN_VOLENVRELEASE, -200);
1405
fluid_voice_update_param(voice, GEN_VOLENVRELEASE);
1406
1407
/* Speed up the modulation envelope */
1408
fluid_voice_gen_set(voice, GEN_MODENVRELEASE, -200);
1409
fluid_voice_update_param(voice, GEN_MODENVRELEASE);
1410
gen[i].val = fluid_gen_info[i].def;
1411
#else
1412
/* Speed up the volume envelope */
1413
fluid_voice_gen_set(voice, GEN_VOLENVRELEASE, -32768);
1414
fluid_voice_update_param(voice, GEN_VOLENVRELEASE);
1415
#endif
1416
1417
at_tick = fluid_channel_get_min_note_length_ticks(voice->channel);
1418
UPDATE_RVOICE_I1(fluid_rvoice_noteoff, at_tick);
1419
1420
1421
return FLUID_OK;
1422
}
1423
1424
/*
1425
* Unlock the overflow rvoice of the voice.
1426
* Decrement the reference count of the sample owned by this rvoice.
1427
*
1428
* Called by fluid_synth when the overflow rvoice has finished by itself.
1429
* Must be called also explicitly at synth destruction to ensure that
1430
* the soundfont be unloaded successfully.
1431
*/
1432
void fluid_voice_overflow_rvoice_finished(fluid_voice_t *voice)
1433
{
1434
voice->can_access_overflow_rvoice = 1;
1435
1436
/* Decrement the reference count of the sample to indicate
1437
that this sample isn't owned by the rvoice anymore */
1438
fluid_voice_sample_unref(&voice->overflow_sample);
1439
}
1440
1441
/*
1442
* fluid_voice_off
1443
*
1444
* Force the voice into finished stage. Useful anywhere a voice
1445
* needs to be cancelled from MIDI API.
1446
*/
1447
void fluid_voice_off(fluid_voice_t *voice)
1448
{
1449
UPDATE_RVOICE0(fluid_rvoice_voiceoff); /* request to finish the voice */
1450
}
1451
1452
/*
1453
* fluid_voice_stop
1454
*
1455
* Purpose:
1456
* Turns off a voice, meaning that it is not processed anymore by the
1457
* DSP loop, i.e. contrary part to fluid_voice_start().
1458
*/
1459
void
1460
fluid_voice_stop(fluid_voice_t *voice)
1461
{
1462
fluid_profile(FLUID_PROF_VOICE_RELEASE, voice->ref, 0, 0);
1463
1464
voice->chan = NO_CHANNEL;
1465
1466
/* Decrement the reference count of the sample, to indicate
1467
that this sample isn't owned by the rvoice anymore.
1468
*/
1469
fluid_voice_sample_unref(&voice->sample);
1470
1471
voice->status = FLUID_VOICE_OFF;
1472
voice->has_noteoff = 1;
1473
1474
/* Decrement voice count */
1475
voice->channel->synth->active_voice_count--;
1476
}
1477
1478
/**
1479
* Adds a modulator to the voice if the modulator has valid sources.
1480
*
1481
* @param voice Voice instance.
1482
* @param mod Modulator info (copied).
1483
* @param mode Determines how to handle an existing identical modulator.
1484
* #FLUID_VOICE_ADD to add (offset) the modulator amounts,
1485
* #FLUID_VOICE_OVERWRITE to replace the modulator,
1486
* #FLUID_VOICE_DEFAULT when adding a default modulator - no duplicate should
1487
* exist so don't check.
1488
*/
1489
void
1490
fluid_voice_add_mod(fluid_voice_t *voice, fluid_mod_t *mod, int mode)
1491
{
1492
/* Ignore the modulator if its sources inputs are invalid */
1493
if(fluid_mod_check_sources(mod, "api fluid_voice_add_mod mod"))
1494
{
1495
fluid_voice_add_mod_local(voice, mod, mode, FLUID_NUM_MOD);
1496
}
1497
}
1498
1499
/**
1500
* Adds a modulator to the voice.
1501
* local version of fluid_voice_add_mod function. Called at noteon time.
1502
* @param voice, mod, mode, same as for fluid_voice_add_mod() (see above).
1503
* @param check_limit_count is the modulator number limit to handle with existing
1504
* identical modulator(i.e mode FLUID_VOICE_OVERWRITE, FLUID_VOICE_ADD).
1505
* - When FLUID_NUM_MOD, all the voices modulators (since the previous call)
1506
* are checked for identity.
1507
* - When check_count_limit is below the actual number of voices modulators
1508
* (voice->mod_count), this will restrict identity check to this number,
1509
* This is useful when we know by advance that there is no duplicate with
1510
* modulators at index above this limit. This avoid wasting cpu cycles at noteon.
1511
*/
1512
void
1513
fluid_voice_add_mod_local(fluid_voice_t *voice, fluid_mod_t *mod, int mode, int check_limit_count)
1514
{
1515
int i;
1516
1517
/* check_limit_count cannot be above voice->mod_count */
1518
if(check_limit_count > voice->mod_count)
1519
{
1520
check_limit_count = voice->mod_count;
1521
}
1522
1523
if(mode == FLUID_VOICE_ADD)
1524
{
1525
1526
/* if identical modulator exists, add them */
1527
for(i = 0; i < check_limit_count; i++)
1528
{
1529
if(fluid_mod_test_identity(&voice->mod[i], mod))
1530
{
1531
// printf("Adding modulator...\n");
1532
voice->mod[i].amount += mod->amount;
1533
return;
1534
}
1535
}
1536
1537
}
1538
else if(mode == FLUID_VOICE_OVERWRITE)
1539
{
1540
1541
/* if identical modulator exists, replace it (only the amount has to be changed) */
1542
for(i = 0; i < check_limit_count; i++)
1543
{
1544
if(fluid_mod_test_identity(&voice->mod[i], mod))
1545
{
1546
// printf("Replacing modulator...amount is %f\n",mod->amount);
1547
voice->mod[i].amount = mod->amount;
1548
return;
1549
}
1550
}
1551
}
1552
1553
/* Add a new modulator (No existing modulator to add / overwrite).
1554
Also, default modulators (FLUID_VOICE_DEFAULT) are added without
1555
checking, if the same modulator already exists. */
1556
if(voice->mod_count < FLUID_NUM_MOD)
1557
{
1558
fluid_mod_clone(&voice->mod[voice->mod_count++], mod);
1559
}
1560
else
1561
{
1562
FLUID_LOG(FLUID_WARN, "Voice %i has more modulators than supported, ignoring.", voice->id);
1563
}
1564
}
1565
1566
/**
1567
* Get the unique ID of the noteon-event.
1568
*
1569
* @param voice Voice instance
1570
* @return Note on unique ID
1571
*
1572
* A SoundFont loader may store pointers to voices it has created for
1573
* real-time control during the operation of a voice (for example: parameter
1574
* changes in SoundFont editor). The synth uses a pool of voices internally which are
1575
* 'recycled' and never deallocated.
1576
*
1577
* However, before modifying an existing voice, check
1578
* - that its state is still 'playing'
1579
* - that the ID is still the same
1580
*
1581
* Otherwise the voice has finished playing.
1582
*/
1583
unsigned int fluid_voice_get_id(const fluid_voice_t *voice)
1584
{
1585
return voice->id;
1586
}
1587
1588
/**
1589
* Check if a voice is producing sound.
1590
*
1591
* Like fluid_voice_is_on() this will return TRUE once a call to
1592
* fluid_synth_start_voice() has been made. Contrary to fluid_voice_is_on(),
1593
* this might also return TRUE after the voice received a noteoff event, as it may
1594
* still be playing in release phase, or because it has been sustained or
1595
* sostenuto'ed.
1596
*
1597
* @param voice Voice instance
1598
* @return TRUE if playing, FALSE otherwise
1599
*/
1600
int fluid_voice_is_playing(const fluid_voice_t *voice)
1601
{
1602
return (voice->status == FLUID_VOICE_ON)
1603
|| fluid_voice_is_sustained(voice)
1604
|| fluid_voice_is_sostenuto(voice);
1605
1606
}
1607
1608
/**
1609
* Check if a voice is ON.
1610
*
1611
* A voice is in ON state as soon as a call to fluid_synth_start_voice() has been made
1612
* (which is typically done in a fluid_preset_t's noteon function).
1613
* A voice stays ON as long as it has not received a noteoff event.
1614
*
1615
* @param voice Voice instance
1616
* @return TRUE if on, FALSE otherwise
1617
*
1618
* @since 1.1.7
1619
*/
1620
int fluid_voice_is_on(const fluid_voice_t *voice)
1621
{
1622
return (voice->status == FLUID_VOICE_ON && !voice->has_noteoff);
1623
}
1624
1625
/**
1626
* Check if a voice keeps playing after it has received a noteoff due to being held by sustain.
1627
*
1628
* @param voice Voice instance
1629
* @return TRUE if sustained, FALSE otherwise
1630
*
1631
* @since 1.1.7
1632
*/
1633
int fluid_voice_is_sustained(const fluid_voice_t *voice)
1634
{
1635
return (voice->status == FLUID_VOICE_SUSTAINED);
1636
}
1637
1638
/**
1639
* Check if a voice keeps playing after it has received a noteoff due to being held by sostenuto.
1640
*
1641
* @param voice Voice instance
1642
* @return TRUE if sostenuto, FALSE otherwise
1643
*
1644
* @since 1.1.7
1645
*/
1646
int fluid_voice_is_sostenuto(const fluid_voice_t *voice)
1647
{
1648
return (voice->status == FLUID_VOICE_HELD_BY_SOSTENUTO);
1649
}
1650
1651
/**
1652
* Return the MIDI channel the voice is playing on.
1653
*
1654
* @param voice Voice instance
1655
* @return The channel assigned to this voice
1656
*
1657
* @note The result of this function is only valid if the voice is playing.
1658
*
1659
* @since 1.1.7
1660
*/
1661
int fluid_voice_get_channel(const fluid_voice_t *voice)
1662
{
1663
return voice->chan;
1664
}
1665
1666
/**
1667
* Return the effective MIDI key of the playing voice.
1668
*
1669
* @param voice Voice instance
1670
* @return The MIDI key this voice is playing at
1671
*
1672
* If the voice was started from an instrument which uses a fixed key generator, it returns that.
1673
* Otherwise returns the same value as \c fluid_voice_get_key.
1674
*
1675
* @note The result of this function is only valid if the voice is playing.
1676
*
1677
* @since 1.1.7
1678
*/
1679
int fluid_voice_get_actual_key(const fluid_voice_t *voice)
1680
{
1681
fluid_real_t x = fluid_voice_gen_value(voice, GEN_KEYNUM);
1682
1683
if(x >= 0)
1684
{
1685
return (int)x;
1686
}
1687
else
1688
{
1689
return fluid_voice_get_key(voice);
1690
}
1691
}
1692
1693
/**
1694
* Return the MIDI key from the starting noteon event.
1695
*
1696
* @param voice Voice instance
1697
* @return The MIDI key of the noteon event that originally turned on this voice
1698
*
1699
* @note The result of this function is only valid if the voice is playing.
1700
*
1701
* @since 1.1.7
1702
*/
1703
int fluid_voice_get_key(const fluid_voice_t *voice)
1704
{
1705
return voice->key;
1706
}
1707
1708
/**
1709
* Return the effective MIDI velocity of the playing voice.
1710
*
1711
* @param voice Voice instance
1712
* @return The MIDI velocity this voice is playing at
1713
*
1714
* If the voice was started from an instrument which uses a fixed velocity generator, it returns that.
1715
* Otherwise it returns the same value as \c fluid_voice_get_velocity.
1716
*
1717
* @note The result of this function is only valid if the voice is playing.
1718
*
1719
* @since 1.1.7
1720
*/
1721
int fluid_voice_get_actual_velocity(const fluid_voice_t *voice)
1722
{
1723
fluid_real_t x = fluid_voice_gen_value(voice, GEN_VELOCITY);
1724
1725
if(x > 0)
1726
{
1727
return (int)x;
1728
}
1729
else
1730
{
1731
return fluid_voice_get_velocity(voice);
1732
}
1733
}
1734
1735
/**
1736
* Return the MIDI velocity from the starting noteon event.
1737
*
1738
* @param voice Voice instance
1739
* @return The MIDI velocity which originally turned on this voice
1740
*
1741
* @note The result of this function is only valid if the voice is playing.
1742
*
1743
* @since 1.1.7
1744
*/
1745
int fluid_voice_get_velocity(const fluid_voice_t *voice)
1746
{
1747
return voice->vel;
1748
}
1749
1750
/*
1751
* fluid_voice_get_lower_boundary_for_attenuation
1752
*
1753
* Purpose:
1754
*
1755
* A lower boundary for the attenuation (as in 'the minimum
1756
* attenuation of this voice, with volume pedals, modulators
1757
* etc. resulting in minimum attenuation, cannot fall below x cB) is
1758
* calculated. This has to be called during fluid_voice_start, after
1759
* all modulators have been run on the voice once. Also,
1760
* voice->attenuation has to be initialized.
1761
* (see fluid_voice_calculate_runtime_synthesis_parameters())
1762
*/
1763
static fluid_real_t
1764
fluid_voice_get_lower_boundary_for_attenuation(fluid_voice_t *voice)
1765
{
1766
int i;
1767
fluid_mod_t *mod;
1768
fluid_real_t possible_att_reduction_cB = 0;
1769
fluid_real_t lower_bound;
1770
1771
for(i = 0; i < voice->mod_count; i++)
1772
{
1773
mod = &voice->mod[i];
1774
1775
/* Modulator has attenuation as target and can change over time? */
1776
if((mod->dest == GEN_ATTENUATION)
1777
&& ((mod->flags1 & FLUID_MOD_CC)
1778
|| (mod->flags2 & FLUID_MOD_CC)
1779
|| (mod->src1 == FLUID_MOD_CHANNELPRESSURE)
1780
|| (mod->src1 == FLUID_MOD_KEYPRESSURE)
1781
|| (mod->src1 == FLUID_MOD_PITCHWHEEL)
1782
|| (mod->src2 == FLUID_MOD_CHANNELPRESSURE)
1783
|| (mod->src2 == FLUID_MOD_KEYPRESSURE)
1784
|| (mod->src2 == FLUID_MOD_PITCHWHEEL)))
1785
{
1786
1787
fluid_real_t current_val = fluid_mod_get_value(mod, voice);
1788
/* min_val is the possible minimum value for this modulator.
1789
it depends of 3 things :
1790
1)the minimum values of src1,src2 (i.e -1 if mapping is bipolar
1791
or 0 if mapping is unipolar).
1792
2)the sign of amount.
1793
3)absolute value of amount.
1794
1795
When at least one source mapping is bipolar:
1796
min_val is -|amount| regardless the sign of amount.
1797
When both sources mapping are unipolar:
1798
min_val is -|amount|, if amount is negative.
1799
min_val is 0, if amount is positive
1800
*/
1801
fluid_real_t min_val = fabs(mod->amount);
1802
1803
/* Can this modulator produce a negative contribution? */
1804
if((mod->flags1 & FLUID_MOD_BIPOLAR)
1805
|| (mod->flags2 & FLUID_MOD_BIPOLAR)
1806
|| (mod->amount < 0))
1807
{
1808
min_val = -min_val; /* min_val = - |amount|*/
1809
}
1810
else
1811
{
1812
/* No negative value possible. But still, the minimum contribution is 0. */
1813
min_val = 0;
1814
}
1815
1816
/* For example:
1817
* - current_val=100
1818
* - min_val=-4000
1819
* - possible reduction contribution of this modulator = current_val - min_val = 4100
1820
*/
1821
if(current_val > min_val)
1822
{
1823
possible_att_reduction_cB += (current_val - min_val);
1824
}
1825
}
1826
}
1827
1828
lower_bound = voice->attenuation - possible_att_reduction_cB;
1829
1830
/* SF2.01 specs do not allow negative attenuation */
1831
if(lower_bound < 0)
1832
{
1833
lower_bound = 0;
1834
}
1835
1836
return lower_bound;
1837
}
1838
1839
int fluid_voice_set_param(fluid_voice_t *voice, int gen, fluid_real_t nrpn_value)
1840
{
1841
voice->gen[gen].nrpn = nrpn_value;
1842
voice->gen[gen].flags = GEN_SET;
1843
fluid_voice_update_param(voice, gen);
1844
return FLUID_OK;
1845
}
1846
1847
int fluid_voice_set_gain(fluid_voice_t *voice, fluid_real_t gain)
1848
{
1849
fluid_real_t left, right, reverb, chorus;
1850
1851
/* avoid division by zero*/
1852
if(gain < 0.0000001f)
1853
{
1854
gain = 0.0000001f;
1855
}
1856
1857
voice->synth_gain = gain;
1858
left = fluid_voice_calculate_gain_amplitude(voice,
1859
fluid_pan(voice->pan, 1) * fluid_balance(voice->balance, 1));
1860
right = fluid_voice_calculate_gain_amplitude(voice,
1861
fluid_pan(voice->pan, 0) * fluid_balance(voice->balance, 0));
1862
reverb = fluid_voice_calculate_gain_amplitude(voice, voice->reverb_send);
1863
chorus = fluid_voice_calculate_gain_amplitude(voice, voice->chorus_send);
1864
1865
UPDATE_RVOICE_R1(fluid_rvoice_set_synth_gain, gain);
1866
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 0, left);
1867
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 1, right);
1868
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 2, reverb);
1869
UPDATE_RVOICE_BUFFERS_AMP(fluid_rvoice_buffers_set_amp, 3, chorus);
1870
1871
return FLUID_OK;
1872
}
1873
1874
/* - Scan the loop
1875
* - determine the peak level
1876
* - Calculate, what factor will make the loop inaudible
1877
* - Store in sample
1878
*/
1879
1880
/**
1881
* Calculate the peak volume of a sample for voice off optimization.
1882
*
1883
* @param s Sample to optimize
1884
* @return #FLUID_OK on success, #FLUID_FAILED otherwise
1885
*
1886
* If the peak volume during the loop is known, then the voice can
1887
* be released earlier during the release phase. Otherwise, the
1888
* voice will operate (inaudibly), until the envelope is at the
1889
* nominal turnoff point. So it's a good idea to call
1890
* fluid_voice_optimize_sample() on each sample once.
1891
*/
1892
int
1893
fluid_voice_optimize_sample(fluid_sample_t *s)
1894
{
1895
int32_t peak_max = 0;
1896
int32_t peak_min = 0;
1897
int32_t peak;
1898
fluid_real_t normalized_amplitude_during_loop;
1899
double result;
1900
unsigned int i;
1901
1902
/* ignore disabled samples */
1903
if(s->start == s->end)
1904
{
1905
return (FLUID_OK);
1906
}
1907
1908
if(!s->amplitude_that_reaches_noise_floor_is_valid) /* Only once */
1909
{
1910
/* Scan the loop */
1911
for(i = s->loopstart; i < s->loopend; i++)
1912
{
1913
int32_t val = fluid_rvoice_get_sample(s->data, s->data24, i);
1914
1915
if(val > peak_max)
1916
{
1917
peak_max = val;
1918
}
1919
else if(val < peak_min)
1920
{
1921
peak_min = val;
1922
}
1923
}
1924
1925
/* Determine the peak level */
1926
if(peak_max > -peak_min)
1927
{
1928
peak = peak_max;
1929
}
1930
else
1931
{
1932
peak = -peak_min;
1933
}
1934
1935
if(peak == 0)
1936
{
1937
/* Avoid division by zero */
1938
peak = 1;
1939
}
1940
1941
/* Calculate what factor will make the loop inaudible
1942
* For example: Take a peak of 3277 (10 % of 32768). The
1943
* normalized amplitude is 0.1 (10 % of 32768). An amplitude
1944
* factor of 0.0001 (as opposed to the default 0.00001) will
1945
* drop this sample to the noise floor.
1946
*/
1947
1948
/* 16 bits => 96+4=100 dB dynamic range => 0.00001 */
1949
normalized_amplitude_during_loop = ((fluid_real_t)peak) / (INT24_MAX * 1.0f);
1950
result = FLUID_NOISE_FLOOR / normalized_amplitude_during_loop;
1951
1952
/* Store in sample */
1953
s->amplitude_that_reaches_noise_floor = (double)result;
1954
s->amplitude_that_reaches_noise_floor_is_valid = 1;
1955
#if 0
1956
printf("Sample peak detection: factor %f\n", (double)result);
1957
#endif
1958
}
1959
1960
return FLUID_OK;
1961
}
1962
1963
float
1964
fluid_voice_get_overflow_prio(fluid_voice_t *voice,
1965
fluid_overflow_prio_t *score,
1966
unsigned int cur_time)
1967
{
1968
float this_voice_prio = 0;
1969
int channel;
1970
1971
/* Are we already overflowing? */
1972
if(!voice->can_access_overflow_rvoice)
1973
{
1974
return OVERFLOW_PRIO_CANNOT_KILL;
1975
}
1976
1977
/* Is this voice on the drum channel?
1978
* Then it is very important.
1979
* Also skip the released and sustained scores.
1980
*/
1981
if(voice->channel->channel_type == CHANNEL_TYPE_DRUM)
1982
{
1983
this_voice_prio += score->percussion;
1984
}
1985
else if(voice->has_noteoff)
1986
{
1987
/* Noteoff has */
1988
this_voice_prio += score->released;
1989
}
1990
else if(fluid_voice_is_sustained(voice) || fluid_voice_is_sostenuto(voice))
1991
{
1992
/* This voice is still active, since the sustain pedal is held down.
1993
* Consider it less important than non-sustained channels.
1994
* This decision is somehow subjective. But usually the sustain pedal
1995
* is used to play 'more-voices-than-fingers', so it shouldn't hurt
1996
* if we kill one voice.
1997
*/
1998
this_voice_prio += score->sustained;
1999
}
2000
2001
/* We are not enthusiastic about releasing voices, which have just been started.
2002
* Otherwise hitting a chord may result in killing notes belonging to that very same
2003
* chord. So give newer voices a higher score. */
2004
if(score->age)
2005
{
2006
cur_time -= voice->start_time;
2007
2008
if(cur_time < 1)
2009
{
2010
cur_time = 1; // Avoid div by zero
2011
}
2012
2013
this_voice_prio += (score->age * voice->output_rate) / cur_time;
2014
}
2015
2016
/* take a rough estimate of loudness into account. Louder voices are more important. */
2017
if(score->volume)
2018
{
2019
fluid_real_t a = voice->attenuation;
2020
2021
if(voice->has_noteoff)
2022
{
2023
// FIXME: Should take into account where on the envelope we are...?
2024
}
2025
2026
if(a < 0.1f)
2027
{
2028
a = 0.1f; // Avoid div by zero
2029
}
2030
2031
this_voice_prio += score->volume / a;
2032
}
2033
2034
/* Check if this voice is on an important channel. If so, then add the
2035
* score for important channels */
2036
channel = fluid_voice_get_channel(voice);
2037
2038
if(channel < score->num_important_channels && score->important_channels[channel])
2039
{
2040
this_voice_prio += score->important;
2041
}
2042
2043
return this_voice_prio;
2044
}
2045
2046
2047
void fluid_voice_set_custom_filter(fluid_voice_t *voice, enum fluid_iir_filter_type type, enum fluid_iir_filter_flags flags)
2048
{
2049
UPDATE_RVOICE_GENERIC_I2(fluid_iir_filter_init, &voice->rvoice->resonant_custom_filter, type, flags);
2050
}
2051
2052