ws-music: music/s3m read + render (OpenMPT-matched master volume / mono rule / default panning), finer comparison bands, voice ownership fix

This commit is contained in:
4DBug committed 2026-10-31 09:00:04 -05:00
1 parent ac3e143ce8
commit cf7c1b8ad2
12 files changed
+598 -15

No files matched your search

+11 -3
View File
@@ -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,
+4 -4
View File
@@ -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)
+71
View File
@@ -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()
+27
View File
@@ -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
+24
View File
@@ -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