-- tests/audio/_adpcm: shared checks of the ADPCM game-audio formats (adx, vag, brstm): a file against the reference -- decoder's s16le output, bit-exact, through decode() and through open() with seeks (forward into unvisited packets, -- back, the end), loop points; the SNR of an encoder's round trip. local Bytes = require("@rosetta/core/Bytes") local H = {} -- same(f32 buffer, s16le reference buffer, first reference sample, count) -> true when every sample is equal function H.same(out: buffer, ref: buffer, from: number, count: number): boolean for i = 0, count - 1 do if buffer.readf32(out, i * 4) * 32768 ~= buffer.readi16(ref, (from + i) * 2) then return false end end return true end function H.snr(x: buffer, y: buffer, count: number): number local sig, err = 0, 0 for i = 0, count - 1 do local a = buffer.readf32(x, i * 4) local e = a - buffer.readf32(y, i * 4) sig += a * a err += e * e end return if err == 0 then 200 else 10 * math.log10(sig / err) end -- check(t, Fmt, c, opts?): c = { name, data, rate, ch, frames?, ref, loopStart?, loopEnd? } function H.check(t: any, Fmt: any, c: any, opts: any?) local ref = buffer.fromstring(c.ref) local frames = buffer.len(ref) // (2 * c.ch) local a, err = Fmt.decode(Bytes.reader(buffer.fromstring(c.data)), opts) t:check(a, c.name .. ": decode: " .. tostring(err)) if not a then return end t:eq(a.frames, frames, c.name .. ": frames") t:eq(a.channels, c.ch, c.name .. ": channels") t:eq(a.sampleRate, c.rate, c.name .. ": rate") t:check(H.same(a.samples, ref, 0, frames * c.ch), c.name .. ": bit-exact with the reference") t:eq(a.meta.source.format, Fmt.name, c.name .. ": meta.source.format") if c.loopStart then local l = a.meta.source.loop t:check(l and l.start == c.loopStart and l.stop == c.loopEnd, c.name .. ": loop " .. tostring(l and l.start) .. " " .. tostring(l and l.stop)) else t:eq(a.meta.source.loop, nil, c.name .. ": no loop") end -- open: seek forward first (unvisited packets), then back, then the end local dec = Fmt.open(Bytes.reader(buffer.fromstring(c.data)), opts) :: any for _, f in { frames * 3 // 4, 5, frames // 2 + 13, frames - 2, 0, 4097 } do f = math.min(f, frames) dec:seek(f / c.rate) local b, n = dec:read(50) t:check(n == math.min(50, frames - f) and H.same(b, ref, f * c.ch, n * c.ch), c.name .. ": seek to " .. f) end end return H