forked from marcel-licence/esp32_basic_synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_interface.ino
384 lines (333 loc) · 8.12 KB
/
midi_interface.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
/*
* a simple implementation to use midi
*
* Author: Marcel Licence
*/
/*
* look for midi interface using 1N136
* to convert the MIDI din signal to
* a uart compatible signal
*/
#ifdef ESP32_AUDIO_KIT
#define RXD2 22 /* U2RRXD */
#else
#define RXD2 16 /* U2RRXD */
#define TXD2 17
#endif
#define MYCHANNEL 1
/* use define to dump midi data */
// #define DUMP_SERIAL2_TO_SERIAL
/*
* structure is used to build the mapping table
*/
struct midiControllerMapping
{
uint8_t channel;
uint8_t data1;
const char *desc;
void(*callback_mid)(uint8_t ch, uint8_t data1, uint8_t data2);
void(*callback_val)(uint8_t userdata, float value);
uint8_t user_data;
};
struct midiMapping_s
{
void (*noteOn)(uint8_t ch, uint8_t note, float vel);
void (*noteOff)(uint8_t ch, uint8_t note);
void (*pitchBend)(uint8_t ch, float bend);
void (*modWheel)(uint8_t ch, float value);
struct midiControllerMapping *controlMapping;
int mapSize;
};
extern struct midiMapping_s midiMapping; /* definition in z_config.ino */
/* constant to normalize midi value to 0.0 - 1.0f */
#define NORM127MUL 0.007874f
inline void Midi_NoteOn(uint8_t ch, uint8_t note, uint8_t vel)
{
#ifdef MYCHANNEL
if( ch == MYCHANNEL ){
#endif
if (vel > 127)
{
/* we will end up here in case of problems with the MIDI connection */
vel = 127;
Serial.printf("to loud note detected!!!!!!!!!!!!!!!!!!!!!!!\n");
}
if (midiMapping.noteOn != NULL)
{
midiMapping.noteOn(ch, note, pow(2, ((vel * NORM127MUL) - 1.0f) * 6));
}
#ifdef MYCHANNEL
}
#endif
}
inline void Midi_NoteOff(uint8_t ch, uint8_t note)
{
#ifdef MYCHANNEL
if( ch == MYCHANNEL ){
#endif
if (midiMapping.noteOff != NULL)
{
midiMapping.noteOff(ch, note);
}
#ifdef MYCHANNEL
}
#endif
}
/*
* this function will be called when a control change message has been received
*/
inline void Midi_ControlChange(uint8_t channel, uint8_t data1, uint8_t data2)
{
#ifdef MYCHANNEL
if( channel == MYCHANNEL ){
#endif
for (int i = 0; i < midiMapping.mapSize; i++)
{
// Serial.printf("CC >%02x %02x %02x\n", channel, data1, data2);
if ((midiMapping.controlMapping[i].channel == channel) && (midiMapping.controlMapping[i].data1 == data1))
{
if (midiMapping.controlMapping[i].callback_mid != NULL)
{
midiMapping.controlMapping[i].callback_mid(channel, data1, data2);
}
if (midiMapping.controlMapping[i].callback_val != NULL)
{
midiMapping.controlMapping[i].callback_val(midiMapping.controlMapping[i].user_data, (float)data2 * NORM127MUL);
}
}
}
if (data1 == 1)
{
if (midiMapping.modWheel != NULL)
{
midiMapping.modWheel(channel, (float)data2 * NORM127MUL);
}
}
#if 0
if (data1 == 17)
{
if (channel < 10)
{
Synth_SetSlider(channel, data2 * NORM127MUL);
}
}
if (data1 == 17)
{
if (channel < 10)
{
Synth_SetSlider(channel, data2 * NORM127MUL);
}
}
if ((data1 == 18) && (channel == 1))
{
Synth_SetSlider(8, data2 * NORM127MUL);
}
if ((data1 == 16) && (channel < 9))
{
Synth_SetRotary(channel, data2 * NORM127MUL);
}
if ((data1 == 18) && (channel == 0))
{
Synth_SetRotary(8, data2 * NORM127MUL);
}
#endif
#ifdef MYCHANNEL
}
#endif
}
inline void Midi_PitchBend(uint8_t ch, uint16_t bend)
{
#ifdef MYCHANNEL
if( ch == MYCHANNEL ){
#endif
float value = ((float)bend - 8192.0f) * (1.0f / 8192.0f) - 1.0f;
if (midiMapping.pitchBend != NULL)
{
midiMapping.pitchBend(ch, value);
}
#ifdef MYCHANNEL
}
#endif
}
/*
* function will be called when a short message has been received over midi
*/
inline void HandleShortMsg(uint8_t *data)
{
uint8_t ch = data[0] & 0x0F;
switch (data[0] & 0xF0)
{
/* note on */
case 0x90:
if (data[2] > 0)
{
Serial.printf(">%02x %02x %02x\n", ch, data[1], data[2]);
Midi_NoteOn(ch, data[1], data[2]);
}else{
Midi_NoteOff(ch, data[1]);
}
break;
/* note off */
case 0x80:
Midi_NoteOff(ch, data[1]);
break;
case 0xb0:
Midi_ControlChange(ch, data[1], data[2]);
break;
/* pitchbend */
case 0xe0:
Midi_PitchBend(ch, ((((uint16_t)data[1]) ) + ((uint16_t)data[2] << 8)));
break;
default:
Serial.printf(">>>>%02x %02x %02x\n", ch, data[1], data[2]);
break;
}
}
void Midi_Setup()
{
#ifdef TXD2
Serial2.begin(31250, SERIAL_8N1, RXD2, TXD2);
#else
Serial2.begin(31250, SERIAL_8N1, RXD2);
#endif
// ist nmit 1K extern pulled up .. weniger Hitze für den ESP32.
// pinMode(RXD2, INPUT_PULLUP); /* 25: GPIO 16, u2_RXD */
}
void Midi_CheckSerial2(void)
{
/*
* watchdog to avoid getting stuck by receiving incomplete or wrong data
*/
static uint32_t inMsgWd = 0;
static uint8_t inMsg[3];
static uint8_t inMsgIndex = 0;
//Choose Serial1 or Serial2 as required
if (Serial2.available())
{
uint8_t incomingByte = Serial2.read();
/* ignore live messages */
if ((incomingByte & 0xF0) == 0xF0)
{
return;
}
/* ignore live MIDI Beat Clock */
if ((incomingByte & 0xF8) == 0xF8)
{
return;
}
/* ignore live message STOP */
if ((incomingByte & 0xFC) == 0xFC)
{
return;
}
/* ignore live message Continue */
if ((incomingByte & 0xFB) == 0xFB)
{
return;
}
/* ignore live message Start */
if ((incomingByte & 0xFA) == 0xFA)
{
return;
}
#ifdef DUMP_SERIAL2_TO_SERIAL
Serial.printf("%02x", incomingByte);
Serial.println(" x");
Serial.print( incomingByte);
Serial.println(" i");
#endif
if (inMsgIndex == 0)
{
if ((incomingByte & 0x80) != 0x80)
{
inMsgIndex = 1;
}
}
inMsg[inMsgIndex] = incomingByte;
inMsgIndex += 1;
if (inMsgIndex >= 3)
{
#ifdef DUMP_SERIAL2_TO_SERIAL
Serial.printf(">%02x %02x %02x\n", inMsg[0], inMsg[1], inMsg[2]);
#endif
HandleShortMsg(inMsg);
inMsgIndex = 0;
}
/*
* reset watchdog to allow new bytes to be received
*/
inMsgWd = 0;
}
else
{
if (inMsgIndex > 0)
{
inMsgWd++;
if (inMsgWd == 0xFFF)
{
inMsgIndex = 0;
}
}
}
}
void Midi_CheckSerial(void)
{
/*
* watchdog to avoid getting stuck by receiving incomplete or wrong data
*/
static uint32_t inMsgWd = 0;
static uint8_t inMsg[3];
static uint8_t inMsgIndex = 0;
//Choose Serial1 or Serial2 as required
if( Serial.available() ){
uint8_t incomingByte = Serial.read();
/* ignore live messages */
if ((incomingByte & 0xF0) == 0xF0)
{
return;
}
if ((incomingByte & 0xF8) == 0xF8)
{
return;
}
if (inMsgIndex == 0)
{
if ((incomingByte & 0x80) != 0x80)
{
inMsgIndex = 1;
}
}
inMsg[inMsgIndex] = incomingByte;
inMsgIndex += 1;
if (inMsgIndex >= 3)
{
HandleShortMsg(inMsg);
inMsgIndex = 0;
}
/*
* reset watchdog to allow new bytes to be received
*/
inMsgWd = 0;
}
else
{
if (inMsgIndex > 0)
{
inMsgWd++;
if (inMsgWd == 0xFFF)
{
inMsgIndex = 0;
}
}
}
}
/*
* this function should be called continuously to ensure that incoming messages can be processed
*/
void Midi_Process()
{
Midi_CheckSerial2();
#ifdef MIDI_RECV_FROM_SERIAL
Midi_CheckSerial();
#endif
}