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