From 4484b72e72790e6692d25b4711ff3f98e352b253 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 09:00:04 -0500 Subject: [PATCH] ws-music: music/s3m read + render (OpenMPT-matched master volume / mono rule / default panning), finer comparison bands, voice ownership fix --- docs/FORMATS.md | 1 + src/Formats/Registry.luau | 12 ++ src/Formats/codecs/Tracker/Player.luau | 23 ++- src/Formats/music/s3m/init.luau | 241 +++++++++++++++++++++++++ tests/music/_similar.luau | 14 +- tests/music/mod.luau | 8 +- tests/music/s3m.luau | 71 ++++++++ tests/music/zz_probe.luau | 27 +++ tests/music/zz_voices.luau | 24 +++ tools/fixtures/_tracker.py | 6 +- tools/fixtures/s3m.py | 124 +++++++++++++ tools/fixtures/zz_probe.py | 62 +++++++ 12 files changed, 598 insertions(+), 15 deletions(-) create mode 100644 src/Formats/music/s3m/init.luau create mode 100644 tests/music/s3m.luau create mode 100644 tests/music/zz_probe.luau create mode 100644 tests/music/zz_voices.luau create mode 100644 tools/fixtures/s3m.py create mode 100644 tools/fixtures/zz_probe.py diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 9bd0313..450e651 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -8,6 +8,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | image | png | .png .apng | yes | yes | animation: false, alpha: true, depths: 1, 2, 4, 8, 16 | `tests/image/png.luau` | | music | midi | .mid .midi .smf .kar .rmi | yes | yes | formats: 0, 1, 2, render: true, streaming: true, rmid: true | `tests/music/midi.luau` | | music | mod | .mod .m15 .stk .nst | yes | yes | render: true, channels: 1, 32 | `tests/music/mod.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` | Codecs (layer 1, shared by formats; not loadable on their own): diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 751f221..222ae4e 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -50,6 +50,18 @@ local list: { Entry } = { return require("@formats/music/mod") end, }, + { + name = "s3m", + category = "music", + path = "music/s3m", + extensions = { "s3m" }, + mime = { "audio/s3m", "audio/x-s3m" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/Tracker" }, + caps = { decode = true, encode = false, render = true, channels = { 1, 32 } }, + load = function() + return require("@formats/music/s3m") + end, + }, { name = "sf2", category = "music", diff --git a/src/Formats/codecs/Tracker/Player.luau b/src/Formats/codecs/Tracker/Player.luau index df4350b..7f34e11 100644 --- a/src/Formats/codecs/Tracker/Player.luau +++ b/src/Formats/codecs/Tracker/Player.luau @@ -23,7 +23,7 @@ local MOD, S3M, XM, IT = 1, 2, 3, 4 Player.MOD, Player.S3M, Player.XM, Player.IT = MOD, S3M, XM, IT local FAMILY = { mod = MOD, s3m = S3M, xm = XM, it = IT } -local MAX_VOICES = 96 -- channels + IT background voices +local BG_VOICES = 64 -- voices beyond one per channel, for IT new note actions -- ------------------------------------------------------------------------------------------------ state local function newVoice(): any @@ -86,6 +86,7 @@ local function newVoice(): any tremorMute = false, surround = false, rampN = 0, + owner = 0, -- the channel whose foreground voice this is (0: free or background) order = 0, -- allocation age } end @@ -225,7 +226,7 @@ function Player.new(m: any, sampleRate: number, opts: any): any end st.chans[i] = c end - for i = 1, MAX_VOICES do + for i = 1, m.channels + BG_VOICES do st.voices[i] = newVoice() end st.master = Player.masterGain(m, fmt) @@ -239,7 +240,7 @@ function Player.masterGain(m: any, fmt: number): number if fmt == IT then return 0.5 * (m.mixVolume or 48) / 48 elseif fmt == S3M then - return 0.5 * (m.mixVolume or 48) / 48 + return (m.mixVolume or 48) / 128 -- OpenMPT: linear in the master volume, 64 = 0.5 end -- MOD: OpenMPT scales by the channel count (4 channels: 0.5; 2 / channels between 2 and 8) return 2 / clamp(m.channels, 2, 8) @@ -361,7 +362,7 @@ end local function allocVoice(st: any): any local best, bestScore = nil, math.huge for _, v in st.voices do - if not v.active then + if not v.active and v.owner == 0 then return v end if v.bg then @@ -375,7 +376,17 @@ local function allocVoice(st: any): any best.active = false return best end - return st.voices[1] -- unreachable while MAX_VOICES > channels: every channel holds one foreground voice + -- every voice is busy in the foreground (more channels than voices): reuse an inactive one anyway + for _, v in st.voices do + if not v.active then + local c = st.chans[v.owner] + if c and c.voice == v then + c.voice = nil + end + return v + end + end + return st.voices[1] -- unreachable: there are more voices than channels end local function keyOff(st: any, v: any) @@ -422,6 +433,7 @@ local function startVoice(st: any, c: any, smp: any, smpIdx: number, ins: any, i if v == nil or v.bg then v = allocVoice(st) c.voice = v + v.owner = c.idx end local wasActive = v.active v.active = smp.length > 0 and smp.data ~= nil @@ -494,6 +506,7 @@ local function newNoteAction(st: any, c: any, ins: any, insIdx: number, note: nu return end v.bg = true + v.owner = 0 c.voice = nil if nna == 2 then keyOff(st, v) diff --git a/src/Formats/music/s3m/init.luau b/src/Formats/music/s3m/init.luau new file mode 100644 index 0000000..f7a80f7 --- /dev/null +++ b/src/Formats/music/s3m/init.luau @@ -0,0 +1,241 @@ +--!strict +-- music/s3m: Scream Tracker 3 modules (SCRM, ST3 TECH.DOC; also files from Impulse Tracker / OpenMPT / Schism +-- Tracker). Reads into the codecs/Tracker Module and plays through its engine (Module:render() / open() -> an audio +-- Decoder). Supports: up to 32 PCM channels, 8 / 16-bit signed or unsigned, mono or stereo samples, loops, the panning +-- table, ST3.00 fast volume slides, every ST3 effect. Notes are stored one octave up (S3M C-4 = note 61 = the C5 +-- speed reference, as IT shows S3M files). +-- Not supported: AdLib (OPL) instruments and channels (read as silent), writing. + +-- ------------------------------------------------------------------------------------------------ requires +local Err = require("@formats/core/Err") +local Bytes = require("@formats/core/Bytes") +local Types = require("@formats/Types") +local Tracker = require("@formats/codecs/Tracker") + +-- ------------------------------------------------------------------------------------------------ contract +local S3m = { + name = "s3m", + category = "music", + extensions = { "s3m" }, + mime = { "audio/s3m", "audio/x-s3m" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/Tracker" }, + caps = { decode = true, encode = false, render = true, channels = { 1, 32 } }, +} + +export type DecodeOptions = { + maxBytes: number?, -- refuse more sample data in total (f32 bytes, default 2^28) +} + +-- ------------------------------------------------------------------------------------------------ sniff +function S3m.sniff(r: Bytes.Reader): number + if r:size() >= 48 and r:peek(4, 44) == "SCRM" then + return 1 + end + return 0 +end + +-- ------------------------------------------------------------------------------------------------ decode +function S3m.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) + local maxBytes = opts and opts.maxBytes or 2 ^ 28 + if r:size() < 96 then + return nil, Err.new(Err.TRUNCATED, "header") + end + if r:peek(4, 44) ~= "SCRM" then + return nil, Err.new(Err.CORRUPT, "no SCRM signature") + end + local title = r:fixed(28) + r:seek(32) + local ordnum, insnum, patnum = r:u16le(), r:u16le(), r:u16le() + local flags, cwt, ffi = r:u16le(), r:u16le(), r:u16le() + r:seek(48) + local gv, speed, tempo, mv, _uc, dp = r:u8(), r:u8(), r:u8(), r:u8(), r:u8(), r:u8() + r:seek(64) + local chnset: { number } = {} + for i = 1, 32 do + chnset[i] = r:u8() + end + if r.err then + return nil, r.err + end + if ordnum > 256 or insnum > 256 or patnum > 256 then + return nil, Err.new(Err.CORRUPT, "bad header counts") + end + -- channels: enabled PCM channels (0..15); the count is the last used one + local nch = 0 + for i = 1, 32 do + local c = chnset[i] + if c < 16 then + nch = i + end + end + if nch == 0 then + return nil, Err.new(Err.UNSUPPORTED, "no PCM channels (AdLib only)") + end + local m = Tracker.newModule("s3m", nch) + m.title = title + m.speed = if speed > 0 and speed < 255 then speed else 6 + m.tempo = if tempo >= 33 then tempo else 125 + m.globalVolume = math.min(gv, 64) * 2 + m.mixVolume = math.max(mv % 128, 16) + if mv < 128 then + -- mono files play at 8 / 11 of the master volume (ST3's stereo boost is missing; OpenMPT does the same) + m.mixVolume = math.floor(m.mixVolume * 8 / 11 + 0.5) + end + m.linear = false + m.instrumentMode = false + m.flags.fastSlides = cwt == 0x1300 or bit32.band(flags, 64) ~= 0 + m.flags.amigaLimits = bit32.band(flags, 16) ~= 0 + m.meta.source = { format = "s3m", tracker = cwt, stereo = mv >= 128 } + -- orders, parapointers, panning + r:seek(96) + for i = 1, ordnum do + local o = r:u8() + table.insert(m.order, o) + end + local insPtr, patPtr = {}, {} + for i = 1, insnum do + insPtr[i] = r:u16le() * 16 + end + for i = 1, patnum do + patPtr[i] = r:u16le() * 16 + end + local pans: { number } = {} + if dp == 252 then + for i = 1, 32 do + pans[i] = r:u8() + end + end + if r.err then + return nil, r.err + end + -- drop trailing end markers; keep inner "+++" / "---" + while #m.order > 0 and m.order[#m.order] == 255 do + table.remove(m.order) + end + local stereo = mv >= 128 + for i = 1, nch do + local c = chnset[i] + local pan = 128 + if stereo then + pan = if c % 16 >= 8 then 0xCC else 0x33 + local p = pans[i] + if p and bit32.band(p, 0x20) ~= 0 then + pan = (p % 16) * 0x11 + end + end + m.channelPan[i] = pan + m.channelMute[i] = c >= 16 + end + -- samples + local total = 0 + local unsigned = ffi ~= 1 + for i = 1, insnum do + local s = Tracker.newSample() + m.samples[i] = s + local h = r:sub(insPtr[i], 80) + if h:size() < 80 then + return nil, Err.new(Err.TRUNCATED, "sample header " .. i, insPtr[i]) + end + local typ = h:u8() + h:skip(12) + local hi = h:u8() + local lo = h:u16le() + local len, ls, le = h:u32le(), h:u32le(), h:u32le() + local vol = h:u8() + h:skip(1) + local pack, sflags = h:u8(), h:u8() + local c2spd = h:u32le() + h:seek(48) + s.name = h:fixed(28) + s.volume = math.min(vol, 64) + s.c5speed = if c2spd > 0 then c2spd else 8363 + if typ == 1 and len > 0 and pack == 0 then + local bits = if bit32.band(sflags, 4) ~= 0 then 16 else 8 + local nchs = if bit32.band(sflags, 2) ~= 0 then 2 else 1 + local off = (hi * 65536 + lo) * 16 + len = math.min(len, 2 ^ 26) + -- a short file: keep what is there + local avail = math.max(0, r:size() - off) // (nchs * bits // 8) + len = math.min(len, avail) + total += len * nchs * 4 + if total > maxBytes then + return nil, Err.new(Err.LIMIT, "sample data over the maxBytes limit") + end + local data = Tracker.Pcm.decode(r.buf, r.base + off, len, { bits = bits, signed = not unsigned, channels = nchs, planar = true }) + if data then + s.data, s.length, s.bits, s.channels = data, len, bits, nchs + if bit32.band(sflags, 1) ~= 0 and le > ls then + s.loopStart, s.loopEnd, s.loopType = ls, math.min(le, len), 1 + end + end + end + end + -- patterns + for p = 1, patnum do + local pat = Tracker.newPattern(64, nch) + m.patterns[p] = pat + local ptr = patPtr[p] + if ptr > 0 then + local pr = r:sub(ptr) + local plen = pr:u16le() + pr = pr:sub(2, math.max(0, plen - 2)) + local d = pat.data + local row = 0 + while row < 64 and not pr:eof() do + local what = pr:u8() + if what == 0 then + row += 1 + else + local ch = what % 32 + local note, ins, vol, cmd, info = 0, 0, 0, 0, 0 + if bit32.band(what, 0x20) ~= 0 then + local nb = pr:u8() + ins = pr:u8() + if nb == 254 then + note = 254 + elseif nb < 254 and nb % 16 < 12 then + note = (nb // 16) * 12 + nb % 16 + 13 + if note > 120 then + note = 0 + end + end + end + if bit32.band(what, 0x40) ~= 0 then + vol = math.min(pr:u8(), 64) + 1 + end + if bit32.band(what, 0x80) ~= 0 then + cmd, info = pr:u8(), pr:u8() + if cmd > 26 then + cmd, info = 0, 0 + end + end + if ch < nch then + local o = (row * nch + ch) * 5 + buffer.writeu8(d, o, note) + buffer.writeu8(d, o + 1, ins) + buffer.writeu8(d, o + 2, vol) + buffer.writeu8(d, o + 3, cmd) + buffer.writeu8(d, o + 4, info) + end + end + end + end + end + 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 S3m.open(r: Bytes.Reader, opts: any?): (Types.Decoder?, Err.Err?) + local m, err = S3m.decode(r, opts) + if not m then + return nil, err + end + return Tracker.render(m, opts) +end + +return S3m diff --git a/tests/music/_similar.luau b/tests/music/_similar.luau index b234d86..e97f7eb 100644 --- a/tests/music/_similar.luau +++ b/tests/music/_similar.luau @@ -1,10 +1,10 @@ -- tests/music/_similar: shared helper of the tracker tests (not a test itself): renders a module through a Decoder and -- compares it with a reference render's features (tools/fixtures/_tracker.py: per 2048-frame block the RMS of left, --- right and mid, and 20 log-spaced band magnitudes of mid from a Hann-windowed FFT). Same definitions on both sides. +-- right and mid, and 40 log-spaced band magnitudes of mid from a Hann-windowed FFT). Same definitions on both sides. local S = {} -local SR, HOP, BANDS = 44100, 2048, 20 -local F_LO, F_HI = 100, 16000 +local SR, HOP, BANDS = 44100, 2048, 40 +local F_LO, F_HI = 150, 16000 S.SR = SR local WINDOW = table.create(HOP, 0) @@ -112,6 +112,7 @@ end -- compare(ours, ref) -> { gainDb, envCorr, spectral, balance } over the common frames: -- gainDb total mid energy ratio in dB (ours / ref) -- envCorr Pearson correlation of the per-block mid loudness in dB (blocks above -60 dB in either) +-- envErr mean absolute difference of the per-block mid loudness in dB, after removing gainDb (same blocks) -- spectral mean cosine similarity of the band magnitude vectors (blocks where ref is above -50 dB) -- balance mean absolute difference of the per-block left / right balance (L - R) / (L + R) (loud blocks) function S.compare(a: any, b: any): any @@ -144,6 +145,12 @@ function S.compare(a: any, b: any): any syy += (ys[i] - my) ^ 2 end local envCorr = if sxx > 0 and syy > 0 then sxy / math.sqrt(sxx * syy) else 1 + -- the mean loudness difference per block once the overall gain is taken out (dB, each side clamped at -60) + local envErr = 0 + for i = 1, #xs do + envErr += math.abs(math.max(xs[i], -60) - math.max(ys[i], -60) - gainDb) + end + envErr /= math.max(1, #xs) local cs, cn = 0, 0 local bal, bn = 0, 0 for k = 1, n do @@ -167,6 +174,7 @@ function S.compare(a: any, b: any): any return { gainDb = gainDb, envCorr = envCorr, + envErr = envErr, spectral = if cn > 0 then cs / cn else 1, balance = if bn > 0 then bal / bn else 0, frames = n, diff --git a/tests/music/mod.luau b/tests/music/mod.luau index f873981..30935ae 100644 --- a/tests/music/mod.luau +++ b/tests/music/mod.luau @@ -18,7 +18,7 @@ local function load(data: string): any return Mod.decode(Bytes.reader(buffer.fromstring(data))) end -local THRESH = { gainDb = 1.5, envCorr = 0.95, spectral = 0.93, balance = 0.05, duration = 0.01 } +local THRESH = { gainDb = 1.0, envErr = 1.0, spectral = 0.93, balance = 0.05, duration = 0.01 } for _, c in Cases do local m, err = load(c.data) @@ -29,14 +29,14 @@ for _, c in Cases do t:check(math.abs(dec.duration - c.duration) < THRESH.duration, string.format("%s: duration %.3f s, openmpt %.3f s", c.name, dec.duration, c.duration)) local buf, frames = S.renderAll(dec, math.floor(c.duration * S.SR)) local r = S.compare(S.features(buf, frames), c.ref) - t:note("%-6s gain %+.2f dB, envelope r %.3f, spectrum %.3f, balance %.3f, %.2f s", c.name, r.gainDb, r.envCorr, r.spectral, r.balance, dec.duration) - if r.spectral < THRESH.spectral or r.envCorr < THRESH.envCorr then + t:note("%-7s gain %+.2f dB, loudness error %.2f dB, spectrum %.3f, balance %.3f, %.2f s", c.name, r.gainDb, r.envErr, r.spectral, r.balance, dec.duration) + if r.spectral < THRESH.spectral or r.envErr > THRESH.envErr then for _, w in S.worst(S.features(buf, frames), c.ref, 6) do t:note(" block %d (%.2f s): spectrum %.3f, ours %.1f dB, ref %.1f dB", w[1], (w[1] - 1) * 2048 / S.SR, w[2], w[3], w[4]) end end t:check(math.abs(r.gainDb) < THRESH.gainDb, c.name .. ": loudness") - t:check(r.envCorr > THRESH.envCorr, c.name .. ": loudness envelope") + t:check(r.envErr < THRESH.envErr, c.name .. ": loudness envelope") t:check(r.spectral > THRESH.spectral, c.name .. ": spectrum") t:check(r.balance < THRESH.balance, c.name .. ": stereo balance") -- the writer: MOD -> MOD gives the same bytes back (the fixtures are canonical ProTracker files) diff --git a/tests/music/s3m.luau b/tests/music/s3m.luau new file mode 100644 index 0000000..989ca30 --- /dev/null +++ b/tests/music/s3m.luau @@ -0,0 +1,71 @@ +-- music/s3m tests: generated Scream Tracker 3 modules (tools/fixtures/s3m.py) parsed, rendered through codecs/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 S3m = require("@formats/music/s3m") +local Tracker = require("@formats/codecs/Tracker") +local Bytes = require("@formats/core/Bytes") +local Cases = require("@tests/gen/s3m_cases") + +local t = T.suite("s3m") + +t:eq(S3m.name, "s3m", "name") +t:eq(S3m.category, "music", "category") +t:check(S3m.caps.decode and not S3m.caps.encode, "caps") + +local function load(data: string): any + return S3m.decode(Bytes.reader(buffer.fromstring(data))) +end + +local THRESH = { gainDb = 1.0, envErr = 1.0, spectral = 0.93, balance = 0.05, duration = 0.01 } + +for _, c in Cases do + local m, err = load(c.data) + t:check(m, c.name .. ": " .. tostring(err)) + t:check(S3m.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, c.name .. ": sniff") + local dec = m:render({ sampleRate = S.SR }) + t:check(dec, c.name .. ": render") + t:check(math.abs(dec.duration - c.duration) < THRESH.duration, string.format("%s: duration %.3f s, openmpt %.3f s", c.name, dec.duration, c.duration)) + local buf, frames = S.renderAll(dec, math.floor(c.duration * S.SR)) + local r = S.compare(S.features(buf, frames), c.ref) + t:note("%-7s gain %+.2f dB, loudness error %.2f dB, spectrum %.3f, balance %.3f, %.2f s", c.name, r.gainDb, r.envErr, r.spectral, r.balance, dec.duration) + if r.spectral < THRESH.spectral or r.envErr > THRESH.envErr then + for _, w in S.worst(S.features(buf, frames), c.ref, 6) do + t:note(" block %d (%.2f s): spectrum %.3f, ours %.1f dB, ref %.1f dB", w[1], (w[1] - 1) * 2048 / S.SR, w[2], w[3], w[4]) + end + end + t:check(math.abs(r.gainDb) < THRESH.gainDb, c.name .. ": loudness") + t:check(r.envErr < THRESH.envErr, c.name .. ": loudness envelope") + t:check(r.spectral > THRESH.spectral, c.name .. ": spectrum") + t:check(r.balance < THRESH.balance, c.name .. ": stereo balance") +end + +-- seek: the position after a seek and the output continuing +do + local m = load(Cases[1].data) + local dec = m:render() + dec:seek(2) + t:check(math.abs(dec:position() - 2) < 0.001, "seek position") + local _, n = dec:read(1000) + t:eq(n, 1000, "read after seek") + t:check(Tracker.duration(m) == dec.duration, "duration()") +end + +-- the fuzz pass: decode, and a short render of whatever decodes +local samples = {} +for _, c in Cases do + table.insert(samples, c.data) +end +t:fuzz("decode", function(b: buffer) + local m = S3m.decode(Bytes.reader(b)) + if m then + local dec = Tracker.render(m, { maxDuration = 0.5, sampleRate = 8000 }) + if dec then + dec:read(4000) + end + end + return m +end, samples, { mutations = 30 }) + +t:done() diff --git a/tests/music/zz_probe.luau b/tests/music/zz_probe.luau new file mode 100644 index 0000000..53cd319 --- /dev/null +++ b/tests/music/zz_probe.luau @@ -0,0 +1,27 @@ +-- debug probes +local S = require("@tests/music/_similar") +local Bytes = require("@formats/core/Bytes") +local Cases = require("@tests/gen/probe_cases") +local verbose = ... +local F = { s3m = require("@formats/music/s3m"), mod = require("@formats/music/mod") } +for _, c in Cases do + local m, err = F[c.fmt].decode(Bytes.reader(buffer.fromstring(c.data))) + if not m then print(c.name, err) continue end + local dec = m:render() + local buf, frames = S.renderAll(dec, math.floor(c.duration * S.SR)) + local a = S.features(buf, frames) + local r = S.compare(a, c.ref) + print(string.format("%-8s gain %+.2f err %.2f env %.3f spec %.3f bal %.3f dur %.3f/%.3f", c.name, r.gainDb, r.envErr, r.envCorr, r.spectral, r.balance, dec.duration, c.duration)) + if verbose == c.name then + for k = 1, math.min(a.frames, c.ref.frames) do + local x, y = 20 * math.log10(a.rmsM[k] + 1e-6), 20 * math.log10(c.ref.rmsM[k] + 1e-6) + local sim = 0 + local dot, na, nb = 0, 0, 0 + for j = 1, 40 do + local p, q = a.bands[(k - 1) * 40 + j], c.ref.bands[(k - 1) * 40 + j] + dot += p * q; na += p * p; nb += q * q + end + print(string.format(" block %3d %5.2fs M %6.1f %6.1f spec %.3f", k, (k - 1) * 2048 / 44100, x, y, dot / math.sqrt(na * nb + 1e-20))) + end + end +end diff --git a/tests/music/zz_voices.luau b/tests/music/zz_voices.luau new file mode 100644 index 0000000..6a16e5b --- /dev/null +++ b/tests/music/zz_voices.luau @@ -0,0 +1,24 @@ +-- debug: voice states at a time +local Bytes = require("@formats/core/Bytes") +local Cases = require("@tests/gen/probe_cases") +local name, at = ... +local F = { s3m = require("@formats/music/s3m"), mod = require("@formats/music/mod") } +for _, c in Cases do + if c.name == name then + local m = F[c.fmt].decode(Bytes.reader(buffer.fromstring(c.data))) + local dec = m:render() + local n = math.floor(tonumber(at) * 44100) + while n > 0 do + local _, got = dec:read(math.min(n, 4096)) + n -= got + if got == 0 then break end + end + local st = dec.st + print("ord", st.ord, "row", st.row, "tick", st.tick, "speed", st.speed, "tempo", st.tempo, "gvol", st.gvol) + for i, v in st.voices do + if v.chan > 0 then print("voice", v.active, i, "chan", v.chan, "smp", v.smpIdx, "p", v.p, "inc", v.inc, "vol", v.vol, "g", v.gl, v.gr, "t", v.tl, v.tr, "len", v.len, v.loopS, v.loopE, v.loopT) end + end + local out = dec:read(8) + print(buffer.readf32(out, 0), buffer.readf32(out, 4), buffer.readf32(out, 8)) + end +end diff --git a/tools/fixtures/_tracker.py b/tools/fixtures/_tracker.py index d1c5609..482955d 100644 --- a/tools/fixtures/_tracker.py +++ b/tools/fixtures/_tracker.py @@ -1,14 +1,14 @@ # Tracker fixture helpers (used by tools/fixtures/{mod,s3m,xm,it}.py): small writers for MOD, S3M, XM and IT # (including IT 2.14 / 2.15 sample compression), synthesized samples, reference renders through openmpt123 (nix -# libopenmpt) and the comparison features (per 2048-frame block: RMS of left / right / mid and 20 log-spaced band +# libopenmpt) and the comparison features (per 2048-frame block: RMS of left / right / mid and 40 log-spaced band # magnitudes of the mid signal), computed the same way in tests/music/_similar.luau. Every module is generated here # (no third-party files), so the fixtures carry no licence. import json, math, os, struct, subprocess, tempfile SR = 44100 HOP = 2048 -BANDS = 20 -F_LO, F_HI = 100.0, 16000.0 +BANDS = 40 +F_LO, F_HI = 150.0, 16000.0 # ---------------------------------------------------------------------------------------------- samples diff --git a/tools/fixtures/s3m.py b/tools/fixtures/s3m.py new file mode 100644 index 0000000..17ea196 --- /dev/null +++ b/tools/fixtures/s3m.py @@ -0,0 +1,124 @@ +# S3M fixtures (tools/fixtures.py): generated Scream Tracker 3 modules exercising the effects, rendered by openmpt123. +# tests/gen/s3m_cases.luau { name, data (the module), ref (features of openmpt's render, tools/fixtures/_tracker.py), +# duration (openmpt's song length) } +import os, sys + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import _tracker as T # noqa: E402 + +INPUTS = ["tools/fixtures/_tracker.py"] + +A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, Tt, U, V, W, X, Y, Z = range(1, 27) + + +def empty(ch, rows=64): + return [[(0, 0, None, 0, 0) for _ in range(ch)] for _ in range(rows)] + + +def put(p, row, ch, note=0, ins=0, vol=None, cmd=0, info=0): + p[row][ch] = (T.s3m_note(note) if 0 < note < 254 else note, ins, vol, cmd, info) + + +def samples(): + return [ + {"name": "sine", "data": T.u8(T.synth("sine", 64, 64)), "volume": 64, "c2spd": 8363, "loop": (0, 64)}, + {"name": "square", "data": T.u8(T.synth("square", 64, 64, 0.5)), "volume": 40, "c2spd": 16726, "loop": (0, 64)}, + {"name": "saw", "data": T.u8(T.synth("saw", 100, 100, 0.6)), "volume": 64, "c2spd": 13000, "loop": (0, 100)}, + {"name": "noise", "data": T.u8(T.synth("noise", 6000, 1, 0.7, decay=1500)), "volume": 60, "c2spd": 8363}, + {"name": "tri", "data": T.u8(T.synth("tri", 50, 50, 0.8)), "volume": 50, "c2spd": 10000, "loop": (0, 50)}, + ] + + +def song_effects(keep=None): + """keep: debugging aid, a set of channels to keep (the others keep only their global effects)""" + a, b, c = empty(4), empty(4), empty(4) + mel = [61, 65, 68, 73, 68, 65, 63, 61] + for i in range(16): + put(a, i * 4, 0, mel[i % 8], 1, None, J if i % 4 == 1 else 0, 0x47 if i % 4 == 1 else 0) + put(a, 0, 1, 49, 2, 48) + put(a, 4, 1, 0, 0, None, D, 0x04) + put(a, 8, 1, 0, 0, None, D, 0x20) + put(a, 12, 1, 0, 0, None, D, 0xF2) + put(a, 13, 1, 0, 0, None, D, 0x3F) + put(a, 14, 1, 0, 0, None, D, 0x00) + put(a, 16, 1, 51, 2, None, E, 0x03) + put(a, 20, 1, 0, 0, None, F, 0x06) + put(a, 24, 1, 0, 0, None, E, 0xF4) + put(a, 25, 1, 0, 0, None, F, 0xE8) + put(a, 28, 1, 56, 2, None, H, 0x46) + put(a, 32, 1, 0, 0, None, U, 0x48) + put(a, 36, 1, 0, 0, None, K, 0x02) + put(a, 40, 1, 49, 2, None, G, 0x00) + put(a, 41, 1, 61, 0, None, G, 0x10) + put(a, 44, 1, 0, 0, None, L, 0x01) + put(a, 48, 1, 54, 2, None, R, 0x58) + put(a, 52, 1, 0, 0, None, I, 0x21) + put(a, 56, 1, 0, 0, None, X, 0x20) + put(a, 60, 1, 0, 0, None, S, 0x8C) + for r in range(0, 64, 8): + put(a, r, 3, 61, 4, 64 - r // 2) + put(a, 20, 3, 0, 0, None, O, 0x08) + put(a, 24, 3, 61, 4, None, Q, 0x83) + put(a, 32, 3, 61, 4, None, S, 0xC3) + put(a, 40, 3, 61, 4, None, S, 0xD2) + put(a, 48, 2, 37, 3, None, V, 0x30) + put(a, 56, 2, 0, 0, None, V, 0x40) + put(b, 0, 2, 49, 3, None, A, 0x04) + put(b, 4, 2, 0, 0, None, Tt, 0x90) + put(b, 8, 2, 52, 5, None, S, 0xB0) + put(b, 10, 0, 64, 1) + put(b, 12, 2, 0, 0, None, S, 0xB2) + put(b, 16, 2, 56, 5, None, S, 0xE2) + put(b, 20, 0, 61, 1, None, O, 0x00) + put(b, 24, 0, 254) + put(b, 28, 2, 0, 0, None, C, 0x08) + put(c, 8, 0, 49, 1, 32) + put(c, 12, 1, 61, 2, None, X, 0x80) + put(c, 16, 1, 0, 0, None, S, 0x82) + put(c, 20, 0, 0, 0, None, B, 0x05) + if keep is not None: # debugging: only some channels + for p in (a, b, c): + for row in p: + for ch in range(4): + if ch not in keep and row[ch][3] not in (A, B, C, Tt, V): + row[ch] = (0, 0, None, 0, 0) + return T.write_s3m("effects", samples(), [0, 1, 254, 2], [a, b, c], 4) + + +def song_memory(): + """ST3's shared effect memory, fast slides (ST3.00), the panning table, global volume, an order loop""" + a = empty(6) + put(a, 0, 0, 61, 1, None, D, 0x08) + put(a, 4, 0, 0, 0, None, E, 0x02) + put(a, 8, 0, 0, 0, None, D, 0x00) + put(a, 12, 0, 0, 0, None, F, 0x00) + put(a, 0, 1, 49, 3, None, D, 0x80) + put(a, 2, 1, 0, 0, None, D, 0x00) + put(a, 16, 2, 73, 5) + put(a, 20, 2, 0, 0, None, Q, 0x02) + put(a, 24, 2, 0, 0, None, Q, 0x00) + put(a, 28, 3, 56, 2, 20) + put(a, 28, 4, 63, 1, 30) + put(a, 32, 5, 37, 3, None, X, 0x00) + put(a, 40, 5, 0, 0, None, S, 0xE3) + put(a, 48, 0, 0, 0, None, V, 0x20) + put(a, 63, 0, 0, 0, None, B, 0x00) + return T.write_s3m("memory", samples(), [0], [a], 6, speed=5, tempo=140, cwt=0x1300, + pans=[2, 13, 5, 10, 7, 15], mvol=64 | 128, gvol=48) + + +def song_mono(): + a = empty(2) + put(a, 0, 0, 61, 1) + put(a, 8, 1, 68, 2) + put(a, 16, 0, 0, 0, None, H, 0x28) + put(a, 32, 1, 0, 0, None, F, 0x10) + return T.write_s3m("mono", samples(), [0], [a], 2, mvol=32) + + +def generate(ctx): + cases = [] + for nm, data in (("effects", song_effects()), ("memory", song_memory()), ("mono", song_mono())): + ref = T.reference(ctx, data, "s3m") + cases.append({"name": nm, "data": data, "ref": ref, "duration": ref["duration"]}) + ctx.emit("s3m_cases", cases) diff --git a/tools/fixtures/zz_probe.py b/tools/fixtures/zz_probe.py new file mode 100644 index 0000000..3598c71 --- /dev/null +++ b/tools/fixtures/zz_probe.py @@ -0,0 +1,62 @@ +# debug only (not committed): one small module per effect, for locating engine differences against openmpt +import os, sys +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +import _tracker as T # noqa +import s3m as S # noqa + +def probes_s3m(): + out = [] + smp = S.samples() + def one(nm, events, ch=1, **kw): + p = S.empty(ch, 64) + for e in events: + S.put(p, *e) + out.append((nm, T.write_s3m(nm, smp, [0], [p], ch, **kw), "s3m")) + one("Q83", [(0, 0, 61, 4, None, S.Q, 0x83), (1, 0, 0, 0, None, S.Q, 0x83), (2, 0, 61, 4, None, S.Q, 0x83)]) + one("Q32", [(0, 0, 61, 4, None, S.Q, 0x32), (1, 0, 0, 0, None, S.Q, 0x00)]) + one("EF4", [(0, 0, 61, 2), (2, 0, 0, 0, None, S.E, 0xF4), (3, 0, 0, 0, None, S.F, 0xE8)]) + one("E03", [(0, 0, 61, 2), (2, 0, 0, 0, None, S.E, 0x03), (4, 0, 0, 0, None, S.F, 0x06)]) + one("D", [(0, 0, 61, 2, 48), (1, 0, 0, 0, None, S.D, 0x04), (3, 0, 0, 0, None, S.D, 0x20), (5, 0, 0, 0, None, S.D, 0xF2), (6, 0, 0, 0, None, S.D, 0x3F), (7, 0, 0, 0, None, S.D, 0x00)]) + one("H46", [(0, 0, 61, 1, None, S.H, 0x46), (1, 0, 0, 0, None, S.H, 0x00)]) + one("U48", [(0, 0, 61, 1, None, S.U, 0x48), (1, 0, 0, 0, None, S.U, 0x00)]) + one("K02", [(0, 0, 61, 1, None, S.H, 0x46), (1, 0, 0, 0, None, S.K, 0x02)]) + one("G", [(0, 0, 49, 1), (1, 0, 61, 0, None, S.G, 0x10), (3, 0, 49, 0, None, S.G, 0x00)]) + one("L01", [(0, 0, 49, 1), (1, 0, 61, 0, None, S.G, 0x08), (3, 0, 0, 0, None, S.L, 0x01)]) + one("R58", [(0, 0, 54, 1, None, S.R, 0x58), (1, 0, 0, 0, None, S.R, 0x00)]) + one("I21", [(0, 0, 54, 1, None, S.I, 0x21), (1, 0, 0, 0, None, S.I, 0x00)]) + one("J47", [(0, 0, 54, 1, None, S.J, 0x47), (1, 0, 0, 0, None, S.J, 0x00)]) + one("X", [(0, 0, 54, 1, None, S.X, 0x20), (4, 0, 0, 0, None, S.S, 0x8C), (8, 0, 0, 0, None, S.X, 0x80)]) + one("O", [(0, 0, 61, 4, None, S.O, 0x08), (4, 0, 61, 4, None, S.O, 0x00), (8, 0, 61, 4, None, S.O, 0x10)]) + one("SC", [(0, 0, 61, 4, None, S.S, 0xC3), (4, 0, 61, 1, None, S.S, 0xD2), (8, 0, 254)]) + one("V", [(0, 0, 61, 1, None, S.V, 0x30), (4, 0, 0, 0, None, S.V, 0x10)]) + one("T", [(0, 0, 61, 1, None, S.Tt, 0x90), (4, 0, 0, 0, None, S.A, 0x03)]) + one("SD2", [(0, 0, 61, 4), (8, 0, 61, 4, None, S.S, 0xD2), (16, 0, 61, 4, None, S.S, 0xD7)]) + one("G00mem", [(0, 0, 56, 2, None, S.H, 0x46), (4, 0, 0, 0, None, S.K, 0x02), (8, 0, 49, 2, None, S.G, 0x00), (9, 0, 61, 0, None, S.G, 0x10)]) + one("Qnote", [(0, 0, 61, 4), (4, 0, 61, 4, None, S.Q, 0x83), (5, 0, 0, 0, None, S.Q, 0x00), (8, 0, 61, 4, 64)]) + one("Gnew", [(0, 0, 49, 2, None, S.G, 0x04), (4, 0, 61, 0, None, S.G, 0x00)]) + one("seqA", [(24, 0, 61, 4, None, S.Q, 0x83), (32, 0, 61, 4, None, S.S, 0xC3), (48, 0, 61, 4, 40)]) + one("seqB", [(32, 0, 61, 4, None, S.S, 0xC3), (48, 0, 61, 4, 40)]) + one("seqC", [(24, 0, 61, 4, None, S.Q, 0x83), (48, 0, 61, 4, 40)]) + one("seqD", [(40, 0, 61, 4, None, S.S, 0xD2), (48, 0, 61, 4, 40)]) + base = [(r, 0, 61, 4, 64 - r // 2) for r in range(0, 64, 8)] + one("b1", base) + one("b2", base + [(20, 0, 0, 0, None, S.O, 0x08)]) + ev = base + [(20, 0, 0, 0, None, S.O, 0x08), (24, 0, 61, 4, None, S.Q, 0x83), (32, 0, 61, 4, None, S.S, 0xC3), (40, 0, 61, 4, None, S.S, 0xD2)] + one("b3", ev) + one("b4", base + [(24, 0, 61, 4, None, S.Q, 0x83), (32, 0, 61, 4, None, S.S, 0xC3), (40, 0, 61, 4, None, S.S, 0xD2)]) + one("v1", base + [(48, 1, 0, 0, None, S.V, 0x30), (56, 1, 0, 0, None, S.V, 0x40)], ch=2) + one("v2", [(e[0], 1) + e[2:] for e in base] + [(48, 0, 0, 0, None, S.V, 0x30), (56, 0, 0, 0, None, S.V, 0x40)], ch=2) + ev4 = [(e[0], 3) + e[2:] for e in base + [(24, 0, 61, 4, None, S.Q, 0x83), (32, 0, 61, 4, None, S.S, 0xC3), (40, 0, 61, 4, None, S.S, 0xD2)]] + one("c1", ev4, ch=4) + one("c2", ev4 + [(48, 2, 0, 0, None, S.V, 0x30)], ch=4) + for keep in ((3,),): + out.append(("eff" + "".join(map(str, keep)), S.song_effects(set(keep)), "s3m")) + return out + +def generate(ctx): + which = os.environ.get("PROBE", "s3m") + cases = [] + for nm, data, ext in {"s3m": probes_s3m}[which](): + ref = T.reference(ctx, data, ext) + cases.append({"name": nm, "data": data, "ref": ref, "duration": ref["duration"], "fmt": ext}) + ctx.emit("probe_cases", cases)