ws-audio2: music/med (MED / OctaMED MMD0..MMD3): xm family with ProTracker periods, MED effects and tempo modes, play sequences, hold / decay envelopes, IFF multi-octave keymaps; engine: EXT tempo > 255, flags.envRowHold

Synthetic / hybrid / MIDI instruments are silent (listed in meta.unsupported).
This commit is contained in:
4DBug committed 2026-10-31 14:51:33 -05:00
1 parent d418458e2a
commit 3d5cf3130e
8 files changed
+935 -5

No files matched your search

+112
View File
@@ -781,3 +781,115 @@ def write_okt(samples, orders, patterns, cmod=(0, 0, 0, 0), speed=6):
for s in samples:
out += chunk(b"SBOD", s["data"])
return bytes(out)
def write_med(version, samples, blocks, sequence, tempo=125, tempo2=6, flags=0, flags2=0, trackvols=None,
pans=None, master=64, play_transpose=0, sections=None, title=None):
"""an MMD0 .. MMD3 module. samples: [{data (signed 8-bit bytes, or big-endian 16-bit with bits 16), bits, volume,
transpose, loop: (start, length) in bytes (even) | None, hold, decay, finetune, octaves (IFF multi-octave count:
data holds the octaves from the lowest, each twice as long)}]; blocks: [[row: [(note, ins, cmd, param)] * tracks]
* rows] with note 0 = none else the MED note (1 = C-1; MMD1+: 0x80 = note off); sequence: block numbers (MMD0 / 1)
or the play sequence (MMD2 / 3; sections: [[playseq numbers]] and playseqs = [sequence])"""
out = bytearray(52)
song_at = 52
out += b"\0" * (63 * 8 + 284)
need_ext = any(s.get("hold") or s.get("finetune") for s in samples) or title is not None
def align():
while len(out) % 4:
out.append(0)
# blocks
block_ptrs = []
for blk in blocks:
align()
block_ptrs.append(len(out))
ntr = len(blk[0])
if version == 0:
out += bytes([ntr, len(blk) - 1])
else:
out += struct.pack(">HHI", ntr, len(blk) - 1, 0)
for row in blk:
for (note, ins, cmd, param) in row:
if version == 0:
out += bytes([(note & 0x3F) | ((ins & 0x10) << 3) | ((ins & 0x20) << 1), ((ins & 15) << 4) | (cmd & 15), param])
else:
out += bytes([note, ins & 0x3F, cmd, param])
align()
block_arr = len(out)
for p in block_ptrs:
out += struct.pack(">I", p)
# instruments
inst_ptrs = []
for s in samples:
align()
inst_ptrs.append(len(out))
oct_ = s.get("octaves", 1)
typ = {1: 0, 5: 1, 3: 2, 2: 3, 4: 4, 6: 5, 7: 6}[oct_] | (0x10 if s.get("bits", 8) == 16 else 0)
out += struct.pack(">Ih", len(s["data"]), typ) + s["data"]
align()
smp_arr = len(out)
for p in inst_ptrs:
out += struct.pack(">I", p)
# play sequences (MMD2 / 3)
seq_info = None
if version >= 2:
align()
psq = []
for sq in [sequence]:
psq.append(len(out))
out += name("seq", 32) + struct.pack(">IIH", 0, 0, len(sq)) + b"".join(struct.pack(">H", b) for b in sq)
align()
ps_table = len(out)
for p in psq:
out += struct.pack(">I", p)
sec_table = len(out)
secs = sections or [0]
for sc in secs:
out += struct.pack(">H", sc)
align()
tv = len(out)
out += bytes(trackvols or [64] * len(blocks[0][0]))
tp = 0
if pans:
tp = len(out)
out += bytes([p & 255 for p in pans])
seq_info = (ps_table, sec_table, tv, tp, len(secs))
# expansion: instrument extras + song name
exp = 0
if need_ext:
align()
ie = len(out)
for s in samples:
ls, ll = s.get("loop") or (0, 0)
out += struct.pack(">BBBbBBHBBIIBBH", s.get("hold", 0), s.get("decay", 0), 0, s.get("finetune", 0), 0,
1 if s.get("loop") else 0, 0, 0, 0, ls, ll, 127, 0, 0)
nm = (title or "").encode() + b"\0"
na = len(out)
out += nm
align()
exp = len(out)
e = bytearray(80)
struct.pack_into(">IIHH", e, 0, 0, ie, len(samples), 22)
struct.pack_into(">II", e, 44, na, len(nm))
out += e
# the song header
for i, s in enumerate(samples):
ls, ll = s.get("loop") or (0, 0)
struct.pack_into(">HHBBBb", out, song_at + 8 * i, ls // 2, ll // 2, 0, 0, s.get("volume", 64), s.get("transpose", 0))
sh = song_at + 63 * 8
if version < 2:
struct.pack_into(">HH", out, sh, len(blocks), len(sequence))
out[sh + 4:sh + 4 + len(sequence)] = bytes(sequence)
else:
ps_table, sec_table, tv, tp, nsec = seq_info
struct.pack_into(">HH", out, sh, len(blocks), nsec)
struct.pack_into(">IIIHHIIHH", out, sh + 4, ps_table, sec_table, tv, len(blocks[0][0]), 1, tp, 0, 100, 0)
struct.pack_into(">HbBBB", out, sh + 260, tempo, play_transpose, flags, flags2, tempo2)
out[sh + 266:sh + 282] = bytes((trackvols or [64] * 16)[:16]) + bytes(max(0, 16 - len(trackvols or [0] * 16)))
out[sh + 282] = master
out[sh + 283] = len(samples)
hdr = bytearray(b"MMD" + bytes([0x30 + version]))
hdr += struct.pack(">IIHHIB3sIIII11sB", len(out), song_at, 0, 0, block_arr, 0, b"\0\0\0", smp_arr, 0, exp, 0, b"\0" * 11, 0)
out[0:52] = hdr
return bytes(out)
+113
View File
@@ -0,0 +1,113 @@
# MED fixtures (tools/fixtures.py): generated MED / OctaMED modules (MMD0, MMD1 with instrument extras, MMD2 with a play
# sequence) exercising the effects, rendered by openmpt123.
# tests/gen/med_cases.luau { name, data (the module), ref (features of openmpt's render, tools/fixtures/_tracker.py),
# duration (openmpt's song length) }
import os, sys
# the shared tracker module, loaded by path (tools/fixtures/ must not go on sys.path: xml.py there would shadow xml)
T = sys.modules.get("_tracker")
if T is None:
import importlib.util
_spec = importlib.util.spec_from_file_location("_tracker", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_tracker.py"))
T = importlib.util.module_from_spec(_spec)
sys.modules["_tracker"] = T
_spec.loader.exec_module(T)
INPUTS = ["tools/fixtures/_tracker.py"]
def empty(ch, rows=64):
return [[(0, 0, 0, 0) for _ in range(ch)] for _ in range(rows)]
def put(p, row, ch, note=0, ins=0, cmd=0, param=0):
p[row][ch] = (note, ins, cmd, param)
def samples(extras=False):
s = [
{"data": T.s8(T.synth("sine", 64, 64)), "volume": 64, "loop": (0, 64)},
{"data": T.s8(T.synth("square", 64, 64, 0.5)), "volume": 44, "loop": (0, 64)},
{"data": T.s8(T.synth("saw", 128, 64, 0.6)), "volume": 64, "loop": (64, 64), "transpose": -12},
{"data": T.s8(T.synth("noise", 4000, 1, 0.7, decay=1200)), "volume": 60},
{"data": T.s8(T.synth("tri", 32, 32, 0.8)), "volume": 52, "loop": (0, 32)},
]
if extras:
s[0]["hold"], s[0]["decay"] = 6, 4 # hold / decay: a volume envelope
s[1]["finetune"] = 3
s[4]["hold"], s[4]["decay"] = 2, 0
return s
def song_basic(keep=None, version=0):
a, b = empty(4), empty(4, 32)
mel = [13, 17, 20, 25, 20, 17, 15, 13]
for i in range(16):
put(a, i * 4, 0, mel[i % 8], 1)
put(a, 2, 0, 0, 0, 0x0, 0x37) # arpeggio
put(a, 0, 1, 1, 2)
put(a, 4, 1, 0, 0, 0x1, 0x02)
put(a, 12, 1, 0, 0, 0x2, 0x03)
put(a, 20, 1, 13, 2, 0xC, 0x32) # volume (decimal: 32)
put(a, 24, 1, 0, 0, 0xD, 0x03) # volume slide down (every tick)
put(a, 28, 1, 0, 0, 0xD, 0x20)
put(a, 32, 1, 8, 2, 0x4, 0x34) # vibrato (twice as deep)
put(a, 40, 1, 13, 2)
put(a, 41, 1, 20, 0, 0x3, 0x10) # tone portamento
put(a, 48, 1, 0, 0, 0x5, 0x01)
for r in range(0, 64, 8):
put(a, r, 3, 13, 4, 0xC, 0x64 - r)
put(a, 2, 2, 25, 3)
put(a, 36, 2, 1, 5, 0xF, 0xF2) # delay note
put(a, 44, 2, 1, 5, 0xF, 0xF1) # play twice
put(a, 34, 3, 0, 0, 0x9, 0x04) # secondary speed
put(a, 50, 3, 0, 0, 0xF, 0x90) # tempo 0x90 (BPM 144 / 0.264...)
put(b, 0, 0, 13, 1)
put(b, 8, 1, 20, 2)
put(b, 16, 2, 13, 3, 0x8, 0x40) # panning (8xx)
put(b, 24, 3, 25, 5, 0xF, 0x00) # pattern break
if keep is not None:
for p in (a, b):
for row in p:
for ch in range(4):
if ch not in keep and row[ch][2] not in (0x9, 0xF):
row[ch] = (0, 0, 0, 0)
return T.write_med(version, samples(version > 0), [a, b], [0, 1, 0], tempo=0x21 if version == 0 else 125, tempo2=6,
flags=0x10 if version > 0 else 0, flags2=0x23 if version > 0 else 0, title="med test" if version > 0 else None)
def song_mmd2():
"""MMD2: 6 tracks, a play sequence with a section, ProTracker-style slides, BPM tempo, track volumes"""
a = empty(6, 48)
for r in range(0, 48, 4):
put(a, r, r // 4 % 6, 13 + (r // 4) % 12, 1 + r // 4 % 5)
put(a, 6, 1, 0, 0, 0x1, 0x04)
put(a, 10, 2, 0, 0, 0xD, 0x02)
put(a, 20, 4, 0, 0, 0x16, 0x00) # loop start
put(a, 23, 4, 0, 0, 0x16, 0x01) # loop once
put(a, 30, 5, 0, 0, 0x1F, 0x20) # note delay 2
put(a, 44, 0, 0, 0, 0x1D, 0x08) # break to row 8 (hex)
b = empty(6, 16)
put(b, 0, 0, 25, 2, 0x0C, 0x20)
put(b, 8, 3, 30, 4)
return T.write_med(2, samples(), [a, b], [0, 1], tempo=120, tempo2=5, flags=0x20 | 0x10, flags2=0x20 | 3,
trackvols=[64, 48, 64, 32, 64, 64])
def song_octaves():
"""MMD1 with an IFF 3-octave sample (three copies of a waveform, each twice as long as the last): a keymap"""
w = T.s8(T.synth("saw", 32, 32, 0.7)) + T.s8(T.synth("saw", 64, 64, 0.7)) + T.s8(T.synth("saw", 128, 128, 0.7))
smp = [{"data": w, "volume": 64, "octaves": 3, "loop": (0, 32)}, samples()[0]]
a = empty(2, 48)
for i, n in enumerate([1, 8, 13, 20, 25, 32, 37, 30, 18, 6, 13, 25]):
put(a, i * 4, 0, n, 1)
put(a, i * 4 + 2, 1, n, 2, 0xC, 0x20)
return T.write_med(1, smp, [a], [0], tempo=125, tempo2=6, flags=0x10, flags2=0x23)
def generate(ctx):
cases = []
for nm, data in (("mmd0", song_basic()), ("mmd1", song_basic(version=1)), ("mmd2", song_mmd2()), ("octaves", song_octaves())):
ref = T.reference(ctx, data, "med")
cases.append({"name": nm, "data": data, "ref": ref, "duration": ref["duration"]})
ctx.emit("med_cases", cases)