Files
rosetta/tests/codecs/musepack.luau
T

59 lines
2.2 KiB
Luau

-- codecs/audio/Musepack tests: the codec API under audio/mpc: magic, the stream fields of SV7 / SV8 fixtures (version,
-- rate, channels, band count, packet layout, gapless length), packets decoded by hand against libmpcdec (the
-- synthesis delay at the front), out-of-order decodes, and a fuzz pass over open + every packet. Fixtures:
-- tools/fixtures/mpc.py.
local T = require("@tests/T")
local Musepack = require("@rosetta/codecs/audio/Musepack")
local Fx = require("@tests/gen/mpc_cases")
local t = T.suite("codecs/musepack")
for _, c in Fx do
local b = buffer.fromstring(c.data)
t:eq(Musepack.magic(b, 0, buffer.len(b)), c.version, c.name .. ": magic")
local s, err = Musepack.open(b, 0, buffer.len(b))
t:check(s, c.name .. ": open: " .. tostring(err))
if not s then
continue
end
t:check(s.version == c.version and s.sampleRate == c.rate and s.channels == c.channels, c.name .. ": fields")
t:eq(s.frames, c.frames, c.name .. ": gapless length")
t:check(s.maxBand >= 1 and s.maxBand <= 31 and s.count >= 1 and s.skip >= 481, c.name .. ": bands / packets")
t:eq(s.stop - s.skip, c.frames, c.name .. ": stop - skip = length")
-- the first packet by hand: output frame k is raw frame skip + k
local n = s.decode(1)
t:check(n and n > 0 and n % 1152 == 0 and n <= s.blockFrames * 1152, c.name .. ": first packet length")
local pcm = s.pcm()
local rb = buffer.fromstring(c.ref)
local ch = c.channels
local maxd = 0
for k = 0, math.min(500, (n :: number) - s.skip - 1) do
for j = 1, ch do
maxd = math.max(maxd, math.abs(pcm[(s.skip + k) * ch + j] - buffer.readf32(rb, (k * ch + j - 1) * 4)))
end
end
t:check(maxd < 1e-5, string.format("%s: first packet matches libmpcdec (max diff %g)", c.name, maxd))
if s.count >= 3 then
s.reset()
t:check(s.decode(3) ~= nil and s.decode(1) ~= nil, c.name .. ": decodes out of order after reset")
end
end
local samples = {}
for _, c in Fx do
table.insert(samples, string.sub(c.data, 1, 2500))
end
t:fuzz("open + every packet", function(b: buffer)
local s = Musepack.open(b, 0, buffer.len(b))
if s then
for i = 1, math.min(s.count, 40) do
if not s.decode(i) then
break
end
end
end
return s
end, samples, { mutations = 25, keepHeader = 4 })
t:done()