How does Elektron's 7-bit SysEx encoding work?
Algorithm
For each block of up to 7 raw bytes: build a header byte where bit (6 − j) is the high bit of raw byte j, then send the header followed by each raw byte with its high bit cleared. A final short block works the same way with fewer bytes.
def encode7(src: bytes) -> bytes:
out = bytearray()
for i in range(0, len(src), 7):
block = src[i:i + 7]
h = 0
for j, x in enumerate(block):
if x & 0x80:
h |= 1 << (6 - j)
out.append(h)
out.extend(x & 0x7F for x in block)
return bytes(out)
def decode7(enc: bytes) -> bytes:
out = bytearray(); i = 0
while i < len(enc):
h = enc[i]; i += 1
n = min(7, len(enc) - i)
for j in range(n):
out.append(enc[i + j] | (((h >> (6 - j)) & 1) << 7))
i += n
return bytes(out)
Size check
A 111,616-byte pattern+kit payload encodes to 127,562 bytes; with the 10-byte header and 5-byte trailer the frame is 127,577 bytes, which is exactly the size of the working-buffer reply we measured.
Checksum and length are computed over the encoded bytes, not the raw ones (see frame layout).
Pitfalls
- Offsets in our notes are always offsets into the decoded payload.
- Do not edit encoded bytes directly; decode, edit, re-encode, recompute checksum and length.
- Round-trip test: decode7(encode7(x)) == x for random x before sending anything.
FAQ
Why does Elektron use 7-bit packing?
SysEx data bytes cannot have the high bit set; packing 7 bytes into 8 keeps the payload MIDI-legal.
Which bit of the header byte belongs to the first data byte?
Bit 6 (0x40), in our observation.
Observed on / Status: Verified by round trips against Digitakt II OS 1.16/1.17 frames. The same scheme is commonly reported for other Elektron devices; we only tested the Digitakt II.
Deutsch
Je 7 Rohbytes wird ein Kopfbyte mit den 7 abgeschnittenen höchsten Bits vorangestellt (Bit 6 = erstes Byte). Aus 7 Bytes werden 8. Prüfsumme und Länge beziehen sich auf die kodierten Bytes.
Related
- What does a Digitakt II SysEx message look like?
- Where are pattern and Song Mode state in the Digitakt II project settings (0x54)?