ws-music: music/it read + render (IT 2.14/2.15 decompression, NNA, filters, pitch-pan, OpenMPT note-fade coding), tracker tests tightened, MOD->XM + API checks, tracker bench lines, NOTICE

This commit is contained in:
4DBug committed 2026-10-31 09:31:34 -05:00
1 parent 8b8b64885f
commit 7754646a86
14 files changed
+964 -28

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
-- music/it tests: generated Impulse Tracker modules (tools/fixtures/it.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 It = require("@formats/music/it")
local Tracker = require("@formats/codecs/Tracker")
local Bytes = require("@formats/core/Bytes")
local Cases = require("@tests/gen/it_cases")
local t = T.suite("it")
t:eq(It.name, "it", "name")
t:eq(It.category, "music", "category")
t:check(It.caps.decode and not It.caps.encode, "caps")
local function load(data: string): any
return It.decode(Bytes.reader(buffer.fromstring(data)))
end
local THRESH = { gainDb = 0.5, envErr = 1.0, spectral = 0.95, balance = 0.04, duration = 0.01 }
for _, c in Cases do
local m, err = load(c.data)
t:check(m, c.name .. ": " .. tostring(err))
t:check(It.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
-- compressed samples (IT 2.14 / 2.15, 8 and 16-bit, mono and stereo) decode to exactly the uncompressed data
for _, c in Cases do
if c.plain then
local a, b = load(c.data), load(c.plain)
local same = #a.samples == #b.samples
for i, s in a.samples do
local o = b.samples[i]
same = same and s.length == o.length and s.channels == o.channels and buffer.tostring(s.data) == buffer.tostring(o.data)
end
t:check(same, c.name .. ": samples equal the uncompressed ones")
end
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 = It.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()
+29 -1
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.0, envErr = 1.0, spectral = 0.93, balance = 0.05, duration = 0.01 }
local THRESH = { gainDb = 0.5, envErr = 1.0, spectral = 0.95, balance = 0.04, duration = 0.01 }
for _, c in Cases do
local m, err = load(c.data)
@@ -47,6 +47,34 @@ for _, c in Cases do
t:eq(buffer.tostring(out :: buffer), c.data, c.name .. ": round trip bytes")
end
-- MOD -> XM: the converted module plays as long, and sounds alike (XM has no channel panning and uses FT2's NTSC
-- Amiga clock, so only the duration and the band spectrum are compared)
local Xm = require("@formats/music/xm")
for _, c in Cases do
local m = load(c.data)
local xb, xerr = Xm.encode(m)
t:check(xb, c.name .. ": mod -> xm " .. tostring(xerr))
local x = Xm.decode(Bytes.reader(xb :: buffer))
t:check(x and x.format == "xm" and #x.instruments == #m.samples, c.name .. ": the xm decodes")
local dec = x:render()
t:check(math.abs(dec.duration - c.duration) < THRESH.duration, c.name .. ": xm duration")
local buf, frames = S.renderAll(dec, math.floor(c.duration * S.SR))
local r = S.compare(S.features(buf, frames), c.ref)
t:check(r.spectral > 0.9, string.format("%s: xm spectrum %.3f", c.name, r.spectral))
end
-- through the API: load sniffs the MOD, open gives a Decoder, save converts to XM, load(as audio) renders
do
local F = require("@formats")
local m, err = F.load(Cases[1].data)
t:check(m and m.kind == "music" and m.format == "mod", "F.load: " .. tostring(err))
local dec = F.open(Cases[1].data)
t:check(dec and dec.channels == 2 and dec.sampleRate == 44100, "F.open: a Decoder")
local xb = F.save(m, nil, { format = "xm" })
t:check(xb and F.load(xb).format == "xm", "F.save as xm")
t:eq(F.saveString(m, { format = "mod" }), Cases[1].data, "F.saveString keeps the MOD bytes")
end
-- seek: the position after a seek and the output continuing
do
local m = load(Cases[1].data)
+1 -1
View File
@@ -18,7 +18,7 @@ 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 }
local THRESH = { gainDb = 0.5, envErr = 1.0, spectral = 0.95, balance = 0.04, duration = 0.01 }
for _, c in Cases do
local m, err = load(c.data)
+1 -1
View File
@@ -18,7 +18,7 @@ local function load(data: string): any
return Xm.decode(Bytes.reader(buffer.fromstring(data)))
end
local THRESH = { gainDb = 1.0, envErr = 1.0, spectral = 0.93, balance = 0.05, duration = 0.01 }
local THRESH = { gainDb = 0.5, envErr = 1.0, spectral = 0.95, balance = 0.04, duration = 0.01 }
for _, c in Cases do
local m, err = load(c.data)