-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvoice_classes.cpp
executable file
·720 lines (639 loc) · 14 KB
/
voice_classes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
The voice producing machine, diverted into two blocks:
VOICES actually calculate a single voice sound. They
do basic voice handling like ADSR, pitch and the unevitable
click and percussion effects.
A NOTEMASTER instantiates as many voice class objects as needed
for the current instrument. The numbers are tunable in "Globals.h",
definition "NUMOFVOICES". It handles all incoming events like note
on and off, all notes off, pedal, pitch bend and so on. The events
will then be assigned to the corresponding voices. The notemaster
assigns a specific wavetable to each voice. Only active voices
are handled by the notemaster - that's the main reason why the CPU
meter goes up when you hold more notes. The positive effect is: The
meter goes down if you use less voices...
*/
#include "voice_classes.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef WIN32
#include <wtypes.h>
#endif
#define DENORMALIZE(fv) (fv<.00000001f && fv>-.00000001f)?0:(fv)
// -------- helper function
char* note2str(long note)
{
static char notestr[4];
int octave=int(note/12);
switch(note%12)
{
case 0:
sprintf(notestr,"C%1d",octave);
break;
case 1:
sprintf(notestr,"C#%1d",octave);
break;
case 2:
sprintf(notestr,"D%1d",octave);
break;
case 3:
sprintf(notestr,"D#%1d",octave);
break;
case 4:
sprintf(notestr,"E%1d",octave);
break;
case 5:
sprintf(notestr,"F%1d",octave);
break;
case 6:
sprintf(notestr,"F#%1d",octave);
break;
case 7:
sprintf(notestr,"G%1d",octave);
break;
case 8:
sprintf(notestr,"G#%1d",octave);
break;
case 9:
sprintf(notestr,"A%1d",octave);
break;
case 10:
sprintf(notestr,"A#%1d",octave);
break;
case 11:
sprintf(notestr,"B%1d",octave);
break;
}
return(notestr);
}
void voice::reset()
{
actual_note=-1;
next_note=-1;
perc_next_note=-1;
perc_ok=false;
vca_phase=VP_IDLE;
output=0;
VCA=0;
perc=0;
perc_vca=0;
percmultiplier=0;
adsr_decay=0;
click=0;
status=VS_IDLE;
pedal=false;
pitch=1;
phase=phaseinc=0;
sustain=0;
}
void voice::suspend()
{
actual_note=-1;
next_note=-1;
perc_next_note=-1;
vca_phase=VP_IDLE;
output=0;
VCA=0;
perc_vca=0;
status=VS_IDLE;
pitch=1;
phase=phaseinc=0;
}
void voice::resume()
{
}
voice::voice()
{
midi_scaler = (1. / 127.);
long i;
double k = 1.059463094359; // 12th root of 2
double a = 6.875; // a
a *= k; // b
a *= k; // bb
a *= k; // c, frequency of midi note 0
for (i = 0; i < 128; i++) // 128 midi notes
{
freqtab[i] = (float)a;
a *= k;
}
samplecount1=samplecount2=0;
sustain=0;
perc_ok=false;
};
void voice::set_samplerate(float samplerate)
{
this->samplerate=samplerate;
clicklp.set_samplerate(samplerate);
}
void voice::voicecalc()
{
/*
This "scaler" stuff is important to keep timing independant from
the actual samplerate.
*/
float scaler;
scaler=samplerate/44100;
clickattack=.004f/scaler;
adsr_attack=.05f/scaler;
adsr_decay=0/scaler;
if(sustain>0)
adsr_release=(.0001f+.0005f*(1-sustain))/scaler;
else
adsr_release=.99f/scaler;
adsr_fast_release=.03f/scaler;
perc_decay=perc_fade*.00035f/scaler;
perc_decay*=3;
samplerate_scaler = (float)((double)my_size / (double)samplerate);
phaseinc=(float)freqtab[actual_note] * samplerate_scaler * pitch;
if(perc_phase==0)
{
// fast sine calculation, precise enough for our percussion effect
float fact=percmultiplier*freqtab[actual_note];
while(fact>3800)
fact*=.5f;
a=2*sinf(3.14159265358979f*pitch*fact/samplerate);
s0=.5f;
s1=0;
}
}
float voice::clock()
{
if(status==VS_IDLE||actual_note<0) // nothing to do...
return(0);
output=0;
/*
This is the part where we read a value from the assigned wavetable.
We use a very simple interpolation to determine the actual sample
value. Since we use almost pure sine waves we don't have to take care
about anti-aliasing here. The few aliasing effects we receive sound just
like those real hardware tonewheels...
*/
iphase=int(phase);
fract=phase-iphase;
y0=my_table[iphase];
y1=my_table[iphase+1];
output=(y0+fract*(y1-y0))*VCA;
phase += phaseinc;
/*
No, we don't use the bit mask stuff as mentioned in the SDK.
It's _not_ slower this way, and we can have random wavetable sizes.
*/
if(phase>my_size)
phase-=my_size;
samplecount1++;
if(samplecount1>5)
{
samplecount1=0;
if(vca_phase==VP_A) // attack
{
if(click<=0)
{
VCA+=adsr_attack;
}
if(VCA>1)
{
VCA=1;
vca_phase=VP_D;
}
}
else if(vca_phase==VP_D)
{
vca_phase=VP_S;
}
else if(vca_phase==VP_S)
{
// output*=adsr_sustain;
}
else if(vca_phase==VP_R) // release
{
if(perc>0)
{
if(sustain==0)
perc_phase=3;
}
if(click>0 && sustain==0)
{
static unsigned long randSeed = 22222, maxrand=(unsigned long)-1;
float rand=0;
randSeed = (randSeed * 196314165) + 907633515;
rand=(float)randSeed/0xffffffff;
clicklp.clock(click*rand*.3f);
noise=clicklp.bp()*clickvol*.7f;
output+=noise;
}
if(sustain>0)
VCA-=adsr_release;
else
VCA*=adsr_release;
if(VCA<=0.00001f)
{
VCA=0;
actual_note=-1;
phase=0;
vca_phase=VP_IDLE;
output=0;
status=VS_IDLE;
}
}
else if(vca_phase==VP_FR) // fast release
{
VCA-=adsr_fast_release;
if(VCA<=0)
{
VCA=0;
actual_note=-1;
if(next_note>=0)
{
actual_note=next_note;
next_note=-1;
vca_phase=VP_A;
phase=0;
perc_phase=0;
this->voicecalc();
if(perc_ok && perc>0 && percmultiplier>0) // retrigger percussion
{
perc_phase=1;
perc_vca=0;
}
}
else
status=VS_IDLE;
}
}
}
if(vca_phase==VP_A && click>0)
{
static unsigned long randSeed = 22222, maxrand=(unsigned long)-1;
float rand=0;
float mattack=0;
if(mattack<1)
mattack=VCA*8;
if(mattack>1)
mattack=1;
randSeed = (randSeed * 196314165) + 907633515;
rand=(float)randSeed/0xffffffff;
clicklp.clock(click*rand*.3f);
noise=clicklp.bp();
noise*=clickvol;
output=mattack*(2-VCA)*(output+noise);
VCA+=clickattack;
}
if(perc_ok && perc_phase>0)
{
s0=s0-a*s1; // calculate sine wave
s1=s1+a*s0; //
output+=perc*s0*perc_vca*perc_vca;
samplecount2++;
if(samplecount2>5)
{
samplecount2=0;
if(perc_phase==1) // percussion attack
{
perc_vca+=adsr_attack;
if(perc_vca>=1)
perc_phase=2; // switch to percussion decay
}
else if(perc_phase==2) // percussion decay
{
perc_vca-=perc_decay;
if(perc_vca<=0)
{
perc_vca=0;
perc_phase=0; // percussion finished
}
}
else if(perc_phase==3) // percussion fast release
{
perc_vca-=adsr_fast_release;
if(perc_vca<=0)
{
perc_vca=0;
perc_phase=0; // percussion finished
}
}
}
}
output=DENORMALIZE(output);
return(output);
}
long voice::get_note()
{
return(actual_note);
}
bool voice::check_note(long note)
{
if(note==actual_note || note==next_note)
return(true);
else
return(false);
}
bool voice::get_active()
{
if(status==VS_IDLE)
return(false);
else
return(true);
}
void voice::note_on(long note, long velocity, float *table, int size, float pitch, bool percenable, float sclick, float sust)
{
my_table=table;
my_size=size;
click= 0; //sclick; // kill the click
perc_ok=percenable;
sustain=sust;
if(note>=0 && note<128)
{
if(click>0)
{
float clickfreq=(freqtab[note]+70)*16;
if(clickfreq>5000)
clickfreq=5000;
//clicklp.setparam(3000+clickfreq*.3f,.1f,samplerate);
clicklp.setparam(clickfreq,.1f,samplerate);
clickvol=note*note*.0008f;
}
//if(actual_note>=0) // fast retrigger
//{
// next_note=note;
// vca_phase=VP_FR;
// perc_phase=3;
//}
//else // normal note on
{
VCA=0;
phase=0;
phaseinc=0;
vca_phase=VP_A;
actual_note=note&0x7f;
perc_phase=0;
this->voicecalc();
perc_phase=1;
}
status=VS_PLAYING;
}
else
note_off(-1);
}
void voice::note_off(long note)
{
if(note==actual_note && next_note>=0)
{
vca_phase=VP_FR;
return;
}
if(!pedal)
vca_phase=VP_R;
else
status=VS_WAIT_PUP;
}
void voice::force_off()
{
next_note=-1;
vca_phase=VP_FR;
}
void voice::set_pedal(bool pedal)
{
if(pedal!=this->pedal)
{
if(!pedal)
if(status==VS_WAIT_PUP)
vca_phase=VP_R;
this->pedal=pedal;
}
}
void voice::set_percussion(float percussion,float perc_multiplier,float percfade)
{
if(percussion>=0)
perc=percussion*2;
if(perc_multiplier>=0)
percmultiplier=perc_multiplier;
if(percfade>=0)
perc_fade=1-percfade+.5f;
}
void voice::set_pitch(float pitch)
{
this->pitch=pitch;
this->voicecalc();
}
notemaster::notemaster(int number)
{
my_samplerate=44100;
pitch=next_pitch=1;
my_percussion=-1;
if(number<1)
number=1;
if(number>MAXVOICES)
number=MAXVOICES;
for(x=0;x<=MAXVOICES;x++)
voices[x]=NULL;
for(x=0;x<=number;x++)
{
voices[x] = new voice();
if(voices[x]!=NULL)
{
age[x]=0;
chan[x]=15;
voices[x]->reset();
voices[x]->set_samplerate(my_samplerate);
volume[x]=1;
}
}
numofvoices=number;
}
notemaster::~notemaster()
{
for(x=0;x<MAXVOICES;x++)
if(voices[x]!=NULL)
voices[x]->~voice();
}
void notemaster::set_numofvoices(int number)
{
// we create one additional voice for channel 2 (bass pedal)
if(number<1)
number=1;
if(number>MAXVOICES)
number=MAXVOICES;
for(x=0;x<=MAXVOICES;x++)
{
if(voices[x]!=NULL)
{
voices[x]->~voice();
}
voices[x]=NULL;
age[x]=0;
}
for(x=0;x<=number;x++)
{
voices[x] = new voice();
if(voices[x]!=NULL)
{
age[x]=0;
chan[x]=15;
voices[x]->reset();
voices[x]->set_samplerate(my_samplerate);
voices[x]->set_percussion(my_percussion,my_perc_multiplier,my_percfade);
volume[x]=1;
}
}
numofvoices=number;
}
void notemaster::note_on(long note, long velocity, float *table, int size1, int channel, bool percenable, float click, float sustain)
{
/*
The most interesting part here is the note priority and "stealing"
algorithm. We do it this way:
If a new note on event is received we look for an idle voice. If
there's none available we choose the oldest active voice and let it
perform a fast note off followed by a note on. This "fast retrigger"
is entirely calculated by the voice itself.
"Mono" mode is defined by numofvoices=1.
*/
int maxpos=0,newpos;
unsigned long maxage;
note-=12;
if(note<0)
return;
if(note>128)
return;
if(channel==2)
{
newpos=numofvoices; // reserved voice for bass pedal (channel 2)
}
else
{
maxage=0;
newpos=-1;
maxpos=0;
// calculate newpos - the voice number to produce the note
for(x=0;x<numofvoices;x++)
{
if(voices[x]==NULL)
continue;
// do we have an existing note?
// -> retrigger
if(voices[x]->get_active() && voices[x]->check_note(note) && chan[x]==(unsigned char)channel)
{
newpos=x;
age[x]=0;
}
if(voices[x]->get_active())
{
// age all active voices
age[x]++;
// let maxpos hold number of oldest voice
if(age[x]>maxage)
{
maxpos=x;
maxage=age[x];
}
}
else if(newpos== -1) // voice is not active. If we don't have
// to retrigger - this is our voice!
newpos=x;
}
if(newpos== -1) // no free voice and nothing to retrigger
newpos=maxpos; // -> choose oldest voice
}
// let the voice play the note. Fast retrigger is handled by the voice.
voices[newpos]->note_on(note,velocity,table,size1,pitch,percenable,click,sustain);
age[newpos]=0;
if(channel>0 && channel<3)
chan[newpos]=(unsigned char)channel;
else
chan[newpos]=0;
}
void notemaster::note_off(long note, int channel)
{
note-=12;
if(note<0)
return;
if(channel==2)
{
if(voices[numofvoices]->check_note(note))
voices[numofvoices]->note_off(note);
}
else
{
for(x=0;x<numofvoices;x++)
{
if(chan[x]==(unsigned char)channel && voices[x]->check_note(note))
{
voices[x]->note_off(note);
age[x]=0;
}
}
}
}
float *notemaster::clock()
{
output[0]=output[1]=output[2]=0;
for(x=0;x<=numofvoices;x++)
if(chan[x]<3)
output[chan[x]]+=volume[chan[x]]*voices[x]->clock();
// output[0]=DENORMALIZE(output[0]);
// output[1]=DENORMALIZE(output[1]);
return(output);
}
void notemaster::all_notes_off()
{
pitch=next_pitch=1;
for(x=0;x<=numofvoices;x++)
{
voices[x]->force_off();
age[x]=0;
}
}
void notemaster::set_pedal(int pedal, int channel)
{
bool my_pedal;
if(pedal<64)
my_pedal=false;
else
my_pedal=true;
for(x=0;x<=numofvoices;x++)
voices[x]->set_pedal(my_pedal);
}
void notemaster::set_samplerate(float samplerate)
{
my_samplerate=samplerate;
for(x=0;x<=numofvoices;x++)
voices[x]->set_samplerate(samplerate);
}
void notemaster::set_percussion(float percussion,float perc_multiplier,float percfade)
{
my_percussion=percussion; // memorize percussion values
my_perc_multiplier=perc_multiplier;
my_percfade=percfade;
for(x=0;x<numofvoices;x++)
voices[x]->set_percussion(percussion,perc_multiplier,percfade);
voices[numofvoices]->set_percussion(percussion*.3f,perc_multiplier,percfade);
}
void notemaster::set_pitch(float pitch, int channel)
{
this->pitch=pitch;
for(x=0;x<=numofvoices;x++)
{
if(chan[x]==channel)
voices[x]->set_pitch(pitch);
}
}
void notemaster::set_volume(float vol, int channel)
{
volume[channel]=vol;
}
void notemaster::reset()
{
for(x=0;x<=numofvoices;x++)
voices[x]->reset();
}
void notemaster::suspend()
{
for(x=0;x<=numofvoices;x++)
voices[x]->suspend();
}
void notemaster::resume()
{
for(x=0;x<=numofvoices;x++)
voices[x]->resume();
}