Files
rosetta/tests/audio/vag.luau
T

90 lines
3.7 KiB
Luau

-- audio/vag tests: VAGp (with and without loop flags), VAGi and KVAG files from our Python encoders, bit-exact with
-- vgmstream / ffmpeg (tools/fixtures/vag.py) through decode and open with seeks; our encoder's files as vgmstream
-- decodes them (loops reported where vgmstream reads them), their quality; errors; the api; the fuzz pass.
local T = require("@tests/T")
local Vag = require("@rosetta/audio/vag")
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/vag_cases")
local Ours = require("@tests/gen/vag_ours")
local Sig = require("@tests/gen/vag_sig")
local Sig1 = require("@tests/gen/vag_sig1")
local t = T.suite("vag")
t:eq(Vag.name, "vag", "name")
local samples = {}
for _, c in Cases do
table.insert(samples, c.data)
t:eq(Vag.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
H.check(t, Vag, c)
if c.title then
t:eq(Vag.decode(Bytes.reader(buffer.fromstring(c.data))).tags.title, c.title, c.name .. ": title")
end
end
t:note("%d files bit-exact with vgmstream / ffmpeg", #Cases)
-- ------------------------------------------------------------------------------------------------ encoder
local function audioOf(S: any): any
local a = Types.Audio.new(S.rate, S.channels, S.frames)
buffer.writestring(a.samples, 0, S.data)
return a
end
for _, c in Ours do
H.check(t, Vag, c)
local src = audioOf(if c.ch == 1 then Sig1 else Sig)
local a = Vag.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
-- the output starts with the blank frame: 28 samples later
local s = H.snr(src.samples, buffer.fromstring(buffer.readstring(a.samples, 28 * c.ch * 4, src.frames * c.ch * 4)), src.frames * c.ch)
t:check(s > 25, string.format("ours %s: %.1f dB", c.name, s))
t:note("encode %s: %.1f dB", c.name, s)
end
do
local mono = audioOf(Sig1)
local out = Vag.encode(mono, { loop = { start = 600, stop = 5000 } })
t:check(out and buffer.tostring(out) == Ours[2].data, "encode is deterministic")
mono.tags = { title = "A Title That Is Long" }
local named = Vag.decode(Bytes.reader(Vag.encode(mono) :: buffer)) :: any
t:eq(named.tags.title, "A Title That Is ", "the name field holds 16 bytes")
local _, e1 = Vag.encode(Types.Audio.new(8000, 3, 10))
t:check(e1 and e1.code == Err.ARG, "3 channels")
local _, e2 = Vag.encode(mono, { loop = { start = 0, stop = 10 } })
t:check(e2 and e2.code == Err.ARG, "a loop inside one frame")
local _, e3 = Vag.encode(audioOf(Sig), { interleave = 100 })
t:check(e3 and e3.code == Err.ARG, "bad interleave")
end
-- ------------------------------------------------------------------------------------------------ errors
do
local base = Cases[1].data
local hevag = buffer.fromstring(base)
buffer.writeu32(hevag, 4, bit32.byteswap(0x00030000))
local _, e1 = Vag.open(Bytes.reader(hevag))
t:check(e1 and e1.code == Err.UNSUPPORTED, "HEVAG: " .. tostring(e1))
local _, e2 = Vag.open(Bytes.reader(buffer.fromstring("VAGp\0\0")))
t:check(e2 and e2.code == Err.TRUNCATED, "short header: " .. tostring(e2))
local _, e3 = Vag.open(Bytes.reader(buffer.fromstring("RIFF0000WAVE")))
t:check(e3 and e3.code == Err.CORRUPT, "not vag")
t:eq(F.detect(base), "vag", "F.detect")
local a = F.audio.load(Cases[4].data)
t:check(a and a.channels == 1 and a.meta.source.codec == "ima", "F.audio.load KVAG")
end
t:fuzz("open + read + seek", function(b: buffer)
local dec = Vag.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 Vag.decode(Bytes.reader(b))
end, samples, { mutations = 10, keepHeader = 0, garbage = 50 })
t:done()