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)