diff --git a/src/MIDI.cpp b/src/MIDI.cpp index f776846..a59ae71 100644 --- a/src/MIDI.cpp +++ b/src/MIDI.cpp @@ -199,6 +199,41 @@ void MidiInterface::send(MidiType inType, else if (inType >= TuneRequest && inType <= SystemReset) sendRealTime(inType); // System Real-time and 1 byte. } + +/*! \brief Encode System Exclusive messages. + SysEx messages are encoded to guarantee transmission of data bytes higher than + 127 without breaking the MIDI protocol. Use this static method to convert the + data you want to send. + \param inData The data to encode. + \param outSysEx The output buffer where to store the encoded message. + \param inLength The lenght of the input buffer. + \return The lenght of the encoded output buffer. + @see decodeSysEx + */ +byte MidiInterface::encodeSysEx(const byte* inData, + byte* outSysEx, + byte inLength) +{ + byte retlen = 0; + byte compteur; + byte count7 = 0; + + outSysEx[0] = 0; + for (compteur = 0; compteur < inLength; compteur++) { + byte c = inData[compteur] & 0x7F; + byte msb = inData[compteur] >> 7; + outSysEx[0] |= msb << count7; + outSysEx[1 + count7] = c; + + if (count7++ == 6) { + outSysEx += 8; + retlen += 8; + outSysEx[0] = 0; + count7 = 0; + } + } + return retlen + count7 + ((count7 != 0)?1:0); +} #endif // MIDI_BUILD_OUTPUT @@ -622,6 +657,41 @@ void MidiInterface::launchCallback() #endif // MIDI_USE_CALLBACKS +// ----------------------------------------------------------------------------- + +/*! \brief Decode System Exclusive messages. + SysEx messages are encoded to guarantee transmission of data bytes higher than + 127 without breaking the MIDI protocol. Use this static method to reassemble + your received message. + \param inSysEx The SysEx data received from MIDI in. + \param outData The output buffer where to store the decrypted message. + \param inLength The lenght of the input buffer. + \return The lenght of the output buffer. + @see encodeSysEx @see getSysExArrayLength + */ +byte MidiInterface::decodeSysEx(const byte* inSysEx, + byte* outData, + byte inLength) +{ + + byte cnt; + byte cnt2 = 0; + byte bits = 0; + + for (cnt = 0; cnt < inLength; ++cnt) { + + if ((cnt % 8) == 0) { + bits = inSysEx[cnt]; + } + else { + outData[cnt2++] = inSysEx[cnt] | ((bits & 1) << 7); + bits >>= 1; + } + + } + return cnt2; +} + #endif // MIDI_BUILD_INPUT diff --git a/src/MIDI.h b/src/MIDI.h index 3b55365..77adca0 100644 --- a/src/MIDI.h +++ b/src/MIDI.h @@ -83,7 +83,10 @@ public: DataByte inData1, DataByte inData2, Channel inChannel); - + +public: + static byte decodeSysEx(const byte* inSysEx, byte* outData, byte inLenght); + private: inline StatusByte getStatus(MidiType inType, Channel inChannel) const; @@ -115,7 +118,8 @@ public: public: static inline MidiType getTypeFromStatusByte(const byte inStatus); - + static byte encodeSysEx(const byte* inData, byte* outSysEx, byte inLenght); + private: bool inputFilter(Channel inChannel); bool parse(Channel inChannel);