From 80f5fd2c5b010842b9d20bdf2a302ed99a4044c7 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:19:23 -0500 Subject: [PATCH] ws-audio2: music/stm (Scream Tracker 2): s3m family with the ST2 tempo byte, deferred Bxx, no effect memory --- docs/FORMATS.md | 1 + src/Formats/Registry.luau | 12 ++ src/Formats/music/stm/init.luau | 233 ++++++++++++++++++++++++++++++++ tests/music/stm.luau | 19 +++ tools/fixtures/_tracker.py | 41 ++++++ tools/fixtures/stm.py | 90 ++++++++++++ 6 files changed, 396 insertions(+) create mode 100644 src/Formats/music/stm/init.luau create mode 100644 tests/music/stm.luau create mode 100644 tools/fixtures/stm.py diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 4432463..dd58d62 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -80,6 +80,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | music | mtm | .mtm | yes | - | render: true, channels: 1, 32 | `tests/music/mtm.luau` | | music | s3m | .s3m | yes | - | render: true, channels: 1, 32 | `tests/music/s3m.luau` | | music | sf2 | .sf2 | yes | - | sm24: true, sf3: false | `tests/music/sf2.luau` | +| music | stm | .stm | yes | - | render: true, channels: 4, 4 | `tests/music/stm.luau` | | music | xm | .xm | yes | yes | render: true, channels: 1, 64 | `tests/music/xm.luau` | | playlist | m3u | .m3u .m3u8 | yes | yes | hls: true | `tests/playlist/m3u.luau` | | playlist | pls | .pls | yes | yes | | `tests/playlist/pls.luau` | diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 2638300..ad8137b 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -914,6 +914,18 @@ local list: { Entry } = { return require("@formats/music/sf2") end, }, + { + name = "stm", + category = "music", + path = "music/stm", + extensions = { "stm" }, + mime = { "audio/x-stm" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" }, + caps = { decode = true, encode = false, render = true, channels = { 4, 4 } }, + load = function() + return require("@formats/music/stm") + end, + }, { name = "xm", category = "music", diff --git a/src/Formats/music/stm/init.luau b/src/Formats/music/stm/init.luau new file mode 100644 index 0000000..c2bf826 --- /dev/null +++ b/src/Formats/music/stm/init.luau @@ -0,0 +1,233 @@ +--!strict +-- music/stm: Scream Tracker 2 modules (STM, "!Scream!" / BMOD2STM / WUZAMOD! / SWavePro; 4 channels, 31 samples). +-- Ported from OpenMPT's Load_stm.cpp (BSD, licenses/openmpt-LICENSE.txt). Reads into the codecs/audio/Tracker Module +-- and plays as the "s3m" effect family with Scream Tracker 2's tempo (flags.st2Tempo: the speed byte also sets the +-- tick length), no effect memory, no fine slides (the loader drops what ST2 ignores) and its deferred Bxx (the jump +-- happens at the end of the pattern or at the next Cxx). +-- Not supported: STX (Scream Tracker Music Interface Kit), ST2's noise for samples at volume 0 (silent here), writing. + +-- ------------------------------------------------------------------------------------------------ requires +local Err = require("@formats/core/Err") +local Bytes = require("@formats/core/Bytes") +local Types = require("@formats/Types") +local Tracker = require("@formats/codecs/audio/Tracker") +local Load = require("@formats/codecs/audio/Tracker/Load") + +-- ------------------------------------------------------------------------------------------------ contract +local Stm = { + name = "stm", + category = "music", + extensions = { "stm" }, + mime = { "audio/x-stm" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Tracker/Load" }, + caps = { decode = true, encode = false, render = true, channels = { 4, 4 } }, +} + +export type DecodeOptions = { + maxBytes: number?, -- refuse more sample data in total (f32 bytes, default 2^28) +} + +-- ------------------------------------------------------------------------------------------------ constants +local IT = Load.IT +-- .ABCDEFGHIJ (K..O do nothing) +local EFFECTS = { "speed", "jump", "break", "volslide", "portadown", "portaup", "toneporta", "vibrato", "tremor", "arpeggio" } + +-- ------------------------------------------------------------------------------------------------ sniff +local function validHeader(r: Bytes.Reader): boolean + if r:size() < 48 then + return false + end + local eof, ftype, major, minor = r:peekU8(28), r:peekU8(29), r:peekU8(30), r:peekU8(31) + local npat, gv = r:peekU8(33), r:peekU8(34) + if ftype ~= 2 or (eof ~= 0x1A and eof ~= 2) or major ~= 2 or (minor ~= 0 and minor ~= 10 and minor ~= 20 and minor ~= 21) then + return false + end + if npat > 64 or (gv > 64 and gv ~= 0x58) then + return false + end + for i = 20, 27 do + local c = r:peekU8(i) + if c < 0x20 or c >= 0x7F then + return false + end + end + return true +end + +function Stm.sniff(r: Bytes.Reader): number + return if validHeader(r) then 0.7 else 0 +end + +-- ------------------------------------------------------------------------------------------------ decode +-- a tempo byte of files before ST 2.21 is decimal +local function tempoByte(v: number, minor: number): number + return if minor < 21 then (v // 10) * 16 + v % 10 else v +end + +function Stm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) + local budget = { left = opts and opts.maxBytes or 2 ^ 28 } + if not validHeader(r) then + return nil, Err.new(if r:size() < 48 then Err.TRUNCATED else Err.CORRUPT, "not a Scream Tracker 2 module") + end + local title = r:fixed(20) + local tracker = r:fixed(8) + r:skip(3) + local minor, initTempo, npat, gv = r:u8(), r:u8(), r:u8(), r:u8() + r:skip(13) + local m = Tracker.newModule("stm", 4, "s3m") + m.title = title + m.linear = false + m.instrumentMode = false + m.flags.st2Tempo = true + initTempo = tempoByte(initTempo, minor) + if initTempo == 0 then + initTempo = 0x60 + end + m.speed, m.tempo = Tracker.st2Tempo(initTempo) + if minor > 10 then + m.globalVolume = math.min(gv, 64) * 2 + end + local offsets: { number } = {} + for i = 1, 31 do + local s = Tracker.newSample(r:fixed(12)) + local zero = r:u8() + r:skip(1) + local off, len, ls, le = r:u16le(), r:u16le(), r:u16le(), r:u16le() + local vol = r:u8() + r:skip(1) + local rate = r:u16le() + r:skip(6) + if r.err then + return nil, r.err + end + if zero ~= 0 and zero ~= 46 then + return nil, Err.new(Err.CORRUPT, "bad sample header " .. i) + end + s.c5speed = if rate > 0 then rate else 8363 + s.volume = math.min(vol, 64) + if len < 2 then + len = 0 + end + if ls < len and le > ls and le ~= 0xFFFF then + s.loopStart, s.loopEnd, s.loopType = ls, le, 1 + len = math.max(le, len) -- ST2 lets a loop run into the next sample's data + end + s.length = len + offsets[i] = off * 16 + m.samples[i] = s + end + for i = 1, (if minor == 0 then 64 else 128) do + local o = r:u8() + if o == 99 or o == 255 then + o = 255 + elseif o > 63 then + return nil, Err.new(Err.CORRUPT, "bad order list") + end + m.order[i] = o + end + if r.err then + return nil, r.err + end + while #m.order > 0 and m.order[#m.order] == 255 do + table.remove(m.order) + end + -- patterns: 64 rows x 4 channels; one byte 0xFB (C-0, volume 0), 0xFC (nothing) or 0xFD (note cut), else 4 bytes + for p = 0, npat - 1 do + local pat = Tracker.newPattern(64, 4) + m.patterns[p + 1] = pat + local d = pat.data + local breakPos, breakRow = -1, 63 -- ST2: Bxx only sets the next order, taken at the pattern's end or a Cxx + for row = 0, 63 do + for ch = 0, 3 do + local q = (row * 4 + ch) * 5 + local note = r:u8() + local insVol, volCmd, info = 0, 0, 0 + if note == 0xFB then + note = 0 -- a C-0 at volume 0 (OpenMPT reads it so) + elseif note == 0xFD then + buffer.writeu8(d, q, 254) + elseif note ~= 0xFC then + insVol, volCmd, info = r:u8(), r:u8(), r:u8() + end + if note ~= 0xFC and note ~= 0xFD then + if note == 0xFE then + buffer.writeu8(d, q, 254) + elseif note < 0x60 then + buffer.writeu8(d, q, (note // 16) * 12 + note % 16 + 37) + end + local ins = insVol // 8 + buffer.writeu8(d, q + 1, if ins > 31 then 0 else ins) + local vol = insVol % 8 + (volCmd // 16) * 8 + if vol <= 64 then + buffer.writeu8(d, q + 2, vol + 1) + end + local name, param = EFFECTS[volCmd % 16], info + if name == "volslide" then + param = if param % 16 > 0 then param % 16 else param - param % 16 + elseif name == "break" then + param = ((param - param % 16) * 10 + param % 16) % 256 + if breakPos >= 0 and param == 0 then + name, param = "jump", breakPos -- Bxx + C00 is a plain Bxx + breakPos = -1 + end + breakRow = math.min(breakRow, row) + if name == "break" then + param = Load.bcd(param) + end + elseif name == "jump" then + breakPos, breakRow = param, 63 + name = nil + elseif name == "speed" then + param = tempoByte(param, minor) + if param == 0 then + name = nil + end + elseif name ~= "tremor" and param == 0 then + name = nil -- no effect memory in ST2 + end + if name then + buffer.writeu8(d, q + 3, IT[name]) + buffer.writeu8(d, q + 4, param) + end + end + end + end + if r.err then + return nil, r.err + end + if breakPos >= 0 then + Load.writeEffect(m, p, breakRow, IT.jump, breakPos, "prev") + end + end + -- sample data: signed 8-bit at the paragraph offsets (samples at volume 0 are not read) + local buf = r.buf + for i = 1, 31 do + local s = m.samples[i] + local len = s.length + s.length = 0 + if len > 0 and s.volume > 0 and offsets[i] > 48 then + local ok, err = Load.readSample(buf, r.base + offsets[i], len, { bits = 8, signed = true, limit = r.limit }, s, budget) + if not ok then + return nil, err + end + end + end + m.meta.source = { format = "stm", tracker = tracker, version = "2." .. minor } + local ok, verr = Tracker.validate(m) + if not ok then + return nil, Err.new(Err.CORRUPT, verr and verr.message or "invalid module") + end + return m, nil +end + +-- ------------------------------------------------------------------------------------------------ open +-- open(r, opts) -> the module rendered as an audio Decoder (opts: Tracker.RenderOptions + DecodeOptions) +function Stm.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?) + local m, err = Stm.decode(r, opts) + if not m then + return nil, err + end + return Tracker.render(m, opts) +end + +return Stm diff --git a/tests/music/stm.luau b/tests/music/stm.luau new file mode 100644 index 0000000..4b2ccfa --- /dev/null +++ b/tests/music/stm.luau @@ -0,0 +1,19 @@ +-- music/stm tests: generated Scream Tracker 2 modules (tools/fixtures/stm.py) parsed, rendered through +-- codecs/audio/Tracker and compared with openmpt123's render (loudness envelope, band spectrum, stereo balance, song +-- length), the contract, and the fuzz no-throw pass. +local T = require("@tests/T") +local S = require("@tests/music/_similar") +local Stm = require("@formats/music/stm") +local Cases = require("@tests/gen/stm_cases") + +local t = T.suite("stm") + +t:eq(Stm.name, "stm", "name") +t:eq(Stm.category, "music", "category") +t:check(Stm.caps.decode and not Stm.caps.encode, "caps") + +local mods = S.against(t, Stm, Cases, { gainDb = 0.5, envErr = 1.0, spectral = 0.95, balance = 0.04, duration = 0.01 }, "s3m") +t:check(mods[1] and mods[1].meta.source.tracker == "!Scream!", "tracker name") + +t:fuzz("decode", S.fuzzer(Stm), S.samples(Cases), { mutations = 30 }) +t:done() diff --git a/tools/fixtures/_tracker.py b/tools/fixtures/_tracker.py index 361f948..c9237e5 100644 --- a/tools/fixtures/_tracker.py +++ b/tools/fixtures/_tracker.py @@ -575,3 +575,44 @@ def write_669(message, samples, orders, patterns, tempos, breaks, restart=0, ext for s in samples: out += s["data"] return bytes(out) + + +def write_stm(title, samples, orders, patterns, tempo=0x60, gvol=64, minor=21): + """samples: [{name, data (signed bytes), volume, c2spd, loop: (start, end) | None}] (up to 31); patterns: [[row: + [(note, ins, vol, cmd, info)] * 4] * 64] with note 0 = none, 254 = cut, else our note (>= 37); vol None = none; + cmd 0 = none (A = 1, letters as in S3M)""" + head = bytearray(name(title, 20) + b"!Scream!" + bytes([0x1A, 2, 2, minor, tempo, len(patterns), gvol]) + b"\0" * 13) + smp_at = len(head) + head += b"\0" * (31 * 32) + head += bytes(orders) + bytes([99] * ((64 if minor == 0 else 128) - len(orders))) + for pat in patterns: + for row in pat: + for (note, ins, vol, cmd, info) in row: + if note in ("skip", "fb"): + head.append(0xFC if note == "skip" else 0xFB) # nothing / a C-0 at volume 0 + continue + if note == 254: + nb = 0xFE + elif note: + n = note - 37 + nb = (n // 12) << 4 | (n % 12) + else: + nb = 0xFF + v = 65 if vol is None else vol + head += bytes([nb, (ins << 3) | (v & 7), ((v >> 3) << 4) | cmd, info]) + body = head + for i in range(31): + h = bytearray(32) + if i < len(samples): + s = samples[i] + while len(body) % 16: + body.append(0) + off = len(body) // 16 + body += s["data"] + ls, le = s.get("loop") or (0, 0xFFFF) + h[0:12] = name(s.get("name", ""), 12) + struct.pack_into("