Files
rosetta/tests/audio/flac_encode.luau
T
4DBugandClaude Opus 5.5 434a304315 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:25:03 -05:00

110 lines
4.5 KiB
Luau

-- audio/flac encoder tests: every fixture-step encode (levels 0-8, 1 / 2 / 6 channels, 8 / 12 / 16 / 20 / 24 bits,
-- odd lengths and block sizes, odd rates, seek tables, padding) passed `flac -t` and carries the MD5 of the quantized
-- input; sizes within 10% of libFLAC at the same level; each decodes bit-exactly through our own decoder; streaming
-- from a Decoder gives the same bytes; tags; option errors. Fixtures: tools/fixtures/flac_enc.py.
local T = require("@tests/T")
local Flac = require("@formats/audio/flac")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Pcm = require("@formats/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
-- errors
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()