-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMidiBox.ino
1180 lines (1082 loc) · 32.6 KB
/
MidiBox.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
/*
MIDI box for STM32
Pin mapping:
3.3 : SD VDD (SD pin 4)
GND : SD VSS (SD pin 3)
PA2 : MIDI 1 TX
PA3 : MIDI 1 RX
PA4 : SD CS (SD pin 1)
PA5 : SD SCK (SD pin 5)
PA6 : SD MISO (SD pin 7)
PA7 : SD MOSI (SD pin 2)
PA8 : Gate 1
PA9 : MIDI MUX TX
PA10: MIDI MUX RX
PA11: USB-
PA12: USB+
PA15: Gate 3
PB1 : Gate 2
PB3 : Gate 4
PB4 : Gate 5
PB5 : Gate 6
PB6 : SCL (LCD)
PB7 : SDA (LCD)
PB8 : Gate 7
PB9 : Gate 8
PB10: MIDI 2 TX
PB11: MIDI 2 RX
PB12: UP
PB13: DOWN
PB14: LEFT
PB15: RIGHT
PC13: LED
*/
// Compile-time settings
#define MIDIBOX_VERSION "1.0"
#define MIDIBOX_USB_SERIAL 0
#define MIDIBOX_USB_MIDI 1
#define MIDIBOX_EXT_COUNT 0
#define MIDIBOX_GATES 0
static const int SD_CS = PA4;
static const char *sysExPath = "/JMC_MIDI/SYSEX/";
static const char *settingsPath = "/JMC_MIDI/SETTINGS/";
// Includes
#include <USBComposite.h>
#include "Midi.h"
#include "Menu.h"
// Globals
#if MIDIBOX_USB_SERIAL || MIDIBOX_USB_MIDI
#define MIDIBOX_USB 1
#endif
MidiSerialPort midi1("MIDI IN 1", "MIDI OUT 1", Serial2);
MidiSerialPort midi2("MIDI IN 2", "MIDI OUT 2", Serial3);
#if MIDIBOX_EXT_COUNT == 0
MidiSerialPort midi3("MIDI IN 3", "MIDI OUT 3", Serial1);
#endif
#if MIDIBOX_EXT_COUNT >= 1
MidiSerialMux serialMux(Serial1);
MidiSerialMuxPort midiMux1("EXT1 MIDI IN 1", "EXT1 MIDI OUT 1", serialMux, 0);
MidiSerialMuxPort midiMux2("EXT1 MIDI IN 2", "EXT1 MIDI OUT 2", serialMux, 1);
#endif
#if MIDIBOX_EXT_COUNT >= 2
MidiSerialMuxPort midiMux3("EXT2 MIDI IN 3", "EXT2 MIDI OUT 3", serialMux, 2);
MidiSerialMuxPort midiMux4("EXT2 MIDI IN 4", "EXT2 MIDI OUT 4", serialMux, 3);
#endif
#if MIDIBOX_EXT_COUNT >= 3
MidiSerialMuxPort midiMux5("EXT3 MIDI IN 5", "EXT3 MIDI OUT 5", serialMux, 4);
MidiSerialMuxPort midiMux6("EXT3 MIDI IN 6", "EXT3 MIDI OUT 6", serialMux, 5);
#endif
#if MIDIBOX_EXT_COUNT >= 4
MidiSerialMuxPort midiMux7("EXT4 MIDI IN 7", "EXT4 MIDI OUT 7", serialMux, 6);
MidiSerialMuxPort midiMux8("EXT4 MIDI IN 8", "EXT4 MIDI OUT 8", serialMux, 7);
#endif
#if MIDIBOX_USB_MIDI
MidiUSBPort usb1("USB PORT", "USB PORT", 0); // USB-MIDI interface
#endif
MidiLoopback bus1("LOOPBACK BUS 1"); // Internal bus
MidiLoopback bus2("LOOPBACK BUS 2"); // Internal bus
MidiParaphonyMapper paraBus("PARAPHONIC MAP."); // Paraphonic mapper
#if MIDIBOX_GATES
MidiGpioGate gates("ANALOG GATES", PA8, PB1, PA15, PB3, PB4, PB5, PB8, PB9);
#endif
MidiIn *inputs[] = {
&midi1,
&midi2,
#if MIDIBOX_EXT_COUNT == 0
&midi3,
#endif
#if MIDIBOX_EXT_COUNT >= 1
&midiMux1,
&midiMux2,
#endif
#if MIDIBOX_EXT_COUNT >= 2
&midiMux3,
&midiMux4,
#endif
#if MIDIBOX_EXT_COUNT >= 3
&midiMux5,
&midiMux6,
#endif
#if MIDIBOX_EXT_COUNT >= 4
&midiMux7,
&midiMux8,
#endif
#if MIDIBOX_USB_MIDI
&usb1,
#endif
&bus1,
&bus2,
¶Bus
};
const int inputCount = sizeof(inputs)/sizeof(inputs[0]);
const int repeaterInputCount = 10;
MidiOut *outputs[] = {
&midi1,
&midi2,
#if MIDIBOX_EXT_COUNT >= 1
&midiMux1,
&midiMux2,
#endif
#if MIDIBOX_EXT_COUNT >= 2
&midiMux3,
&midiMux4,
#endif
#if MIDIBOX_EXT_COUNT >= 3
&midiMux5,
&midiMux6,
#endif
#if MIDIBOX_EXT_COUNT >= 4
&midiMux7,
&midiMux8,
#endif
#if MIDIBOX_USB_MIDI
&usb1,
#endif
&bus1,
&bus2,
#if MIDIBOX_GATES
&gates,
#endif
¶Bus
};
const int outputCount = sizeof(outputs)/sizeof(outputs[0]);
#if MIDIBOX_EXT_COUNT
const int repeaterOutputCount = (MIDIBOX_EXT_COUNT) * 2 + 2;
#endif
// Global variables
MidiIn *sysExSource = NULL;
MidiOut *sysExTarget = NULL;
char currentProfile = 1;
#if MIDIBOX_EXT_COUNT
char repeater = 0;
#endif
int pollMillis = 0;
int blinkPhase = 0;
#if MIDIBOX_USB_SERIAL
// USB serial for firmware and configuration upload
USBCompositeSerial usbSerial;
#endif
// Global functions
void routeMidi();
// Select a MIDI input or output
template<typename StreamType>
struct MenuStreamSelect: public MenuItem {
MenuStreamSelect(const char *name_, MenuItem &subMenu_, StreamType **streams_, int streamCount_):
MenuItem(name_),
subMenu(subMenu_),
streams(streams_),
streamCount(streamCount_) {
if(&subMenu != this)
subMenu.parent = this;
}
virtual void onEnter() {
stream = 0;
}
virtual const char * line2() {
return streams[stream]->name;
}
virtual MenuItem * onKeyPressed(int keys) {
if(keys == Menu::KEY_LEFT && stream > 0)
--stream;
else if(keys == Menu::KEY_RIGHT && stream < streamCount - 1)
++stream;
else if(keys == Menu::KEY_DOWN)
return &subMenu;
return this;
}
MenuItem &subMenu;
StreamType **streams;
int streamCount;
int stream;
};
static const char *msgTypeName[] = {
"CHANNEL 1",
"CHANNEL 2",
"CHANNEL 3",
"CHANNEL 4",
"CHANNEL 5",
"CHANNEL 6",
"CHANNEL 7",
"CHANNEL 8",
"CHANNEL 9",
"CHANNEL 10",
"CHANNEL 11",
"CHANNEL 12",
"CHANNEL 13",
"CHANNEL 14",
"CHANNEL 15",
"CHANNEL 16",
"SYSTEM EXCL.", // F0
"TIME CODE", // F1
"SONG POSITION", // F2
"SONG SELECT", // F3
"TYPE F4", // F4
"TYPE F5", // F5
"TUNE REQUEST", // F6
"SYSTEM EX END", // F7
"MIDI CLOCK", // F8
"TYPE F9", // F9
"START", // FA
"CONTINUE", // FB
"STOP", // FC
"TYPE FD", // FD
"ACTIVE SENSE", // FE
"RESET" // FF
};
const char *getSettingsPath() {
static char fileName[256];
strcpy(fileName, settingsPath);
int l = strlen(fileName);
fileName[l] = currentProfile + '0';
strcpy(&fileName[l+1], ".TXT");
return fileName;
}
struct MenuFilterEdit: public MenuItem {
MenuFilterEdit(const char *name_): MenuItem(name_), mask(0) {}
virtual void onEnter() {
msgType = -2;
}
virtual const char * line2() {
if(msgType == -2)
return "ENABLE ALL";
if(msgType == -1)
return "DISABLE ALL";
bool copyName = true;
for(int i = 0; i < 15; ++i) {
if(copyName) {
char c = msgTypeName[msgType][i];
if(c) {
displayLine[i] = c;
} else {
displayLine[i] = ' ';
copyName = false;
}
} else {
displayLine[i] = ' ';
}
}
displayLine[15] = (mask & (1 << msgType)) ? (char)0x7E /* -> */ : 'x';
displayLine[16] = '\0';
return displayLine;
}
virtual MenuItem * onKeyPressed(int keys) {
if(keys == Menu::KEY_LEFT && msgType > -2)
--msgType;
else if(keys == Menu::KEY_RIGHT && msgType < 31)
++msgType;
else if(keys == Menu::KEY_DOWN) {
if(msgType == -2)
mask = ~0;
else if(msgType == -1)
mask = 0;
else
mask ^= (1 << msgType);
}
return this;
}
char displayLine[17];
int msgType; // 0-31
int mask;
};
struct MenuProfileSelector: public MenuNumberSelect {
MenuProfileSelector(const char *name_, MenuItem &subMenu_):
MenuNumberSelect(name_, subMenu_, 1, 9) {
strcpy(l2, "PROFILE ?");
}
virtual void onEnter() {
number = currentProfile;
l2[8] = '0' + currentProfile;
loadSettings();
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
currentProfile = number;
if(keys != Menu::KEY_DOWN)
onEnter();
return nextMenu;
}
virtual const char * line2() {
return l2;
}
char l2[17];
};
struct MenuSaveSettings: public MenuConfirm {
MenuSaveSettings(const char *name_): MenuConfirm(name_, "DOWN TO SAVE") {}
virtual void onConfirmed() {
if(SD.begin(SD_CS)) {
SD.mkdir(settingsPath);
SD.remove(getSettingsPath());
File f = SD.open(getSettingsPath(), FILE_WRITE);
if(f)
saveSettings(&f);
f.close();
SD.end();
}
}
};
struct MenuSyncDivider: public MenuNumberSelect {
MenuSyncDivider(const char *name_):
MenuNumberSelect(name_, *this, 1, 64) {}
virtual void onEnter() {
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
number = stream.getSyncDivider();
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
stream.setSyncDivider(number);
return nextMenu;
}
};
struct MenuChannelMap: public MenuNumberSelect {
MenuChannelMap(const char *name_):
MenuNumberSelect(name_, *this, 0, 16) {}
virtual void onEnter() {
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
number = stream.getChannelMapping(channelMenu->number);
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
stream.setChannelMapping(channelMenu->number, number);
return nextMenu;
}
};
struct MenuTranspose: public MenuNumberSelect {
MenuTranspose(const char *name_):
MenuNumberSelect(name_, *this, -127, 127) {}
virtual void onEnter() {
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
number = stream.getTransposition(channelMenu->number);
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
stream.transpose(channelMenu->number, number);
return nextMenu;
}
};
struct MenuVelocityScale: public MenuNumberSelect {
MenuVelocityScale(const char *name_):
MenuNumberSelect(name_, *this, 0, 100) {}
virtual void onEnter() {
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
number = stream.getVelocityScale(channelMenu->number);
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
stream.setVelocityScale(channelMenu->number, number);
return nextMenu;
}
};
struct MenuVelocityOffset: public MenuNumberSelect {
MenuVelocityOffset(const char *name_):
MenuNumberSelect(name_, *this, -127, 127) {}
virtual void onEnter() {
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
number = stream.getVelocityOffset(channelMenu->number);
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(channelMenu->parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
stream.setVelocityOffset(channelMenu->number, number);
return nextMenu;
}
};
struct MenuChannelFilter: public MenuFilterEdit {
MenuChannelFilter(const char *name_): MenuFilterEdit(name_) {}
virtual void onEnter() {
MenuFilterEdit::onEnter();
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
mask = stream.getFilter();
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuFilterEdit::onKeyPressed(keys);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
stream.setFilter(mask);
return nextMenu;
}
};
struct MenuOutputRoute: public MenuStreamSelect<MidiOut> {
MenuOutputRoute(const char *name_): MenuStreamSelect(name_, *this, outputs, outputCount) {}
virtual void onEnter() {
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &mux = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
for(stream = 0; stream < outputCount; ++stream) {
if(mux.out == outputs[stream])
return;
}
stream = 0;
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuStreamSelect::onKeyPressed(keys);
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &mux = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
mux.out = outputs[stream];
return nextMenu;
}
};
struct MenuSysExRecord: public MenuItem {
MenuSysExRecord(const char *name_): MenuItem(name_) {}
virtual void onEnter() {
MenuStreamSelect<MidiIn> *streamSelect = (MenuStreamSelect<MidiIn> *)parent;
MidiIn *port = inputs[streamSelect->stream];
if(!SD.begin(SD_CS)) {
return;
}
SD.mkdir(sysExPath);
char fileName[128];
// Find an available file
int i;
for(i = 0; i < 10000; ++i) {
snprintf(fileName, sizeof(fileName), "%sREC%04d.SYX", sysExPath, i);
if(!SD.exists(fileName)) {
// File found
break;
}
}
if(i >= 10000) {
// Directory full !
SD.end();
return;
}
file = SD.open(fileName, FILE_WRITE);
fileStream.setFile(&file);
sysExSource = port;
sysExTarget = &fileStream;
}
virtual const char * line2() {
if(!file) {
return "SD CARD ERROR";
}
return "ANY KEY TO STOP";
}
virtual MenuItem * onKeyPressed(int keys) {
return parent;
}
virtual void onExit() {
sysExSource = NULL;
sysExTarget = NULL;
if(file) {
file.close();
SD.end();
}
}
File file;
SysExFileRecorder fileStream;
};
struct MenuSysExReplay: public MenuItem {
MenuSysExReplay(const char *name_): MenuItem(name_) {}
virtual void onEnter() {
MenuStreamSelect<MidiOut> *streamSelect = (MenuStreamSelect<MidiOut> *)(parent->parent);
MidiOut *port = outputs[streamSelect->stream];
fileStream.setFile(&((MenuFileSelect *)parent)->file);
sysExSource = &fileStream;
sysExTarget = port;
}
virtual const char * line2() {
return "SENDING ...";
}
virtual MenuItem * onKeyPressed(int keys) {
return parent;
}
virtual void onExit() {
sysExSource = NULL;
sysExTarget = NULL;
}
SysExFilePlayer fileStream;
};
struct MenuPanic: public MenuConfirm {
MenuPanic(const char *name_): MenuConfirm(name_, "DOWN: ALL NOTEOFF") {}
virtual void onConfirmed() {
for(int i = 0; i < outputCount; ++i) {
MidiOut *output = outputs[i];
for(int c = 0; c < 16; ++c) {
// Wait until the port is ready ...
// It's done that way because it's an emergency situation
while(!output->availableForWrite(0, this));
routeMidi();
// Send the actual "Note Off" message
output->write(MIDI_CTL | c, this);
output->write(123, this);
output->write(0, this);
}
}
}
};
struct MenuResetChannelProcessing: public MenuConfirm {
MenuResetChannelProcessing(const char *name_): MenuConfirm(name_, "DOWN TO RESET") {}
virtual void onConfirmed() {
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
MidiRoute &stream = inputs[inputMenu->stream]->getRoute(routeMenu->number - 1);
stream.resetProcessing();
}
};
struct MenuResetProfile: public MenuConfirm {
MenuResetProfile(const char *name_): MenuConfirm(name_, "DOWN TO RESET") {}
virtual void onConfirmed() {
resetSettings();
}
};
struct MenuParaTargetChannel: public MenuNumberSelect {
MenuParaTargetChannel(const char *name_):
MenuNumberSelect(name_, *this, 1, 16) {}
virtual void onEnter() {
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
number = paraBus.getNextChannel(channelMenu->number);
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
paraBus.setNextChannel(channelMenu->number, number);
return nextMenu;
}
};
struct MenuParaPolyphony: public MenuNumberSelect {
MenuParaPolyphony(const char *name_):
MenuNumberSelect(name_, *this, 1, MidiParaphonyMapper::maxPoly) {}
virtual void onEnter() {
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
number = paraBus.getPolyphony(channelMenu->number);
}
virtual MenuItem * onKeyPressed(int keys) {
MenuItem *nextMenu = MenuNumberSelect::onKeyPressed(keys);
MenuNumberSelect *channelMenu = (MenuNumberSelect *)(parent->parent);
paraBus.setPolyphony(channelMenu->number, number);
return nextMenu;
}
};
struct MenuDeleteRoute: public MenuConfirm {
MenuDeleteRoute(const char *name_): MenuConfirm(name_, "DOWN TO DELETE") {}
virtual MenuItem * onKeyPressed(int keys) {
if(keys == Menu::KEY_DOWN) {
MenuNumberSelect *routeMenu = (MenuNumberSelect *)(parent->parent);
MenuStreamSelect<MidiIn> *inputMenu = (MenuStreamSelect<MidiIn> *)(routeMenu->parent);
inputs[inputMenu->stream]->deleteRoute(routeMenu->number - 1);
return parent->parent->parent;
}
return MenuConfirm::onKeyPressed(keys);
}
};
MenuConfirm menuNoMoreRoute(" CANNOT CREATE"," ROUTE");
struct MenuRouteSelect: public MenuNumberSelect {
MenuRouteSelect(const char *name_, MenuItem &subMenu_):
MenuNumberSelect(name_, subMenu_, 1, 1) {}
virtual void onEnter() {
MenuNumberSelect::onEnter();
MidiIn *in = inputs[((MenuStreamSelect<MidiIn> *)parent)->stream];
maximum = in->inRouteCount;
if(MidiIn::countRoutes() < MidiIn::maxRouteCount && maximum < MidiIn::maxRoutes)
++maximum;
}
virtual MenuItem * onKeyPressed(int keys) {
if(keys == Menu::KEY_DOWN) {
MidiIn *in = inputs[((MenuStreamSelect<MidiIn> *)parent)->stream];
if(number > in->inRouteCount) {
// Create a new route
if(!in->createRoute(&midi1))
return &menuNoMoreRoute;
}
return &subMenu;
}
return MenuNumberSelect::onKeyPressed(keys);
}
};
MenuParaPolyphony paraPolyphony("CHAN. POLYPHONY");
MenuParaTargetChannel paraTargetChannel("NEXT CHANNEL");
MenuList paraSettings("PARA. SETTINGS", paraPolyphony, paraTargetChannel);
MenuNumberSelect connectionsParaChannel("PARA. CHANNEL", paraSettings, 1, 16);
MenuPanic menuPanic("ALL NOTES OFF");
MenuResetProfile menuResetProfile("RESET PROFILE");
MenuSaveSettings menuSaveSettings("SAVE PROFILE");
MenuSysExRecord sysExRecordStart("RECORDING SYSEX");
MenuSysExReplay sysExReplayStart("REPLAYING SYSEX");
MenuFileSelect sysExReplayFile("SYSEX REPLAY", sysExReplayStart, SD_CS, sysExPath);
MenuStreamSelect<MidiIn> sysExRecordPort("RECORD FROM", sysExRecordStart, inputs, inputCount);
MenuStreamSelect<MidiOut> sysExReplayPort("REPLAY TO", sysExReplayFile, outputs, outputCount);
MenuResetChannelProcessing resetChannelProcessing("RESET CHAN PROC.");
MenuTranspose transposeMenu("TRANSPOSE SEMI.");
MenuVelocityScale velocityScaleMenu("VELOCITY SCALE");
MenuVelocityOffset velocityOffsetMenu("VELOCITY OFFSET");
MenuChannelMap mapChannelMenu("MAP TO CHANNEL");
MenuList processingMenu("CHANNEL PROCESS.", mapChannelMenu, transposeMenu, velocityScaleMenu, velocityOffsetMenu);
MenuNumberSelect connectionsChannelProcessing("CHANNEL PROCESS.", processingMenu, 0, 16);
MenuSyncDivider connectionsSyncDivider("CLOCK DIVIDER");
MenuDeleteRoute deleteRoute("DELETE ROUTE");
MenuChannelFilter connectionsFilterMenu("ROUTE FILTER");
MenuOutputRoute connectionsOutputMenu("OUTPUT");
MenuList connectionsSetupMenu("ROUTE SETUP", connectionsOutputMenu, connectionsFilterMenu, deleteRoute, connectionsSyncDivider, connectionsChannelProcessing, resetChannelProcessing);
MenuRouteSelect connectionsRouteMenu("ROUTE", connectionsSetupMenu);
MenuStreamSelect<MidiIn> connectionsMenu("CONNECTIONS", connectionsRouteMenu, inputs, inputCount);
MenuList mainMenu("MAIN MENU", connectionsMenu, connectionsParaChannel, menuSaveSettings, menuResetProfile, sysExRecordPort, sysExReplayPort, menuPanic);
MenuProfileSelector profileSelector("DOWN FOR MENU", mainMenu);
Menu menu(profileSelector, PB12, PB13, PB14, PB15);
template<typename T>
void saveSettings(T *file) {
bool hasOutput = false;
file->println("VERSION:" MIDIBOX_VERSION);
file->println("");
for(int f = 0; f < inputCount; ++f) {
if(hasOutput) {
file->println("");
file->println("");
}
hasOutput = false;
for(int t = 0; t < inputs[f]->inRouteCount; ++t) {
MidiRoute &m = inputs[f]->getRoute(t);
if(!hasOutput)
{
hasOutput = true;
file->print("INPUT:");
file->println(inputs[f]->name);
}
file->println("");
file->print(" OUTPUT:");
file->println(m.out->name);
file->print(" FILTER:");
for(int bit = 0; bit < 32; ++bit) {
file->print(m.getFilter() & (1 << bit) ? '1':'0');
}
file->println("");
file->print(" DIVIDE:");
file->println(m.getSyncDivider());
for(int c = 1; c <= 16; ++c) {
if(!m.processingEnabled(c))
continue;
file->print(" CHANNEL:");
file->println(c);
file->print(" MAP:");
file->println(m.getChannelMapping(c));
file->print(" TRANSPOSE:");
file->println(m.getTransposition(c));
file->print(" VELOC_SCALE:");
file->println(m.getVelocityScale(c));
file->print(" VELOC_OFFSET:");
file->println(m.getVelocityOffset(c));
}
}
}
file->println("");
hasOutput = false;
for(int c = 1; c <= 16; ++c) {
if(paraBus.getPolyphony(c) == 16 && paraBus.getNextChannel(c) == c)
continue;
hasOutput = true;
file->println("");
file->print("PARA_CHANNEL:");
file->println(c);
file->print(" POLYPHONY:");
file->println(paraBus.getPolyphony(c));
file->print(" NEXT_CHANNEL:");
file->println(paraBus.getNextChannel(c));
}
if(hasOutput)
file->println("");
}
int getInputStreamIndex(const char *name) {
for(int i = 0; i < inputCount; ++i)
if(strcmp(inputs[i]->name, name) == 0)
return i;
return -1;
}
MidiOut * getOutputByName(const char *name) {
for(int i = 0; i < outputCount; ++i)
if(strcmp(outputs[i]->name, name) == 0)
return outputs[i];
return NULL;
}
void resetSettings() {
for(int i = 0; i < inputCount; ++i)
inputs[i]->clearRoutes();
paraBus.init();
}
template<typename T>
void loadSettingsFrom(T *file) {
String key;
String value;
int from = -1;
int channel = -1;
int paraChannel = -1;
MidiOut *to = NULL;
MidiRoute *mux = NULL;
resetSettings();
while(file->available()) {
while(file->available() && (file->peek() == ' ' || file->peek() == '\r' || file->peek() == '\n' || file->peek() == '\t'))
file->read(); // Skip leading spaces
key = file->readStringUntil(':');
value = file->readStringUntil('\n');
// Trim \r
if(value.length() && value[value.length() - 1] == '\r')
value = value.substring(0, value.length() - 1);
if(key.length() < 1 || key.length() > 16 || value.length() < 1 || value.length() > 64)
continue;
if(key == "INPUT") {
from = getInputStreamIndex(value.c_str());
mux = NULL;
} else if(key == "OUTPUT") {
to = getOutputByName(value.c_str());
if(from != -1 && to)
mux = inputs[from]->createRoute(to);
else
mux = NULL;
} else if(key == "FILTER") {
int mask = 0;
if(value.length() == 32) {
for(int bit = 0; bit < 32; ++bit) {
if(value[bit] == '1')
mask |= (1 << bit);
}
}
if(mux)
mux->setFilter(mask);
} else if(key == "DIVIDE") {
if(mux)
mux->setSyncDivider(value.toInt());
} else if(key == "CHANNEL") {
channel = value.toInt();
if(channel < 1 || channel > 16)
channel = -1;
} else if(key == "MAP") {
if(mux && channel > 0) {
mux->setChannelMapping(channel, value.toInt());
}
} else if(key == "TRANSPOSE") {
if(mux && channel > 0) {
mux->transpose(channel, value.toInt());
}
} else if(key == "VELOC_SCALE") {
if(mux && channel > 0) {
mux->setVelocityScale(channel, value.toInt());
}
} else if(key == "VELOC_OFFSET") {
if(mux && channel > 0) {
mux->setVelocityOffset(channel, value.toInt());
}
} else if(key == "PARA_CHANNEL") {
paraChannel = value.toInt();
} else if(key == "POLYPHONY") {
paraBus.setPolyphony(paraChannel, value.toInt());
} else if(key == "NEXT_CHANNEL") {
paraBus.setNextChannel(paraChannel, value.toInt());
}
}
}
void loadSettings() {
resetSettings();
#if MIDIBOX_EXT_COUNT
if(repeater)
return;
#endif
if(SD.begin(SD_CS)) {
File file = SD.open(getSettingsPath());
if(file) {
loadSettingsFrom(&file);
file.close();
SD.end();
return;
}
SD.end();
}
// Load failed
for(int i = 0; i < inputCount; ++i) {
// Default profile: route all inputs to the first output
// This allows using this as a midi merger by default
MidiRoute *r = inputs[i]->createRoute(outputs[0]);
r->setFilter(~0);
}
}
void routeMidi() {
if(sysExSource && sysExTarget && sysExSource->available() && sysExTarget->availableForWrite(0, sysExSource)) {
// Transmitting a sysEx file
sysExTarget->write(sysExSource->read(), sysExSource);
if(sysExSource->eof()) {
// Finished transfer: reset and go back to main menu
menu.switchToMain();
}
}
for(int i = 0; i < inputCount; ++i)
inputs[i]->route();
MidiIn::routeAll();
}
#if MIDIBOX_EXT_COUNT
void routeRepeater() {
byte b;
// Handle MUX -> MIDI
#if MIDIBOX_EXT_COUNT >= 1
if(midiMux1.available()) {
b = midiMux1.read();
if(repeater != 1)
midiMux1.MidiOut::write(b, NULL);
else
midi1.MidiOut::write(b, NULL);
}
if(midiMux2.available()) {
b = midiMux2.read();
if(repeater != 1)
midiMux2.MidiOut::write(b, NULL);
else {
midi2.MidiOut::write(b, NULL);
#if MIDIBOX_GATES
gates.MidiOut::write(b, NULL);
#endif
}
}
#endif
#if MIDIBOX_EXT_COUNT >= 2
if(midiMux3.available()) {
b = midiMux3.read();
if(repeater != 2)
midiMux3.MidiOut::write(b, NULL);
else
midi1.MidiOut::write(b, NULL);
}
if(midiMux4.available()) {
b = midiMux4.read();
if(repeater != 2)
midiMux4.MidiOut::write(b, NULL);
else {
midi2.MidiOut::write(b, NULL);
#if MIDIBOX_GATES
gates.MidiOut::write(b, NULL);
#endif
}
}
#endif
#if MIDIBOX_EXT_COUNT >= 3
if(midiMux5.available()) {
b = midiMux5.read();
if(repeater != 3)
midiMux5.MidiOut::write(b, NULL);
else
midi1.MidiOut::write(b, NULL);
}
if(midiMux6.available()) {
b = midiMux6.read();
if(repeater != 3)
midiMux6.MidiOut::write(b, NULL);
else {
midi2.MidiOut::write(b, NULL);
#if MIDIBOX_GATES
gates.MidiOut::write(b, NULL);
#endif
}
}
#endif
#if MIDIBOX_EXT_COUNT >= 4
if(midiMux7.available()) {
b = midiMux7.read();
if(repeater != 4)
midiMux7.MidiOut::write(b, NULL);
else
midi1.MidiOut::write(b, NULL);
}
if(midiMux8.available()) {
b = midiMux8.read();
if(repeater != 4)
midiMux8.MidiOut::write(b, NULL);
else {
midi2.MidiOut::write(b, NULL);
#if MIDIBOX_GATES
gates.MidiOut::write(b, NULL);