Files
rosetta/tests/audio/voc.luau
T

130 lines
5.5 KiB
Luau

-- audio/voc tests: files written by ffmpeg (type 1 + 8 blocks for 8-bit, type 9 otherwise) and sox against ffmpeg's
-- decode; silence / text / continuation blocks and repeat markers built here; streaming reads and seeks; our
-- encoder's output for every sample format decoded by ffmpeg (fixture step) and by us; errors; fuzz.
-- Fixtures: tools/fixtures/voc.py.
local T = require("@tests/T")
local Voc = require("@rosetta/audio/voc")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Cases = require("@tests/gen/voc_cases")
local Enc = require("@tests/gen/voc_enc")
local Sig = require("@tests/gen/voc_sig")
local t = T.suite("voc")
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
t:eq(Voc.name, "voc", "name")
t:eq(Voc.category, "audio", "category")
t:check(Voc.caps.decode and Voc.caps.encode, "caps")
for _, c in Cases do
local a, err = Voc.decode(reader(c.data))
t:check(a, c.name .. ": " .. tostring(err))
t:check(a.sampleRate == c.rate and a.channels == c.channels, string.format("%s: format %d Hz x %d, want %d x %d", c.name, a.sampleRate, a.channels, c.rate, c.channels))
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 = Voc.open(reader(c.data)) :: Types.Decoder
dec:seek(500 / c.rate)
local b, k = dec:read(64)
t:check(k == 64 and maxDiff(b, 0, ref, 500 * c.channels * 4, 64 * c.channels) < 1e-6, c.name .. ": seek")
end
t:note("%d reference files match ffmpeg", #Cases)
-- hand-built blocks: text, type 1 u8, silence, continuation, repeat markers, terminator
do
local hdr = "Creative Voice File\26\26\0\10\1\41\17"
local function blk(typ: number, payload: string): string
local n = #payload
return string.char(typ, n % 256, (n // 256) % 256, n // 65536) .. payload
end
local data = hdr
.. blk(5, "made by hand\0")
.. blk(1, string.char(156, 0) .. string.char(128, 192, 64)) -- 10 kHz, 3 samples
.. blk(3, string.char(4, 0, 156)) -- 5 frames of silence
.. blk(6, string.char(2, 0)) -- repeat start (ignored)
.. blk(2, string.char(255, 0)) -- continuation
.. blk(7, "") -- repeat end
.. blk(4, string.char(1, 0)) -- marker
.. "\0"
local a, err = Voc.decode(reader(data))
t:check(a, "hand-built: " .. tostring(err))
t:check(a.sampleRate == 10000 and a.channels == 1 and a.frames == 10, "hand-built: 10 kHz, 3 + 5 + 2 frames")
t:eq(a.tags.comment, "made by hand", "text block")
local want = { 0, 0.5, -0.5, 0, 0, 0, 0, 0, 127 / 128, -1 }
local ok = true
for i, v in want do
ok = ok and buffer.readf32(a.samples, (i - 1) * 4) == v
end
t:check(ok, "hand-built samples")
-- no terminator, a block cut short: the data present
local cut = Voc.decode(reader(string.sub(data, 1, 26 + 17 + 4 + 2 + 2)))
t:check(cut and cut.frames == 2 and cut.meta.source.truncated, "cut in a block")
-- Creative ADPCM: unsupported
local _, e = Voc.decode(reader(hdr .. blk(1, string.char(156, 1) .. "\0\0") .. "\0"))
t:check(e and e.code == Err.UNSUPPORTED, "ADPCM: UNSUPPORTED")
local _, e2 = Voc.decode(reader(hdr .. blk(2, "\0\0") .. "\0"))
t:check(e2 and e2.code == Err.CORRUPT, "continuation first: CORRUPT")
local _, e3 = Voc.decode(reader(hdr .. "\0"))
t:check(e3 and e3.code == Err.CORRUPT, "no sound: CORRUPT")
local _, e4 = Voc.decode(reader("Creative Voice Fil"))
t:check(e4 and e4.code == Err.CORRUPT, "short: CORRUPT")
t:eq(Voc.sniff(reader(data)), 1, "sniff")
t:eq(Voc.sniff(reader(".snd")), 0, "sniff other")
end
local TOL = { s16 = 0.5 / 32768, ["s16-mono"] = 0.5 / 32768, u8 = 0.5 / 128, 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 = Voc.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(22050, 2, 1)
a.tags = { comment = "hi" }
local b = Voc.encode(a) :: buffer
local back = Voc.decode(Bytes.reader(b)) :: Types.Audio
t:check(back.frames == 1 and back.tags.comment == "hi", "one frame + comment")
local _, e1 = Voc.encode(nil :: any)
t:check(e1 and e1.code == Err.ARG, "encode nil: ARG")
local _, e2 = Voc.encode(a, { sampleFormat = "s24" } :: any)
t:check(e2 and e2.code == Err.ARG, "bad sampleFormat: ARG")
local _, e3 = Voc.decode(reader(Cases[1].data), { maxFrames = 5 })
t:check(e3 and e3.code == Err.LIMIT, "maxFrames: LIMIT")
end
do
local samples = {}
for _, c in Cases do
table.insert(samples, string.sub(c.data, 1, 300))
end
t:fuzz("decode", function(b: buffer)
return Voc.decode(Bytes.reader(b))
end, samples, { mutations = 30 })
end
t:done()