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