-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtpMIDI-HUB.ino
372 lines (303 loc) · 9.85 KB
/
rtpMIDI-HUB.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
/*
Developed for Teensy 4.1 with a switch and a led
version 2024 may
*/
#include <NativeEthernet.h> //Teensy (for Arduino IDE 2.0.4 or later) version 1.59.0
#include <EthernetBonjour.h> // https://github.com/TrippyLighting/EthernetBonjour
#include <USBHost_t36.h> // https://github.com/PaulStoffregen/USBHost_t36
/*
#define SerialMon Serial
#include <AppleMIDI_Debug.h>
*/
#define USE_EXT_CALLBACKS
#include <AppleMIDI.h> //https://github.com/lathoub/Arduino-AppleMidi-Library
/*
MaxNumberOfParticipants set to 5 (default 2) in AppleMIDI_Settings.h
*/
/**********************************************************
Network, Bonjour and pin configuratins
**********************************************************/
//YELLOW A
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAD };
IPAddress staticip(192, 168, 0, 199);
IPAddress autoassignedip(169, 254, 1, 99);
IPAddress autoassignedipDNS(169, 254, 1, 1);
const char BonjourName[] ="YellowBox";
const char BonjourService[] = "YellowBox._apple-midi";
//RED B
/*
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xBC };
IPAddress staticip(192, 168, 0, 198);
IPAddress autoassignedip(169,254,10,99);
IPAddress autoassignedipDNS(169, 254, 10, 1);
const char BonjourName[] ="RedBox";
const char BonjourService[] = "RedBox._apple-midi";
*/
const int buttonPin = 28;
const int ledPin = 25;
//////////////////////// Variables /////////////////////////
int buttonState = 0;
int buttonStateOLD;
unsigned long t1 = millis();
unsigned long t_led = 0;
unsigned int blink_length = 200;
unsigned int blink_number = 5;
unsigned int blink_done = 0;
unsigned int led_on = 1;
bool blinking = 0;
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
USBHub hub3(myusb);
USBHub hub4(myusb);
MIDIDevice midi01(myusb);
MIDIDevice midi02(myusb);
MIDIDevice midi03(myusb);
MIDIDevice midi04(myusb);
MIDIDevice midi05(myusb);
MIDIDevice midi06(myusb);
MIDIDevice midi07(myusb);
MIDIDevice midi08(myusb);
MIDIDevice midi09(myusb);
MIDIDevice midi10(myusb);
MIDIDevice * midilist[10] = {
&midi01, &midi02, &midi03, &midi04, &midi05, &midi06, &midi07, &midi08, &midi09, &midi10
};
//////////////////// UDP communication ////////////////////
int8_t isConnected = 0;
APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE();
void OnAppleMidiException(const APPLEMIDI_NAMESPACE::ssrc_t&, const APPLEMIDI_NAMESPACE::Exception&, const int32_t);
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
blink_led(0);
buttonState = digitalRead(buttonPin);
if (buttonState) { //pressed:
Ethernet.begin(mac, staticip);
} else { //released
if (Ethernet.begin(mac) == 0) {
Ethernet.begin(mac, autoassignedip, autoassignedipDNS, autoassignedipDNS, { 255, 255, 0, 0 });
}
}
buttonStateOLD = buttonState;
myusb.begin();
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
while (Ethernet.linkStatus() == LinkOFF) {
delay(500);
}
MIDI.begin(MIDI_CHANNEL_OMNI);
EthernetBonjour.begin(BonjourName);
EthernetBonjour.addServiceRecord(BonjourService,
AppleMIDI.getPort(),
MDNSServiceUDP);
// Stay informed on connection status
AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) {
isConnected++;
});
AppleMIDI.setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
isConnected--;
});
AppleMIDI.setHandleException(OnAppleMidiException);
MIDI.setHandleNoteOn(OnNoteOn);
MIDI.setHandleNoteOff(OnNoteOff);
MIDI.setHandleControlChange(OnControlChange);
MIDI.setHandleAfterTouchPoly(OnAfterTouchPoly);
MIDI.setHandleProgramChange(OnProgramChange);
MIDI.setHandleAfterTouchChannel(OnAfterTouchChannel);
MIDI.setHandlePitchBend(OnPitchBend);
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void loop()
{
MIDI.read();
EthernetBonjour.run();
//READ DATA FROM DEVICE AND SEND TO rtp networ device
for (int port=0; port < 10; port++) {
if (midilist[port]->read()) {
uint8_t type = midilist[port]->getType();
uint8_t data1 = midilist[port]->getData1();
uint8_t data2 = midilist[port]->getData2();
uint8_t channel = midilist[port]->getChannel();
const uint8_t *sys = midilist[port]->getSysExArray();
sendToComputer(type, data1, data2, channel, sys);
blink_led(1);
}
}
blink_led(0);
buttonState = digitalRead(buttonPin);
if (buttonState != buttonStateOLD) {
buttonStateOLD = buttonState;
if (buttonState) { //pressed:
Ethernet.begin(mac, staticip);
} else { //released
if (Ethernet.begin(mac) == 0) {
Ethernet.begin(mac, autoassignedip, autoassignedipDNS, autoassignedipDNS, { 255, 255, 0, 0 });
}
}
}
}
// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------
void sendToComputer(byte type, byte data1, byte data2, byte channel, const uint8_t *sysexarray){
MIDI.send(type, data1, data2, channel);
}
void blink_led(int i){
t_led = millis();
if (i && !blinking) {
t1 = millis();
blink_done = 0;
blinking = true;
} else if (blink_done >= (blink_number * 2)){
blinking = false;
}
if (blinking && (t_led > (t1 + blink_length))) {
if (led_on) {
led_on = 0;
} else {
led_on = 1;
}
t1 = millis();
blink_done++;
}
if (led_on) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
void OnAppleMidiException(const APPLEMIDI_NAMESPACE::ssrc_t& ssrc, const APPLEMIDI_NAMESPACE::Exception& e, const int32_t value ) {
switch (e)
{
case APPLEMIDI_NAMESPACE::Exception::BufferFullException:
//DBG(F("*** BufferFullException"));
break;
case APPLEMIDI_NAMESPACE::Exception::ParseException:
//DBG(F("*** ParseException"));
break;
case APPLEMIDI_NAMESPACE::Exception::TooManyParticipantsException:
//DBG(F("*** TooManyParticipantsException"));
break;
case APPLEMIDI_NAMESPACE::Exception::UnexpectedInviteException:
//DBG(F("*** UnexpectedInviteException"));
break;
case APPLEMIDI_NAMESPACE::Exception::ParticipantNotFoundException:
//DBG(F("*** ParticipantNotFoundException"), value);
break;
case APPLEMIDI_NAMESPACE::Exception::ComputerNotInDirectory:
//DBG(F("*** ComputerNotInDirectory"), value);
break;
case APPLEMIDI_NAMESPACE::Exception::NotAcceptingAnyone:
//DBG(F("*** NotAcceptingAnyone"), value);
break;
case APPLEMIDI_NAMESPACE::Exception::ListenerTimeOutException:
//DBG(F("*** ListenerTimeOutException"));
break;
case APPLEMIDI_NAMESPACE::Exception::MaxAttemptsException:
//DBG(F("*** MaxAttemptsException"));
break;
case APPLEMIDI_NAMESPACE::Exception::NoResponseFromConnectionRequestException:
//DBG(F("***:yyy did't respond to the connection request. Check the address and port, and any firewall or router settings. (time)"));
break;
case APPLEMIDI_NAMESPACE::Exception::SendPacketsDropped:
//DBG(F("*** SendPacketsDropped"), value);
break;
case APPLEMIDI_NAMESPACE::Exception::ReceivedPacketsDropped:
//DBG(F("*** ReceivedPacketsDropped"), value);
break;
}
}
static void OnNoteOn(byte channel, byte note, byte velocity) {
for (int port=0; port < 10; port++) {
midilist[port]->sendNoteOn(note, velocity, channel);
}
blink_led(1);
}
static void OnNoteOff(byte channel, byte note, byte velocity) {
for (int port=0; port < 10; port++) {
midilist[port]->sendNoteOff(note, velocity, channel);
}
blink_led(1);
}
static void OnAfterTouchPoly(byte channel, byte note, byte pressure) {
for (int port=0; port < 10; port++) {
midilist[port]->sendAfterTouchPoly(note, pressure, channel);
}
blink_led(1);
}
static void OnControlChange(byte channel, byte number, byte value) {
for (int port=0; port < 10; port++) {
midilist[port]->sendControlChange(number, value, channel);
}
blink_led(1);
}
static void OnProgramChange(byte channel, byte number) {
for (int port=0; port < 10; port++) {
midilist[port]->sendProgramChange(number, channel);
}
blink_led(1);
}
static void OnAfterTouchChannel(byte channel, byte pressure) {
for (int port=0; port < 10; port++) {
midilist[port]->sendAfterTouch(pressure, channel);
}
blink_led(1);
}
static void OnPitchBend(byte channel, int bend) {
for (int port=0; port < 10; port++) {
midilist[port]->sendPitchBend(bend, channel);
}
blink_led(1);
}
////////////////////// FOR FUTURE UPDATES ////////////////////
/*
static void OnSystemExclusive(byte * array, unsigned size) {
Serial.println(F("SystemExclusive"));
}
static void OnTimeCodeQuarterFrame(byte data) {
Serial.print(F("TimeCodeQuarterFrame: "));
Serial.println(data, HEX);
}
static void OnSongPosition(unsigned beats) {
Serial.print(F("SongPosition: "));
Serial.println(beats);
}
static void OnSongSelect(byte songnumber) {
Serial.print(F("SongSelect: "));
Serial.println(songnumber);
}
static void OnTuneRequest() {
Serial.println(F("TuneRequest"));
}
static void OnClock() {
Serial.println(F("Clock"));
}
static void OnStart() {
Serial.println(F("Start"));
}
static void OnContinue() {
Serial.println(F("Continue"));
}
static void OnStop() {
Serial.println(F("Stop"));
}
static void OnActiveSensing() {
Serial.println(F("ActiveSensing"));
}
static void OnSystemReset() {
Serial.println(F("SystemReset"));
}
*/