Arduino MIDI Library  Version 4.3
MIDI.cpp
Go to the documentation of this file.
1 
29 #include "MIDI.h"
30 
31 // -----------------------------------------------------------------------------
32 
34 
46 unsigned encodeSysEx(const byte* inData, byte* outSysEx, unsigned inLength)
47 {
48  unsigned outLength = 0; // Num bytes in output array.
49  byte count = 0; // Num 7bytes in a block.
50  outSysEx[0] = 0;
51 
52  for (unsigned i = 0; i < inLength; ++i)
53  {
54  const byte data = inData[i];
55  const byte msb = data >> 7;
56  const byte body = data & 0x7f;
57 
58  outSysEx[0] |= (msb << (6 - count));
59  outSysEx[1 + count] = body;
60 
61  if (count++ == 6)
62  {
63  outSysEx += 8;
64  outLength += 8;
65  outSysEx[0] = 0;
66  count = 0;
67  }
68  }
69  return outLength + count + (count != 0 ? 1 : 0);
70 }
71 
83 unsigned decodeSysEx(const byte* inSysEx, byte* outData, unsigned inLength)
84 {
85  unsigned count = 0;
86  byte msbStorage = 0;
87  byte byteIndex = 0;
88 
89  for (unsigned i = 0; i < inLength; ++i)
90  {
91  if ((i % 8) == 0)
92  {
93  msbStorage = inSysEx[i];
94  byteIndex = 6;
95  }
96  else
97  {
98  const byte body = inSysEx[i];
99  const byte msb = ((msbStorage >> byteIndex--) & 1) << 7;
100  outData[count++] = msb | body;
101  }
102  }
103  return count;
104 }
105 
#define BEGIN_MIDI_NAMESPACE
#define END_MIDI_NAMESPACE
unsigned decodeSysEx(const byte *inSysEx, byte *outData, unsigned inLength)
Decode System Exclusive messages. SysEx messages are encoded to guarantee transmission of data bytes ...
Definition: MIDI.cpp:83
uint8_t byte
Definition: midi_Defs.h:37
BEGIN_MIDI_NAMESPACE unsigned encodeSysEx(const byte *inData, byte *outSysEx, unsigned inLength)
Encode System Exclusive messages. SysEx messages are encoded to guarantee transmission of data bytes ...
Definition: MIDI.cpp:46
MIDI Library for the Arduino.