Add an SMC-PAD preset editor that saves pad colours the way MidiSuite does.
The record layout and writes come from usbmon captures of MidiSuite: a pad colour is one three-byte write to its preset address, which lights the lamp straight away, and Save is the empty write that keeps it after a power cycle. Pad records are channel, note, min and max velocity, RGB, then Led. Tempo, swing, pad curve, and transpose come from the global block, memory type 4. The factory image from MidiSuite's Reset is kept as a test fixture, and scripts/decode_usbmon.py prints the SysEx in a capture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
adca8e17a4
commit
63b3129cca
13 files changed
+1811
No files matched your search
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Print the SMC-PAD SysEx in a usbmon capture.
|
||||
|
||||
sudo tcpdump -i usbmon1 -s 0 -U -w - > pad.pcap
|
||||
python3 scripts/decode_usbmon.py pad.pcap --device 21
|
||||
|
||||
Each USB-MIDI packet is reassembled into messages per cable, then SINCO
|
||||
SysEx is decoded to its command, memory, address, and data.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import struct
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from padeditor.sysex import decode # noqa: E402
|
||||
|
||||
LINKTYPE_USB_LINUX_MMAPPED = 220
|
||||
HEADER = struct.Struct("<QBBBBHBBqiiIIQiiII") # 64 bytes
|
||||
# Payload bytes carried by each USB-MIDI code index number.
|
||||
CIN_SIZE = {0x2: 2, 0x3: 3, 0x4: 3, 0x5: 1, 0x6: 2, 0x7: 3, 0x8: 3, 0x9: 3,
|
||||
0xA: 3, 0xB: 3, 0xC: 2, 0xD: 2, 0xE: 3, 0xF: 1}
|
||||
COMMANDS = {0x00: "ack", 0x22: "write", 0x23: "read"}
|
||||
|
||||
|
||||
def packets(path: Path):
|
||||
raw = path.read_bytes()
|
||||
magic = struct.unpack_from("<I", raw)[0]
|
||||
if magic not in (0xA1B2C3D4, 0xA1B23C4D):
|
||||
raise SystemExit(f"{path} is not a little-endian pcap")
|
||||
linktype = struct.unpack_from("<I", raw, 20)[0]
|
||||
if linktype != LINKTYPE_USB_LINUX_MMAPPED:
|
||||
raise SystemExit(f"{path} has link type {linktype}, expected usbmon ({LINKTYPE_USB_LINUX_MMAPPED})")
|
||||
at = 24
|
||||
while at + 16 <= len(raw):
|
||||
sec, frac, incl, _orig = struct.unpack_from("<IIII", raw, at)
|
||||
at += 16
|
||||
yield sec + frac / 1e6, raw[at:at + incl]
|
||||
at += incl
|
||||
|
||||
|
||||
def transfers(path: Path, device: int | None):
|
||||
"""(time, direction, data) for bulk and interrupt transfers that carry data."""
|
||||
for stamp, frame in packets(path):
|
||||
if len(frame) < HEADER.size:
|
||||
continue
|
||||
fields = HEADER.unpack_from(frame)
|
||||
kind, xfer, endpoint, devnum = fields[1], fields[2], fields[3], fields[4]
|
||||
if device is not None and devnum != device:
|
||||
continue
|
||||
if xfer not in (1, 3):
|
||||
continue
|
||||
data = frame[HEADER.size:]
|
||||
inbound = bool(endpoint & 0x80)
|
||||
# Host-to-pad data rides on the submit, pad-to-host on the completion.
|
||||
if data and ((not inbound and kind == ord("S")) or (inbound and kind == ord("C"))):
|
||||
yield stamp, "pad→host" if inbound else "host→pad", data
|
||||
|
||||
|
||||
def messages(path: Path, device: int | None):
|
||||
pending: dict[tuple[str, int], bytearray] = {}
|
||||
for stamp, direction, data in transfers(path, device):
|
||||
for at in range(0, len(data) - 3, 4):
|
||||
head = data[at]
|
||||
cin, cable = head & 0x0F, head >> 4
|
||||
if head == 0:
|
||||
continue
|
||||
body = data[at + 1:at + 1 + CIN_SIZE.get(cin, 0)]
|
||||
key = (direction, cable)
|
||||
if cin in (0x4, 0x5, 0x6, 0x7) or (pending.get(key) and cin == 0xF):
|
||||
buf = pending.setdefault(key, bytearray())
|
||||
buf += body
|
||||
if cin != 0x4 and buf.endswith(b"\xf7"):
|
||||
yield stamp, direction, cable, bytes(buf)
|
||||
del pending[key]
|
||||
else:
|
||||
yield stamp, direction, cable, bytes(body)
|
||||
|
||||
|
||||
def describe(message: bytes) -> str:
|
||||
if message[:1] != b"\xf0" or message[-1:] != b"\xf7":
|
||||
return message.hex(" ")
|
||||
inner = decode(message[1:-1])
|
||||
if inner[:2] != b"\x00\x59" or len(inner) < 7:
|
||||
return f"sysex {inner.hex(' ')}"
|
||||
command = COMMANDS.get(inner[2], f"cmd {inner[2]:#04x}")
|
||||
length = int.from_bytes(inner[3:6], "little")
|
||||
payload = inner[6:6 + length]
|
||||
if inner[2] in (0x22, 0x23) and len(payload) >= 8:
|
||||
memory = payload[0]
|
||||
address = int.from_bytes(payload[1:5], "little")
|
||||
size = int.from_bytes(payload[5:8], "little")
|
||||
data = payload[8:]
|
||||
text = f"{command} mem {memory} addr {address:#010x} len {size}"
|
||||
return f"{text} data {data.hex()}" if data else text
|
||||
return f"{command} {payload.hex(' ')}"
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("capture", type=Path)
|
||||
parser.add_argument("--device", type=int, help="USB device number from lsusb")
|
||||
args = parser.parse_args(argv)
|
||||
start = None
|
||||
for stamp, direction, cable, message in messages(args.capture, args.device):
|
||||
start = stamp if start is None else start
|
||||
print(f"{stamp - start:8.3f} {direction} cable {cable} {describe(message)}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user