Files
rosetta/tests/audio/flac_encode.luau
T

107 lines
4.2 KiB
Luau

-- audio/flac encoder tests: each fixture-step encode passed `flac -t`, has the input's MD5, is within 10% of libFLAC.
-- Fixtures: tools/fixtures/flac_enc.py.
local T = require("@tests/T")
local Flac = require("@rosetta/audio/flac")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Pcm = require("@rosetta/codecs/audio/Pcm")
local Enc = require("@tests/gen/flac_enc")
local Sigs = require("@tests/gen/flac_enc_sigs")
local t = T.suite("flac_encode")
local sigCache: { [string]: any } = {}
local function sig(name: string): Types.Audio
local s = sigCache[name]
if not s then
local m = Sigs[name]
s = Types.Audio.new(m.rate, m.channels, m.frames)
buffer.writestring(s.samples, 0, m.data)
sigCache[name] = s
end
return s
end
t:check(Flac.caps.encode and Flac.encode ~= nil, "caps.encode")
local worst = 0
for _, e in Enc do
t:eq(e.flacTest, "", e.name .. ": flac -t")
t:eq(e.md5, e.wantMd5, e.name .. ": STREAMINFO MD5 = MD5 of the quantized input")
if e.refSize then
local ratio = e.size / e.refSize
worst = math.max(worst, ratio)
t:check(ratio < 1.1, string.format("%s: %d bytes, libFLAC %d (%.3f)", e.name, e.size, e.refSize, ratio))
end
-- our decoder: bit-exact against the input quantized to e.bits
local a = sig(e.sig)
local back, err = Flac.decode(Bytes.reader(buffer.fromstring(e.data)))
t:check(back, e.name .. ": our decoder: " .. tostring(err))
t:check(back.frames == a.frames and back.channels == a.channels and back.sampleRate == a.sampleRate, e.name .. ": format")
local s = 2 ^ (e.bits - 1)
local exact = true
for i = 0, a.frames * a.channels - 1 do
local want = math.clamp(math.floor(buffer.readf32(a.samples, i * 4) * s + 0.5), -s, s - 1)
if math.floor(buffer.readf32(back.samples, i * 4) * s + 0.5) ~= want then
exact = false
break
end
end
t:check(exact, e.name .. ": bit-exact round trip")
t:eq(back.meta.source.bitDepth, e.bits, e.name .. ": bits per sample")
end
t:note("%d encodes pass flac -t with the right MD5 and decode bit-exactly; worst size vs libFLAC %.3f", #Enc, worst)
-- streaming: a Decoder source gives the same bytes as the Audio
do
local a = sig("flacsig_music_2_7000_44100")
local f1 = Flac.encode(a, { level = 5 }) :: buffer
local f2 = Flac.encode(Pcm.audioDecoder(a), { level = 5 }) :: buffer
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
-- a decoder of our own output re-encodes to the same stream (its bit depth carries over)
local dec = Flac.open(Bytes.reader(f1)) :: any
local f3 = Flac.encode(dec, { level = 5 }) :: buffer
t:check(buffer.tostring(f3) == buffer.tostring(f1), "FLAC -> FLAC through the streaming decoder")
end
-- tags, bit depth from the source
do
local a = Types.Audio.new(48000, 1, 3000)
for i = 0, 2999 do
buffer.writef32(a.samples, i * 4, math.floor(math.sin(i / 7) * 100) / 128)
end
a.tags = { title = "Tïtle", artist = "Artist", track = "3" }
a.meta.source = { format = "wav", bitDepth = 8 }
local f = Flac.encode(a) :: buffer
local back = Flac.decode(Bytes.reader(f)) :: Types.Audio
t:eq(back.meta.source.bitDepth, 8, "bit depth from meta.source")
-- (the absorbed FLAC decoder keeps Vorbis comment keys upper case)
local tags = back.tags :: any
t:eq(tags.title, "Tïtle", "title")
t:eq(tags.artist, "Artist", "artist")
t:eq(tags.track, "3", "track number")
local empty = Flac.encode(Types.Audio.new(44100, 2, 0)) :: buffer
local e0 = Flac.decode(Bytes.reader(empty))
t:check(e0 and e0.frames == 0, "empty audio")
end
do
local a = Types.Audio.new(44100, 2, 10)
local function code(v: any, o: any): string?
local _, e = Flac.encode(v, o)
return e and e.code
end
t:eq(code({}, nil), Err.ARG, "junk value")
t:eq(code(a, { level = 9 }), Err.ARG, "level 9")
t:eq(code(a, { level = 2.5 }), Err.ARG, "level 2.5")
t:eq(code(a, { bits = 32 }), Err.ARG, "32 bits")
t:eq(code(a, { bits = 3 }), Err.ARG, "3 bits")
t:eq(code(a, { blockSize = 8 }), Err.ARG, "block size 8")
t:eq(code(Types.Audio.new(44100, 9, 1), nil), Err.ARG, "9 channels")
t:eq(code(Types.Audio.new(44100.5, 1, 1), nil), Err.ARG, "fractional rate")
end
-- (the decoder's fuzz pass is in tests/audio/flac.luau; the encoder takes no untrusted bytes)
t:done()