-- audio/vorbis encoder tests: every fixture-step encode (qualities -1 .. 10, mono / stereo / 5.1, 8 .. 96 kHz, odd -- lengths, silence) passes ogginfo, decodes in oggdec (libvorbisfile) with the exact input length and an SNR over a -- per-quality floor, and in ffmpeg; our own decoder gives the same; the codebooks are valid prefix codes; streaming -- from a Decoder gives the same bytes; tags round-trip; option errors. Fixtures: tools/fixtures/vorbis_enc.py. local T = require("@tests/T") local Vorbis = require("@rosetta/audio/vorbis") local VEnc = require("@rosetta/audio/vorbis/Encode") 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/vorbis_enc") local Sigs = require("@tests/gen/vorbis_enc_sigs") local t = T.suite("vorbis_encode") local function audio(name: string): Types.Audio local s = Sigs[name] local a = Types.Audio.new(s.rate, s.channels, s.frames) buffer.writestring(a.samples, 0, s.data) return a end local function snr(x: Types.Audio, y: Types.Audio): number local sig, er = 0, 0 for i = 0, math.min(x.frames, y.frames) * x.channels - 1 do local a = buffer.readf32(x.samples, i * 4) local d = a - buffer.readf32(y.samples, i * 4) sig += a * a er += d * d end if er == 0 then return 200 end return if sig > 0 then 10 * math.log10(sig / er) else -200 end local function floorFor(q: number): number return 8 + 1.2 * math.max(q, 0) end t:check(Vorbis.caps.encode and Vorbis.encode ~= nil, "caps.encode") -- the codebooks: Kraft sums of exactly 1 (complete prefix codes) and lengths 1 .. 24 for i, b in VEnc.BOOKS do local k, okLen = 0, true for _, l in b.lengths do k += 2 ^ -l okLen = okLen and l >= 1 and l <= 24 end t:check(math.abs(k - 1) < 1e-9 and okLen, "codebook " .. (i - 1) .. ": complete prefix code") end local minSnr = math.huge for _, e in Enc do t:eq(e.ogginfo, "", e.name .. ": ogginfo reports no warnings / errors") t:eq(e.oggdecFrames, e.frames, e.name .. ": oggdec frames = input frames") t:eq(e.ffErrors, "", e.name .. ": ffmpeg decodes without errors") local silent = string.find(e.name, "silence", 1, true) ~= nil local f = if silent then -math.huge else floorFor(e.quality) t:check(e.oggdecSnr >= f, string.format("%s: oggdec SNR %.1f dB (floor %.1f)", e.name, e.oggdecSnr, f)) if e.frames >= 2048 then -- (ffmpeg does not trim the end of a stream that fits on one Ogg page: up to a block more) t:check(e.ffFrames >= e.frames and e.ffFrames <= e.frames + 1024, string.format("%s: ffmpeg frames %d", e.name, e.ffFrames)) t:check(e.ffSnr >= f, string.format("%s: ffmpeg SNR %.1f dB", e.name, e.ffSnr)) end if not silent then minSnr = math.min(minSnr, e.oggdecSnr) end -- our decoder: the same length and quality (it is sample-exact with libvorbis) local x = audio(e.sig) local y, err = Vorbis.decode(Bytes.reader(buffer.fromstring(e.data))) t:check(y, e.name .. ": our decoder: " .. tostring(err)) t:eq(y.frames, e.frames, e.name .. ": our decoder's frames") local s = snr(x, y) t:check(math.abs(s - e.oggdecSnr) < 0.5 or s >= 100, string.format("%s: our decoder %.1f dB, oggdec %.1f dB", e.name, s, e.oggdecSnr)) end t:note("%d encodes pass ogginfo / oggdec / ffmpeg; lowest SNR %.1f dB", #Enc, minSnr) -- streaming, tags do local a = audio("music_2_3000_44100") local f1 = Vorbis.encode(a) :: buffer local f2 = Vorbis.encode(Pcm.audioDecoder(a)) :: buffer t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source") a.tags = { title = "Tïtle", artist = "Artist", track = "9" } local f3 = Vorbis.encode(a, { quality = 1, serial = 77 }) :: buffer local d = Vorbis.open(Bytes.reader(f3)) :: any t:eq(d.tags.title, "Tïtle", "title tag") t:eq(d.tags.track, "9", "track tag") t:check(string.find(d.vendor, "rosetta", 1, true) ~= nil, "vendor") local empty = Vorbis.encode(Types.Audio.new(44100, 1, 0)) :: buffer local e0 = Vorbis.decode(Bytes.reader(empty)) t:check(e0 and e0.frames == 0, "empty input") end -- errors do local a = Types.Audio.new(44100, 2, 10) local function code(v: any, o: any): string? local _, e = Vorbis.encode(v, o) return e and e.code end t:eq(code("x", nil), Err.ARG, "junk") t:eq(code(a, { quality = 11 }), Err.ARG, "quality 11") t:eq(code(a, { quality = -2 }), Err.ARG, "quality -2") t:eq(code(a, { serial = -1 }), Err.ARG, "serial -1") 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/vorbis.luau; the encoder takes no untrusted bytes) t:done()