Files
rosetta/tools/fixtures/xm.py
T
4DBug 9466c5f471 ws-audio2: music/xm decodes ModPlug ADPCM samples (were silence), through codecs/audio/Adpcm.modplug
Fixture: tools/fixtures/_tracker.py write_xm gains an optional per-sample "adpcm" payload (two lines, shared with
the tracker generators, backward compatible); a new xm case renders the same as openmpt123.
2026-10-31 14:37:23 -05:00

197 lines
7.8 KiB
Python

# XM fixtures (tools/fixtures.py): generated FastTracker 2 modules exercising instruments (envelopes, fadeout,
# auto-vibrato, keymaps, 16-bit and ping-pong samples) and the effects, rendered by openmpt123.
# tests/gen/xm_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"]
OFF = 97
Gx, Hx, Kx, Lx, Px, Rx, Tx, Xx = 16, 17, 20, 21, 25, 27, 29, 33
def empty(ch, rows=64):
return [[(0, 0, 0, 0, 0) for _ in range(ch)] for _ in range(rows)]
def put(p, row, ch, note=0, ins=0, vol=0, fx=0, param=0):
p[row][ch] = (note, ins, vol, fx, param)
def instruments():
sine = {"name": "sine", "data": T.s8(T.synth("sine", 64, 64)), "volume": 64, "pan": 128, "loop": (0, 64, 1)}
tri = {"name": "tri", "data": T.s8(T.synth("tri", 32, 32, 0.8)), "volume": 48, "pan": 96, "relnote": 12, "finetune": -20,
"loop": (0, 32, 1)}
saw16 = {"name": "saw16", "data": T.s16(T.synth("saw", 128, 64, 0.6)), "bits": 16, "volume": 56, "pan": 160,
"loop": (0, 128, 1)}
ping = {"name": "ping", "data": T.s8(T.synth("saw", 200, 200, 0.7)), "volume": 64, "pan": 128, "finetune": 30,
"loop": (20, 150, 2)}
noise = {"name": "noise", "data": T.s8(T.synth("noise", 5000, 1, 0.7, decay=1500)), "volume": 60, "pan": 128}
square = {"name": "square", "data": T.s8(T.synth("square", 64, 64, 0.5)), "volume": 64, "pan": 128, "loop": (0, 64, 1)}
return [
{"name": "env", "samples": [sine], "volenv": {"points": [(0, 0), (4, 64), (10, 40), (30, 20), (40, 0)], "sustain": 2},
"fadeout": 1024},
{"name": "panloop", "samples": [square], "panenv": {"points": [(0, 0), (8, 64), (16, 0)], "loop": (0, 2)},
"volenv": {"points": [(0, 64), (20, 48), (60, 32)], "loop": (1, 2)}, "fadeout": 512},
{"name": "autovib", "samples": [saw16], "vib": (0, 10, 8, 6)},
{"name": "multi", "samples": [sine, tri], "keymap": [0] * 48 + [1] * 48},
{"name": "ping", "samples": [ping]},
{"name": "noise", "samples": [noise]},
{"name": "sq vib", "samples": [square], "vib": (1, 0, 4, 20)},
{"name": "empty"},
]
def song_instruments():
a, b = empty(4), empty(4)
put(a, 0, 0, 49, 1)
put(a, 12, 0, OFF)
put(a, 20, 0, 56, 1)
put(a, 24, 0, 0, 0, 0, Kx, 3)
put(a, 32, 0, 61, 1)
put(a, 33, 0, 0, 0, 0, Lx, 20)
put(a, 40, 0, 63)
put(a, 44, 0, OFF)
put(a, 48, 0, 0, 1)
put(a, 0, 1, 37, 2)
put(a, 30, 1, OFF)
put(a, 40, 1, 44, 2)
put(a, 0, 2, 49, 3)
put(a, 32, 2, 56, 3)
put(a, 8, 3, 37, 4)
put(a, 16, 3, 61, 4)
put(a, 24, 3, 73, 4)
put(a, 32, 3, 85, 4)
put(a, 40, 3, 49, 5)
put(a, 52, 3, 61, 5)
put(a, 60, 3, 61, 8)
put(b, 0, 0, 49, 7)
put(b, 0, 1, 56, 3, 0x30)
put(b, 8, 1, 0, 3)
put(b, 16, 2, 61, 6, 0x40)
put(b, 20, 2, 61, 6, 0, 0x9, 0x08)
put(b, 24, 2, 61, 6, 0, 0xE, 0x93)
put(b, 32, 3, 49, 1)
put(b, 36, 3, 61)
put(b, 40, 3, OFF)
put(b, 44, 3, 63)
put(b, 48, 0, 0, 0, 0, 0xD, 0x00)
return T.write_xm("instruments", instruments(), [0, 1], [a, b], 4)
def song_effects(linear=True):
a, b = empty(6), empty(6, 48)
put(a, 0, 0, 49, 7, 0, 0x1, 0x04)
put(a, 4, 0, 0, 0, 0, 0x1, 0x00)
put(a, 8, 0, 0, 0, 0, 0x2, 0x08)
put(a, 12, 0, 0, 0, 0, 0xE, 0x18)
put(a, 13, 0, 0, 0, 0, 0xE, 0x10)
put(a, 14, 0, 0, 0, 0, 0xE, 0x2F)
put(a, 16, 0, 0, 0, 0, Xx, 0x1F)
put(a, 17, 0, 0, 0, 0, Xx, 0x10)
put(a, 18, 0, 0, 0, 0, Xx, 0x28)
put(a, 20, 0, 49, 7, 0, 0x3, 0x10)
put(a, 21, 0, 61, 0, 0, 0x3, 0x00)
put(a, 24, 0, 0, 0, 0, 0x5, 0x02)
put(a, 28, 0, 56, 0, 0, 0x4, 0x48)
put(a, 32, 0, 0, 0, 0, 0x6, 0x20)
put(a, 36, 0, 0, 0, 0, 0x7, 0x6F)
put(a, 40, 0, 0, 0, 0, Tx, 0x21)
put(a, 44, 0, 0, 0, 0, 0xA, 0x0F)
put(a, 46, 0, 0, 0, 0, 0xE, 0xA8)
put(a, 48, 0, 61, 7, 0, 0x0, 0x37)
put(a, 52, 0, 0, 0, 0, 0xE, 0xC3)
# channel 1: the volume column
put(a, 0, 1, 56, 6, 0x30)
put(a, 4, 1, 0, 0, 0x64)
put(a, 8, 1, 0, 0, 0x73)
put(a, 12, 1, 0, 0, 0x85)
put(a, 13, 1, 0, 0, 0x9A)
put(a, 16, 1, 0, 0, 0xA6)
put(a, 17, 1, 0, 0, 0xB8)
put(a, 24, 1, 0, 0, 0xC2)
put(a, 28, 1, 0, 0, 0xE4)
put(a, 32, 1, 0, 0, 0xD6)
put(a, 36, 1, 61, 0, 0xF8)
put(a, 40, 1, 49, 0, 0xF0)
# channel 2: panning, retrig, offset, delay
put(a, 0, 2, 61, 2, 0, 0x8, 0x20)
put(a, 8, 2, 0, 0, 0, Px, 0x30)
put(a, 16, 2, 0, 0, 0, Px, 0x06)
put(a, 24, 2, 61, 6, 0, Rx, 0x83)
put(a, 32, 2, 61, 6, 0, Rx, 0x32)
put(a, 40, 2, 61, 6, 0, 0x9, 0x06)
put(a, 48, 2, 61, 6, 0, 0xE, 0xD3)
put(a, 56, 2, 61, 6, 0x20, 0xE, 0xC2)
# channel 3: global volume, pattern loop, pattern delay, speed / tempo
put(a, 0, 3, 37, 3, 0, Gx, 0x30)
put(a, 8, 3, 0, 0, 0, Hx, 0x02)
put(a, 16, 3, 0, 0, 0, Hx, 0x40)
put(a, 24, 3, 0, 0, 0, 0xE, 0x60)
put(a, 26, 3, 0, 0, 0, 0xE, 0x62)
put(a, 32, 3, 0, 0, 0, 0xE, 0xE2)
put(a, 40, 3, 0, 0, 0, 0xF, 0x05)
put(a, 48, 3, 0, 0, 0, 0xF, 0x96)
# channel 4 / 5: an instrument change without a note, an empty instrument, a note after key off
put(a, 0, 4, 49, 1)
put(a, 8, 4, 0, 2)
put(a, 16, 4, OFF)
put(a, 24, 4, 51)
put(a, 32, 4, 51, 8)
put(a, 40, 4, 53, 1)
put(a, 4, 5, 73, 5, 0, 0xE, 0x80)
put(a, 20, 5, 0, 0, 0, 0x8, 0xC0)
# pattern b (48 rows): portamento in the other direction, arpeggio, a break and a jump back (song loop)
put(b, 0, 0, 73, 7, 0, 0x4, 0x00)
put(b, 0, 1, 61, 4)
put(b, 4, 1, 0, 0, 0, 0x0, 0x47)
put(b, 16, 1, 25, 4, 0, 0x2, 0x10)
put(b, 32, 0, 0, 0, 0, 0xB, 0x01)
return T.write_xm("effects", instruments(), [0, 1], [a, b], 6, speed=5, tempo=130, linear=linear, restart=1)
TABLE = [0, 1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64, -128]
def modplug_adpcm(raw):
"""ModPlug ADPCM of 8-bit sample bytes: the delta table, then one nibble per sample (low first), each nibble the
table delta that lands nearest (the running value wraps as a signed byte)"""
cur, nibs = 0, []
for b in raw:
want = b - 256 if b >= 128 else b
best = min(range(16), key=lambda k: abs(((cur + TABLE[k] + 128) % 256 - 128) - want))
cur = (cur + TABLE[best] + 128) % 256 - 128
nibs.append(best)
if len(nibs) % 2:
nibs.append(0)
return bytes(t & 255 for t in TABLE) + bytes(nibs[i] | nibs[i + 1] << 4 for i in range(0, len(nibs), 2))
def song_adpcm():
"""a ModPlug ADPCM sample (OpenMPT's compressed 8-bit samples) played at a few pitches"""
raw = T.s8(T.synth("saw", 400, 100, 0.6))
smp = {"name": "adpcm", "data": raw, "adpcm": modplug_adpcm(raw), "volume": 64, "pan": 128, "loop": (0, 400, 1)}
p = empty(2)
for i, note in enumerate((49, 53, 56, 61)):
put(p, i * 8, 0, note, 1)
put(p, i * 8 + 4, 1, note - 12, 1)
return T.write_xm("adpcm", [{"name": "adpcm", "samples": [smp]}], [0], [p], 2, speed=4, tempo=125)
def generate(ctx):
cases = []
for nm, data in (("instruments", song_instruments()), ("effects", song_effects()), ("amiga", song_effects(False)),
("adpcm", song_adpcm())):
ref = T.reference(ctx, data, "xm")
cases.append({"name": nm, "data": data, "ref": ref, "duration": ref["duration"]})
ctx.emit("xm_cases", cases)