From 6fbc9bd5a7a15860fd127d3d95c5208a7587eb3b Mon Sep 17 00:00:00 2001 From: Francois Best Date: Fri, 5 Jul 2013 11:21:13 +0200 Subject: [PATCH] Reworked encoding/decoding methods to support larger arrays. --- src/MIDI.cpp | 61 +++++++++++++++++++++++++++------------------------- src/MIDI.h | 4 ++-- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/MIDI.cpp b/src/MIDI.cpp index 0e36af9..22860aa 100644 --- a/src/MIDI.cpp +++ b/src/MIDI.cpp @@ -45,27 +45,30 @@ BEGIN_MIDI_NAMESPACE \return The lenght of the encoded output buffer. @see decodeSysEx */ -byte encodeSysEx(const byte* inData, byte* outSysEx, byte inLength) +unsigned encodeSysEx(const byte* inData, byte* outSysEx, unsigned 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; + unsigned outLength = 0; // Num bytes in output array. + byte count = 0; // Num 7bytes in a block. + outSysEx[0] = 0; + + for (unsigned i = 0; i < inLength; ++i) + { + const byte data = inData[i]; + const byte msb = data >> 7; + const byte body = data & 0x7f; + + outSysEx[0] |= (msb << count); + outSysEx[1 + count] = body; - if (count7++ == 6) { - outSysEx += 8; - retlen += 8; + if (count++ == 6) + { + outSysEx += 8; + outLength += 8; outSysEx[0] = 0; - count7 = 0; + count = 0; } } - return retlen + count7 + ((count7 != 0)?1:0); + return outLength + count + (count != 0 ? 1 : 0); } /*! \brief Decode System Exclusive messages. @@ -78,24 +81,24 @@ byte encodeSysEx(const byte* inData, byte* outSysEx, byte inLength) \return The lenght of the output buffer. @see encodeSysEx @see getSysExArrayLength */ -byte decodeSysEx(const byte* inSysEx, byte* outData, byte inLength) +unsigned decodeSysEx(const byte* inSysEx, byte* outData, unsigned inLength) { - byte cnt; - byte cnt2 = 0; - byte bits = 0; + unsigned count = 0; + byte msbStorage = 0; - for (cnt = 0; cnt < inLength; ++cnt) { - - if ((cnt % 8) == 0) { - bits = inSysEx[cnt]; + for (unsigned i = 0; i < inLength; ++i) + { + if ((i % 8) == 0) + { + msbStorage = inSysEx[i]; } - else { - outData[cnt2++] = inSysEx[cnt] | ((bits & 1) << 7); - bits >>= 1; + else + { + outData[count++] = inSysEx[i] | ((msbStorage & 1) << 7); + msbStorage >>= 1; } - } - return cnt2; + return count; } END_MIDI_NAMESPACE diff --git a/src/MIDI.h b/src/MIDI.h index 74d7c47..f1356b9 100644 --- a/src/MIDI.h +++ b/src/MIDI.h @@ -223,8 +223,8 @@ private: // ----------------------------------------------------------------------------- -byte encodeSysEx(const byte* inData, byte* outSysEx, byte inLenght); -byte decodeSysEx(const byte* inSysEx, byte* outData, byte inLenght); +unsigned encodeSysEx(const byte* inData, byte* outSysEx, unsigned inLenght); +unsigned decodeSysEx(const byte* inSysEx, byte* outData, unsigned inLenght); END_MIDI_NAMESPACE