-
Notifications
You must be signed in to change notification settings - Fork 16
/
BLEParser.h
522 lines (489 loc) · 15.8 KB
/
BLEParser.h
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
/*
* Copyright (c) 2014 Matthias Frick
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ____BLEParser__
#define ____BLEParser__
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
namespace mfk
{
namespace midi
{
static int MIDI_STATE_TIMESTAMP = 0;
static int MIDI_STATE_WAIT = 1;
static int MIDI_STATE_SIGNAL_2BYTES_2 = 21;
static int MIDI_STATE_SIGNAL_3BYTES_2 = 31;
static int MIDI_STATE_SIGNAL_3BYTES_3 = 32;
static int MIDI_STATE_SIGNAL_SYSEX = 41;
// for Timestamp
static int MAX_TIMESTAMP = 8192;
static int BUFFER_LENGTH_MILLIS = 10;
// for RPN/NRPN messages
static int PARAMETER_MODE_NONE = 0;
static int PARAMETER_MODE_RPN = 1;
static int PARAMETER_MODE_NRPN = 2;
int parameterMode = 0;
int parameterNumber = 0x3fff;
int parameterValue = 0x3fff;
template <int kMaxBufferSize, typename Receiver>
class BLEMIDIParser
{
public:
void parseMidiEvent(uint8_t header, const uint8_t event)
{
uint8_t midiEvent = event & 0xff;
uint8_t timeToWait;
// printf((char*)midiEvent);
if (midiState == MIDI_STATE_TIMESTAMP)
{
// printf("Timestamp");
if ((midiEvent & 0x80) == 0)
{
// running status
midiState = MIDI_STATE_WAIT;
}
if (midiEvent == 0xf7)
{
// make sure this is the end of sysex
// and send alternative recovery stream
if (sysExRecBufferPtr > 0)
{
uint8_t removed = replaceLastByteInRecoveryBuffer(midiEvent);
sendSysexRecovery();
resetRecoveryBuffer();
}
midiState = MIDI_STATE_TIMESTAMP;
return;
}
else
{
// reset alternative sysex stream
resetRecoveryBuffer();
}
} // end of timestamp
if (midiState == MIDI_STATE_TIMESTAMP)
{
timestamp = ((header & 0x3f) << 7) | (midiEvent & 0x7f);
midiState = MIDI_STATE_WAIT;
}
else if (midiState == MIDI_STATE_WAIT)
{
switch (midiEvent & 0xf0) {
case 0xf0: {
switch (midiEvent) {
case 0xf0:
resetRecoveryBuffer();
resetSysExBuffer();
addByteToSysExBuffer(midiEvent);
midiState = MIDI_STATE_SIGNAL_SYSEX;
break;
case 0xf1:
case 0xf3:
// 0xf1 MIDI Time Code Quarter Frame. : 2bytes
// 0xf3 Song Select. : 2bytes
midiEventKind = midiEvent;
addByteToMidiBuffer(midiEvent);
midiState = MIDI_STATE_SIGNAL_2BYTES_2;
break;
case 0xf2:
// 0xf2 Song Position Pointer. : 3bytes
midiEventKind = midiEvent;
addByteToMidiBuffer(midiEvent);
midiState = MIDI_STATE_SIGNAL_3BYTES_2;
break;
case 0xf6:
// 0xf6 Tune Request : 1byte
addByteToMidiBuffer(midiEvent);
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xf8:
// 0xf8 Timing Clock : 1byte
#pragma mark send timeclock
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xfa:
// 0xfa Start : 1byte
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xfb:
// 0xfb Continue : 1byte
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xfc:
// 0xfc Stop : 1byte
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xfe:
// 0xfe Active Sensing : 1byte
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xff:
// 0xff Reset : 1byte
midiState = MIDI_STATE_TIMESTAMP;
break;
default:
break;
}
}
break;
case 0x80:
case 0x90:
case 0xa0:
case 0xb0:
case 0xe0:
// 3bytes pattern
midiEventKind = midiEvent;
midiState = MIDI_STATE_SIGNAL_3BYTES_2;
break;
case 0xc0: // program change
case 0xd0: // channel after-touch
// 2bytes pattern
midiEventKind = midiEvent;
midiState = MIDI_STATE_SIGNAL_2BYTES_2;
break;
default:
// 0x00 - 0x70: running status
if ((midiEventKind & 0xf0) != 0xf0) {
// previous event kind is multi-bytes pattern
midiEventNote = midiEvent;
midiState = MIDI_STATE_SIGNAL_3BYTES_3;
}
break;
}
}
else if (midiState == MIDI_STATE_SIGNAL_2BYTES_2)
{
switch (midiEventKind & 0xf0)
{
// 2bytes pattern
case 0xc0: // program change
midiEventNote = midiEvent;
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xd0: // channel after-touch
midiEventNote = midiEvent;
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xf0:
{
switch (midiEventKind)
{
case 0xf1:
// 0xf1 MIDI Time Code Quarter Frame. : 2bytes
midiEventNote = midiEvent;
addByteToMidiBuffer(midiEventNote);
sendMidi(2);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xf3:
// 0xf3 Song Select. : 2bytes
midiEventNote = midiEvent;
addByteToMidiBuffer(midiEventNote);
sendMidi(2);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
default:
// illegal state
midiState = MIDI_STATE_TIMESTAMP;
break;
}
}
break;
default:
// illegal state
midiState = MIDI_STATE_TIMESTAMP;
break;
}
}
else if (midiState == MIDI_STATE_SIGNAL_3BYTES_2)
{
switch (midiEventKind & 0xf0)
{
case 0x80:
case 0x90:
case 0xa0:
case 0xb0:
case 0xe0:
case 0xf0:
// 3bytes pattern
midiEventNote = midiEvent;
midiState = MIDI_STATE_SIGNAL_3BYTES_3;
break;
default:
// illegal state
midiState = MIDI_STATE_TIMESTAMP;
break;
}
}
else if (midiState == MIDI_STATE_SIGNAL_3BYTES_3)
{
switch (midiEventKind & 0xf0)
{
// 3bytes pattern
case 0x80: // note off
midiEventVelocity = midiEvent;
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(midiEventNote);
addByteToMidiBuffer(midiEventVelocity);
sendMidi(3);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0x90: // note on
midiEventVelocity = midiEvent;
//timeToWait = calculateTimeToWait(timestamp);
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(midiEventNote);
addByteToMidiBuffer(midiEventVelocity);
sendMidi(3);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xa0: // control polyphonic key pressure
midiEventVelocity = midiEvent;
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(midiEventNote);
addByteToMidiBuffer(midiEventVelocity);
sendMidi(3);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xb0: // control change
midiEventVelocity = midiEvent;
switch (midiEventNote & 0x7f)
{
case 98:
// NRPN LSB
parameterNumber &= 0x3f80;
parameterNumber |= midiEventVelocity & 0x7f;
parameterMode = PARAMETER_MODE_NRPN;
break;
case 99:
// NRPN MSB
parameterNumber &= 0x007f;
parameterNumber |= (midiEventVelocity & 0x7f) << 7;
parameterMode = PARAMETER_MODE_NRPN;
break;
case 100:
// RPN LSB
parameterNumber &= 0x3f80;
parameterNumber |= midiEventVelocity & 0x7f;
parameterMode = PARAMETER_MODE_RPN;
break;
case 101:
// RPN MSB
parameterNumber &= 0x007f;
parameterNumber |= (midiEventVelocity & 0x7f) << 7;
parameterMode = PARAMETER_MODE_RPN;
break;
case 38:
// data LSB
parameterValue &= 0x3f80;
parameterValue |= midiEventVelocity & 0x7f;
if (parameterNumber != 0x3fff) {
if (parameterMode == PARAMETER_MODE_RPN)
{
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(parameterNumber);
addByteToMidiBuffer(parameterValue);
sendMidi(3);
resetMidiBuffer();
}
else if (parameterMode == PARAMETER_MODE_NRPN)
{
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(parameterNumber);
addByteToMidiBuffer(parameterValue);
sendMidi(3);
resetMidiBuffer();
}
}
break;
case 6:
// data MSB
parameterValue &= 0x007f;
parameterValue |= (midiEventVelocity & 0x7f) << 7;
if (parameterNumber != 0x3fff)
{
if (parameterMode == PARAMETER_MODE_RPN)
{
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(parameterNumber);
addByteToMidiBuffer(parameterValue);
sendMidi(3);
resetMidiBuffer();
}
else if (parameterMode == PARAMETER_MODE_NRPN)
{
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(parameterNumber);
addByteToMidiBuffer(parameterValue);
sendMidi(3);
resetMidiBuffer();
}
}
break;
default:
// do nothing
break;
}
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(midiEventNote);
addByteToMidiBuffer(midiEventVelocity);
sendMidi(3);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xe0: // pitch bend
midiEventVelocity = midiEvent;
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(midiEventNote);
addByteToMidiBuffer(midiEventVelocity);
sendMidi(3);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
case 0xf0: // Song Position Pointer.
midiEventVelocity = midiEvent;
addByteToMidiBuffer(midiEventKind);
addByteToMidiBuffer(midiEventNote);
addByteToMidiBuffer(midiEventVelocity);
sendMidi(3);
resetMidiBuffer();
midiState = MIDI_STATE_TIMESTAMP;
break;
default:
// illegal state
midiState = MIDI_STATE_TIMESTAMP;
break;
}
}
else if (midiState == MIDI_STATE_SIGNAL_SYSEX)
{
if (midiEvent == 0xf7)
{
uint8_t repEvt = replaceLastByteInSysExBuffer(midiEvent);
resetRecoveryBuffer();
createSysExRecovery();
replaceLastByteInRecoveryBuffer(repEvt);
addByteToRecoveryBuffer(midiEvent);
sendSysex();
resetSysExBuffer();
midiState = MIDI_STATE_TIMESTAMP;
}
else
{
addByteToSysExBuffer(midiEvent);
}
}
}
void setUSBMidiHandle(Receiver *MidiHdl)
{
midiRecv = MidiHdl;
}
private:
void addByteToMidiBuffer(uint8_t midiEvent)
{
midiBuffer[midiBufferPtr] = midiEvent;
midiBufferPtr++;
}
void addByteToSysExBuffer(uint8_t midiEvent)
{
sysExBuffer[sysExBufferPtr] = midiEvent;
sysExBufferPtr++;
}
uint8_t replaceLastByteInSysExBuffer(uint8_t midiEvent)
{
sysExBufferPtr--;
uint8_t lastEvt = sysExBuffer[sysExBufferPtr];
sysExBuffer[sysExBufferPtr] = midiEvent;
sysExBufferPtr++;
return lastEvt;
}
void sendSysex()
{
midiRecv->SendSysEx(sysExBuffer, sysExBufferPtr, 0);
}
void createSysExRecovery()
{
sysExRecBufferPtr = sysExBufferPtr;
memcpy(alterSysExBuffer, sysExBuffer, sysExBufferPtr);
}
void sendSysexRecovery()
{
midiRecv->SendSysEx(alterSysExBuffer, sysExRecBufferPtr, 0);
}
uint8_t replaceLastByteInRecoveryBuffer(uint8_t midiEvent)
{
sysExRecBufferPtr--;
uint8_t lastEvt = alterSysExBuffer[sysExRecBufferPtr];
alterSysExBuffer[sysExRecBufferPtr] = midiEvent;
sysExRecBufferPtr++;
return lastEvt;
}
void addByteToRecoveryBuffer(uint8_t midiEvent)
{
alterSysExBuffer[sysExRecBufferPtr] = midiEvent;
sysExRecBufferPtr++;
}
void resetMidiBuffer()
{
memset(&midiBuffer[0], 0, sizeof(midiBuffer));
midiBufferPtr = 0;
}
void resetSysExBuffer()
{
memset(&sysExBuffer[0], 0, kMaxBufferSize);
sysExBufferPtr = 0;
}
void resetRecoveryBuffer()
{
memset(&alterSysExBuffer[0], 0, sizeof(alterSysExBuffer));
sysExRecBufferPtr = 0;
}
void sendMidi(uint8_t size)
{
midiRecv->SendData(midiBuffer, 0);
}
uint8_t midiBuffer[3];
uint8_t sysExBuffer[kMaxBufferSize];
uint8_t alterSysExBuffer[kMaxBufferSize];
int midiBufferPtr = 0;
int sysExRecBufferPtr = 0;
int sysExBufferPtr = 0;
// MIDI event messages, state & stamps
int midiEventKind;
int midiEventNote;
int midiEventVelocity;
int midiState = MIDI_STATE_TIMESTAMP;
int timestamp;
bool useTimestamp = true;
int lastTimestamp;
long lastTimestampRecorded = 0;
int zeroTimestampCount = 0;
Receiver *midiRecv;
};
}
}
#endif /* defined(____BLEParser__) */