-
Notifications
You must be signed in to change notification settings - Fork 3
/
6_note_MIDI_CV_poly.ino
1022 lines (897 loc) · 30.9 KB
/
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 30
//Note DACS
#define DAC_NOTE1 7
#define DAC_NOTE2 8
#define DAC_NOTE3 9
#define DAC_NOTE4 10
#define DAC_NOTE5 24
#define DAC_NOTE6 25
// Pitchbend and CC DAC
#define DAC7 26
//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
#define ENC_A 14
#define ENC_B 15
#define ENC_BTN 16
#define GATE(CH) (CH==0 ? GATE_NOTE1 : (CH==1 ? GATE_NOTE2 : (CH==2 ? GATE_NOTE3: (CH==3 ? GATE_NOTE4: (CH==4 ? GATE_NOTE5 : (CH==5 ? GATE_NOTE6 : GATE_NOTE6))))))
#define TRIG(CH) (CH==0 ? TRIG_NOTE1 : (CH==1 ? TRIG_NOTE2 : (CH==2 ? TRIG_NOTE3: (CH==3 ? TRIG_NOTE4: (CH==4 ? TRIG_NOTE5 : (CH==5 ? TRIG_NOTE6 : TRIG_NOTE6))))))
#define NOTE_DAC(CH) (CH==0 ? DAC_NOTE1 : (CH==1 ? DAC_NOTE2 : (CH==2 ? DAC_NOTE3: (CH==3 ? DAC_NOTE4: (CH==4 ? DAC_NOTE5 : (CH==5 ? DAC_NOTE6 : DAC_NOTE6))))))
#define NOTE_AB(CH) (CH==1 ? 1 : 0)
#define VEL_DAC(CH) (CH==0 ? DAC_NOTE1 : (CH==1 ? DAC_NOTE2 : (CH==2 ? DAC_NOTE3: (CH==3 ? DAC_NOTE4: (CH==4 ? DAC_NOTE5 : (CH==5 ? DAC_NOTE6 : DAC_NOTE6))))))
#define VEL_AB(CH) (CH==1 ? 0 : 1)
#define PITCH_DAC DAC7
#define PITCH_AB 0
#define CC_DAC DAC7
#define CC_AB 1
#define UNISON_ON 2
// Scale Factor will generate 0.5v/octave
// 4 octave keyboard on a 3.3v powered DAC
#define NOTE_SF 41.91f
#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 = 0;
int8_t d2, i;
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
int keyboardMode = 0;
int octave = 0;
int realoctave = 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_KEYBOARD_MODE 0
#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
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(DAC_NOTE4, OUTPUT);
pinMode(DAC_NOTE5, OUTPUT);
pinMode(DAC_NOTE6, OUTPUT);
pinMode(DAC7, OUTPUT);
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
pinMode(ENC_BTN, INPUT_PULLUP);
pinMode(UNISON_ON, INPUT);
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(DAC_NOTE4, HIGH);
digitalWrite(DAC_NOTE5, HIGH);
digitalWrite(DAC_NOTE6, HIGH);
digitalWrite(DAC7, HIGH);
SPI.begin();
//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 > 2) 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
setVoltage(PITCH_DAC, PITCH_AB, 1, 1024); // DAC7, channel 0, gain = 1X
setVoltage(CC_DAC, CC_AB, 1, 0); // DAC7, channel 1, gain = 1X
menu = SETTINGS;
updateSelection();
}
void myPitchBend(byte channel, int bend) {
if ((MIDI.getChannel() == pitchBendChan) || (pitchBendChan == 0 )) {
// Pitch bend output from 0 to 1023 mV. Left shift d2 by 4 to scale from 0 to 2047.
// With DAC gain = 1X, this will yield a range from 0 to 1023 mV. Additional amplification
// after DAC will rescale to -1 to +1V.
d2 = MIDI.getData2(); // d2 from 0 to 127, mid point = 64
// Serial.print("MIDI Pitch Bend ");
// Serial.println(bend);
setVoltage(PITCH_DAC, PITCH_AB, 1, int((bend / 8) + 1024)); // DAC7, channel 0, gain = 1X
}
}
void myControlChange(byte channel, byte number, byte value) {
if ((MIDI.getChannel() == ccChan || ccChan == 0)) {
d2 = MIDI.getData2();
// CC range from 0 to 2047 mV Left shift d2 by 5 to scale from 0 to 2047,
// and choose gain = 2X
// Serial.print("Mod Wheel ");
// Serial.println(value);
setVoltage(CC_DAC, CC_AB, 1, value << 4); // DAC7, channel 1, gain = 1X
}
}
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 == 2)
// MONO MODE
{ voices[0].note = note;
voices[0].velocity = velocity;
voices[0].timeOn = millis();
updateVoice1();
digitalWrite(GATE_NOTE1, HIGH);
digitalWrite(TRIG_NOTE1, HIGH);
monoTrig = millis();
while (millis() < monoTrig + trigTimeout) {
// wait 50 milliseconds
}
digitalWrite(TRIG_NOTE1, LOW);
voiceOn[0] = true;
} else if (keyboardMode == 1)
{
//UNISON MODE
voices[0].note = note;
voices[0].velocity = velocity;
voices[0].timeOn = millis();
updateVoice1();
voices[1].note = note;
voices[1].velocity = velocity;
voices[1].timeOn = millis();
updateVoice2();
voices[2].note = note;
voices[2].velocity = velocity;
voices[2].timeOn = millis();
updateVoice3();
voices[3].note = note;
voices[3].velocity = velocity;
voices[3].timeOn = millis();
updateVoice4();
voices[4].note = note;
voices[4].velocity = velocity;
voices[4].timeOn = millis();
updateVoice5();
voices[5].note = note;
voices[5].velocity = velocity;
voices[5].timeOn = millis();
updateVoice6();
digitalWrite(GATE_NOTE1, HIGH);
digitalWrite(TRIG_NOTE1, HIGH);
digitalWrite(GATE_NOTE2, HIGH);
digitalWrite(TRIG_NOTE2, HIGH);
digitalWrite(GATE_NOTE3, HIGH);
digitalWrite(TRIG_NOTE3, HIGH);
digitalWrite(GATE_NOTE4, HIGH);
digitalWrite(TRIG_NOTE4, HIGH);
digitalWrite(GATE_NOTE5, HIGH);
digitalWrite(TRIG_NOTE5, HIGH);
digitalWrite(GATE_NOTE6, HIGH);
digitalWrite(TRIG_NOTE6, HIGH);
unisonTrig = millis();
while (millis() < unisonTrig + 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);
voiceOn[0] = true;
voiceOn[1] = true;
voiceOn[2] = true;
voiceOn[3] = true;
voiceOn[4] = true;
voiceOn[5] = true;
}
}
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 == 2)
{
//MONO MODE
firstNoteOff();
} else if (keyboardMode == 1)
{
//UNISON MODE
allNotesOff();
}
}
void allNotesOff() {
digitalWrite(GATE_NOTE1, LOW);
digitalWrite(GATE_NOTE2, LOW);
digitalWrite(GATE_NOTE3, LOW);
digitalWrite(GATE_NOTE4, LOW);
digitalWrite(GATE_NOTE5, LOW);
digitalWrite(GATE_NOTE6, LOW);
voices[0].note = -1;
voices[1].note = -1;
voices[2].note = -1;
voices[3].note = -1;
voices[4].note = -1;
voices[5].note = -1;
voiceOn[0] = false;
voiceOn[1] = false;
voiceOn[2] = false;
voiceOn[3] = false;
voiceOn[4] = false;
voiceOn[5] = false;
}
void firstNoteOff() {
digitalWrite(GATE_NOTE1, LOW);
voices[0].note = -1;
voiceOn[0] = false;
}
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() {
if (keyboardMode == 1) {
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[0] + 0.5);
setVoltage(DAC_NOTE1, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * 1.25);
setVoltage(DAC_NOTE1, 1, 1, velmV << 4 );
}
else
{
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[0] + 0.5);
setVoltage(DAC_NOTE1, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * 1.25);
setVoltage(DAC_NOTE1, 1, 1, velmV << 4 );
}
}
void updateVoice2() {
if (keyboardMode == 1) {
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[1] + 0.5);
setVoltage(DAC_NOTE2, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * 1.25);
setVoltage(DAC_NOTE2, 1, 1, velmV << 4 );
}
else
{
unsigned int mV = (unsigned int) ((float) (voices[1].note + transpose + realoctave) * NOTE_SF * sfAdj[1] + 0.5);
setVoltage(DAC_NOTE2, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[1].velocity) * 1.25);
setVoltage(DAC_NOTE2, 1, 1, velmV << 4 );
}
}
void updateVoice3() {
if (keyboardMode == 1) {
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[2] + 0.5);
setVoltage(DAC_NOTE3, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * 1.25);
setVoltage(DAC_NOTE3, 1, 1, velmV << 4 );
}
else
{
unsigned int mV = (unsigned int) ((float) (voices[2].note + transpose + realoctave) * NOTE_SF * sfAdj[2] + 0.5);
setVoltage(DAC_NOTE3, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[2].velocity) * 1.25);
setVoltage(DAC_NOTE3, 1, 1, velmV << 4 );
}
}
void updateVoice4() {
if (keyboardMode == 1) {
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[3] + 0.5);
setVoltage(DAC_NOTE4, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * 1.25);
setVoltage(DAC_NOTE4, 1, 1, velmV << 4 );
}
else
{
unsigned int mV = (unsigned int) ((float) (voices[3].note + transpose + realoctave) * NOTE_SF * sfAdj[3] + 0.5);
setVoltage(DAC_NOTE4, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[3].velocity) * 1.25);
setVoltage(DAC_NOTE4, 1, 1, velmV << 4 );
}
}
void updateVoice5() {
if (keyboardMode == 1) {
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[4] + 0.5);
setVoltage(DAC_NOTE5, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * 1.25);
setVoltage(DAC_NOTE5, 1, 1, velmV << 4 );
}
else
{
unsigned int mV = (unsigned int) ((float) (voices[4].note + transpose + realoctave) * NOTE_SF * sfAdj[4] + 0.5);
setVoltage(DAC_NOTE5, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[4].velocity) * 1.25);
setVoltage(DAC_NOTE5, 1, 1, velmV << 4 );
}
}
void updateVoice6() {
if (keyboardMode == 1) {
unsigned int mV = (unsigned int) ((float) (voices[0].note + transpose + realoctave) * NOTE_SF * sfAdj[5] + 0.5);
setVoltage(DAC_NOTE6, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[0].velocity) * 1.25);
setVoltage(DAC_NOTE6, 1, 1, velmV << 4 );
}
else
{
unsigned int mV = (unsigned int) ((float) (voices[5].note + transpose + realoctave) * NOTE_SF * sfAdj[5] + 0.5);
setVoltage(DAC_NOTE6, 0, 1, mV);
unsigned int velmV = ((unsigned int) ((float) voices[5].velocity) * 1.25);
setVoltage(DAC_NOTE6, 1, 1, velmV << 4 );
}
}
bool notes[6][88] = {0}, initial_loop = 1;
int8_t noteOrder[6][10] = {0}, orderIndx[6] = {0};
void updateUnisonCheck()
{
if (digitalRead(UNISON_ON) == 1 && keyboardMode == 0) //poly
{
allNotesOff();
keyboardMode = 1;
}
if (digitalRead(UNISON_ON) == 1 && keyboardMode == 2) // mono
{
allNotesOff();
keyboardMode = 1;
}
if (digitalRead(UNISON_ON) == 0 && keyboardMode == 1 ) //poly
{
allNotesOff();
keyboardMode = previousMode;
}
}
void loop()
{
// int8_t noteMsg, velocity, channel, i;
//int8_t i;
updateEncoderPos();
updateEncoderPosB();
encButton.update();
// updateUnisonCheck();
if (encButton.fell()) {
if (initial_loop == 1) {
initial_loop = 0; // Ignore first push after power-up
}
else {
updateMenu();
}
}
// Check if highlighting timer expired, and remove highlighting if so
if (highlightEnabled && ((millis() - highlightTimer) > HIGHLIGHT_TIMEOUT)) {
highlightEnabled = false;
menu = SETTINGS; // Return to top level menu
updateSelection(); // Refresh screen without selection highlight
}
myusb.Task();
midi1.read(masterChan); //USB HOST MIDI Class Compliant
MIDI.read(masterChan);//MIDI 5 Pin DIN
usbMIDI.read(masterChan); //USB Client MIDI
}
void setVoltage(int dacpin, bool channel, bool gain, unsigned int mV)
{
int command = channel ? 0x9000 : 0x1000;
command |= gain ? 0x0000 : 0x2000;
command |= (mV & 0x0FFF);
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
digitalWrite(dacpin, LOW);
SPI.transfer(command >> 8);
SPI.transfer(command & 0xFF);
digitalWrite(dacpin, HIGH);
SPI.endTransaction();
}
void updateEncoderPos() {
static int encoderA, encoderB, encoderA_prev;
encoderA = digitalRead(ENC_A);
if ((!encoderA) && (encoderA_prev)) { // A has gone from high to low
if (highlightEnabled) { // Update encoder position
encoderPosPrev = encoderPos;
encoderB ? encoderPos++ : encoderPos--;
}
else {
highlightEnabled = true;
encoderPos = 0; // Reset encoder position if highlight timed out
encoderPosPrev = 0;
}
highlightTimer = millis();
updateSelection();
}
encoderA_prev = encoderA;
}
void updateEncoderPosB() {
static int encoderA, encoderB, encoderB_prev;
encoderB = digitalRead(ENC_B);
if ((!encoderB) && (encoderB_prev)) { // A has gone from high to low
if (highlightEnabled) { // Update encoder position
encoderPosPrev = encoderPos;
encoderA ? encoderPos-- : encoderPos++;
}
else {
highlightEnabled = true;
encoderPos = 0; // Reset encoder position if highlight timed out
encoderPosPrev = 0;
}
highlightTimer = millis();
updateSelection();
}
encoderB_prev = encoderB;
}
int setCh;
char setMode[6];
void updateMenu() { // Called whenever button is pushed
if (highlightEnabled) { // Highlight is active, choose selection
switch (menu) {
case SETTINGS:
switch (mod(encoderPos, 5)) {
case 0:
menu = KEYBOARD_MODE_SET_CH;
break;
case 1:
menu = MIDI_CHANNEL_SET_CH;
break;
case 2:
menu = TRANSPOSE_SET_CH;
break;
case 3:
menu = OCTAVE_SET_CH;
break;
case 4:
menu = SCALE_FACTOR;
break;
}
break;
case KEYBOARD_MODE_SET_CH: // Save keyboard mode setting to EEPROM
menu = SETTINGS;
EEPROM.write(ADDR_KEYBOARD_MODE, keyboardMode);
break;
case MIDI_CHANNEL_SET_CH: // Save midi channel setting to EEPROM
menu = SETTINGS;
EEPROM.write(ADDR_MASTER_CHAN, masterChan);
break;
case TRANSPOSE_SET_CH: // Save transpose setting to EEPROM
menu = SETTINGS;
EEPROM.write(ADDR_TRANSPOSE, masterTran);
EEPROM.write(ADDR_REAL_TRANSPOSE, masterTran - 12);
transpose = (masterTran - 12);
break;
case OCTAVE_SET_CH: // Save octave adjust setting to EEPROM
menu = SETTINGS;
EEPROM.write(ADDR_OCTAVE, octave);
if (octave == 0) realoctave = -36;
if (octave == 1) realoctave = -24;
if (octave == 2) realoctave = -12;
if (octave == 3) realoctave = 0;
EEPROM.write(ADDR_REALOCTAVE, realoctave);
break;
case SCALE_FACTOR:
setCh = mod(encoderPos, 7);
switch (setCh) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
menu = SCALE_FACTOR_SET_CH;
break;
case 6:
menu = SETTINGS;
break;
}
break;
case SCALE_FACTOR_SET_CH: // Save scale factor to EEPROM
menu = SCALE_FACTOR;
EEPROM.put(ADDR_SF_ADJUST + setCh * sizeof(float), sfAdj[setCh]);
break;
}
}
else { // Highlight wasn't visible, reinitialize highlight timer
highlightTimer = millis();
highlightEnabled = true;
}
encoderPos = 0; // Reset encoder position
encoderPosPrev = 0;
updateSelection(); // Refresh screen
}
void updateSelection() { // Called whenever encoder is turned
display.clearDisplay();
switch (menu) {
case KEYBOARD_MODE_SET_CH:
if (menu == KEYBOARD_MODE_SET_CH) keyboardMode = mod(encoderPos, 3);
case MIDI_CHANNEL_SET_CH:
if (menu == MIDI_CHANNEL_SET_CH) masterChan = mod(encoderPos, 17);
case TRANSPOSE_SET_CH:
if (menu == TRANSPOSE_SET_CH) masterTran = mod(encoderPos, 25);
case OCTAVE_SET_CH:
if (menu == OCTAVE_SET_CH) octave = mod(encoderPos, 4);
case SETTINGS:
display.setCursor(0, 0);
display.setTextColor(WHITE, BLACK);
display.print(F("SETTINGS"));
display.setCursor(0, 16);
if (menu == SETTINGS) setHighlight(0, 5);
display.print(F("Keyboard Mode "));
if (menu == KEYBOARD_MODE_SET_CH) display.setTextColor(BLACK, WHITE);
if (keyboardMode == 0) display.print("Poly ");
if (keyboardMode == 1) display.print("Unison");
if (keyboardMode == 2) display.print("Mono ");
display.println(F(""));
display.setTextColor(WHITE, BLACK);
if (menu == SETTINGS) setHighlight(1, 5);
display.print(F("Midi Channel "));
if (menu == MIDI_CHANNEL_SET_CH) display.setTextColor(BLACK, WHITE);
if (masterChan == 0) display.print("Omni");
else display.print(masterChan);
display.println(F(" "));
display.setTextColor(WHITE, BLACK);
if (menu == SETTINGS) setHighlight(2, 5);
display.print(F("Transpose "));
if (menu == TRANSPOSE_SET_CH) display.setTextColor(BLACK, WHITE);
display.print(masterTran - 12);
display.println(F(" "));
display.setTextColor(WHITE, BLACK);
if (menu == SETTINGS) setHighlight(3, 5);
display.print(F("Octave Adjust "));
if (menu == OCTAVE_SET_CH) display.setTextColor(BLACK, WHITE);
if (octave == 0) display.print("-3 ");
if (octave == 1) display.print("-2 ");
if (octave == 2) display.print("-1 ");
if (octave == 3) display.print(" 0 ");
display.println(F(" "));
display.setTextColor(WHITE, BLACK);
if (menu == SETTINGS) setHighlight(4, 5);
else display.setTextColor(WHITE, BLACK);
display.println(F("Scale Factor "));
break;
case SCALE_FACTOR_SET_CH:
if ((encoderPos > encoderPosPrev) && (sfAdj[setCh] < 1.1))
sfAdj[setCh] += 0.001f;
else if ((encoderPos < encoderPosPrev) && (sfAdj[setCh] > 0.9))
sfAdj[setCh] -= 0.001f;
case SCALE_FACTOR:
display.setCursor(0, 0);
display.setTextColor(WHITE, BLACK);
display.print(F("SET SCALE FACTOR"));
display.setCursor(0, 8);
if (menu == SCALE_FACTOR) setHighlight(0, 7);
display.print(F("Note 1: "));
if ((menu == SCALE_FACTOR_SET_CH) && setCh == 0) display.setTextColor(BLACK, WHITE);
display.println(sfAdj[0], 3);
if (menu == SCALE_FACTOR) setHighlight(1, 7);
else display.setTextColor(WHITE, BLACK);
display.print(F("Note 2: "));
if ((menu == SCALE_FACTOR_SET_CH) && setCh == 1) display.setTextColor(BLACK, WHITE);
display.println(sfAdj[1], 3);
if (menu == SCALE_FACTOR) setHighlight(2, 7);
else display.setTextColor(WHITE, BLACK);
display.print(F("Note 3: "));
if ((menu == SCALE_FACTOR_SET_CH) && setCh == 2) display.setTextColor(BLACK, WHITE);
display.println(sfAdj[2], 3);
if (menu == SCALE_FACTOR) setHighlight(3, 7);
else display.setTextColor(WHITE, BLACK);
display.print(F("Note 4: "));
if ((menu == SCALE_FACTOR_SET_CH) && setCh == 3) display.setTextColor(BLACK, WHITE);
display.println(sfAdj[3], 3);
if (menu == SCALE_FACTOR) setHighlight(4, 7);
else display.setTextColor(WHITE, BLACK);
display.print(F("Note 5: "));
if ((menu == SCALE_FACTOR_SET_CH) && setCh == 4) display.setTextColor(BLACK, WHITE);
display.println(sfAdj[4], 3);
if (menu == SCALE_FACTOR) setHighlight(5, 7);
else display.setTextColor(WHITE, BLACK);
display.print(F("Note 6: "));
if ((menu == SCALE_FACTOR_SET_CH) && setCh == 5) display.setTextColor(BLACK, WHITE);
display.println(sfAdj[5], 3);