Files
rosetta/tests/music/mus.luau
T

91 lines
3.4 KiB
Luau

-- music/mus tests: Doom MUS files against wildmidi -x's conversion (times at wildmidi's tick rate).
-- Fixtures: tools/fixtures/mus.py.
local T = require("@tests/T")
local V = require("@tests/music/_midivar")
local Mus = require("@rosetta/music/mus")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Render = require("@rosetta/codecs/audio/MidiRender")
local Cases = require("@tests/gen/mus_cases")
local Measures = require("@tests/gen/mus_measures")
local t = T.suite("mus")
local function decode(s: string, opts: any?): (any, any)
return Mus.decode(Bytes.reader(buffer.fromstring(s)), opts)
end
t:eq(Mus.name, "mus", "name")
t:check(Mus.caps.decode and not Mus.caps.encode, "caps")
t:eq(Mus.sniff(Bytes.reader(buffer.fromstring("MUS\x1a"))), 1, "sniff")
t:eq(Mus.sniff(Bytes.reader(buffer.fromstring("MThd"))), 0, "sniff MThd")
local samples = {}
for _, c in Cases do
local m, err = decode(c.data, { rate = c.rate })
t:check(m, c.name .. ": " .. tostring(err))
if m then
t:eq(m.kind, "midi", c.name .. ": kind")
t:eq(m.meta.source.format, "mus", c.name .. ": source")
t:eq(#m.meta.source.instruments, 3, c.name .. ": instrument list")
V.compare(t, m, c.ref, c.name)
-- Doom's 140 Hz: the same events, times scaled by rate / 140
local d = assert(decode(c.data))
t:check(math.abs(d.meta.length * 140 - m.meta.length * c.rate) < 1e-5 * d.meta.length * 140 + 1e-6, c.name .. ": 140 Hz default")
end
table.insert(samples, buffer.fromstring(c.data))
end
-- system events and measure ends inserted with no delay: the same notes at the same times, more controllers
do
local p, q = assert(decode(Measures.plain)), assert(decode(Measures.marked))
local a, b = Render.timeline(p), Render.timeline(q)
local function notes(tl: any): { string }
local out = {}
for i = 1, #tl.time do
if tl.status[i] // 16 == 8 or tl.status[i] // 16 == 9 then
table.insert(out, string.format("%.6f %d %d %d", tl.time[i], tl.status[i], tl.d1[i], tl.d2[i]))
end
end
return out
end
local na, nb = notes(a), notes(b)
t:eq(#nb, #na, "measures: note count")
t:eq(table.concat(nb, ","), table.concat(na, ","), "measures: same notes")
t:check(#b.time > #a.time, "measures: system events become controllers")
t:eq(q.meta.length, p.meta.length, "measures: same length")
end
do
local m = assert(decode(Cases[1].data))
local dec = m:render({ sampleRate = 22050, tail = 0.2 })
t:check(dec.frames > 0 and math.abs(dec.duration - (m.meta.length + 0.2)) < 0.01, "render duration")
local buf, n = dec:read(dec.frames)
local peak = 0
for i = 0, n * 2 - 1 do
peak = math.max(peak, math.abs(buffer.readf32(buf, i * 4)))
end
t:check(peak > 0.01, "render makes sound")
end
local _, e1 = decode("MUS\x1a\1\0")
t:eq(e1 and e1.code, Err.TRUNCATED, "short header")
local _, e2 = decode("MUS\x1a\2\0\200\0\1\0\0\0\0\0\0\0")
t:eq(e2 and e2.code, Err.TRUNCATED, "score past the end")
local _, e3 = decode("MUS\x1a\2\0\16\0\1\0\0\0\0\0\0\0\112\0")
t:eq(e3 and e3.code, Err.CORRUPT, "event type 7")
local _, e4 = decode(Cases[1].data, { rate = 0 })
t:eq(e4 and e4.code, Err.ARG, "bad rate")
t:fuzz("decode", function(b: buffer)
return Mus.decode(Bytes.reader(b))
end, samples)
t:fuzz("decode + render", function(b: buffer)
local m = Mus.decode(Bytes.reader(b))
if m and m.meta.length < 30 then
m:render({ sampleRate = 8000 }):read(2000)
end
return m
end, samples, { mutations = 60, garbage = 10 })
t:done()