Types.luau: Midi gets an optional render(self, opts) -> Decoder field (backward compatible; set by music/midi decode).
73 lines
3.9 KiB
Python
73 lines
3.9 KiB
Python
# MIDI fixtures (tools/fixtures.py). Reference parses from python mido (tools/fixtures/_midi_ref.py, run in a nix
|
|
# python with mido) plus hand-made byte strings for what mido does not write.
|
|
# tests/gen/midi_cases.luau { { name, data, type, division, tracks = { { {tick, type, channel, data, meta} } },
|
|
# length?, times?, exact? } }: channel / meta = -1 when absent; exact = the bytes are
|
|
# what an encoder with running status writes (mido's own output)
|
|
# tests/gen/midi_bad.luau { { name, data, code } }: crafted bad files and the Err code expected
|
|
# tests/gen/midi_trunc.luau { name, data }: a mido file cut short (lenient decode keeps the events, strict fails)
|
|
import json, struct
|
|
|
|
INPUTS = ["tools/fixtures/_midi_ref.py"]
|
|
|
|
|
|
def vlq(v):
|
|
out = [v & 127]
|
|
v >>= 7
|
|
while v:
|
|
out.insert(0, (v & 127) | 128)
|
|
v >>= 7
|
|
return bytes(out)
|
|
|
|
|
|
def smf(tracks, fmt=0, division=96, extra=b""):
|
|
out = b"MThd" + struct.pack(">IHHh", 6, fmt, len(tracks), division) + extra
|
|
for body in tracks:
|
|
out += b"MTrk" + struct.pack(">I", len(body)) + body
|
|
return out
|
|
|
|
|
|
def generate(ctx):
|
|
ref = json.loads(ctx.python(["mido"], ctx.read("tools/fixtures/_midi_ref.py").decode()))
|
|
cases = []
|
|
by_name = {}
|
|
for c in ref:
|
|
case = {"name": c["name"], "data": bytes.fromhex(c["hex"]), "type": c["type"], "division": c["division"],
|
|
"tracks": c["tracks"], "exact": True}
|
|
if "length" in c:
|
|
case["length"] = c["length"]
|
|
case["times"] = c["times"]
|
|
cases.append(case)
|
|
by_name[c["name"]] = case
|
|
|
|
# running status across meta and escape events (mido's writer never relies on it), and an F7 escape
|
|
body = (b"\x00\x90\x3c\x40" + b"\x00\xff\x01\x01\x41" + b"\x10\x3c\x00" + b"\x00\xf7\x02\xf3\x01" + b"\x05\x3e\x7f"
|
|
+ b"\x00\xff\x2f\x00")
|
|
cases.append({"name": "running-after-meta", "data": smf([body]), "type": 0, "division": 96, "exact": False, "tracks": [[
|
|
[0, "noteOn", 0, [60, 64], -1], [0, "meta", -1, [0x41], 1], [16, "noteOn", 0, [60, 0], -1],
|
|
[16, "escape", -1, [0xF3, 0x01], -1], [21, "noteOn", 0, [62, 127], -1], [21, "meta", -1, [], 0x2F]]]})
|
|
# an unknown chunk between MThd and MTrk, a longer MThd, and an RMID wrapper
|
|
f0 = by_name["format0"]
|
|
raw = f0["data"]
|
|
hdr, rest = raw[:14], raw[14:]
|
|
cases.append(dict(f0, name="unknown-chunk", exact=False, data=hdr + b"XFIH" + struct.pack(">I", 5) + b"abcde" + rest))
|
|
long_hdr = b"MThd" + struct.pack(">I", 8) + raw[8:14] + b"\x00\x00" + rest
|
|
cases.append(dict(f0, name="long-mthd", exact=False, data=long_hdr))
|
|
riff = b"RMID" + b"data" + struct.pack("<I", len(raw)) + raw + (b"\x00" if len(raw) % 2 else b"")
|
|
cases.append(dict(f0, name="rmid", exact=False, data=b"RIFF" + struct.pack("<I", len(riff)) + riff))
|
|
ctx.emit("midi_cases", cases)
|
|
|
|
bad = [
|
|
{"name": "not-midi", "data": b"MTrk\x00\x00\x00\x00", "code": "CORRUPT"},
|
|
{"name": "data-over-127", "data": smf([b"\x00\x90\x3c\x80\x00\xff\x2f\x00"]), "code": "CORRUPT"},
|
|
{"name": "running-without-status", "data": smf([b"\x00\x3c\x40\x00\xff\x2f\x00"]), "code": "CORRUPT"},
|
|
{"name": "long-delta", "data": smf([b"\x81\x81\x81\x81\x01\x90\x3c\x40"]), "code": "CORRUPT"},
|
|
{"name": "format-3", "data": b"MThd" + struct.pack(">IHHh", 6, 3, 0, 96), "code": "UNSUPPORTED"},
|
|
{"name": "zero-division", "data": b"MThd" + struct.pack(">IHHh", 6, 0, 0, 0), "code": "CORRUPT"},
|
|
{"name": "short-header", "data": b"MThd\x00\x00\x00\x06\x00", "code": "TRUNCATED"},
|
|
{"name": "system-common", "data": smf([b"\x00\xf2\x01\x02"]), "code": "CORRUPT"},
|
|
{"name": "rmid-without-data", "data": b"RIFF\x0c\x00\x00\x00RMIDLIST\x00\x00\x00\x00", "code": "CORRUPT"},
|
|
]
|
|
ctx.emit("midi_bad", bad)
|
|
f1 = by_name["format1"]["data"]
|
|
ctx.emit("midi_trunc", {"name": "format1-cut", "data": f1[: len(f1) * 3 // 5]})
|