File versions 3.95+ (3.99+ verified; the 3.95-3.98 range-coder model is ported without a reference fixture), every level (fast .. insane), 8/16/24/32-bit int and 32-bit float, 1-32 channels, special frames, frame CRCs, seeking, APEv2 tags, ID3v2 in front. 35 fixtures (mac 13.28, the FATE luckynight sample) bit-exact against mac -d. tests/audio/_lossless.luau: the per-fixture checks shared by the lossless format tests. tools/bench.luau: the ws-audio2 lossless block.
91 lines
3.4 KiB
Luau
91 lines
3.4 KiB
Luau
-- audio/ape tests: every fixture from tools/fixtures/ape.py (mac -c1000 .. -c5000, 8 - 32-bit int and 32-bit float,
|
|
-- 1 - 8 channels, special frames (silence, pseudo stereo), odd rates and lengths, a second frame, APEv2 tags, the FATE
|
|
-- sample of an older 3.99 encoder cut mid-frame) decoded bit-exact against `mac -d`; seeking; f32 scaling and output
|
|
-- reuse; decode() from a sub-range Reader; an ID3v2 tag in front; the header facts; sniff; the api; a damaged frame
|
|
-- (CRC); the fuzz pass. Fixtures: tools/fixtures/ape.py.
|
|
local T = require("@tests/T")
|
|
local Ape = require("@formats/audio/ape")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local F = require("@formats")
|
|
local Cases = require("@tests/gen/ape_cases")
|
|
local L = require("@tests/audio/_lossless")
|
|
|
|
local t = T.suite("ape")
|
|
local byName = {}
|
|
for _, c in Cases do
|
|
byName[c.name] = c
|
|
end
|
|
local function open(b: any, opts: any?): (any, any)
|
|
return L.open(Ape, b, opts)
|
|
end
|
|
|
|
t:eq(Ape.name, "ape", "name")
|
|
t:check(Ape.caps.decode and not Ape.caps.encode, "caps")
|
|
|
|
local seekChecks = L.cases(t, Ape, Cases)
|
|
t:note("%d fixtures bit-exact against mac -d, %d seeks", #Cases, seekChecks)
|
|
|
|
for _, name in { "s16_c2000", "tags", "f32", "ch6" } do
|
|
L.subrange(t, Ape, byName[name], "ape")
|
|
end
|
|
|
|
-- header facts
|
|
do
|
|
local d = open(byName.s16_c5000.data)
|
|
t:eq(d.level, "insane", "level")
|
|
t:eq(d.bitsPerSample, 16, "bits")
|
|
t:check(d.version >= 3990, "version")
|
|
t:eq(open(byName.s24_c4000.data).level, "extra high", "level 4000")
|
|
t:eq(open(byName.s16_c1000.data).level, "fast", "level 1000")
|
|
end
|
|
|
|
-- an ID3v2 tag in front of the descriptor (the seek table counts from the descriptor)
|
|
do
|
|
local fx = byName.s16_c3000
|
|
local id3 = "ID3\4\0\0\0\0\0\20" .. "TIT2\0\0\0\10\0\0\3hello id3"
|
|
local b = buffer.fromstring(id3 .. fx.data)
|
|
t:eq(Ape.sniff(Bytes.reader(b)), 1, "sniff ID3 + MAC")
|
|
local dec = open(b)
|
|
t:check(dec and (L.all(dec, fx)) == fx.crc, "ID3v2 in front")
|
|
end
|
|
|
|
-- the api
|
|
do
|
|
local fx = byName.s16_c2000
|
|
local _ = fx
|
|
t:eq(F.detect(buffer.fromstring(fx.data)), "ape", "detect")
|
|
local a = F.audio.load(fx.data)
|
|
t:check(a and a.frames == fx.frames and a.meta.source.format == "ape", "F.audio.load")
|
|
end
|
|
|
|
-- errors: a damaged frame fails its CRC, bad headers
|
|
do
|
|
local fx = byName.s16_long -- two frames: the second is damaged
|
|
local b = buffer.fromstring(fx.data)
|
|
local at = #fx.data - 2000
|
|
buffer.writeu8(b, at, bit32.bxor(buffer.readu8(b, at), 0x40))
|
|
local dec = open(b)
|
|
local _, got = dec:read(fx.frames)
|
|
t:check(dec.error and (dec.error.code == Err.CHECKSUM or dec.error.code == Err.CORRUPT) and got == 73728, "damaged frame: " .. tostring(dec.error))
|
|
local _, e0 = open(buffer.fromstring(string.sub(fx.data, 1, 2000) .. string.rep("\0", 100)))
|
|
t:check(e0 ~= nil, "a broken first frame fails open: " .. tostring(e0))
|
|
local _, e1 = open("MAC ")
|
|
t:check(e1 and Err.is(e1), "too short: " .. tostring(e1))
|
|
local old = buffer.fromstring(fx.data)
|
|
buffer.writeu16(old, 4, 3800)
|
|
local _, e2 = open(old)
|
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "old version: " .. tostring(e2))
|
|
t:eq(Ape.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff RIFF")
|
|
end
|
|
|
|
-- the fuzz pass
|
|
local samples = {}
|
|
for _, name in { "s16_c2000", "s16_c4000", "mono", "s24", "s32", "f32", "ch6", "silence", "pseudo_stereo" } do
|
|
table.insert(samples, buffer.fromstring(byName[name].data))
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Ape.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 50 })
|
|
t:done()
|