Files
rosetta/tests/music/mod.luau
T
4DBugandClaude Opus 5.5 434a304315 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:25:03 -05:00

128 lines
5.3 KiB
Luau

-- music/mod tests: generated ProTracker modules (tools/fixtures/mod.py) parsed, rendered through codecs/audio/Tracker and
-- compared with openmpt123's render (loudness envelope, band spectrum, stereo balance, song length), the MOD writer's
-- round trip, the contract, and the fuzz no-throw pass.
local T = require("@tests/T")
local S = require("@tests/music/_similar")
local Mod = require("@formats/music/mod")
local Tracker = require("@formats/codecs/audio/Tracker")
local Bytes = require("@formats/core/Bytes")
local Cases = require("@tests/gen/mod_cases")
local t = T.suite("mod")
t:eq(Mod.name, "mod", "name")
t:eq(Mod.category, "music", "category")
t:check(Mod.caps.decode and Mod.caps.encode, "caps")
local function load(data: string): any
return Mod.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(Mod.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")
-- the writer: MOD -> MOD gives the same bytes back (the fixtures are canonical ProTracker files)
local out, werr = Mod.encode(m)
t:check(out, c.name .. ": encode " .. tostring(werr))
local m2 = Mod.decode(Bytes.reader(out :: buffer))
t:check(m2, c.name .. ": re-decode")
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)
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()")
-- the same samples as playing up to that point (after the short fade-in that follows a seek)
local a, b = m:render(), m:render()
a:seek(3.5)
local at = math.floor(3.5 * S.SR)
while at > 0 do
local _, n = b:read(math.min(at, 8192))
at -= n
end
local x, y = a:read(4096), b:read(4096)
local worst = 0
for i = 256, 4095 * 2 do
worst = math.max(worst, math.abs(buffer.readf32(x, i * 4) - buffer.readf32(y, i * 4)))
end
t:check(worst < 1e-4, "seek then read matches reading through (" .. worst .. ")")
-- loop = true keeps playing past the end (frames / duration still give one pass)
local l = m:render({ loop = true, sampleRate = 8000 })
local total = 0
for _ = 1, 30 do
local _, n = l:read(8000)
total += n
end
t:check(total == 240000 and math.abs(l.duration - dec.duration) < 0.01, "loop plays past the end")
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 = Mod.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()