This commit is contained in:
lathoub 2021-08-02 21:18:33 +02:00
parent 5cfcf20262
commit a76ebc82c6
1 changed files with 31 additions and 39 deletions

View File

@ -10,7 +10,7 @@
#include "BLEMIDI_Defs.h" #include "BLEMIDI_Defs.h"
#include "BLEMIDI_Namespace.h" #include "BLEMIDI_Namespace.h"
#define CHECK_BIT(var,pos) (!!((var) & (1<<(pos)))) #define CHECK_BIT(var, pos) (!!((var) & (1 << (pos))))
BEGIN_BLEMIDI_NAMESPACE BEGIN_BLEMIDI_NAMESPACE
@ -77,7 +77,7 @@ public:
void endTransmission() void endTransmission()
{ {
if (mTxBuffer[mTxIndex - 1] == 0xF7) if (mTxBuffer[mTxIndex - 1] == MIDI_NAMESPACE::MidiType::SystemExclusiveEnd)
{ {
if (mTxIndex >= sizeof(mTxBuffer)) if (mTxIndex >= sizeof(mTxBuffer))
{ {
@ -90,7 +90,7 @@ public:
{ {
mTxBuffer[mTxIndex - 1] = mTimestampLow; // or generate new ? mTxBuffer[mTxIndex - 1] = mTimestampLow; // or generate new ?
} }
mTxBuffer[mTxIndex++] = 0xF7; mTxBuffer[mTxIndex++] = MIDI_NAMESPACE::MidiType::SystemExclusiveEnd;
} }
mBleClass.write(mTxBuffer, mTxIndex); mBleClass.write(mTxBuffer, mTxIndex);
@ -168,8 +168,11 @@ protected:
*timestamp = (currentTimeStamp & 0x7F) | 0x80; // 7 bits plus MSB *timestamp = (currentTimeStamp & 0x7F) | 0x80; // 7 bits plus MSB
} }
static void setMidiTimestamp(uint8_t header, uint8_t *timestamp) static uint16_t setMidiTimestamp(uint8_t header, uint8_t timestamp)
{ {
auto timestampHigh = 0x3f & header;
auto timestampLow = 0x7f & timestamp;
return (timestampLow + (timestampHigh << 7));
} }
public: public:
@ -241,9 +244,7 @@ public:
if (signatureIs1) if (signatureIs1)
{ {
auto timestampLow = 0x7f & timestampByte; timestamp = setMidiTimestamp(headerByte, timestampByte);
timestamp = 0;
timestampLow + (timestampHigh << 7);
} }
else else
{ {
@ -286,17 +287,17 @@ public:
auto midiType = buffer[lPtr] & 0xF0; auto midiType = buffer[lPtr] & 0xF0;
if (sysExContinuation) if (sysExContinuation)
midiType = 0xF0; midiType = MIDI_NAMESPACE::MidiType::SystemExclusive;
// Too much data // Too much data
// If not System Common or System Real-Time, send it as running status // If not System Common or System Real-Time, send it as running status
switch (midiType) switch (midiType)
{ {
case 0x80: case MIDI_NAMESPACE::MidiType::NoteOff:
case 0x90: case MIDI_NAMESPACE::MidiType::NoteOn:
case 0xA0: case MIDI_NAMESPACE::MidiType::AfterTouchPoly:
case 0xB0: case MIDI_NAMESPACE::MidiType::ControlChange:
case 0xE0: case MIDI_NAMESPACE::MidiType::PitchBend:
for (auto i = lPtr; i < rPtr; i = i + 2) for (auto i = lPtr; i < rPtr; i = i + 2)
{ {
mBleClass.add(lastStatus); mBleClass.add(lastStatus);
@ -304,15 +305,15 @@ public:
mBleClass.add(buffer[i + 2]); mBleClass.add(buffer[i + 2]);
} }
break; break;
case 0xC0: case MIDI_NAMESPACE::MidiType::ProgramChange:
case 0xD0: case MIDI_NAMESPACE::MidiType::AfterTouchChannel:
for (auto i = lPtr; i < rPtr; i = i + 1) for (auto i = lPtr; i < rPtr; i = i + 1)
{ {
mBleClass.add(lastStatus); mBleClass.add(lastStatus);
mBleClass.add(buffer[i + 1]); mBleClass.add(buffer[i + 1]);
} }
break; break;
case 0xF0: case MIDI_NAMESPACE::MidiType::SystemExclusive:
mBleClass.add(buffer[lPtr]); mBleClass.add(buffer[lPtr]);
for (auto i = lPtr; i < rPtr; i++) for (auto i = lPtr; i < rPtr; i++)
mBleClass.add(buffer[i + 1]); mBleClass.add(buffer[i + 1]);
@ -325,14 +326,10 @@ public:
signatureIs1 = CHECK_BIT(timestampByte, 7 - 1); signatureIs1 = CHECK_BIT(timestampByte, 7 - 1);
if (signatureIs1) if (signatureIs1)
{ {
auto timestampLow = 0x7f & timestampByte; timestamp = setMidiTimestamp(headerByte, timestampByte);
timestamp = timestampLow + (timestampHigh << 7);
// std::cout << "timestamp low: 0x" << std::hex << (int)timestampByte << std::endl;
} }
rPtr++; rPtr++;
// std::cout << "end of SysEx: 0x" << std::hex << (int)buffer[rPtr] << std::endl;
break; break;
default: default:
@ -349,14 +346,9 @@ public:
signatureIs1 = CHECK_BIT(timestampByte, 7 - 1); signatureIs1 = CHECK_BIT(timestampByte, 7 - 1);
if (signatureIs1) if (signatureIs1)
{ {
auto timestampLow = 0x7f & timestampByte; timestamp = setMidiTimestamp(headerByte, timestampByte);
timestamp = timestampLow + (timestampHigh << 7);
// std::cout << "timestamp low is 0x" << std::hex << (int)timestampByte << std::endl;
} }
// std::cout << "first" << std::hex << (int)buffer[lPtr] << std::endl;
// Point to next status // Point to next status
lPtr = rPtr; lPtr = rPtr;
if (lPtr >= length) if (lPtr >= length)