101 lines
3.9 KiB
Luau
101 lines
3.9 KiB
Luau
-- audio/tta tests: every fixture from tools/fixtures/tta.py (ttaenc 3.4.1: 8 / 16 / 24 bits, 1 - 6 channels, odd
|
|
-- rates and lengths, several frames, silence, noise; the FATE sample inside.tta cut mid-file) decoded bit-exact
|
|
-- against `ttaenc -d`; seeking; f32 scaling and output reuse; decode() from a sub-range Reader; an ID3v2 tag in front
|
|
-- and an APEv2 tag at the end; password-protected files (UNSUPPORTED); header / seek table / frame CRCs; sniff; the
|
|
-- api; the fuzz pass. Fixtures: tools/fixtures/tta.py.
|
|
local T = require("@tests/T")
|
|
local Tta = require("@rosetta/audio/tta")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local F = require("@rosetta")
|
|
local Cases = require("@tests/gen/tta_cases")
|
|
local L = require("@tests/audio/_lossless")
|
|
|
|
local t = T.suite("tta")
|
|
local byName = {}
|
|
for _, c in Cases do
|
|
byName[c.name] = c
|
|
end
|
|
local function open(b: any, opts: any?): (any, any)
|
|
return L.open(Tta, b, opts)
|
|
end
|
|
|
|
t:eq(Tta.name, "tta", "name")
|
|
t:check(Tta.caps.decode and not Tta.caps.encode, "caps")
|
|
|
|
local seekChecks = L.cases(t, Tta, Cases)
|
|
t:note("%d fixtures bit-exact against ttaenc -d, %d seeks", #Cases, seekChecks)
|
|
|
|
for _, name in { "s16", "s24", "ch6", "u8" } do
|
|
L.subrange(t, Tta, byName[name], "tta")
|
|
end
|
|
|
|
local function u32(v: number): string
|
|
return string.char(v % 256, (v // 256) % 256, (v // 65536) % 256, (v // 16777216) % 256)
|
|
end
|
|
|
|
-- an ID3v2 tag in front, an APEv2 tag at the end
|
|
do
|
|
local fx = byName.s16
|
|
local id3 = "ID3\4\0\0\0\0\0\20" .. "TIT2\0\0\0\10\0\0\3hello id3"
|
|
local item = u32(5) .. u32(0) .. "Title\0" .. "Hello"
|
|
local tail = item .. "APETAGEX" .. u32(2000) .. u32(#item + 32) .. u32(1) .. u32(0) .. string.rep("\0", 8)
|
|
local b = buffer.fromstring(id3 .. fx.data .. tail)
|
|
t:eq(Tta.sniff(Bytes.reader(b)), 1, "sniff ID3 + TTA1")
|
|
local dec = open(b)
|
|
t:check(dec and (L.all(dec, fx)) == fx.crc, "ID3v2 + APEv2 around the file")
|
|
t:eq(dec.tags.title, "Hello", "APEv2 title")
|
|
end
|
|
|
|
-- errors: password protection, CRCs
|
|
do
|
|
local fx = byName.s16
|
|
local b = buffer.fromstring(fx.data)
|
|
buffer.writeu16(b, 4, 2)
|
|
buffer.writeu32(b, 18, Crc32.of(b, 0, 18))
|
|
local _, e1 = open(b)
|
|
t:check(e1 and e1.code == Err.UNSUPPORTED, "password-protected: " .. tostring(e1))
|
|
local h = buffer.fromstring(fx.data)
|
|
buffer.writeu8(h, 10, 0x45)
|
|
local _, e2 = open(h)
|
|
t:check(e2 and e2.code == Err.CHECKSUM, "header CRC: " .. tostring(e2))
|
|
local long = byName.s16_long
|
|
local d = buffer.fromstring(long.data)
|
|
local at = #long.data - 3000
|
|
buffer.writeu8(d, at, bit32.bxor(buffer.readu8(d, at), 1))
|
|
local dec = open(d)
|
|
local _, got = dec:read(long.frames)
|
|
t:check(dec.error and dec.error.code == Err.CHECKSUM and got == 2 * 46080, "frame CRC: " .. tostring(dec.error))
|
|
local _, e3 = open("TTA1")
|
|
t:check(e3 and Err.is(e3), "too short: " .. tostring(e3))
|
|
t:eq(Tta.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff RIFF")
|
|
end
|
|
|
|
-- the api
|
|
do
|
|
local fx = byName.s16
|
|
t:eq(F.detect(buffer.fromstring(fx.data)), "tta", "detect")
|
|
local a = F.audio.load(fx.data)
|
|
t:check(a and a.frames == fx.frames and a.meta.source.format == "tta", "F.audio.load")
|
|
end
|
|
|
|
-- the fuzz pass (the frame CRC rejects most mutations; the second set skips it to reach the decoder)
|
|
local samples = {}
|
|
for _, name in { "s16", "mono", "s24", "u8", "ch6" } do
|
|
table.insert(samples, buffer.fromstring(byName[name].data))
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Tta.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 150 })
|
|
local Decode = require("@rosetta/codecs/audio/Tta/Decode")
|
|
t:fuzz("frames without CRC", function(b: buffer)
|
|
local n = buffer.len(b)
|
|
local c = buffer.create(n + 4)
|
|
buffer.copy(c, 0, b)
|
|
buffer.writeu32(c, n, Crc32.of(b))
|
|
local st = Decode.state(2, 2)
|
|
return Decode.frame(st, c, 0, n + 4, 2000, table.create(4000, 0))
|
|
end, { buffer.fromstring(string.sub(byName.s16.data, 22 + 4 + 4 + 1, 22 + 4 + 4 + 3000)) }, { mutations = 300 })
|
|
t:done()
|