Skip to content

Commit 31817fb

Browse files
RobertoRoberto
authored andcommitted
Parser Upgraded
1 parent 6d00baa commit 31817fb

File tree

1 file changed

+66
-60
lines changed

1 file changed

+66
-60
lines changed

src/BLEMIDI_Transport.h

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class BLEMIDI_Transport
4747
{
4848
mBleClass.begin(mDeviceName, this);
4949
}
50+
51+
void end()
52+
{
53+
mBleClass.end();
54+
}
5055

5156
bool beginTransmission(MIDI_NAMESPACE::MidiType type)
5257
{
@@ -95,11 +100,6 @@ class BLEMIDI_Transport
95100
return mRxBuffer[--mRxIndex];
96101
}
97102

98-
bool end()
99-
{
100-
return mBleClass.end();
101-
}
102-
103103
unsigned available()
104104
{
105105
uint8_t byte;
@@ -165,8 +165,11 @@ class BLEMIDI_Transport
165165
*timestamp = (currentTimeStamp & 0x7F) | 0x80; // 7 bits plus MSB
166166
}
167167

168-
static void setMidiTimestamp(uint8_t header, uint8_t *timestamp)
168+
static uint16_t setMidiTimestamp(uint8_t header, uint8_t timestamp)
169169
{
170+
auto timestampHigh = 0x3f & header;
171+
auto timestampLow = 0x7f & timestamp;
172+
return (timestampLow + (timestampHigh << 7));
170173
}
171174

172175
public:
@@ -215,6 +218,14 @@ class BLEMIDI_Transport
215218
MIDI messages. In the MIDI BLE protocol, the System Real-Time messages must be deinterleaved
216219
from other messages – except for System Exclusive messages.
217220
*/
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+
218229
void receive(byte *buffer, size_t length)
219230
{
220231
// Pointers used to search through payload.
@@ -236,12 +247,12 @@ class BLEMIDI_Transport
236247
bool sysExContinuation = false;
237248
bool runningStatusContinuation = false;
238249

239-
if (timestampByte >= 80)
250+
if (timestampByte >= 80) // if bit 7 is 1, it's a timestampByte
240251
{
241252
auto timestampLow = 0x7f & timestampByte;
242253
timestamp = timestampLow + (timestampHigh << 7);
243254
}
244-
else
255+
else // if bit 7 is 0, it's the Continuation of a previous SysEx
245256
{
246257
sysExContinuation = true;
247258
lPtr--; // the second byte is part of the SysEx
@@ -270,68 +281,58 @@ class BLEMIDI_Transport
270281

271282
if (!runningStatusContinuation)
272283
{
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
294285

295-
auto midiType = lastStatus & 0xF0;
296-
if (sysExContinuation)
297-
midiType = 0xF0;
286+
auto midiType = lastStatus & 0xF0;
287+
if (sysExContinuation)
288+
midiType = 0xF0;
298289

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)
300301
{
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
322303
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]);
325326

326-
break;
327+
break;
327328

328-
default:
329-
break;
330-
}
329+
default:
330+
break;
331331
}
332332
}
333333
else
334334
{
335+
#ifndef RUNNING_ENABLE
335336
auto midiType = lastStatus & 0xF0;
336337
if (sysExContinuation)
337338
midiType = 0xF0;
@@ -364,6 +365,11 @@ class BLEMIDI_Transport
364365
default:
365366
break;
366367
}
368+
#else
369+
mBleClass.add(lastStatus);
370+
for (auto i = lPtr; i <= rPtr; i++)
371+
mBleClass.add(buffer[i]);
372+
#endif
367373
runningStatusContinuation = false;
368374
}
369375

0 commit comments

Comments
 (0)