-
Notifications
You must be signed in to change notification settings - Fork 18
/
TSynth.ino
2748 lines (2529 loc) · 85.7 KB
/
TSynth.ino
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
/*
MIT License
Copyright (c) 2020-21 ElectroTechnique
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
ElectroTechnique TSynth - Firmware Rev 1.29
Includes code by:
Dave Benn - Handling MUXs, a few other bits and original inspiration https://www.notesandvolts.com/2019/01/teensy-synth-part-10-hardware.html
Alexander Davis / Vince R. Pearson - Stereo ensemble chorus effect https://github.com/quarterturn/teensy3-ensemble-chorus
Gustavo Silveira - Band limited wavetables https://forum.pjrc.com/threads/41905-Band-limited-Sawtooth-wavetable-C-generator-for-quot-Arbitrary-Waveform-quot-(and-its-use)
Holger Wirtz - Modified library integration and special thanks https://www.parasitstudio.de/
Arduino IDE
Tools Settings:
Board: "Teensy3.6"
USB Type: "Serial + MIDI + Audio"
CPU Speed: "180MHz"
Optimize: "Faster"
+++ Compiling with overclocking can lead to problems, test carefully +++
Performance Tests CPU Mem
180Mhz Faster 81.6 46
180Mhz Fastest 77.8 46
180Mhz Fastest+PC 79.0 46
180Mhz Fastest+LTO 76.7 46
240MHz Fastest+LTO 55.9 46
Additional libraries:
Agileware CircularBuffer, Adafruit_GFX (available in Arduino libraries manager)
*/
#include "Audio.h" //Using local version to override Teensyduino version
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <MIDI.h>
#include <USBHost_t36.h>
#include "MidiCC.h"
#include "AudioPatching.h"
#include "Constants.h"
#include "Parameters.h"
#include "PatchMgr.h"
#include "HWControls.h"
#include "EepromMgr.h"
#include "sawtoothWave.h"
#include "squareWave.h"
#include "Velocity.h"
#define PARAMETER 0 //The main page for displaying the current patch and control (parameter) changes
#define RECALL 1 //Patches list
#define SAVE 2 //Save patch page
#define REINITIALISE 3 // Reinitialise message
#define PATCH 4 // Show current patch bypassing PARAMETER
#define PATCHNAMING 5 // Patch naming page
#define DELETE 6 //Delete patch page
#define DELETEMSG 7 //Delete patch message page
#define SETTINGS 8 //Settings page
#define SETTINGSVALUE 9 //Settings page
unsigned int state = PARAMETER;
#define WAVEFORM_SAWTOOTH_WT 101
#define WAVEFORM_SQUARE_WT 102
#define WAVEFORM_PARABOLIC 103
#define WAVEFORM_HARMONIC 104
struct VoiceAndNote {
int note;
long timeOn;
boolean voiceOn;
};
struct VoiceAndNote voices[NO_OF_VOICES] = {
{ -1, 0, false},
{ -1, 0, false},
{ -1, 0, false},
{ -1, 0, false},
{ -1, 0, false},
{ -1, 0, false}
};
#include "ST7735Display.h"
#include "Settings.h"
boolean cardStatus = false;
boolean firstPatchLoaded = false;
//USB HOST MIDI Class Compliant
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
MIDIDevice midi1(myusb);
//MIDI 5 Pin DIN
MIDI_CREATE_INSTANCE(HardwareSerial, Serial4, MIDI); //RX - Pin 31
int prevNote = 48;//This is for glide to use previous note to glide from
float previousMillis = millis(); //For MIDI Clk Sync
int count = 0;//For MIDI Clk Sync
int patchNo = 1;//Current patch no
int voiceToReturn = -1; //Initialise
long earliestTime = millis(); //For voice allocation - initialise to now
void setup()
{
setupDisplay();
setUpSettings();
setupHardware();
AudioMemory(48);
sgtl5000_1.enable();
sgtl5000_1.dacVolumeRamp();
sgtl5000_1.volume(SGTL_MAXVOLUME); //Headphones
sgtl5000_1.audioPostProcessorEnable();
sgtl5000_1.enhanceBass(0.85, 0.87, 0, 4);//Normal level, bass level, HPF bypass (1 - on), bass cutoff freq
sgtl5000_1.enhanceBassDisable();//Turned on from EEPROM
cardStatus = SD.begin(BUILTIN_SDCARD);
if (cardStatus)
{
Serial.println("SD card is connected");
//Get patch numbers and names from SD card
loadPatches();
if (patches.size() == 0)
{
//save an initialised patch to SD card
savePatch("1", INITPATCH);
loadPatches();
}
}
else
{
Serial.println("SD card is not connected or unusable");
reinitialiseToPanel();
showPatchPage("No SD", "conn'd / usable");
}
//Read MIDI Channel from EEPROM
midiChannel = getMIDIChannel();
Serial.println("MIDI Ch:" + String(midiChannel) + " (0 is Omni On)");
//USB HOST MIDI Class Compliant
delay(200); //Wait to turn on USB Host
myusb.begin();
midi1.setHandleControlChange(myControlChange);
midi1.setHandleNoteOff(myNoteOff);
midi1.setHandleNoteOn(myNoteOn);
midi1.setHandlePitchChange(myPitchBend);
midi1.setHandleProgramChange(myProgramChange);
midi1.setHandleClock(myMIDIClock);
midi1.setHandleStart(myMIDIClockStart);
midi1.setHandleStop(myMIDIClockStop);
Serial.println("USB HOST MIDI Class Compliant Listening");
//USB Client MIDI
usbMIDI.setHandleControlChange(myControlChange);
usbMIDI.setHandleNoteOff(myNoteOff);
usbMIDI.setHandleNoteOn(myNoteOn);
usbMIDI.setHandlePitchChange(myPitchBend);
usbMIDI.setHandleProgramChange(myProgramChange);
usbMIDI.setHandleClock(myMIDIClock);
usbMIDI.setHandleStart(myMIDIClockStart);
usbMIDI.setHandleStop(myMIDIClockStop);
Serial.println("USB Client MIDI Listening");
//MIDI 5 Pin DIN
MIDI.begin();
MIDI.setHandleNoteOn(myNoteOn);
MIDI.setHandleNoteOff(myNoteOff);
MIDI.setHandlePitchBend(myPitchBend);
MIDI.setHandleControlChange(myControlChange);
MIDI.setHandleProgramChange(myProgramChange);
MIDI.setHandleClock(myMIDIClock);
MIDI.setHandleStart(myMIDIClockStart);
MIDI.setHandleStop(myMIDIClockStop);
Serial.println("MIDI In DIN Listening");
constant1Dc.amplitude(ONE);
noiseMixer.gain(0, ONE);
noiseMixer.gain(1, ONE);
noiseMixer.gain(2, 0);
noiseMixer.gain(3, 0);
voiceMixer1.gain(0, VOICEMIXERLEVEL);
voiceMixer1.gain(1, VOICEMIXERLEVEL);
voiceMixer1.gain(2, VOICEMIXERLEVEL);
voiceMixer1.gain(3, 0);
voiceMixer2.gain(0, VOICEMIXERLEVEL);
voiceMixer2.gain(1, VOICEMIXERLEVEL);
voiceMixer2.gain(2, VOICEMIXERLEVEL);
voiceMixer2.gain(3, 0);
voiceMixerM.gain(0, 0.5f);
voiceMixerM.gain(1, 0.5f);
voiceMixerM.gain(2, 0);
voiceMixerM.gain(3, 0);
pwmLfo.amplitude(ONE);
pwmLfo.begin(PWMWAVEFORM);
waveformMod1a.frequency(440);
waveformMod1a.amplitude(ONE);
waveformMod1a.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod1a.begin(oscWaveformA);
waveformMod1b.frequency(440);
waveformMod1b.amplitude(ONE);
waveformMod1b.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod1b.begin(oscWaveformB);
waveformMod2a.frequency(440);
waveformMod2a.amplitude(ONE);
waveformMod2a.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod2a.begin(oscWaveformA);
waveformMod2b.frequency(440);
waveformMod2b.amplitude(ONE);
waveformMod2b.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod2b.begin(oscWaveformB);
waveformMod3a.frequency(440);
waveformMod3a.amplitude(ONE);
waveformMod3a.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod3a.begin(oscWaveformA);
waveformMod3b.frequency(440);
waveformMod3b.amplitude(ONE);
waveformMod3b.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod3b.begin(oscWaveformB);
waveformMod4a.frequency(440);
waveformMod4a.amplitude(ONE);
waveformMod4a.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod4a.begin(oscWaveformA);
waveformMod4b.frequency(440);
waveformMod4b.amplitude(ONE);
waveformMod4b.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod4b.begin(oscWaveformB);
waveformMod5a.frequency(440);
waveformMod5a.amplitude(ONE);
waveformMod5a.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod5a.begin(oscWaveformA);
waveformMod5b.frequency(440);
waveformMod5b.amplitude(ONE);
waveformMod5b.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod5b.begin(oscWaveformB);
waveformMod6a.frequency(440);
waveformMod6a.amplitude(ONE);
waveformMod6a.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod6a.begin(oscWaveformA);
waveformMod6b.frequency(440);
waveformMod6b.amplitude(ONE);
waveformMod6b.frequencyModulation(PITCHLFOOCTAVERANGE);
waveformMod6b.begin(oscWaveformB);
//Arbitary waveform needs initialising to something
loadArbWaveformA(PARABOLIC_WAVE);
loadArbWaveformB(PARABOLIC_WAVE);
waveformMixer1.gain(2, ONE); //Noise
waveformMixer1.gain(3, 0); //Osc FX
waveformMixer2.gain(2, ONE); //Noise
waveformMixer2.gain(3, 0); //Osc FX
waveformMixer3.gain(2, ONE); //Noise
waveformMixer3.gain(3, 0); //Osc FX
waveformMixer4.gain(2, ONE); //Noise
waveformMixer4.gain(3, 0); //Osc FX
waveformMixer5.gain(2, ONE); //Noise
waveformMixer5.gain(3, 0); //Osc FX
waveformMixer6.gain(2, ONE); //Noise
waveformMixer6.gain(3, 0); //Osc FX
filterModMixer1.gain(1, ONE); //LFO
filterModMixer1.gain(3, 0); //Not used
filterModMixer2.gain(1, ONE); //LFO
filterModMixer2.gain(3, 0); //Not used
filterModMixer3.gain(1, ONE); //LFO
filterModMixer3.gain(3, 0); //Not used
filterModMixer4.gain(1, ONE); //LFO
filterModMixer4.gain(3, 0); //Not used
filterModMixer5.gain(1, ONE); //LFO
filterModMixer5.gain(3, 0); //Not used
filterModMixer6.gain(1, ONE); //LFO
filterModMixer6.gain(3, 0); //Not used
filterMixer1.gain(3, 0); //Not used
filterMixer2.gain(3, 0); //Not used
filterMixer3.gain(3, 0); //Not used
filterMixer4.gain(3, 0); //Not used
filterMixer5.gain(3, 0); //Not used
filterMixer6.gain(3, 0); //Not used
//This removes dc offset (mostly from unison pulse waves) before the ensemble effect
dcOffsetFilter.octaveControl(1.0);
dcOffsetFilter.frequency(12.0);//Lower values will give clicks on note on/off
ensemble.lfoRate(fxAmt);
effectMixerL.gain(2, 0);
effectMixerL.gain(3, 0);
effectMixerR.gain(2, 0);
effectMixerR.gain(3, 0);
volumePrevious = RE_READ; //Force volume control to be read and set to current
//Read Pitch Bend Range from EEPROM
pitchBendRange = getPitchBendRange();
//Read Mod Wheel Depth from EEPROM
modWheelDepth = getModWheelDepth();
//Read MIDI Out Channel from EEPROM
midiOutCh = getMIDIOutCh();
//Read Encoder Direction from EEPROM
encCW = getEncoderDir();
//Read bass enhance enable from EEPROM
if (getBassEnhanceEnable()) sgtl5000_1.enhanceBassEnable();
//Read Pick-up enable from EEPROM - experimental feature
pickUp = getPickupEnable();
//Read oscilloscope enable from EEPROM
enableScope(getScopeEnable());
//Read VU enable from EEPROM
vuMeter = getVUEnable();
}
void myNoteOn(byte channel, byte note, byte velocity)
{
//Check for out of range notes
if (note + oscPitchA < 0 || note + oscPitchA > 127 || note + oscPitchB < 0 || note + oscPitchB > 127)
return;
if (oscLfoRetrig == 1)
{
pitchLfo.sync();
}
if (filterLfoRetrig == 1)
{
filterLfo.sync();
}
if (unison == 0)
{
switch (getVoiceNo(-1))
{
case 1:
keytracking1.amplitude(note * DIV127 * keytrackingAmount);
voices[0].note = note;
voices[0].timeOn = millis();
voiceMixer1.gain(0, VELOCITY[velocitySens][velocity] * VOICEMIXERLEVEL);
updateVoice1();
filterEnvelope1.noteOn();
ampEnvelope1.noteOn();
voices[0].voiceOn = true;
if (glideSpeed > 0 && note != prevNote)
{
glide1.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide1.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
}
prevNote = note;
break;
case 2:
keytracking2.amplitude(note * DIV127 * keytrackingAmount);
voices[1].note = note;
voices[1].timeOn = millis();
voiceMixer1.gain(1, VELOCITY[velocitySens][velocity] * VOICEMIXERLEVEL);
updateVoice2();
filterEnvelope2.noteOn();
ampEnvelope2.noteOn();
voices[1].voiceOn = true;
if (glideSpeed > 0 && note != prevNote)
{
glide2.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide2.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
}
prevNote = note;
break;
case 3:
keytracking3.amplitude(note * DIV127 * keytrackingAmount);
voices[2].note = note;
voices[2].timeOn = millis();
voiceMixer1.gain(2, VELOCITY[velocitySens][velocity] * VOICEMIXERLEVEL);
updateVoice3();
filterEnvelope3.noteOn();
ampEnvelope3.noteOn();
voices[2].voiceOn = true;
if (glideSpeed > 0 && note != prevNote)
{
glide3.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide3.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
}
prevNote = note;
break;
case 4:
keytracking4.amplitude(note * DIV127 * keytrackingAmount);
voices[3].note = note;
voices[3].timeOn = millis();
voiceMixer2.gain(0, VELOCITY[velocitySens][velocity] * VOICEMIXERLEVEL);
updateVoice4();
filterEnvelope4.noteOn();
ampEnvelope4.noteOn();
voices[3].voiceOn = true;
if (glideSpeed > 0 && note != prevNote)
{
glide4.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide4.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
}
prevNote = note;
break;
case 5:
keytracking5.amplitude(note * DIV127 * keytrackingAmount);
voices[4].note = note;
voices[4].timeOn = millis();
voiceMixer2.gain(1, VELOCITY[velocitySens][velocity] * VOICEMIXERLEVEL);
updateVoice5();
filterEnvelope5.noteOn();
ampEnvelope5.noteOn();
voices[4].voiceOn = true;
if (glideSpeed > 0 && note != prevNote)
{
glide5.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide5.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
}
prevNote = note;
break;
case 6:
keytracking6.amplitude(note * DIV127 * keytrackingAmount);
voices[5].note = note;
voices[5].timeOn = millis();
voiceMixer2.gain(2, VELOCITY[velocitySens][velocity] * VOICEMIXERLEVEL);
updateVoice6();
filterEnvelope6.noteOn();
ampEnvelope6.noteOn();
voices[5].voiceOn = true;
if (glideSpeed > 0 && note != prevNote)
{
glide6.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide6.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
}
prevNote = note;
break;
}
}
else
{
//UNISON MODE
keytracking1.amplitude(note * DIV127 * keytrackingAmount);
keytracking2.amplitude(note * DIV127 * keytrackingAmount);
keytracking3.amplitude(note * DIV127 * keytrackingAmount);
keytracking4.amplitude(note * DIV127 * keytrackingAmount);
keytracking5.amplitude(note * DIV127 * keytrackingAmount);
keytracking6.amplitude(note * DIV127 * keytrackingAmount);
voices[0].note = note;
voices[0].timeOn = millis();
voiceMixer1.gain(0, VELOCITY[velocitySens][velocity] * UNISONVOICEMIXERLEVEL);
updateVoice1();
voices[1].note = note;
voices[1].timeOn = millis();
voiceMixer1.gain(1, VELOCITY[velocitySens][velocity] * UNISONVOICEMIXERLEVEL);
updateVoice2();
voices[2].note = note;
voices[2].timeOn = millis();
voiceMixer1.gain(2, VELOCITY[velocitySens][velocity] * UNISONVOICEMIXERLEVEL);
updateVoice3();
voices[3].note = note;
voices[3].timeOn = millis();
voiceMixer2.gain(0, VELOCITY[velocitySens][velocity] * UNISONVOICEMIXERLEVEL);
updateVoice4();
voices[4].note = note;
voices[4].timeOn = millis();
voiceMixer2.gain(1, VELOCITY[velocitySens][velocity] * UNISONVOICEMIXERLEVEL);
updateVoice5();
voices[5].note = note;
voices[5].timeOn = millis();
voiceMixer2.gain(2, VELOCITY[velocitySens][velocity] * UNISONVOICEMIXERLEVEL);
updateVoice6();
filterEnvelope1.noteOn();
filterEnvelope2.noteOn();
filterEnvelope3.noteOn();
filterEnvelope4.noteOn();
filterEnvelope5.noteOn();
filterEnvelope6.noteOn();
ampEnvelope1.noteOn();
ampEnvelope2.noteOn();
ampEnvelope3.noteOn();
ampEnvelope4.noteOn();
ampEnvelope5.noteOn();
ampEnvelope6.noteOn();
voices[0].voiceOn = true;
voices[1].voiceOn = true;
voices[2].voiceOn = true;
voices[3].voiceOn = true;
voices[4].voiceOn = true;
voices[5].voiceOn = true;
if (glideSpeed > 0 && note != prevNote)
{
glide1.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide1.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
glide2.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide2.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
glide3.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide3.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
glide4.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide4.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
glide5.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide5.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
glide6.amplitude((prevNote - note) * DIV24); //Set glide to previous note frequency (limited to 1 octave max)
glide6.amplitude(0, glideSpeed * GLIDEFACTOR); //Glide to current note
}
prevNote = note;
}
}
void myNoteOff(byte channel, byte note, byte velocity)
{
if (unison == 0)
{
switch (getVoiceNo(note))
{
case 1:
filterEnvelope1.noteOff();
ampEnvelope1.noteOff();
voices[0].voiceOn = false;
break;
case 2:
filterEnvelope2.noteOff();
ampEnvelope2.noteOff();
voices[1].voiceOn = false;
break;
case 3:
filterEnvelope3.noteOff();
ampEnvelope3.noteOff();
voices[2].voiceOn = false;
break;
case 4:
filterEnvelope4.noteOff();
ampEnvelope4.noteOff();
voices[3].voiceOn = false;
break;
case 5:
filterEnvelope5.noteOff();
ampEnvelope5.noteOff();
voices[4].voiceOn = false;
break;
case 6:
filterEnvelope6.noteOff();
ampEnvelope6.noteOff();
voices[5].voiceOn = false;
break;
}
}
else
{
//UNISON MODE
//If statement prevents the previous different note
//ending the current note when released
if (voices[0].note == note)allNotesOff();
}
}
void allNotesOff()
{
filterEnvelope1.noteOff();
ampEnvelope1.noteOff();
filterEnvelope2.noteOff();
ampEnvelope2.noteOff();
filterEnvelope3.noteOff();
ampEnvelope3.noteOff();
filterEnvelope4.noteOff();
ampEnvelope4.noteOff();
filterEnvelope5.noteOff();
ampEnvelope5.noteOff();
filterEnvelope6.noteOff();
ampEnvelope6.noteOff();
voices[0].voiceOn = false;
voices[1].voiceOn = false;
voices[2].voiceOn = false;
voices[3].voiceOn = false;
voices[4].voiceOn = false;
voices[5].voiceOn = false;
}
int getVoiceNo(int note)
{
voiceToReturn = -1; //Initialise
earliestTime = millis(); //Initialise to now
if (note == -1)
{
//NoteOn() - Get the oldest free voice (recent voices may be still on release stage)
for (int i = 0; i < NO_OF_VOICES; i++)
{
if (voices[i].voiceOn == false)
{
if (voices[i].timeOn < earliestTime)
{
earliestTime = voices[i].timeOn;
voiceToReturn = i;
}
}
}
if (voiceToReturn == -1)
{
//No free voices, need to steal oldest sounding voice
earliestTime = millis(); //Reinitialise
for (int i = 0; i < NO_OF_VOICES; i++)
{
if (voices[i].timeOn < earliestTime)
{
earliestTime = voices[i].timeOn;
voiceToReturn = i;
}
}
}
return voiceToReturn + 1;
}
else
{
//NoteOff() - Get voice number from note
for (int i = 0; i < NO_OF_VOICES; i++)
{
if (voices[i].note == note && voices[i].voiceOn)
{
return i + 1;
}
}
}
//Shouldn't get here, return voice 1
return 1;
}
void updateVoice1()
{
if (oscWaveformA == WAVEFORM_SAWTOOTH_WT) waveformMod1a.arbitraryWaveform(sawtoothWavetable[(voices[0].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SAWTOOTH_WT) waveformMod1b.arbitraryWaveform(sawtoothWavetable[(voices[0].note + oscPitchB) / 3 + 1], AWFREQ);
if (oscWaveformA == WAVEFORM_SQUARE_WT) waveformMod1a.arbitraryWaveform(squareWavetable[(voices[0].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SQUARE_WT) waveformMod1b.arbitraryWaveform(squareWavetable[(voices[0].note + oscPitchB) / 3 + 1], AWFREQ);
waveformMod1a.frequency(NOTEFREQS[voices[0].note + oscPitchA]);
if (unison == 1)
{
waveformMod1b.frequency(NOTEFREQS[voices[0].note + oscPitchB] * (detune + ((1 - detune) * 0.09)));
}
else
{
waveformMod1b.frequency(NOTEFREQS[voices[0].note + oscPitchB] * detune);
}
}
void updateVoice2()
{
if (oscWaveformA == WAVEFORM_SAWTOOTH_WT) waveformMod2a.arbitraryWaveform(sawtoothWavetable[(voices[1].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SAWTOOTH_WT) waveformMod2b.arbitraryWaveform(sawtoothWavetable[(voices[1].note + oscPitchB) / 3 + 1], AWFREQ);
if (oscWaveformA == WAVEFORM_SQUARE_WT) waveformMod2a.arbitraryWaveform(squareWavetable[(voices[1].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SQUARE_WT) waveformMod2b.arbitraryWaveform(squareWavetable[(voices[1].note + oscPitchB) / 3 + 1], AWFREQ);
if (unison == 1)
{
waveformMod2a.frequency(NOTEFREQS[voices[1].note + oscPitchA] * (detune + ((1 - detune) * 0.18)));
waveformMod2b.frequency(NOTEFREQS[voices[1].note + oscPitchB] * (detune + ((1 - detune) * 0.27)));
}
else
{
waveformMod2a.frequency(NOTEFREQS[voices[1].note + oscPitchA]);
waveformMod2b.frequency(NOTEFREQS[voices[1].note + oscPitchB] * detune);
}
}
void updateVoice3()
{
if (oscWaveformA == WAVEFORM_SAWTOOTH_WT) waveformMod3a.arbitraryWaveform(sawtoothWavetable[(voices[2].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SAWTOOTH_WT) waveformMod3b.arbitraryWaveform(sawtoothWavetable[(voices[2].note + oscPitchB) / 3 + 1], AWFREQ);
if (oscWaveformA == WAVEFORM_SQUARE_WT) waveformMod3a.arbitraryWaveform(squareWavetable[(voices[2].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SQUARE_WT) waveformMod3b.arbitraryWaveform(squareWavetable[(voices[2].note + oscPitchB) / 3 + 1], AWFREQ);
if (unison == 1)
{
waveformMod3a.frequency(NOTEFREQS[voices[2].note + oscPitchA] * (detune + ((1 - detune) * 0.36)));
waveformMod3b.frequency(NOTEFREQS[voices[2].note + oscPitchB] * (detune + ((1 - detune) * 0.46)));
}
else
{
waveformMod3a.frequency(NOTEFREQS[voices[2].note + oscPitchA]);
waveformMod3b.frequency(NOTEFREQS[voices[2].note + oscPitchB] * detune);
}
}
void updateVoice4()
{
if (oscWaveformA == WAVEFORM_SAWTOOTH_WT) waveformMod4a.arbitraryWaveform(sawtoothWavetable[(voices[3].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SAWTOOTH_WT) waveformMod4b.arbitraryWaveform(sawtoothWavetable[(voices[3].note + oscPitchB) / 3 + 1], AWFREQ);
if (oscWaveformA == WAVEFORM_SQUARE_WT) waveformMod4a.arbitraryWaveform(squareWavetable[(voices[3].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SQUARE_WT) waveformMod4b.arbitraryWaveform(squareWavetable[(voices[3].note + oscPitchB) / 3 + 1], AWFREQ);
if (unison == 1)
{
waveformMod4a.frequency(NOTEFREQS[voices[3].note + oscPitchA] * (detune + ((1 - detune) * 0.55)));
waveformMod4b.frequency(NOTEFREQS[voices[3].note + oscPitchB] * (detune + ((1 - detune) * 0.64)));
}
else
{
waveformMod4a.frequency(NOTEFREQS[voices[3].note + oscPitchA]);
waveformMod4b.frequency(NOTEFREQS[voices[3].note + oscPitchB] * detune);
}
}
void updateVoice5()
{
if (oscWaveformA == WAVEFORM_SAWTOOTH_WT) waveformMod5a.arbitraryWaveform(sawtoothWavetable[(voices[4].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SAWTOOTH_WT) waveformMod5b.arbitraryWaveform(sawtoothWavetable[(voices[4].note + oscPitchB) / 3 + 1], AWFREQ);
if (oscWaveformA == WAVEFORM_SQUARE_WT) waveformMod5a.arbitraryWaveform(squareWavetable[(voices[4].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SQUARE_WT) waveformMod5b.arbitraryWaveform(squareWavetable[(voices[4].note + oscPitchB) / 3 + 1], AWFREQ);
if (unison == 1)
{
waveformMod5a.frequency(NOTEFREQS[voices[4].note + oscPitchA] * (detune + ((1 - detune) * 0.73)));
waveformMod5b.frequency(NOTEFREQS[voices[4].note + oscPitchB] * (detune + ((1 - detune) * 0.82)));
}
else
{
waveformMod5a.frequency(NOTEFREQS[voices[4].note + oscPitchA]);
waveformMod5b.frequency(NOTEFREQS[voices[4].note + oscPitchB] * detune);
}
}
void updateVoice6()
{
if (oscWaveformA == WAVEFORM_SAWTOOTH_WT) waveformMod6a.arbitraryWaveform(sawtoothWavetable[(voices[5].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SAWTOOTH_WT) waveformMod6b.arbitraryWaveform(sawtoothWavetable[(voices[5].note + oscPitchB) / 3 + 1], AWFREQ);
if (oscWaveformA == WAVEFORM_SQUARE_WT) waveformMod6a.arbitraryWaveform(squareWavetable[(voices[5].note + oscPitchA) / 3 + 1], AWFREQ);
if (oscWaveformB == WAVEFORM_SQUARE_WT) waveformMod6b.arbitraryWaveform(squareWavetable[(voices[5].note + oscPitchB) / 3 + 1], AWFREQ);
if (unison == 1)
{
waveformMod6a.frequency(NOTEFREQS[voices[5].note + oscPitchA] * (detune + ((1 - detune) * 0.90)));
}
else
{
waveformMod6a.frequency(NOTEFREQS[voices[5].note + oscPitchA]);
}
waveformMod6b.frequency(NOTEFREQS[voices[5].note + oscPitchB] * detune);
}
int getLFOWaveform(int value)
{
if (value >= 0 && value < 8)
{
return WAVEFORM_SINE;
}
else if (value >= 8 && value < 30)
{
return WAVEFORM_TRIANGLE;
}
else if (value >= 30 && value < 63)
{
return WAVEFORM_SAWTOOTH_REVERSE;
}
else if (value >= 63 && value < 92)
{
return WAVEFORM_SAWTOOTH;
}
else if (value >= 92 && value < 111)
{
return WAVEFORM_SQUARE;
}
else
{
return WAVEFORM_SAMPLE_HOLD;
}
}
String getWaveformStr(int value)
{
switch (value)
{
case WAVEFORM_SILENT:
return "Off";
case WAVEFORM_SAMPLE_HOLD:
return "Sample & Hold";
case WAVEFORM_SINE:
return "Sine";
case WAVEFORM_SQUARE_WT:
case WAVEFORM_SQUARE:
return "Square";
case WAVEFORM_TRIANGLE:
return "Triangle";
case WAVEFORM_SAWTOOTH_WT:
case WAVEFORM_SAWTOOTH:
return "Sawtooth";
case WAVEFORM_SAWTOOTH_REVERSE:
return "Ramp";
case WAVEFORM_PULSE:
return "Var. Pulse";
case WAVEFORM_TRIANGLE_VARIABLE:
return "Var. Triangle";
case WAVEFORM_PARABOLIC:
return "Parabolic";
case WAVEFORM_HARMONIC:
return "Harmonic";
default:
return "ERR_WAVE";
}
}
void loadArbWaveformA(const int16_t * wavedata) {
waveformMod1a.arbitraryWaveform(wavedata, AWFREQ);
waveformMod2a.arbitraryWaveform(wavedata, AWFREQ);
waveformMod3a.arbitraryWaveform(wavedata, AWFREQ);
waveformMod4a.arbitraryWaveform(wavedata, AWFREQ);
waveformMod5a.arbitraryWaveform(wavedata, AWFREQ);
waveformMod6a.arbitraryWaveform(wavedata, AWFREQ);
}
void loadArbWaveformB(const int16_t * wavedata) {
waveformMod1b.arbitraryWaveform(wavedata, AWFREQ);
waveformMod2b.arbitraryWaveform(wavedata, AWFREQ);
waveformMod3b.arbitraryWaveform(wavedata, AWFREQ);
waveformMod4b.arbitraryWaveform(wavedata, AWFREQ);
waveformMod5b.arbitraryWaveform(wavedata, AWFREQ);
waveformMod6b.arbitraryWaveform(wavedata, AWFREQ);
}
float getLFOTempoRate(int value)
{
lfoTempoValue = LFOTEMPO[value];
return lfoSyncFreq * LFOTEMPO[value];
}
int getWaveformA(int value)
{
if (value >= 0 && value < 7)
{
//This will turn the osc off
return WAVEFORM_SILENT;
}
else if (value >= 7 && value < 23)
{
return WAVEFORM_TRIANGLE;
}
else if (value >= 23 && value < 40)
{
return WAVEFORM_SQUARE_WT;
}
else if (value >= 40 && value < 60)
{
return WAVEFORM_SAWTOOTH_WT;
}
else if (value >= 60 && value < 80)
{
return WAVEFORM_PULSE;
}
else if (value >= 80 && value < 100)
{
return WAVEFORM_TRIANGLE_VARIABLE;
}
else if (value >= 100 && value < 120)
{
return WAVEFORM_PARABOLIC;
}
else
{
return WAVEFORM_HARMONIC;
}
}
int getWaveformB(int value)
{
if (value >= 0 && value < 7)
{
//This will turn the osc off
return WAVEFORM_SILENT;
}
else if (value >= 7 && value < 23)
{
return WAVEFORM_SAMPLE_HOLD;
}
else if (value >= 23 && value < 40)
{
return WAVEFORM_SQUARE_WT;
}
else if (value >= 40 && value < 60)
{
return WAVEFORM_SAWTOOTH_WT;
}
else if (value >= 60 && value < 80)
{
return WAVEFORM_PULSE;
}
else if (value >= 80 && value < 100)
{
return WAVEFORM_TRIANGLE_VARIABLE;
}
else if (value >= 100 && value < 120)
{
return WAVEFORM_PARABOLIC;
}
else
{
return WAVEFORM_HARMONIC;
}
}
int getPitch(int value)
{
return PITCH[value];
}
void setPwmMixerALFO(float value)
{
pwMixer1a.gain(0, value);
pwMixer2a.gain(0, value);
pwMixer3a.gain(0, value);
pwMixer4a.gain(0, value);
pwMixer5a.gain(0, value);
pwMixer6a.gain(0, value);
showCurrentParameterPage("1. PWM LFO", String(value));
}
void setPwmMixerBLFO(float value)
{
pwMixer1b.gain(0, value);
pwMixer2b.gain(0, value);
pwMixer3b.gain(0, value);
pwMixer4b.gain(0, value);
pwMixer5b.gain(0, value);
pwMixer6b.gain(0, value);
showCurrentParameterPage("2. PWM LFO", String(value));
}
void setPwmMixerAPW(float value)
{
pwMixer1a.gain(1, value);
pwMixer2a.gain(1, value);
pwMixer3a.gain(1, value);
pwMixer4a.gain(1, value);
pwMixer5a.gain(1, value);
pwMixer6a.gain(1, value);
}
void setPwmMixerBPW(float value)
{
pwMixer1b.gain(1, value);
pwMixer2b.gain(1, value);
pwMixer3b.gain(1, value);
pwMixer4b.gain(1, value);
pwMixer5b.gain(1, value);
pwMixer6b.gain(1, value);
}
void setPwmMixerAFEnv(float value)
{
pwMixer1a.gain(2, value);
pwMixer2a.gain(2, value);
pwMixer3a.gain(2, value);
pwMixer4a.gain(2, value);
pwMixer5a.gain(2, value);
pwMixer6a.gain(2, value);
showCurrentParameterPage("1. PWM F Env", String(value));
}
void setPwmMixerBFEnv(float value)
{
pwMixer1b.gain(2, value);
pwMixer2b.gain(2, value);
pwMixer3b.gain(2, value);
pwMixer4b.gain(2, value);
pwMixer5b.gain(2, value);
pwMixer6b.gain(2, value);
showCurrentParameterPage("2. PWM F Env", String(value));
}