ws-audio2: music/ptm (PolyTracker): it family, MOD letters with S3M fine slides, note slides, delta 8 / 16-bit samples; engine flags.panLaw = ft2 (square-root panning)

ops/MusicOps: accept modules of the other tracker formats by their engine family (Tracker.family), a one-line
backward-compatible change.
This commit is contained in:
4DBug committed 2026-10-31 14:29:01 -05:00
1 parent 48b4b4cc4e
commit 60544a3a77
9 files changed
+409 -4

No files matched your search

+53
View File
@@ -663,3 +663,56 @@ def write_ult(title, samples, orders, patterns, channels, version=4, pans=None,
for s in samples:
out += s["data"]
return bytes(out)
def write_ptm(title, samples, orders, patterns, channels, pans=None):
"""samples: [{name, data (signed bytes; 16-bit little-endian), bits 8|16, volume, c4spd, loop: (start, end) in
frames | None, pingpong}] (stored delta coded: 8-bit deltas of the byte stream, also for 16-bit); patterns: [[row:
[(note, ins, vol, cmd, param)] * channels] * 64] with note 0 = none, 254 = cut, else our note (1..120); vol None =
none; cmd None = none, else 0..0x16 (MOD effects 0..F, then PolyTracker's G..)"""
head = bytearray(name(title, 28) + bytes([26, 3, 2, 0]))
head += struct.pack("<HHHHH", len(orders), len(samples), len(patterns), channels, 0) + b"\0\0PTMF" + b"\0" * 16
pp = list(pans or [(i % 2) * 15 for i in range(channels)])
head += bytes(pp + [0] * (32 - len(pp)))
head += bytes(orders) + b"\xff" * (256 - len(orders))
pat_at = len(head)
head += b"\0" * 256
smp_at = len(head)
head += b"\0" * (80 * len(samples))
body = head
for k, pat in enumerate(patterns):
while len(body) % 16:
body.append(0)
struct.pack_into("<H", body, pat_at + 2 * k, len(body) // 16)
for row in pat:
for ch, (note, ins, vol, cmd, param) in enumerate(row):
what = (0x20 if note or ins else 0) | (0x40 if cmd is not None else 0) | (0x80 if vol is not None else 0)
if what:
body.append(what | ch)
if what & 0x20:
body += bytes([note, ins])
if what & 0x40:
body += bytes([cmd, param])
if what & 0x80:
body.append(vol)
body.append(0)
for i, s in enumerate(samples):
bps = 2 if s.get("bits", 8) == 16 else 1
raw = s["data"]
enc, prev = bytearray(), 0
for b in raw:
enc.append((b - prev) & 255)
prev = b
off = len(body)
body += enc
ls, le = s.get("loop") or (0, 0)
fl = 1 | (4 if s.get("loop") else 0) | (8 if s.get("pingpong") else 0) | (16 if bps == 2 else 0)
h = bytearray(80)
h[0] = fl
h[1:13] = name(s.get("name", "")[:8] + ".smp", 12)
h[13] = s.get("volume", 64)
struct.pack_into("<HHIIII", h, 14, s.get("c4spd", 8363), 0, off, len(raw), ls * bps, (le * bps + 1) if s.get("loop") else 0)
h[48:76] = name(s.get("name", ""), 28)
h[76:80] = b"PTMS"
body[smp_at + 80 * i:smp_at + 80 * i + 80] = h
return bytes(body)