Files
rosetta/tests/audio/adx.luau
T

91 lines
3.7 KiB
Luau

-- audio/adx tests: ffmpeg's ADX files (version 3) and a version 4 file with history and loop points, bit-exact with
-- vgmstream (tools/fixtures/adx.py) through decode and open with seeks; our encoder's files as vgmstream decodes them
-- (equal to our decode, loops reported) and their quality; header errors; the api; the fuzz pass.
local T = require("@tests/T")
local Adx = require("@rosetta/audio/adx")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local F = require("@rosetta")
local H = require("@tests/audio/_adpcm")
local Cases = require("@tests/gen/adx_cases")
local Ours = require("@tests/gen/adx_ours")
local Sig = require("@tests/gen/adx_sig")
local t = T.suite("adx")
t:eq(Adx.name, "adx", "name")
t:check(Adx.caps.decode and Adx.caps.encode, "caps")
local samples = {}
for _, c in Cases do
table.insert(samples, c.data)
t:eq(Adx.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
H.check(t, Adx, c)
end
t:note("%d files bit-exact with vgmstream", #Cases)
-- ------------------------------------------------------------------------------------------------ encoder
local audio = Types.Audio.new(Sig.rate, Sig.channels, Sig.frames)
buffer.writestring(audio.samples, 0, Sig.data)
for _, c in Ours do
H.check(t, Adx, c)
local a = Adx.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
local s = H.snr(audio.samples, a.samples, Sig.frames * Sig.channels)
t:check(s > 22, string.format("ours %s: %.1f dB", c.name, s))
t:note("encode %s: %.1f dB", c.name, s)
-- deterministic, and the same from a streaming Decoder; the loop is kept through a re-encode
local again = Adx.encode(a)
local back = Adx.decode(Bytes.reader(again :: buffer)) :: any
t:check(back and (c.loopStart == nil or (back.meta.source.loop.start == c.loopStart)), c.name .. ": loop kept on re-encode")
end
do
local out = Adx.encode(audio, { loop = { start = 1000, stop = 8000 } })
t:check(out and buffer.tostring(out) == Ours[2].data, "encode is deterministic")
local _, e1 = Adx.encode(audio, { loop = { start = 10, stop = 5 } })
t:check(e1 and e1.code == Err.ARG, "bad loop")
local _, e2 = Adx.encode(Types.Audio.new(8000, 9, 10))
t:check(e2 and e2.code == Err.ARG, "9 channels")
local _, e3 = Adx.encode(audio, { cutoff = 1e6 })
t:check(e3 and e3.code == Err.ARG, "bad cutoff")
end
-- ------------------------------------------------------------------------------------------------ errors
do
local base = Cases[1].data
local function patch(at: number, bytes: string): buffer
local b = buffer.fromstring(base)
buffer.writestring(b, at, bytes)
return b
end
local function fails(b: buffer, code: string, label: string)
local d, e = Adx.open(Bytes.reader(b))
t:check(d == nil and e and e.code == code, label .. ": " .. tostring(e))
end
fails(patch(0x12, "\4\9"), Err.UNSUPPORTED, "encrypted")
fails(patch(0x04, "\4"), Err.UNSUPPORTED, "exponential type")
fails(patch(0x04, "\17"), Err.UNSUPPORTED, "AHX")
fails(patch(0x07, "\0"), Err.CORRUPT, "0 channels")
fails(patch(0x12, "\6\0"), Err.UNSUPPORTED, "version 6")
fails(buffer.fromstring("not adx at all"), Err.CORRUPT, "not adx")
t:eq(F.detect(base), "adx", "F.detect")
local a = F.audio.load(base)
t:check(a and a.frames == Cases[1].frames, "F.audio.load")
local out = F.save(a, nil, { format = "adx" })
t:check(out and F.detect(out) == "adx", "F.save adx")
end
t:fuzz("open + read + seek", function(b: buffer)
local dec = Adx.open(Bytes.reader(b))
if dec then
dec:read(100000)
dec:seek(dec.duration / 2)
dec:read(33)
end
return dec
end, samples, { mutations = 10 })
t:fuzz("header", function(b: buffer)
return Adx.decode(Bytes.reader(b))
end, samples, { mutations = 10, keepHeader = 0, garbage = 50 })
t:done()