Files

131 lines
4.6 KiB
Luau

-- Shared checks of the lossless audio format tests (wavpack, ape, tta): bit-exact against the reference decoder.
-- partial: a cut file whose decode stops with an error; the reference decoded its first `frames` frames.
local T = require("@tests/T")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local L = {}
local f32buf = buffer.create(4)
local function f32(x: number): number
buffer.writef32(f32buf, 0, x)
return buffer.readf32(f32buf, 0)
end
local function open(mod: any, b: any, opts: any?): (any, any)
return mod.open(Bytes.reader(if type(b) == "string" then buffer.fromstring(b) else b), opts)
end
L.open = open
function L.all(dec: any, fx: any): (number, number, { number })
local ch = dec.channels
local vals = table.create(fx.frames * ch, 0)
local crc, n, k = 0, 0, 0
local sizes = { 4096, 1, 777, 10000, 3 }
while true do
k += 1
local out, got
if fx.float then
out, got = dec:read(sizes[k % #sizes + 1])
else
out, got = dec:readInt(sizes[k % #sizes + 1])
end
if got == 0 then
break
end
if fx.partial then -- a cut file: the reference decoded fx.frames (whole frames); the rest may decode too
got = math.min(got, fx.frames - n)
if got <= 0 then
continue
end
end
crc = Crc32.of(out, 0, got * ch * 4, crc)
for i = 0, got * ch - 1 do
vals[n * ch + i + 1] = if fx.float then buffer.readf32(out, i * 4) else buffer.readi32(out, i * 4)
end
n += got
end
return crc, n, vals
end
function L.cases(t: any, mod: any, cases: { any }, optsOf: ((any) -> any)?): number
local seekChecks = 0
for _, fx in cases do
local name = fx.name
local dec, err = open(mod, fx.data, optsOf and optsOf(fx))
t:check(dec, name .. ": open: " .. tostring(err))
t:check(dec.sampleRate == fx.rate and dec.channels == fx.channels, name .. ": format")
if not fx.partial then
t:eq(dec.frames, fx.frames, name .. ": frames")
t:check(math.abs(dec.duration - fx.frames / fx.rate) < 1e-9, name .. ": duration")
end
t:eq(dec.float, fx.float, name .. ": float")
local crc, n, vals = L.all(dec, fx)
if fx.partial then
t:check(dec.error ~= nil, name .. ": the cut frame is reported")
else
t:eq(dec.error, nil, name .. ": no decode error")
end
t:eq(n, fx.frames, name .. ": decoded frames")
t:eq(crc, fx.crc, name .. ": samples identical to the reference decoder")
local ch = dec.channels
dec:seek(0)
local reuse = buffer.create(100 * ch * 4)
local f, gotf = dec:read(100, reuse)
t:check(f == reuse, name .. ": output buffer reused")
local scale = if fx.float then 1 else 2 ^ ((fx.container or fx.bits) - 1)
local bad = 0
for i = 0, math.min(gotf, 100) * ch - 1 do
if buffer.readf32(f, i * 4) ~= f32(vals[i + 1] / scale) then
bad += 1
end
end
t:eq(bad, 0, name .. ": f32 samples")
-- seeking (sample-accurate: seek(s / rate) lands on frame s)
local last = if fx.partial then fx.frames - 50 else fx.frames
local rand = T.rng(7)
local targets = { 0, 1, last - 1, last // 2, 22050, 22049, 73729, 73727 }
for _ = 1, 6 do
table.insert(targets, rand(math.max(last, 1)))
end
for _, s in targets do
if s >= 0 and s < last then
dec:seek(s / fx.rate)
t:check(math.abs(dec:position() * fx.rate - s) < 1e-6, name .. ": position after seek " .. s)
local out, got = dec:read(50)
t:eq(got, math.min(50, fx.frames - s), name .. ": read after seek " .. s)
local diff = 0
for i = 0, got * ch - 1 do
if buffer.readf32(out, i * 4) ~= f32(vals[s * ch + i + 1] / scale) then
diff += 1
end
end
t:eq(diff, 0, name .. ": samples after seek " .. s)
seekChecks += 1
end
end
dec:seek(dec.duration + 5)
t:eq(select(2, dec:read(10)), 0, name .. ": read past the end")
for key, v in fx.tags or {} do
t:eq(dec.tags[key], v, name .. ": tag " .. key)
end
t:eq(mod.sniff(Bytes.reader(buffer.fromstring(fx.data))), 1, name .. ": sniff")
end
return seekChecks
end
-- decode() from a Reader over a sub-range equals read()
function L.subrange(t: any, mod: any, fx: any, format: string)
local name = fx.name
local big = buffer.create(#fx.data + 13)
buffer.writestring(big, 6, fx.data)
local a, err = mod.decode(Bytes.reader(big, 6, #fx.data))
t:check(a and a.kind == "audio" and a.frames == fx.frames and a.channels == fx.channels, name .. ": decode: " .. tostring(err))
local ref = open(mod, fx.data):read(fx.frames)
t:check(buffer.tostring(ref) == buffer.tostring(a.samples), name .. ": decode = read")
t:eq(a.meta.source.format, format, name .. ": meta.source")
t:eq(a.meta.source.bitDepth, if fx.float then 32 else fx.bits, name .. ": bit depth")
end
return L