@@ -47,6 +47,11 @@ class BLEMIDI_Transport
47
47
{
48
48
mBleClass .begin (mDeviceName , this );
49
49
}
50
+
51
+ void end ()
52
+ {
53
+ mBleClass .end ();
54
+ }
50
55
51
56
bool beginTransmission (MIDI_NAMESPACE::MidiType type)
52
57
{
@@ -95,11 +100,6 @@ class BLEMIDI_Transport
95
100
return mRxBuffer [--mRxIndex ];
96
101
}
97
102
98
- bool end ()
99
- {
100
- return mBleClass .end ();
101
- }
102
-
103
103
unsigned available ()
104
104
{
105
105
uint8_t byte;
@@ -165,8 +165,11 @@ class BLEMIDI_Transport
165
165
*timestamp = (currentTimeStamp & 0x7F ) | 0x80 ; // 7 bits plus MSB
166
166
}
167
167
168
- static void setMidiTimestamp (uint8_t header, uint8_t * timestamp)
168
+ static uint16_t setMidiTimestamp (uint8_t header, uint8_t timestamp)
169
169
{
170
+ auto timestampHigh = 0x3f & header;
171
+ auto timestampLow = 0x7f & timestamp;
172
+ return (timestampLow + (timestampHigh << 7 ));
170
173
}
171
174
172
175
public:
@@ -215,6 +218,14 @@ class BLEMIDI_Transport
215
218
MIDI messages. In the MIDI BLE protocol, the System Real-Time messages must be deinterleaved
216
219
from other messages – except for System Exclusive messages.
217
220
*/
221
+
222
+ /* *
223
+ * If #define RUNNING_ENABLE is commented, it will transform all incoming runningStatus messages in full midi messages.
224
+ * Else, it will put in the buffer the same info that it had received (runningStatus will be not transformated).
225
+ * It recommend not use runningStatus by default. Only use if parser accepts runningStatus and your application has a so high transmission rate.
226
+ */
227
+ // #define RUNNING_ENABLE
228
+
218
229
void receive (byte *buffer, size_t length)
219
230
{
220
231
// Pointers used to search through payload.
@@ -236,12 +247,12 @@ class BLEMIDI_Transport
236
247
bool sysExContinuation = false ;
237
248
bool runningStatusContinuation = false ;
238
249
239
- if (timestampByte >= 80 )
250
+ if (timestampByte >= 80 ) // if bit 7 is 1, it's a timestampByte
240
251
{
241
252
auto timestampLow = 0x7f & timestampByte;
242
253
timestamp = timestampLow + (timestampHigh << 7 );
243
254
}
244
- else
255
+ else // if bit 7 is 0, it's the Continuation of a previous SysEx
245
256
{
246
257
sysExContinuation = true ;
247
258
lPtr--; // the second byte is part of the SysEx
@@ -270,68 +281,58 @@ class BLEMIDI_Transport
270
281
271
282
if (!runningStatusContinuation)
272
283
{
273
- // look at l and r pointers and decode by size.
274
- if (rPtr - lPtr < 1 )
275
- {
276
- // Time code or system
277
- mBleClass .add (buffer[lPtr]);
278
- }
279
- else if (rPtr - lPtr < 2 )
280
- {
281
- mBleClass .add (buffer[lPtr]);
282
- mBleClass .add (buffer[lPtr + 1 ]);
283
- }
284
- else if (rPtr - lPtr < 3 )
285
- {
286
- mBleClass .add (buffer[lPtr]);
287
- mBleClass .add (buffer[lPtr + 1 ]);
288
- mBleClass .add (buffer[lPtr + 2 ]);
289
- }
290
- else
291
- {
292
- // Too much data
293
- // If not System Common or System Real-Time, send it as running status
284
+ // If not System Common or System Real-Time, send it as running status
294
285
295
- auto midiType = lastStatus & 0xF0 ;
296
- if (sysExContinuation)
297
- midiType = 0xF0 ;
286
+ auto midiType = lastStatus & 0xF0 ;
287
+ if (sysExContinuation)
288
+ midiType = 0xF0 ;
298
289
299
- switch (midiType)
290
+ switch (midiType)
291
+ {
292
+ case 0x80 :
293
+ case 0x90 :
294
+ case 0xA0 :
295
+ case 0xB0 :
296
+ case 0xE0 :
297
+ #ifdef RUNNING_ENABLE
298
+ mBleClass .add (lastStatus);
299
+ #endif
300
+ for (auto i = lPtr; i < rPtr; i = i + 2 )
300
301
{
301
- case 0x80 :
302
- case 0x90 :
303
- case 0xA0 :
304
- case 0xB0 :
305
- case 0xE0 :
306
- for (auto i = lPtr; i < rPtr; i = i + 2 )
307
- {
308
- mBleClass .add (lastStatus);
309
- mBleClass .add (buffer[i + 1 ]);
310
- mBleClass .add (buffer[i + 2 ]);
311
- }
312
- break ;
313
- case 0xC0 :
314
- case 0xD0 :
315
- for (auto i = lPtr; i < rPtr; i = i + 1 )
316
- {
317
- mBleClass .add (lastStatus);
318
- mBleClass .add (buffer[i + 1 ]);
319
- }
320
- break ;
321
- case 0xF0 :
302
+ #ifndef RUNNING_ENABLE
322
303
mBleClass .add (lastStatus);
323
- for (auto i = lPtr; i < rPtr; i++)
324
- mBleClass .add (buffer[i + 1 ]);
304
+ #endif
305
+ mBleClass .add (buffer[i + 1 ]);
306
+ mBleClass .add (buffer[i + 2 ]);
307
+ }
308
+ break ;
309
+ case 0xC0 :
310
+ case 0xD0 :
311
+ #ifdef RUNNING_ENABLE
312
+ mBleClass .add (lastStatus);
313
+ #endif
314
+ for (auto i = lPtr; i < rPtr; i = i + 1 )
315
+ {
316
+ #ifndef RUNNING_ENABLE
317
+ mBleClass .add (lastStatus);
318
+ #endif
319
+ mBleClass .add (buffer[i + 1 ]);
320
+ }
321
+ break ;
322
+ case 0xF0 :
323
+ mBleClass .add (lastStatus);
324
+ for (auto i = lPtr; i < rPtr; i++)
325
+ mBleClass .add (buffer[i + 1 ]);
325
326
326
- break ;
327
+ break ;
327
328
328
- default :
329
- break ;
330
- }
329
+ default :
330
+ break ;
331
331
}
332
332
}
333
333
else
334
334
{
335
+ #ifndef RUNNING_ENABLE
335
336
auto midiType = lastStatus & 0xF0 ;
336
337
if (sysExContinuation)
337
338
midiType = 0xF0 ;
@@ -364,6 +365,11 @@ class BLEMIDI_Transport
364
365
default :
365
366
break ;
366
367
}
368
+ #else
369
+ mBleClass .add (lastStatus);
370
+ for (auto i = lPtr; i <= rPtr; i++)
371
+ mBleClass .add (buffer[i]);
372
+ #endif
367
373
runningStatusContinuation = false ;
368
374
}
369
375
0 commit comments