-
Notifications
You must be signed in to change notification settings - Fork 1
/
16bit_6_note_MIDI_CV_poly.ino
1345 lines (1187 loc) · 39.6 KB
/
16bit_6_note_MIDI_CV_poly.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
/*
MIDI2CV_Poly
Copyright (C) 2020 Craig Barnes
A big thankyou to Elkayem for his midi to cv code
A big thankyou to ElectroTechnique for his polyphonic tsynth that I used for the poly notes routine
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 <http://www.gnu.org/licenses/> for more details.
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
#include <Bounce2.h>
#include <MIDI.h>
#include <USBHost_t36.h>
// OLED I2C is used on pins 18 and 19 for Teensy 3.x
// Voices available
#define NO_OF_VOICES 6
#define trigTimeout 20
//Note DACS
#define DAC_NOTE1 7
#define DAC_NOTE2 8
#define DAC_NOTE3 9
//Autotune MUX
#define MUX_S0 10
#define MUX_S1 24
#define MUX_S2 25
#define MUX_S3 26
#define MUX_ENABLE 3
#define MUX_OUT 4
//Trig outputs
#define TRIG_NOTE1 32
#define TRIG_NOTE2 31
#define TRIG_NOTE3 30
#define TRIG_NOTE4 29
#define TRIG_NOTE5 28
#define TRIG_NOTE6 27
//Gate outputs
#define GATE_NOTE1 33
#define GATE_NOTE2 34
#define GATE_NOTE3 35
#define GATE_NOTE4 36
#define GATE_NOTE5 37
#define GATE_NOTE6 38
//Encoder or buttons
#define ENC_A 14
#define ENC_B 15
#define ENC_BTN 16
#define AUTOTUNE 17
#define UNISON_ON 2
// Scale Factor will generate 0.5v/octave
// 4 octave keyboard on a 3.3v powered DAC
#define NOTE_SF 547.00f
#define VEL_SF 256.0
#define OLED_RESET 17
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int encoderPos, encoderPosPrev;
Bounce encButton = Bounce();
Bounce encoderA = Bounce();
Bounce encoderB = Bounce();
enum Menu {
SETTINGS,
KEYBOARD_MODE_SET_CH,
MIDI_CHANNEL_SET_CH,
TRANSPOSE_SET_CH,
OCTAVE_SET_CH,
SCALE_FACTOR,
SCALE_FACTOR_SET_CH
} menu;
char gateTrig[] = "TTTTTT";
float sfAdj[6];
uint8_t pitchBendChan;
uint8_t ccChan;
int masterChan;
int masterTran;
int previousMode;
int transpose;
int8_t d2, i;
int noteMsg;
int keyboardMode;
int octave;
int realoctave;
float noteTrig[6];
float monoTrig;
float unisonTrig;
struct VoiceAndNote {
int note;
int velocity;
long timeOn;
};
struct VoiceAndNote voices[NO_OF_VOICES] = {
{ -1, -1, 0},
{ -1, -1, 0},
{ -1, -1, 0},
{ -1, -1, 0},
{ -1, -1, 0},
{ -1, -1, 0}
};
boolean voiceOn[NO_OF_VOICES] = {false, false, false, false, false, false};
int voiceToReturn = -1;//Initialise to 'null'
long earliestTime = millis();//For voice allocation - initialise to now
int prevNote = 0;//Initialised to middle value
bool notes[88] = {0}, initial_loop = 1;
int8_t noteOrder[40] = {0}, orderIndx = {0};
bool S1, S2;
unsigned long trigTimer = 0;
// MIDI setup
//USB HOST MIDI Class Compliant
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
MIDIDevice midi1(myusb);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
const int channel = 1;
// EEPROM Addresses
#define ADDR_GATE_TRIG 6
#define ADDR_PITCH_BEND 12
#define ADDR_CC 13
#define ADDR_SF_ADJUST 14
#define ADDR_MASTER_CHAN 20
#define ADDR_TRANSPOSE 26
#define ADDR_REAL_TRANSPOSE 27
#define ADDR_OCTAVE 28
#define ADDR_REALOCTAVE 29
#define ADDR_KEYBOARD_MODE 30
uint32_t int_ref_on_flexible_mode = 0b00001001000010100000000000000000; // { 0000 , 1001 , 0000 , 1010000000000000 , 0000 }
uint32_t sample_data = 0b00000000000000000000000000000000;
uint32_t channel_a = 0b00000010000000000000000000000000;
uint32_t channel_b = 0b00000010000100000000000000000000;
uint32_t channel_c = 0b00000010001000000000000000000000;
uint32_t channel_d = 0b00000010001100000000000000000000;
uint32_t channel_e = 0b00000010010000000000000000000000;
uint32_t channel_f = 0b00000010010100000000000000000000;
uint32_t channel_g = 0b00000010011000000000000000000000;
uint32_t channel_h = 0b00000010011100000000000000000000;
bool highlightEnabled = false; // Flag indicating whether highighting should be enabled on menu
#define HIGHLIGHT_TIMEOUT 20000 // Highlight disappears after 20 seconds. Timer resets whenever encoder turned or button pushed
unsigned long int highlightTimer = 0;
void setup()
{
pinMode(GATE_NOTE1, OUTPUT);
pinMode(GATE_NOTE2, OUTPUT);
pinMode(GATE_NOTE3, OUTPUT);
pinMode(GATE_NOTE4, OUTPUT);
pinMode(GATE_NOTE5, OUTPUT);
pinMode(GATE_NOTE6, OUTPUT);
pinMode(TRIG_NOTE1, OUTPUT);
pinMode(TRIG_NOTE2, OUTPUT);
pinMode(TRIG_NOTE3, OUTPUT);
pinMode(TRIG_NOTE4, OUTPUT);
pinMode(TRIG_NOTE5, OUTPUT);
pinMode(TRIG_NOTE6, OUTPUT);
pinMode(DAC_NOTE1, OUTPUT);
pinMode(DAC_NOTE2, OUTPUT);
pinMode(DAC_NOTE3, OUTPUT);
pinMode(MUX_S0, OUTPUT);
pinMode(MUX_S1, OUTPUT);
pinMode(MUX_S2, OUTPUT);
pinMode(MUX_S3, OUTPUT);
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
pinMode(ENC_BTN, INPUT_PULLUP);
pinMode(UNISON_ON, INPUT_PULLUP);
pinMode(AUTOTUNE, INPUT_PULLUP);
pinMode(MUX_OUT, INPUT);
pinMode(MUX_ENABLE, OUTPUT);
digitalWrite(GATE_NOTE1, LOW);
digitalWrite(GATE_NOTE2, LOW);
digitalWrite(GATE_NOTE3, LOW);
digitalWrite(GATE_NOTE4, LOW);
digitalWrite(GATE_NOTE5, LOW);
digitalWrite(GATE_NOTE6, LOW);
digitalWrite(TRIG_NOTE1, LOW);
digitalWrite(TRIG_NOTE2, LOW);
digitalWrite(TRIG_NOTE3, LOW);
digitalWrite(TRIG_NOTE4, LOW);
digitalWrite(TRIG_NOTE5, LOW);
digitalWrite(TRIG_NOTE6, LOW);
digitalWrite(DAC_NOTE1, HIGH);
digitalWrite(DAC_NOTE2, HIGH);
digitalWrite(DAC_NOTE3, HIGH);
digitalWrite(MUX_S0, LOW);
digitalWrite(MUX_S1, LOW);
digitalWrite(MUX_S2, LOW);
digitalWrite(MUX_S3, LOW);
digitalWrite(MUX_ENABLE, LOW);
SPI.setDataMode(SPI_MODE1);
SPI.begin();
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1));
digitalWrite(DAC_NOTE1, LOW);
delayMicroseconds(1);
SPI.transfer(int_ref_on_flexible_mode >> 24);
SPI.transfer(int_ref_on_flexible_mode >> 16);
SPI.transfer(int_ref_on_flexible_mode >> 8);
SPI.transfer(int_ref_on_flexible_mode);
digitalWrite(DAC_NOTE1, HIGH);
SPI.endTransaction();
digitalWrite(DAC_NOTE2, LOW);
delayMicroseconds(1);
SPI.transfer(int_ref_on_flexible_mode >> 24);
SPI.transfer(int_ref_on_flexible_mode >> 16);
SPI.transfer(int_ref_on_flexible_mode >> 8);
SPI.transfer(int_ref_on_flexible_mode);
digitalWrite(DAC_NOTE2, HIGH);
SPI.endTransaction();
digitalWrite(DAC_NOTE3, LOW);
delayMicroseconds(1);
SPI.transfer(int_ref_on_flexible_mode >> 24);
SPI.transfer(int_ref_on_flexible_mode >> 16);
SPI.transfer(int_ref_on_flexible_mode >> 8);
SPI.transfer(int_ref_on_flexible_mode);
digitalWrite(DAC_NOTE3, HIGH);
SPI.endTransaction();
//USB HOST MIDI Class Compliant
delay(300); //Wait to turn on USB Host
myusb.begin();
midi1.setHandleControlChange(myControlChange);
midi1.setHandleNoteOff(myNoteOff);
midi1.setHandleNoteOn(myNoteOn);
midi1.setHandlePitchChange(myPitchBend);
Serial.println("USB HOST MIDI Class Compliant Listening");
//MIDI 5 Pin DIN
MIDI.begin(masterChan);
MIDI.setHandleNoteOn(myNoteOn);
MIDI.setHandleNoteOff(myNoteOff);
MIDI.setHandlePitchBend(myPitchBend);
MIDI.setHandleControlChange(myControlChange);
Serial.println("MIDI In DIN Listening");
//USB Client MIDI
usbMIDI.setHandleControlChange(myControlChange);
usbMIDI.setHandleNoteOff(myNoteOff);
usbMIDI.setHandleNoteOn(myNoteOn);
usbMIDI.setHandlePitchChange(myPitchBend);
Serial.println("USB Client MIDI Listening");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // OLED I2C Address, may need to change for different device,
// Check with I2C_Scanner
// Wire.setClock(100000L); // Uncomment to slow down I2C speed
// Read Settings from EEPROM
for (int i = 0; i < 6; i++) {
gateTrig[i] = (char)EEPROM.read(ADDR_GATE_TRIG + i);
if (gateTrig[i] != 'G' || gateTrig[i] != 'T') EEPROM.write(ADDR_GATE_TRIG + i, 'T');
gateTrig[i] = (char)EEPROM.read(ADDR_GATE_TRIG + i);
EEPROM.get(ADDR_SF_ADJUST + i * sizeof(float), sfAdj[i]);
if ((sfAdj[i] < 0.9f) || (sfAdj[i] > 1.1f) || isnan(sfAdj[i])) sfAdj[i] = 1.0f;
}
keyboardMode = (int)EEPROM.read(ADDR_KEYBOARD_MODE);
previousMode = (int)EEPROM.read(ADDR_KEYBOARD_MODE);
masterChan = (int)EEPROM.read(ADDR_MASTER_CHAN);
masterTran = (int)EEPROM.read(ADDR_TRANSPOSE);
transpose = (int)EEPROM.read(ADDR_REAL_TRANSPOSE);
octave = (int)EEPROM.read(ADDR_OCTAVE);
realoctave = (int)EEPROM.read(ADDR_REALOCTAVE);
pitchBendChan = masterChan;
ccChan = masterChan;
// Set defaults if EEPROM not initialized
if (keyboardMode > 6) keyboardMode = 0;
if (masterTran > 25) masterTran = 13;
if (masterChan > 15) masterChan = 0;
if (octave > 3) octave = 3;
if (octave == 0) realoctave = -36;
if (octave == 1) realoctave = -24;
if (octave == 2) realoctave = -12;
if (octave == 3) realoctave = 0;
if (pitchBendChan > 15) pitchBendChan = masterChan;
if (ccChan > 15) ccChan = masterChan;
encButton.attach(ENC_BTN);
encButton.interval(5); // interval in ms
sample_data = ((channel_h & 0xFFF0000F) | ( 13180 & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = ((channel_g & 0xFFF0000F) | ( 0 & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
menu = SETTINGS;
updateSelection();
}
void myPitchBend(byte channel, int bend) {
if ((MIDI.getChannel() == pitchBendChan) || (pitchBendChan == 0 )) {
d2 = MIDI.getData2(); // d2 from 0 to 127, mid point = 64
sample_data = (channel_h & 0xFFF0000F) | (((int(bend * 1.605) + 13180) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
}
void myControlChange(byte channel, byte number, byte value) {
if ((MIDI.getChannel() == ccChan || ccChan == 0)) {
if ( number == 1 )
{
sample_data = (channel_g & 0xFFF0000F) | (((int(value * 207)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
else
{
MIDI.sendControlChange(number, value, channel);
}
}
}
void commandTopNote()
{
int topNote = 0;
bool noteActive = false;
for (int i = 0; i < 88; i++)
{
if (notes[i]) {
topNote = i;
noteActive = true;
}
}
if (noteActive)
commandNote(topNote);
else // All notes are off, turn off gate
digitalWrite(GATE_NOTE1, LOW);
}
void commandBottomNote()
{
int bottomNote = 0;
bool noteActive = false;
for (int i = 87; i >= 0; i--)
{
if (notes[i]) {
bottomNote = i;
noteActive = true;
}
}
if (noteActive)
commandNote(bottomNote);
else // All notes are off, turn off gate
digitalWrite(GATE_NOTE1, LOW);
}
void commandLastNote()
{
int8_t noteIndx;
for (int i = 0; i < 40; i++) {
noteIndx = noteOrder[ mod(orderIndx - i, 40) ];
if (notes[noteIndx]) {
commandNote(noteIndx);
return;
}
}
digitalWrite(GATE_NOTE1, LOW); // All notes are off
}
void commandNote(int noteMsg) {
unsigned int mV = (unsigned int) ((float) (noteMsg + transpose + realoctave) * NOTE_SF * sfAdj[0] + 0.5);
sample_data = (channel_a & 0xFFF0000F) | (((int(mV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
digitalWrite(GATE_NOTE1, HIGH);
digitalWrite(TRIG_NOTE1, HIGH);
trigTimer = millis();
while (millis() < trigTimer + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE1, LOW);
}
void commandTopNoteUni()
{
int topNote = 0;
bool noteActive = false;
for (int i = 0; i < 88; i++)
{
if (notes[i]) {
topNote = i;
noteActive = true;
}
}
if (noteActive)
commandNoteUni(topNote);
else // All notes are off, turn off gate
digitalWrite(GATE_NOTE1, LOW);
digitalWrite(GATE_NOTE2, LOW);
digitalWrite(GATE_NOTE3, LOW);
digitalWrite(GATE_NOTE4, LOW);
digitalWrite(GATE_NOTE5, LOW);
digitalWrite(GATE_NOTE6, LOW);
}
void commandBottomNoteUni()
{
int bottomNote = 0;
bool noteActive = false;
for (int i = 87; i >= 0; i--)
{
if (notes[i]) {
bottomNote = i;
noteActive = true;
}
}
if (noteActive)
commandNoteUni(bottomNote);
else // All notes are off, turn off gate
digitalWrite(GATE_NOTE1, LOW);
digitalWrite(GATE_NOTE2, LOW);
digitalWrite(GATE_NOTE3, LOW);
digitalWrite(GATE_NOTE4, LOW);
digitalWrite(GATE_NOTE5, LOW);
digitalWrite(GATE_NOTE6, LOW);
}
void commandLastNoteUni()
{
int8_t noteIndx;
for (int i = 0; i < 40; i++) {
noteIndx = noteOrder[ mod(orderIndx - i, 40) ];
if (notes[noteIndx]) {
commandNoteUni(noteIndx);
return;
}
}
digitalWrite(GATE_NOTE1, LOW);
digitalWrite(GATE_NOTE2, LOW);
digitalWrite(GATE_NOTE3, LOW);
digitalWrite(GATE_NOTE4, LOW);
digitalWrite(GATE_NOTE5, LOW);
digitalWrite(GATE_NOTE6, LOW);// All notes are off
}
void commandNoteUni(int noteMsg) {
unsigned int mV1 = (unsigned int) ((float) (noteMsg + transpose + realoctave) * NOTE_SF * sfAdj[0] + 0.5);
sample_data = (channel_a & 0xFFF0000F) | (((int(mV1)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int mV2 = (unsigned int) ((float) (noteMsg + transpose + realoctave) * NOTE_SF * sfAdj[1] + 0.5);
sample_data = (channel_b & 0xFFF0000F) | (((int(mV2)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int mV3 = (unsigned int) ((float) (noteMsg + transpose + realoctave) * NOTE_SF * sfAdj[2] + 0.5);
sample_data = (channel_c & 0xFFF0000F) | (((int(mV3)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int mV4 = (unsigned int) ((float) (noteMsg + transpose + realoctave) * NOTE_SF * sfAdj[3] + 0.5);
sample_data = (channel_d & 0xFFF0000F) | (((int(mV4)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int mV5 = (unsigned int) ((float) (noteMsg + transpose + realoctave) * NOTE_SF * sfAdj[4] + 0.5);
sample_data = (channel_e & 0xFFF0000F) | (((int(mV5)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int mV6 = (unsigned int) ((float) (noteMsg + transpose + realoctave) * NOTE_SF * sfAdj[5] + 0.5);
sample_data = (channel_f & 0xFFF0000F) | (((int(mV6)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
digitalWrite(TRIG_NOTE1, HIGH);
digitalWrite(GATE_NOTE1, HIGH);
digitalWrite(TRIG_NOTE2, HIGH);
digitalWrite(GATE_NOTE2, HIGH);
digitalWrite(TRIG_NOTE3, HIGH);
digitalWrite(GATE_NOTE3, HIGH);
digitalWrite(TRIG_NOTE4, HIGH);
digitalWrite(GATE_NOTE4, HIGH);
digitalWrite(TRIG_NOTE5, HIGH);
digitalWrite(GATE_NOTE5, HIGH);
digitalWrite(TRIG_NOTE6, HIGH);
digitalWrite(GATE_NOTE6, HIGH);
trigTimer = millis();
while (millis() < trigTimer + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE1, LOW);
digitalWrite(TRIG_NOTE2, LOW);
digitalWrite(TRIG_NOTE3, LOW);
digitalWrite(TRIG_NOTE4, LOW);
digitalWrite(TRIG_NOTE5, LOW);
digitalWrite(TRIG_NOTE6, LOW);
}
void myNoteOn(byte channel, byte note, byte velocity) {
//Check for out of range notes
if (note < 0 || note > 127) return;
prevNote = note;
if (keyboardMode == 0) {
switch (getVoiceNo(-1)) {
case 1:
voices[0].note = note;
voices[0].velocity = velocity;
voices[0].timeOn = millis();
updateVoice1();
digitalWrite(GATE_NOTE1, HIGH);
digitalWrite(TRIG_NOTE1, HIGH);
noteTrig[0] = millis();
while (millis() < noteTrig[0] + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE1, LOW); // Set trigger low after 20 msec
voiceOn[0] = true;
break;
case 2:
voices[1].note = note;
voices[1].velocity = velocity;
voices[1].timeOn = millis();
updateVoice2();
digitalWrite(GATE_NOTE2, HIGH);
digitalWrite(TRIG_NOTE2, HIGH);
noteTrig[1] = millis();
while (millis() < noteTrig[1] + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE2, LOW);
voiceOn[1] = true;
break;
case 3:
voices[2].note = note;
voices[2].velocity = velocity;
voices[2].timeOn = millis();
updateVoice3();
digitalWrite(GATE_NOTE3, HIGH);
digitalWrite(TRIG_NOTE3, HIGH);
noteTrig[2] = millis();
while (millis() < noteTrig[2] + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE3, LOW);
voiceOn[2] = true;
break;
case 4:
voices[3].note = note;
voices[3].velocity = velocity;
voices[3].timeOn = millis();
updateVoice4();
digitalWrite(GATE_NOTE4, HIGH);
digitalWrite(TRIG_NOTE4, HIGH);
noteTrig[3] = millis();
while (millis() < noteTrig[3] + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE4, LOW);
voiceOn[3] = true;
break;
case 5:
voices[4].note = note;
voices[4].velocity = velocity;
voices[4].timeOn = millis();
updateVoice5();
digitalWrite(GATE_NOTE5, HIGH);
digitalWrite(TRIG_NOTE5, HIGH);
noteTrig[4] = millis();
while (millis() < noteTrig[4] + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE5, LOW);
voiceOn[4] = true;
break;
case 6:
voices[5].note = note;
voices[5].velocity = velocity;
voices[5].timeOn = millis();
updateVoice6();
digitalWrite(GATE_NOTE6, HIGH);
digitalWrite(TRIG_NOTE6, HIGH);
noteTrig[5] = millis();
while (millis() < noteTrig[5] + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE6, LOW);
voiceOn[5] = true;
break;
}
}
else if (keyboardMode == 4 || keyboardMode == 5 || keyboardMode == 6)
{
if (keyboardMode == 4)
{
S1 = 1;
S2 = 1;
}
if (keyboardMode == 5)
{
S1 = 0;
S2 = 1;
}
if (keyboardMode == 6)
{
S1 = 0;
S2 = 0;
}
noteMsg = note;
if (velocity == 0) {
notes[noteMsg] = false;
}
else {
notes[noteMsg] = true;
}
unsigned int velmV = ((unsigned int) ((float) velocity) * VEL_SF);
sample_data = (channel_a & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
if (S1 && S2) { // Highest note priority
commandTopNote();
}
else if (!S1 && S2) { // Lowest note priority
commandBottomNote();
}
else { // Last note priority
if (notes[noteMsg]) { // If note is on and using last note priority, add to ordered list
orderIndx = (orderIndx + 1) % 40;
noteOrder[orderIndx] = noteMsg;
}
commandLastNote();
}
}
else if (keyboardMode == 1 || keyboardMode == 2 || keyboardMode == 3)
{
if (keyboardMode == 1)
{
S1 = 1;
S2 = 1;
}
if (keyboardMode == 2)
{
S1 = 0;
S2 = 1;
}
if (keyboardMode == 3)
{
S1 = 0;
S2 = 0;
}
noteMsg = note;
if (velocity == 0) {
notes[noteMsg] = false;
}
else {
notes[noteMsg] = true;
}
unsigned int velmV = ((unsigned int) ((float) velocity) * VEL_SF);
sample_data = (channel_a & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_b & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_c & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_d & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_e & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_f & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
if (S1 && S2) { // Highest note priority
commandTopNoteUni();
}
else if (!S1 && S2) { // Lowest note priority
commandBottomNoteUni();
}
else { // Last note priority
if (notes[noteMsg]) { // If note is on and using last note priority, add to ordered list
orderIndx = (orderIndx + 1) % 40;
noteOrder[orderIndx] = noteMsg;
}
commandLastNoteUni();
}
}
}
void myNoteOff(byte channel, byte note, byte velocity) {
if (keyboardMode == 0) {
switch (getVoiceNo(note)) {
case 1:
digitalWrite(GATE_NOTE1, LOW);
voices[0].note = -1;
voiceOn[0] = false;
break;
case 2:
digitalWrite(GATE_NOTE2, LOW);
voices[1].note = -1;
voiceOn[1] = false;
break;
case 3:
digitalWrite(GATE_NOTE3, LOW);
voices[2].note = -1;
voiceOn[2] = false;
break;
case 4:
digitalWrite(GATE_NOTE4, LOW);
voices[3].note = -1;
voiceOn[3] = false;
break;
case 5:
digitalWrite(GATE_NOTE5, LOW);
voices[4].note = -1;
voiceOn[4] = false;
break;
case 6:
digitalWrite(GATE_NOTE6, LOW);
voices[5].note = -1;
voiceOn[5] = false;
break;
}
}
else if (keyboardMode == 4 || keyboardMode == 5 || keyboardMode == 6)
{
if (keyboardMode == 4)
{
S1 = 1;
S2 = 1;
}
if (keyboardMode == 5)
{
S1 = 0;
S2 = 1;
}
if (keyboardMode == 6)
{
S1 = 0;
S2 = 0;
}
noteMsg = note;
if (velocity == 0) {
notes[noteMsg] = false;
}
else {
notes[noteMsg] = true;
}
// Pins NP_SEL1 and NP_SEL2 indictate note priority
unsigned int velmV = ((unsigned int) ((float) velocity) * VEL_SF);
sample_data = (channel_a & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
if (S1 && S2) { // Highest note priority
commandTopNote();
}
else if (!S1 && S2) { // Lowest note priority
commandBottomNote();
}
else { // Last note priority
if (notes[noteMsg]) { // If note is on and using last note priority, add to ordered list
orderIndx = (orderIndx + 1) % 40;
noteOrder[orderIndx] = noteMsg;
}
commandLastNote();
}
} else if (keyboardMode == 1 || keyboardMode == 2 || keyboardMode == 3)
{
if (keyboardMode == 1)
{
S1 = 1;
S2 = 1;
}
if (keyboardMode == 2)
{
S1 = 0;
S2 = 1;
}
if (keyboardMode == 3)
{
S1 = 0;
S2 = 0;
}
noteMsg = note;
if (velocity == 0) {
notes[noteMsg] = false;
}
else {
notes[noteMsg] = true;
}
unsigned int velmV = ((unsigned int) ((float) velocity) * VEL_SF);
sample_data = (channel_a & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_b & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_c & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_d & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_e & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
sample_data = (channel_f & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
if (S1 && S2) { // Highest note priority
commandTopNoteUni();
}
else if (!S1 && S2) { // Lowest note priority
commandBottomNoteUni();
}
else { // Last note priority
if (notes[noteMsg]) { // If note is on and using last note priority, add to ordered list
orderIndx = (orderIndx + 1) % 40;
noteOrder[orderIndx] = noteMsg;
}
commandLastNoteUni();
}
}
}
int getVoiceNo(int note) {
voiceToReturn = -1;//Initialise to 'null'
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].note == -1) {
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) {
return i + 1;
}
}
}
//Shouldn't get here, return voice 1
return 1;
}
void updateVoice1()
{
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[0] + 0.5);
sample_data = (channel_a & 0xFFF0000F) | (((int(mV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * VEL_SF);
sample_data = (channel_a & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
void updateVoice2()
{
unsigned int mV = (unsigned int) ((float) (voices[1].note + transpose + realoctave) * NOTE_SF * sfAdj[1] + 0.5);
sample_data = (channel_b & 0xFFF0000F) | (((int(mV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int velmV = ((unsigned int) ((float) voices[1].velocity) * VEL_SF);
sample_data = (channel_b & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
void updateVoice3()
{
unsigned int mV = (unsigned int) ((float) (voices[2].note + transpose + realoctave) * NOTE_SF * sfAdj[2] + 0.5);
sample_data = (channel_c & 0xFFF0000F) | (((int(mV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int velmV = ((unsigned int) ((float) voices[2].velocity) * VEL_SF);
sample_data = (channel_c & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
void updateVoice4()
{
unsigned int mV = (unsigned int) ((float) (voices[3].note + transpose + realoctave) * NOTE_SF * sfAdj[3] + 0.5);
sample_data = (channel_d & 0xFFF0000F) | (((int(mV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int velmV = ((unsigned int) ((float) voices[3].velocity) * VEL_SF);
sample_data = (channel_d & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
void updateVoice5()
{
unsigned int mV = (unsigned int) ((float) (voices[4].note + transpose + realoctave) * NOTE_SF * sfAdj[4] + 0.5);
sample_data = (channel_e & 0xFFF0000F) | (((int(mV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int velmV = ((unsigned int) ((float) voices[4].velocity) * VEL_SF);
sample_data = (channel_e & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
void updateVoice6()
{
unsigned int mV = (unsigned int) ((float) (voices[5].note + transpose + realoctave) * NOTE_SF * sfAdj[5] + 0.5);
sample_data = (channel_f & 0xFFF0000F) | (((int(mV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE1, sample_data);
outputDAC(DAC_NOTE2, sample_data);
unsigned int velmV = ((unsigned int) ((float) voices[5].velocity) * VEL_SF);
sample_data = (channel_f & 0xFFF0000F) | (((int(velmV)) & 0xFFFF) << 4);
outputDAC(DAC_NOTE3, sample_data);
}
void updateUnisonCheck()
{
if (digitalRead(UNISON_ON) == 1 && keyboardMode == 0) //poly
{
allNotesOff();
keyboardMode = 3;
}
if (digitalRead(UNISON_ON) == 1 && (keyboardMode == 4 || keyboardMode == 5 || keyboardMode == 6 )) // mono
{
allNotesOff();
keyboardMode = 3;
}
if (digitalRead(UNISON_ON) == 0 && (keyboardMode == 1 || keyboardMode == 2 || keyboardMode == 3 )) //switch back from unison
{
allNotesOff();
keyboardMode = previousMode;
}