147 lines
5.7 KiB
Luau
147 lines
5.7 KiB
Luau
-- audio/au tests: files written by ffmpeg and sox against ffmpeg's decode; our encoder's output via ffmpeg.
|
|
-- Fixtures: tools/fixtures/au.py.
|
|
local T = require("@tests/T")
|
|
local Au = require("@rosetta/audio/au")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Cases = require("@tests/gen/au_cases")
|
|
local Enc = require("@tests/gen/au_enc")
|
|
local Sig = require("@tests/gen/au_sig")
|
|
|
|
local t = T.suite("au")
|
|
|
|
local function reader(s: string): Bytes.Reader
|
|
return Bytes.reader(buffer.fromstring(s))
|
|
end
|
|
local function maxDiff(a: buffer, ao: number, b: buffer, bo: number, n: number): number
|
|
local m = 0
|
|
for i = 0, n - 1 do
|
|
local d = math.abs(buffer.readf32(a, ao + i * 4) - buffer.readf32(b, bo + i * 4))
|
|
if d > m then
|
|
m = d
|
|
end
|
|
end
|
|
return m
|
|
end
|
|
local function case(name: string): any
|
|
for _, c in Cases do
|
|
if c.name == name then
|
|
return c
|
|
end
|
|
end
|
|
error("no case " .. name)
|
|
end
|
|
|
|
t:eq(Au.name, "au", "name")
|
|
t:eq(Au.category, "audio", "category")
|
|
t:check(Au.caps.decode and Au.caps.encode and table.find(Au.extensions, "snd") ~= nil, "caps / extensions")
|
|
|
|
for _, c in Cases do
|
|
local a, err = Au.decode(reader(c.data))
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
t:check(a.sampleRate == c.rate and a.channels == c.channels, c.name .. ": format")
|
|
t:eq(a.frames, c.frames, c.name .. ": frames")
|
|
local ref = buffer.fromstring(c.ref)
|
|
t:check(maxDiff(a.samples, 0, ref, 0, c.frames * c.channels) < 1e-6, c.name .. ": samples match ffmpeg")
|
|
local dec = Au.open(reader(c.data)) :: Types.Decoder
|
|
local got, ok = 0, true
|
|
while true do
|
|
local b, k = dec:read(1000)
|
|
if k == 0 then
|
|
break
|
|
end
|
|
ok = ok and maxDiff(b, 0, ref, got * c.channels * 4, k * c.channels) < 1e-6
|
|
got += k
|
|
end
|
|
t:check(ok and got == c.frames, c.name .. ": streamed reads")
|
|
dec:seek(700 / c.rate)
|
|
local b, k = dec:read(40)
|
|
t:check(k == 40 and maxDiff(b, 0, ref, 700 * c.channels * 4, 40 * c.channels) < 1e-6, c.name .. ": seek")
|
|
end
|
|
t:note("%d reference files match ffmpeg", #Cases)
|
|
|
|
do
|
|
local a = Au.decode(reader(case("sox-comment").data)) :: Types.Audio
|
|
t:check(a.tags.comment ~= nil and string.find(a.tags.comment, "hello sun", 1, true) ~= nil, "annotation as comment")
|
|
-- an unknown data size (0xFFFFFFFF) runs to the end of the file
|
|
local c = case("ffmpeg-s16be")
|
|
local unk = string.sub(c.data, 1, 8) .. "\255\255\255\255" .. string.sub(c.data, 13)
|
|
local a2 = Au.decode(reader(unk)) :: Types.Audio
|
|
t:check(a2 and a2.frames == c.frames, "unknown data size")
|
|
local cut = Au.decode(reader(string.sub(c.data, 1, #c.data - 400))) :: Types.Audio
|
|
t:check(cut and cut.frames == c.frames - 100 and cut.meta.source.truncated, "truncated data")
|
|
local big = buffer.create(#c.data + 3)
|
|
buffer.writestring(big, 3, c.data)
|
|
local a3 = Au.decode(Bytes.reader(big, 3, #c.data)) :: Types.Audio
|
|
t:check(a3 and maxDiff(a3.samples, 0, buffer.fromstring(c.ref), 0, c.frames * 2) < 1e-6, "decode from a sub-range")
|
|
end
|
|
|
|
local TOL = { s8 = 0.5 / 128, ["s16-mono"] = 0.5 / 32768, s16 = 0.5 / 32768, s24 = 0.5 / 8388608, s32 = 1e-7, f32 = 0, f64 = 0, ulaw = 0.04, alaw = 0.04 }
|
|
local sig = buffer.fromstring(Sig.data)
|
|
for _, e in Enc do
|
|
t:eq(e.ffmpegErrors, "", e.name .. ": ffmpeg reads our file without errors")
|
|
t:check(e.frames == Sig.frames and e.rate == Sig.rate, e.name .. ": ffmpeg frames / rate")
|
|
t:check(e.maxDiff <= TOL[e.name] + 1e-7, string.format("%s: ffmpeg decode within %g (max diff %g)", e.name, TOL[e.name], e.maxDiff))
|
|
t:check(e.sameStreamed, e.name .. ": encoding a Decoder gives the same bytes")
|
|
local back = Au.decode(reader(e.data)) :: Types.Audio
|
|
t:check(back and back.frames == Sig.frames and back.channels == e.channels, e.name .. ": our decoder reads our file")
|
|
if e.channels == 2 then
|
|
t:check(maxDiff(back.samples, 0, sig, 0, Sig.frames * 2) <= TOL[e.name] + 1e-7, e.name .. ": round trip")
|
|
end
|
|
end
|
|
t:note("%d encoder outputs read back by ffmpeg and by us", #Enc)
|
|
|
|
do
|
|
local a = Types.Audio.new(8000, 1, 2)
|
|
buffer.writef32(a.samples, 0, 0.25)
|
|
buffer.writef32(a.samples, 4, -0.25)
|
|
a.tags = { comment = "annotated!" }
|
|
local b = Au.encode(a) :: buffer
|
|
t:eq(bit32.byteswap(buffer.readu32(b, 4)) % 8, 0, "data offset is a multiple of 8")
|
|
local back = Au.decode(Bytes.reader(b)) :: Types.Audio
|
|
t:check(back.frames == 2 and back.tags.comment == "annotated!" and buffer.readf32(back.samples, 0) == 0.25, "comment + samples")
|
|
local _, e1 = Au.encode(42 :: any)
|
|
t:check(e1 and e1.code == Err.ARG, "encode junk: ARG")
|
|
local _, e2 = Au.encode(a, { sampleFormat = "u8" } :: any)
|
|
t:check(e2 and e2.code == Err.ARG, "bad sampleFormat: ARG")
|
|
local frac = Types.Audio.new(8000.5, 1, 1)
|
|
local _, e3 = Au.encode(frac)
|
|
t:check(e3 and e3.code == Err.ARG, "fractional rate: ARG")
|
|
t:eq(Au.sniff(Bytes.reader(b)), 1, "sniff")
|
|
t:eq(Au.sniff(reader("FORM")), 0, "sniff other")
|
|
-- G.721 ADPCM (encoding 23): unsupported
|
|
local adpcm = string.sub(buffer.tostring(b), 1, 12) .. "\0\0\0\23" .. string.sub(buffer.tostring(b), 17)
|
|
local _, e4 = Au.decode(reader(adpcm))
|
|
t:check(e4 and e4.code == Err.UNSUPPORTED, "encoding 23: UNSUPPORTED")
|
|
local _, e5 = Au.decode(reader(".snd\0\0\0\24"))
|
|
t:check(e5 and e5.code == Err.CORRUPT, "short header: CORRUPT")
|
|
local _, e6 = Au.decode(reader(case("ffmpeg-s16be").data), { maxFrames = 5 })
|
|
t:check(e6 and e6.code == Err.LIMIT, "maxFrames: LIMIT")
|
|
end
|
|
|
|
do
|
|
local samples = {}
|
|
for i, c in Cases do
|
|
if i % 2 == 1 then
|
|
table.insert(samples, string.sub(c.data, 1, 400))
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Au.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 30 })
|
|
t:fuzz("open + read", function(b: buffer)
|
|
local d = Au.open(Bytes.reader(b))
|
|
if d then
|
|
d:read(64)
|
|
d:seek(1e9)
|
|
d:read(64)
|
|
d:seek(-1)
|
|
d:read(64)
|
|
end
|
|
return d
|
|
end, samples, { mutations = 10, garbage = 50 })
|
|
end
|
|
|
|
t:done()
|