-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTimbre.cpp
3157 lines (2579 loc) · 117 KB
/
Timbre.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013 Xavier Hosxe
*
* Author: Xavier Hosxe (xavier <.> hosxe < a t > gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "Timbre.h"
#include "Voice.h"
#define INV127 .00787401574803149606f
#define INV16 .0625
#define INV_BLOCK_SIZE (1.0f / BLOCK_SIZE)
// Regular memory
float midiNoteScale[2][NUMBER_OF_TIMBRES][128] __attribute__((section(".ram_d1")));
float Timbre::unisonPhase[14] = { .37f, .11f, .495f, .53f, .03f, .19f, .89f, 0.23f, .71f, .19f, .31f, .43f, .59f, .97f };
float Timbre::delayBuffer[NUMBER_OF_TIMBRES][delayBufferSize] __attribute__ ((section(".ram_d2b")));
#define CALLED_PER_SECOND (PREENFM_FREQUENCY / 32.0f)
// Static to all 6 instrument
uint32_t Timbre::voiceIndex_;
enum ArpeggiatorDirection {
ARPEGGIO_DIRECTION_UP = 0,
ARPEGGIO_DIRECTION_DOWN,
ARPEGGIO_DIRECTION_UP_DOWN,
ARPEGGIO_DIRECTION_PLAYED,
ARPEGGIO_DIRECTION_RANDOM,
ARPEGGIO_DIRECTION_CHORD,
/*
* ROTATE modes rotate the first note played, e.g. UP: C-E-G -> E-G-C -> G-C-E -> repeat
*/
ARPEGGIO_DIRECTION_ROTATE_UP, ARPEGGIO_DIRECTION_ROTATE_DOWN, ARPEGGIO_DIRECTION_ROTATE_UP_DOWN,
/*
* SHIFT modes rotate and extend with transpose, e.g. UP: C-E-G -> E-G-C1 -> G-C1-E1 -> repeat
*/
ARPEGGIO_DIRECTION_SHIFT_UP, ARPEGGIO_DIRECTION_SHIFT_DOWN, ARPEGGIO_DIRECTION_SHIFT_UP_DOWN,
ARPEGGIO_DIRECTION_COUNT
};
// TODO Maybe add something like struct ArpDirectionParams { dir, can_change, use_start_step }
inline static int __getDirection( int _direction ) {
switch( _direction ) {
case ARPEGGIO_DIRECTION_DOWN:
case ARPEGGIO_DIRECTION_ROTATE_DOWN:
case ARPEGGIO_DIRECTION_SHIFT_DOWN:
return -1;
default:
return 1;
}
}
inline static int __canChangeDir( int _direction ) {
switch( _direction ) {
case ARPEGGIO_DIRECTION_UP_DOWN:
case ARPEGGIO_DIRECTION_ROTATE_UP_DOWN:
case ARPEGGIO_DIRECTION_SHIFT_UP_DOWN:
return 1;
default:
return 0;
}
}
inline static int __canTranspose( int _direction ) {
switch( _direction ) {
case ARPEGGIO_DIRECTION_SHIFT_UP:
case ARPEGGIO_DIRECTION_SHIFT_DOWN:
case ARPEGGIO_DIRECTION_SHIFT_UP_DOWN:
return 1;
default:
return 0;
}
}
enum NewNoteType {
NEW_NOTE_FREE = 0,
NEW_NOTE_RELEASE,
NEW_NOTE_OLD,
NEW_NOTE_NONE
};
arp_pattern_t lut_res_arpeggiator_patterns[ ARPEGGIATOR_PRESET_PATTERN_COUNT ] = {
ARP_PATTERN(21845), ARP_PATTERN(62965), ARP_PATTERN(46517), ARP_PATTERN(54741),
ARP_PATTERN(43861), ARP_PATTERN(22869), ARP_PATTERN(38293), ARP_PATTERN(2313),
ARP_PATTERN(37449), ARP_PATTERN(21065), ARP_PATTERN(18761), ARP_PATTERN(54553),
ARP_PATTERN(27499), ARP_PATTERN(23387), ARP_PATTERN(30583), ARP_PATTERN(28087),
ARP_PATTERN(22359), ARP_PATTERN(28527), ARP_PATTERN(30431), ARP_PATTERN(43281),
ARP_PATTERN(28609), ARP_PATTERN(53505)
};
uint16_t Timbre::getArpeggiatorPattern() const
{
const int pattern = (int)params_.engineArp2.pattern;
if ( pattern < ARPEGGIATOR_PRESET_PATTERN_COUNT )
return ARP_PATTERN_GETMASK(lut_res_arpeggiator_patterns[ pattern ]);
else
return ARP_PATTERN_GETMASK( params_.engineArpUserPatterns.patterns[ pattern - ARPEGGIATOR_PRESET_PATTERN_COUNT ] );
}
const uint8_t midi_clock_tick_per_step[17] = {
192, 144, 96, 72, 64, 48, 36, 32, 24, 16, 12, 8, 6, 4, 3, 2, 1
};
extern float noise[32];
float panTable[] = {
0.0000, 0.0007, 0.0020, 0.0036, 0.0055, 0.0077, 0.0101, 0.0128, 0.0156, 0.0186,
0.0218, 0.0252, 0.0287, 0.0324, 0.0362, 0.0401, 0.0442, 0.0484, 0.0527, 0.0572,
0.0618, 0.0665, 0.0713, 0.0762, 0.0812, 0.0863, 0.0915, 0.0969, 0.1023, 0.1078,
0.1135, 0.1192, 0.1250, 0.1309, 0.1369, 0.1430, 0.1492, 0.1554, 0.1618, 0.1682,
0.1747, 0.1813, 0.1880, 0.1947, 0.2015, 0.2085, 0.2154, 0.2225, 0.2296, 0.2369,
0.2441, 0.2515, 0.2589, 0.2664, 0.2740, 0.2817, 0.2894, 0.2972, 0.3050, 0.3129,
0.3209, 0.3290, 0.3371, 0.3453, 0.3536, 0.3619, 0.3703, 0.3787, 0.3872, 0.3958,
0.4044, 0.4131, 0.4219, 0.4307, 0.4396, 0.4485, 0.4575, 0.4666, 0.4757, 0.4849,
0.4941, 0.5034, 0.5128, 0.5222, 0.5316, 0.5411, 0.5507, 0.5604, 0.5700, 0.5798,
0.5896, 0.5994, 0.6093, 0.6193, 0.6293, 0.6394, 0.6495, 0.6597, 0.6699, 0.6802,
0.6905, 0.7009, 0.7114, 0.7218, 0.7324, 0.7430, 0.7536, 0.7643, 0.7750, 0.7858,
0.7967, 0.8076, 0.8185, 0.8295, 0.8405, 0.8516, 0.8627, 0.8739, 0.8851, 0.8964,
0.9077, 0.9191, 0.9305, 0.9420, 0.9535, 0.9651, 0.9767, 0.9883, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000
} ;
inline
float modulo2(float readPos, int bufferLen) {
return unlikely(readPos < 0) ? readPos + bufferLen: readPos;
}
inline
float modulo(float d, float max) {
return unlikely(d >= max) ? d - max : d;
}
inline
float clamp(float d, float min, float max) {
const float t = unlikely(d < min) ? min : d;
return unlikely(t > max) ? max : t;
}
inline
float sqrt3(const float x)
{
union
{
int i;
float x;
} u;
u.x = x;
u.i = (1 << 29) + (u.i >> 1) - (1 << 22);
return u.x;
}
inline
float foldAbs(float x) {
x *= 0.5f;
float f = (fabsf(x - roundf(x)));
return f + f;
}
inline
float fold(float x4) {
return fabsf(x4 + 0.25f - roundf(x4 + 0.25f)) - 0.25f;
}
inline
float sigmoid(float x)
{
return x * (1.5f - 0.5f * x * x);
}
inline
float sigmoidPos(float x)
{
//x : 0 -> 1
return (sigmoid((x * 2) - 1) + 1) * 0.5f;
}
inline
float tanh4(float x)
{
return x / sqrt3(x * x + 1);
}
inline
float fastSin(float x) {
return 3.9961f * x * ( 1 - fabsf(x) );
}
inline
float hann(float x) {
float s = sqrt3(x * (1 - x));
return s + s;
}
// all pass params
const float f1 = 0.0156f;
const float apcoef1 = (1.0f - f1) / (1.0f + f1);
const float f2 = (0.17f + f1);
const float apcoef2 = (1.0f - f2) / (1.0f + f2);
const float f3 = (0.17f + f2);
const float apcoef3 = (1.0f - f3) / (1.0f + f3);
const float f4 = (0.17f + f3);
const float apcoef4 = (1.0f - f4) / (1.0f + f4);
// delay sizes
const float delayBufferSizeF = delayBufferSize;
const float delayBufferSize90 = delayBufferSize * 0.25f;
const float delayBufferSize180 = delayBufferSize * 0.5f;
const int delayBufferSizeM1 = delayBufferSize - 1;
const int delayBufferSizeM4 = delayBufferSize - 4;
const float delayBufferSizeInv = 1.0f / delayBufferSize;
const int delayBufStereoSize = delayBufferSize * 0.5f;
const int delayBufStereoSizeM1 = delayBufStereoSize - 1;
const float delayBufStereoDiv4 = delayBufStereoSize * 0.25f;
const float delayBufStereoSizeInv = 1.0f / delayBufStereoSize;
Timbre::Timbre() {
recomputeNext_ = true;
currentGate_ = 0;
sbMax_ = &sampleBlock_[64];
holdPedal_ = false;
lastPlayedNote_ = 0;
// arpegiator
setNewBPMValue(90);
arpegiatorStep_ = 0.0;
idle_ticks_ = 96;
running_ = 0;
ignore_note_off_messages_ = 0;
recording_ = 0;
note_stack_.Init();
event_scheduler_.Init();
// Arpeggiator start
Start();
}
Timbre::~Timbre() {
}
void Timbre::init(SynthState *synthState, int timbreNumber) {
mixerState_ = &synthState->mixerState;
env1_.init(¶ms_.env1Time, ¶ms_.env1Level, 0, ¶ms_.engine1.algo, ¶ms_.env1Curve);
env2_.init(¶ms_.env2Time, ¶ms_.env2Level, 1, ¶ms_.engine1.algo, ¶ms_.env2Curve);
env3_.init(¶ms_.env3Time, ¶ms_.env3Level, 2, ¶ms_.engine1.algo, ¶ms_.env3Curve);
env4_.init(¶ms_.env4Time, ¶ms_.env4Level, 3, ¶ms_.engine1.algo, ¶ms_.env4Curve);
env5_.init(¶ms_.env5Time, ¶ms_.env5Level, 4, ¶ms_.engine1.algo, ¶ms_.env5Curve);
env6_.init(¶ms_.env6Time, ¶ms_.env6Level, 5, ¶ms_.engine1.algo, ¶ms_.env6Curve);
osc1_.init(synthState, ¶ms_.osc1, OSC1_FREQ);
osc2_.init(synthState, ¶ms_.osc2, OSC2_FREQ);
osc3_.init(synthState, ¶ms_.osc3, OSC3_FREQ);
osc4_.init(synthState, ¶ms_.osc4, OSC4_FREQ);
osc5_.init(synthState, ¶ms_.osc5, OSC5_FREQ);
osc6_.init(synthState, ¶ms_.osc6, OSC6_FREQ);
timbreNumber_ = timbreNumber;
for (int s = 0; s < 2; s++) {
for (int n = 0; n < 128; n++) {
midiNoteScale[s][timbreNumber][n] = INV127 * (float) n;
}
}
for (int lfo = 0; lfo < NUMBER_OF_LFO; lfo++) {
lfoUSed_[lfo] = 0;
}
lowerNote_ = 64;
lowerNoteReleased_ = true;
/** --------------FX init-------------- */
delayBuffer_ = delayBuffer[timbreNumber_];
for (int s = 0; s < delayBufferSize; s++) {
delayBuffer_[s] = 0;
}
}
void Timbre::setVoiceNumber(int v, int n) {
voiceNumber_[v] = n;
if (n >= 0) {
voices_[n]->setCurrentTimbre(this);
}
}
void Timbre::initVoicePointer(int n, Voice *voice) {
voices_[n] = voice;
}
void Timbre::noteOn(char note, char velocity) {
if (params_.engineArp1.clock) {
arpeggiatorNoteOn(note, velocity);
} else {
preenNoteOn(note, velocity);
}
}
void Timbre::noteOff(char note) {
if (params_.engineArp1.clock) {
arpeggiatorNoteOff(note);
} else {
preenNoteOff(note);
}
}
void Timbre::noteOnMPE(uint8_t channel, uint8_t note, uint8_t velocity) {
int voiceToUse = voiceNumber_[channel -1];
if (unlikely(voiceToUse == -1)) {
return;
}
preenNoteOnUpdateMatrix(voiceToUse, note, velocity);
float noteFrequency = mixerState_->instrumentState_[0].scaleFrequencies[(int) note];
if (voices_[voiceToUse]->isPlaying()) {
voices_[voiceToUse]->noteOnWithoutPop(note, noteFrequency, velocity, voiceIndex_++);
} else {
voices_[voiceToUse]->noteOn(note, noteFrequency, velocity, voiceIndex_++);
}
}
void Timbre::noteOffMPE(uint8_t channel, uint8_t note, uint8_t velocityOff) {
int voiceToUse = voiceNumber_[channel -1];
if (unlikely(voiceToUse == -1)) {
return;
}
if (voices_[voiceToUse]->isPlaying()) {
voices_[voiceToUse]->noteOff();
}
}
void Timbre::preenNoteOn(char note, char velocity) {
// NumberOfVoice = 0 or no mapping in scala frequencies
if (unlikely(numberOfVoices_ == 0 || mixerState_->instrumentState_[timbreNumber_].scaleFrequencies[(int) note] == 0.0f)) {
return;
}
note &= 0x7f;
int iNov = params_.engine1.playMode == PLAY_MODE_POLY ? (int) numberOfVoices_ : 1;
// Frequency depends on the current instrument scale
float noteFrequency = mixerState_->instrumentState_[timbreNumber_].scaleFrequencies[(int) note];
uint32_t indexMin = UINT32_MAX;
int voiceToUse = -1;
int newNoteType = NEW_NOTE_NONE;
for (int k = 0; k < iNov; k++) {
// voice number k of timbre
int n = voiceNumber_[k];
if (unlikely(voices_[n]->isNewNotePending())) {
continue;
}
// same note = priority 1 : take the voice immediatly
if (unlikely(voices_[n]->isPlaying() && voices_[n]->getNote() == note)) {
if (likely(params_.engine1.playMode != PLAY_MODE_UNISON)) {
preenNoteOnUpdateMatrix(n, note, velocity);
voices_[n]->noteOnWithoutPop(note, noteFrequency, velocity, voiceIndex_++);
} else {
// Unison !!
float noteFrequencyUnison = noteFrequency + noteFrequency * params_.engine2.unisonDetune * .05f;
float noteFrequencyUnisonInc = noteFrequency * params_.engine2.unisonDetune * numberOfVoiceInverse_ * .1f;
for (int k = 0; k < numberOfVoices_; k++) {
int n = voiceNumber_[k];
preenNoteOnUpdateMatrix(n, note, velocity);
voices_[n]->noteOnWithoutPop(note, noteFrequencyUnison, velocity, voiceIndex_++, unisonPhase[k]);
noteFrequencyUnison += noteFrequencyUnisonInc;
}
}
lastPlayedNote_ = n;
// Just in case we tune Osc Freq/Ftune while playing the same note
lowerNoteFrequency = voices_[n]->getNoteRealFrequencyEstimation(noteFrequency);
return;
}
// unlikely because if it true, CPU is not full
if (unlikely(newNoteType > NEW_NOTE_FREE)) {
if (!voices_[n]->isPlaying()) {
voiceToUse = n;
newNoteType = NEW_NOTE_FREE;
} else if (voices_[n]->isReleased()) {
uint32_t indexVoice = voices_[n]->getIndex();
if (indexVoice < indexMin) {
indexMin = indexVoice;
voiceToUse = n;
newNoteType = NEW_NOTE_RELEASE;
}
}
}
}
if (voiceToUse == -1) {
for (int k = 0; k < iNov; k++) {
// voice number k of timbre
int n = voiceNumber_[k];
unsigned int indexVoice = voices_[n]->getIndex();
if (indexVoice < indexMin && !voices_[n]->isNewNotePending()) {
newNoteType = NEW_NOTE_OLD;
indexMin = indexVoice;
voiceToUse = n;
}
}
}
// All voices in newnotepending state ?
if (voiceToUse != -1) {
if (likely(params_.engine1.playMode != PLAY_MODE_UNISON)) {
preenNoteOnUpdateMatrix(voiceToUse, note, velocity);
switch (newNoteType) {
case NEW_NOTE_FREE:
voices_[voiceToUse]->noteOn(note, noteFrequency, velocity, voiceIndex_++);
break;
case NEW_NOTE_OLD:
case NEW_NOTE_RELEASE:
voices_[voiceToUse]->noteOnWithoutPop(note, noteFrequency, velocity, voiceIndex_++);
break;
}
} else {
// Unisons : we start all voices with different frequency
float noteFrequencyUnison = noteFrequency + noteFrequency * params_.engine2.unisonDetune * .05f;
float noteFrequencyUnisonInc = noteFrequency * params_.engine2.unisonDetune * numberOfVoiceInverse_ * .1f;
for (int k = 0; k < numberOfVoices_; k++) {
int n = voiceNumber_[k];
preenNoteOnUpdateMatrix(n, note, velocity);
switch (newNoteType) {
case NEW_NOTE_FREE:
voices_[n]->noteOn(note, noteFrequencyUnison, velocity, voiceIndex_++, unisonPhase[k]);
break;
case NEW_NOTE_OLD:
case NEW_NOTE_RELEASE:
voices_[n]->noteOnWithoutPop(note, noteFrequencyUnison, velocity, voiceIndex_++, unisonPhase[k]);
break;
}
noteFrequencyUnison += noteFrequencyUnisonInc;
}
}
lastPlayedNote_ = voiceToUse;
if (note <= lowerNote_ || lowerNoteReleased_ || iNov == 1) {
lowerNote_ = note;
lowerNoteReleased_ = false;
lowerNoteFrequency = voices_[voiceToUse]->getNoteRealFrequencyEstimation(noteFrequency);
}
}
}
void Timbre::preenNoteOnUpdateMatrix(int voiceToUse, int note, int velocity) {
// Update voice matrix with midi note and velocity
voices_[voiceToUse]->matrix.setSource(MATRIX_SOURCE_NOTE1, midiNoteScale[0][timbreNumber_][note]);
voices_[voiceToUse]->matrix.setSource(MATRIX_SOURCE_NOTE2, midiNoteScale[1][timbreNumber_][note]);
voices_[voiceToUse]->matrix.setSource(MATRIX_SOURCE_VELOCITY, INV127 * velocity);
voices_[voiceToUse]->matrix.setSource(MATRIX_SOURCE_RANDOM, noise[voiceToUse]);
}
void Timbre::preenNoteOff(char note) {
bool isUnison = params_.engine1.playMode == PLAY_MODE_UNISON;
if (note == lowerNote_) {
lowerNoteReleased_ = true;
}
// Unison we must check all voices.... (different from noteOn)
int iNov = params_.engine1.playMode == PLAY_MODE_MONO ? 1 : (int) numberOfVoices_;
for (int k = 0; k < iNov; k++) {
// voice number k of timbre
int n = voiceNumber_[k];
// Not playing = free CPU
if (unlikely(!voices_[n]->isPlaying())) {
continue;
}
if (likely(voices_[n]->getNextGlidingNote() == 0 || voices_[n]->isNewGlide())) {
if (voices_[n]->getNote() == note) {
if (unlikely(holdPedal_)) {
voices_[n]->setHoldedByPedal(true);
if (likely(!isUnison)) {
return;
}
} else {
voices_[n]->noteOff();
if (likely(!isUnison)) {
return;
}
}
}
} else {
// if gliding and releasing first note
if (voices_[n]->getNote() == note) {
voices_[n]->glideFirstNoteOff();
if (likely(!isUnison)) {
return;
}
}
// if gliding and releasing next note
if (voices_[n]->getNextGlidingNote() == note) {
voices_[n]->glideToNote(voices_[n]->getNote(), voices_[n]->getNoteFrequency());
voices_[n]->glideFirstNoteOff();
// Sync Osccilo
lowerNoteFrequency = voices_[n]->getNoteRealFrequencyEstimation(voices_[n]->getNoteFrequency());
if (likely(!isUnison)) {
return;
}
}
}
}
}
void Timbre::stopPlayingNow() {
for (int k = 0; k < numberOfVoices_; k++) {
// voice number k of timbre
int n = voiceNumber_[k];
if (n != -1) {
voices_[n]->killNow();
}
}
}
void Timbre::setHoldPedal(int value) {
if (value < 64) {
holdPedal_ = false;
int nVoices = numberOfVoices_;
for (int k = 0; k < nVoices; k++) {
// voice number k of timbre
int n = voiceNumber_[k];
if (voices_[n]->isHoldedByPedal()) {
voices_[n]->noteOff();
}
}
arpeggiatorSetHoldPedal(0);
} else {
holdPedal_ = true;
arpeggiatorSetHoldPedal(127);
}
}
void Timbre::setNewBPMValue(float bpm) {
ticksPerSecond_ = bpm * 24.0f / 60.0f;
ticksEveryNCalls_ = CALLED_PER_SECOND / ticksPerSecond_;
ticksEveyNCallsInteger_ = (int) ticksEveryNCalls_;
}
void Timbre::setArpeggiatorClock(float clockValue) {
if (clockValue == CLOCK_OFF) {
FlushQueue();
note_stack_.Clear();
}
if (clockValue == CLOCK_INTERNAL) {
setNewBPMValue(params_.engineArp1.BPM);
}
if (clockValue == CLOCK_EXTERNAL) {
// Let's consider we're running
running_ = 1;
}
}
void Timbre::updateArpegiatorInternalClock() {
// Apeggiator clock : internal
if (params_.engineArp1.clock == CLOCK_INTERNAL) {
arpegiatorStep_ += 1.0f;
if (unlikely((arpegiatorStep_) > ticksEveryNCalls_)) {
arpegiatorStep_ -= ticksEveyNCallsInteger_;
Tick();
}
}
}
void Timbre::cleanNextBlock() {
float *sp = sampleBlock_;
while (sp < sbMax_) {
*sp++ = 0;
*sp++ = 0;
*sp++ = 0;
*sp++ = 0;
*sp++ = 0;
*sp++ = 0;
*sp++ = 0;
*sp++ = 0;
}
}
void Timbre::prepareMatrixForNewBlock() {
for (int k = 0; k < numberOfVoices_; k++) {
voices_[voiceNumber_[k]]->prepareMatrixForNewBlock();
}
}
void Timbre::glide() {
if (unlikely(params_.engine1.playMode == PLAY_MODE_UNISON)) {
for (int vv = 0; vv < numberOfVoices_; vv++) {
int v = voiceNumber_[vv];
if (v != -1 && voices_[v]->isGliding()) {
voices_[v]->glide();
}
}
} else {
if (voiceNumber_[0] != -1 && voices_[voiceNumber_[0]]->isGliding()) {
voices_[voiceNumber_[0]]->glide();
}
}
}
uint8_t Timbre::voicesNextBlock() {
uint8_t numberOfPlayingVoices_ = 0;
if (unlikely(params_.engine1.playMode == PLAY_MODE_UNISON)) {
cleanNextBlock();
// UNISON
float pansSav[6];
pansSav[0] = params_.engineMix1.panOsc1;
pansSav[1] = params_.engineMix1.panOsc2;
pansSav[2] = params_.engineMix2.panOsc3;
pansSav[3] = params_.engineMix2.panOsc4;
pansSav[4] = params_.engineMix3.panOsc5;
pansSav[5] = params_.engineMix3.panOsc6;
float* pans[] = {
¶ms_.engineMix1.panOsc1,
¶ms_.engineMix1.panOsc2,
¶ms_.engineMix2.panOsc3,
¶ms_.engineMix2.panOsc4,
¶ms_.engineMix3.panOsc5,
¶ms_.engineMix3.panOsc6
};
int currentAlgo = (int)params_.engine1.algo;
float algoNumberOfMix = algoInformation[currentAlgo].mix;
float numberOfCarrierOp = numberOfVoices_ * algoNumberOfMix;
float opPan = - params_.engine2.unisonSpread;
float opPanInc = 2.0f / numberOfCarrierOp * params_.engine2.unisonSpread;
if (likely(voices_[voiceNumber_[0]]->isPlaying())) {
for (int vv = 0; vv < numberOfVoices_; vv++) {
int v = voiceNumber_[vv];
if (unlikely(v < 0)) {
continue;
}
bool otherSide = false;
for (int op = 0; op < 6; op ++) {
if (algoOpInformation[currentAlgo][op] == 1) {
if (otherSide) {
*pans[op] = -opPan;
} else {
*pans[op] = opPan;
}
opPan += opPanInc;
otherSide =! otherSide;
}
}
voices_[v]->nextBlock();
if (vv > 0) {
// We accumulate in the first voice buffer
float *source = voices_[v]->getSampleBlock();
float *voice0SampleBlock = voices_[voiceNumber_[0]]->getSampleBlock();
for (int s = 0; s < BLOCK_SIZE; s++) {
*voice0SampleBlock++ += *source++;
*voice0SampleBlock++ += *source++;
}
}
numberOfPlayingVoices_++;
}
voices_[voiceNumber_[0]]->fxAfterBlock();
} else {
voices_[voiceNumber_[0]]->emptyBuffer();
}
params_.engineMix1.panOsc1 = pansSav[0];
params_.engineMix1.panOsc2 = pansSav[1];
params_.engineMix2.panOsc3 = pansSav[2];
params_.engineMix2.panOsc4 = pansSav[3];
params_.engineMix3.panOsc5 = pansSav[4];
params_.engineMix3.panOsc6 = pansSav[5];
} else {
for (int k = 0; k < numberOfVoices_; k++) {
int v = voiceNumber_[k];
if (likely(voices_[v]->isPlaying())) {
voices_[v]->nextBlock();
voices_[v]->fxAfterBlock();
numberOfPlayingVoices_++;
} else {
voices_[v]->emptyBuffer();
}
}
}
return numberOfPlayingVoices_;
}
void Timbre::fxAfterBlock() {
int fx2Type = params_.effect2.type;
if(!voices_[lastPlayedNote_]->isPlaying()) {
// hack : this voice is not playing but still need to calculate lfo
voices_[lastPlayedNote_]->matrix.computeAllDestinations();
}
float matrixFilterFrequency = voices_[lastPlayedNote_]->matrix.getDestination(FILTER2_PARAM1);
float matrixFilterParam2 = voices_[lastPlayedNote_]->matrix.getDestination(FILTER2_PARAM2);
float matrixFilterAmp = voices_[lastPlayedNote_]->matrix.getDestination(FILTER2_AMP);
float matrixFilterPan = clamp( voices_[lastPlayedNote_]->matrix.getDestination(ALL_PAN), -1, 1);
float gainTmp = clamp(this->params_.effect2.param3 + matrixFilterAmp, 0, 16);
if(prevFx2Type != fx2Type) {
//anti click on fx change
mixerGain_ = 0;
feedbackInput = 0;
feedback = 0;
for (int s = 0; s < delayBufferSize; s++) {
delayBuffer_[s] = 0;
}
}
prevFx2Type = fx2Type;
switch (fx2Type) {
case FILTER2_FLANGE: {
mixerGain_ = 0.02f * gainTmp + .98f * mixerGain_;
float mixerGain_01 = clamp(mixerGain_, 0, 1);
int mixerGain255 = mixerGain_01 * 255;
float dry = panTable[255 - mixerGain255];
float wet = panTable[mixerGain255] * 0.5f;
float extraAmp = clamp(mixerGain_ - 1, 0, 1);
wet += extraAmp;
param1S = 0.02f * this->params_.effect2.param1 + .98f * param1S;
float speed = clamp(param1S * param1S * param1S, 0, 1);
float lfo1Inc = clamp(hb8_y1 * speed * 0.015f, -1, 1);
hb8_x1 += lfo1Inc;
if(hb8_x1 >= 1) {
hb8_x1 = 1;
hb8_y1 = -1;
}
if(hb8_x1 <= 0) {
hb8_x1 = 0;
hb8_y1 = 1;
}
float fxParamTmp = foldAbs(hb8_x1 + matrixFilterFrequency) * 0.5f;
delayReadFrac = (fxParamTmp + 99 * delayReadFrac) * 0.01f; // smooth change
float currentDelaySize1 = delaySize1;
delaySize1 = clamp(delayBufStereoDiv4 * delayReadFrac, 0, delayBufStereoSizeM1);
float delaySizeInc1 = (delaySize1 - currentDelaySize1) * INV_BLOCK_SIZE;
float currentFeedback = feedback;
feedback = clamp( sqrt3(fabsf(this->params_.effect2.param2 + matrixFilterParam2)), -1, 1) * 0.95f;
float feedbackInc = (feedback - currentFeedback) * INV_BLOCK_SIZE;
float *sp = sampleBlock_;
float delayReadPos90;
float filterB2 = 0.1f;
float filterB = (filterB2 * filterB2 * 0.5f);
float _in3_b1 = (1 - filterB);
float _in3_a0 = (1 + _in3_b1 * _in3_b1 * _in3_b1) * 0.5f;
const float f = 0.8f;
const float f2 = 0.85f;
const float fnotch = 1.03f;
for (int k = 0; k < BLOCK_SIZE; k++) {
low3 += f * band3;
band3 += f * (*sp - low3 - band3);
low4 += f * band4;
band4 += f * (*(sp + 1) - low4 - band4);
// feedback
float feedL = low5 * currentFeedback;
float feedR = low6 * currentFeedback;
// L
_ly1 = apcoef2 * (_ly1 + feedL) - _lx1; // allpass
_lx1 = feedL;
hb4_y1 = apcoef4 * (hb4_y1 + _ly1) - hb4_x1; // allpass
hb4_x1 = _ly1;
// R
_ly2 = apcoef2 * (_ly2 + feedR) - _lx2; // allpass
_lx2 = feedR;
hb4_y2 = apcoef4 * (hb4_y2 + _ly2) - hb4_x2; // allpass
hb4_x2 = _ly2;
// audio in hp
float hp_in_x0 = ((low3 + low3 - hb4_y1));
hp_in_y0 = _in3_a0 * (hp_in_x0 - hp_in_x1) + _in3_b1 * hp_in_y1;
hp_in_y1 = hp_in_y0;
hp_in_x1 = hp_in_x0;
float hp_in2_x0 = ((low4 + low4 - hb4_y2));
hp_in2_y0 = _in3_a0 * (hp_in2_x0 - hp_in2_x1) + _in3_b1 * hp_in2_y1;
hp_in2_y1 = hp_in2_y0;
hp_in2_x1 = hp_in2_x0;
delayWritePos = (delayWritePos + 1) & delayBufStereoSizeM1;
delayBuffer_[delayWritePos] = hp_in_y0;
delayBuffer_[delayWritePos + delayBufStereoSize] = hp_in2_y0;
delayReadPos = modulo2(delayWritePos - currentDelaySize1, delayBufStereoSize);
delayReadPos90 = modulo2(delayReadPos - 37.f, delayBufStereoSize);
low5 = delayInterpolation(delayReadPos, delayBuffer_, delayBufStereoSizeM1);
low6 = delayInterpolation2(delayReadPos90, delayBuffer_, delayBufStereoSizeM1, delayBufStereoSize);
low1 += f2 * band1;
band1 += f2 * (low5 - low1 - band1);
low2 += f2 * band2;
band2 += f2 * (low6 - low2 - band2);
// notch L
hb1_x1 += fnotch * hb1_y1;
float high7 = low1 - hb1_x1 - hb1_y1;
hb1_y1 += fnotch * high7;
float notchL = (high7 + hb1_x1);
// notch R
hb1_x2 += fnotch * hb1_y2;
float high8 = low2 - hb1_x2 - hb1_y2;
hb1_y2 += fnotch * high8;
float notchR = (high8 + hb1_x2);
*sp = *sp * dry + notchL * wet;
sp++;
*sp = *sp * dry + notchR * wet;
sp++;
currentDelaySize1 += delaySizeInc1;
currentFeedback += feedbackInc;
}
}
break;
case FILTER2_CHORUS: {
mixerGain_ = 0.02f * gainTmp + .98f * mixerGain_;
float mixerGain_01 = clamp(mixerGain_, 0, 1);
int mixerGain255 = mixerGain_01 * 255;
float dry = panTable[255 - mixerGain255];
float wet = panTable[mixerGain255] * 0.5f;
float extraAmp = clamp(mixerGain_ - 1, 0, 1);
wet += extraAmp;
float wetL = wet * (1 + matrixFilterPan);
float wetR = wet * (1 - matrixFilterPan);
param1S = 0.02f * this->params_.effect2.param1 + .98f * param1S;
float speed = clamp(param1S * param1S + matrixFilterFrequency, 0, 1);
float speedMod = speed * 4;
param2S = 0.01f * (this->params_.effect2.param2) + .99f * param2S;
float width = param2S * 0.6f;
float lfo1Inc = clamp(hb8_y1 * 0.0007f * speedMod, -1.f, 1.f);
hb8_x1 += lfo1Inc;
if(hb8_x1 >= 1) {
hb8_x1 = 1;
hb8_y1 = -1;
}
if(hb8_x1 <= 0) {
hb8_x1 = 0;
hb8_y1 = 1;
}
float lfo2Inc = clamp(hb8_y2 * 0.008f * speedMod, -1.f, 1.f);
hb8_x2 += lfo2Inc;
if(hb8_x2 >= 1) {
hb8_x2 = 1;
hb8_y2 = -1;
}
if(hb8_x2 <= 0) {
hb8_x2 = 0;
hb8_y2 = 1;
}
float lfo = (hb8_x1 * 0.3f + sigmoidPos(hb8_x2) * width) * 0.5f;
float fxParamTmp = (lfo + matrixFilterParam2);
delayReadFrac = (fxParamTmp + 99 * delayReadFrac) * 0.01f; // smooth change
float readPos1 = foldAbs(delayReadFrac);
float readPos2 = foldAbs(readPos1 + 0.3333f);
float readPos3 = foldAbs(readPos1 + 0.6666f);
float currentDelaySize1 = delaySize1;
float currentDelaySize2 = delaySize2;
float currentDelaySize3 = delaySize3;
float chorusSize = delayBufStereoSize; // 21ms
delaySize1 = 1 + chorusSize * readPos1;
delaySize2 = 1 + chorusSize * readPos2;
delaySize3 = 1 + chorusSize * readPos3;
float delaySizeInc1 = (delaySize1 - currentDelaySize1) * INV_BLOCK_SIZE;
float delaySizeInc2 = (delaySize2 - currentDelaySize2) * INV_BLOCK_SIZE;
float delaySizeInc3 = (delaySize3 - currentDelaySize3) * INV_BLOCK_SIZE;
float *sp = sampleBlock_;
float delReadPos1, delReadPos2, delReadPos3, monoIn;
float delayOut1 = 0, delayOut2 = 0, delayOut3 = 0;
const float f = 0.8f;
const float f2 = 0.87f;
// hi pass params
float filterB2 = 0.25f;
float filterB = (filterB2 * filterB2 * 0.5f);
float _in_b1 = (1 - filterB);
float _in_a0 = (1 + _in_b1 * _in_b1 * _in_b1) * 0.5f;
for (int k = 0; k < BLOCK_SIZE; k++) {
monoIn = (*sp + *(sp + 1)) * 0.5f;
// input lp
low3 += f * band3;
band3 += f * (monoIn - low3 - band3);
// audio in hp
float hp_in_x0 = low3;
hp_in_y0 = _in_a0 * (hp_in_x0 - hp_in_x1) + _in_b1 * hp_in_y1;
hp_in_y1 = hp_in_y0;
hp_in_x1 = hp_in_x0;
float bass = low3 - hp_in_y0;
delayWritePos = (delayWritePos + 1) & delayBufferSizeM1;
delayBuffer_[delayWritePos] = hp_in_y0;
delayWritePosF = (float) delayWritePos;
delReadPos1 = modulo2(delayWritePosF - currentDelaySize1, delayBufferSize);
delayOut1 = delayInterpolation(delReadPos1, delayBuffer_, delayBufferSizeM1);
delReadPos2 = modulo2(delayWritePosF - currentDelaySize2, delayBufferSize);
delayOut2 = delayInterpolation(delReadPos2, delayBuffer_, delayBufferSizeM1);
delReadPos3 = modulo2(delayWritePosF - currentDelaySize3, delayBufferSize);
delayOut3 = delayInterpolation(delReadPos3, delayBuffer_, delayBufferSizeM1);
float delaySumOut = bass + (delayOut1 - delayOut3 + delayOut2) ;
float delaySumOut2 = bass + (delayOut3 - delayOut1 + delayOut2) ;
low1 += f2 * band1;
band1 += f2 * (delaySumOut - low1 - band1);
low2 += f2 * band2;
band2 += f2 * (low1 - low2 - band2);
low5 += f2 * band5;
band5 += f2 * (delaySumOut2 - low5 - band5);
low6 += f2 * band6;