Files
rosetta/tests/codecs/resample.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

134 lines
5.3 KiB
Luau

-- codecs/audio/Resample tests: sines through common rate pairs (44100 <-> 48000, 8000 -> 48000, 48000 -> 22050, an inexact
-- ratio) against the analytic signal, alias rejection when downsampling, output lengths, streaming in odd chunks =
-- the whole-buffer result, multichannel, a Pcm source wrapper, bad arguments, and a no-throw pass over garbage.
local T = require("@tests/T")
local Resample = require("@formats/codecs/audio/Resample")
local Pcm = require("@formats/codecs/audio/Pcm")
local Types = require("@formats/Types")
local t = T.suite("resample")
local function sine(frames: number, ch: number, rate: number, freqs: { number }): buffer
local b = buffer.create(frames * ch * 4)
for i = 0, frames - 1 do
for c = 0, ch - 1 do
local f = freqs[c + 1] or freqs[1]
buffer.writef32(b, (i * ch + c) * 4, 0.5 * math.sin(2 * math.pi * f * i / rate))
end
end
return b
end
-- snr(out, frames, ch, rate, freqs, skip) -> dB of the analytic sine against the error, away from the edges
local function snr(b: buffer, frames: number, ch: number, rate: number, freqs: { number }, skip: number): number
local sig, err = 0, 0
for i = skip, frames - 1 - skip do
for c = 0, ch - 1 do
local f = freqs[c + 1] or freqs[1]
local want = 0.5 * math.sin(2 * math.pi * f * i / rate)
local got = buffer.readf32(b, (i * ch + c) * 4)
sig += want * want
err += (got - want) ^ 2
end
end
return 10 * math.log10(sig / math.max(err, 1e-30))
end
local function rms(b: buffer, frames: number, skip: number): number
local s = 0
for i = skip, frames - 1 - skip do
s += buffer.readf32(b, i * 4) ^ 2
end
return math.sqrt(s / math.max(1, frames - 2 * skip))
end
-- 1. accuracy on in-band sines
for _, c in { { 44100, 48000, 1000, "medium", 80 }, { 48000, 44100, 3000, "medium", 80 }, { 8000, 48000, 440, "medium", 80 }, { 48000, 22050, 5000, "high", 90 }, { 44100, 48000, 1000, "low", 55 }, { 44100, 47999, 1000, "medium", 75 } } do
local from, to, f, q, want = c[1], c[2], c[3], c[4], c[5]
local n = from // 4
local out, frames = Resample.buffer(sine(n, 1, from, { f }), n, 1, from, to, { quality = q })
t:check(out ~= nil, "resample " .. from .. " -> " .. to)
if out then
local fr = frames :: number
t:eq(fr, math.floor(n * to / from + 0.5), from .. " -> " .. to .. ": length")
local db = snr(out, fr, 1, to, { f }, to // 50)
t:note("%d -> %d (%s), %d Hz: %.1f dB", from, to, q, f, db)
t:check(db > want, string.format("%d -> %d %s: %.1f dB > %d", from, to, q, db, want))
end
end
-- 2. a tone above the new Nyquist is removed when downsampling (48 kHz -> 16 kHz, 10 kHz tone)
do
local n = 12000
local out, frames = Resample.buffer(sine(n, 1, 48000, { 10000 }), n, 1, 48000, 16000)
local level = rms(out :: buffer, frames :: number, 200) / (0.5 / math.sqrt(2))
t:check(level < 1e-3, string.format("alias rejection: %.2e", level))
end
-- 3. streaming in odd chunk sizes gives the whole-buffer result; stereo channels stay apart
do
local n, ch = 10007, 2
local src = sine(n, ch, 44100, { 700, 1900 })
local whole, wf = Resample.buffer(src, n, ch, 44100, 48000)
local rs = Resample.new(44100, 48000, ch) :: Resample.Resampler
local got = buffer.create((wf :: number) * ch * 4)
local gf = 0
local pos = 0
local rand = T.rng(7)
while pos < n do
local k = math.min(n - pos, 1 + rand(3000))
local part = buffer.create(k * ch * 4)
buffer.copy(part, 0, src, pos * ch * 4, k * ch * 4)
local b, m = rs:process(part, k)
buffer.copy(got, gf * ch * 4, b, 0, m * ch * 4)
gf += m
pos += k
end
local b, m = rs:flush()
buffer.copy(got, gf * ch * 4, b, 0, m * ch * 4)
gf += m
t:eq(gf, wf, "streamed length")
t:check(buffer.tostring(got) == buffer.tostring(whole :: buffer), "streamed = whole")
t:check(snr(got, gf, ch, 48000, { 700, 1900 }, 500) > 80, "stereo accuracy")
end
-- 4. a codecs/audio/Pcm source at a new rate
do
local a = Types.Audio.new(22050, 1, 5000)
a.samples = sine(5000, 1, 22050, { 500 })
local src = Resample.source(Pcm.source(a), 32000)
t:check(src and src.frames == math.floor(5000 * 32000 / 22050 + 0.5), "source frames")
local col = Pcm.collect(src)
t:check(col and col.sampleRate == 32000 and col.frames == src.frames, "source collects every frame")
t:check(col and snr(col.samples, col.frames, 1, 32000, { 500 }, 300) > 80, "source accuracy")
end
-- 5. identity, empty input, bad arguments
do
local src = sine(1000, 1, 48000, { 1000 })
local out = Resample.buffer(src, 1000, 1, 48000, 48000)
t:check(out and snr(out, 1000, 1, 48000, { 1000 }, 50) > 100, "identity")
local e, ef = Resample.buffer(buffer.create(0), 0, 2, 44100, 48000)
t:check(e and ef == 0, "empty input")
t:check(Resample.new(0, 48000, 1) == nil, "zero rate")
t:check(Resample.new(44100, 48000, 0) == nil, "zero channels")
t:check(Resample.new(44100, 48000, 1, { quality = "insane" :: any }) == nil, "bad quality")
t:check(Resample.buffer(buffer.create(4), 10, 1, 44100, 48000) == nil, "short buffer")
end
-- 6. no throw on garbage (NaN / Inf samples, odd lengths, chunk sizes)
local samples = {}
for i = 1, 4 do
table.insert(samples, buffer.tostring(sine(100 * i, 1, 8000, { 300 })))
end
t:fuzz("resample", function(b: buffer)
local rs = Resample.new(11025, 44100, 2, { quality = "low" })
if not rs then
return nil
end
local _, n = rs:process(b, buffer.len(b) // 8)
local _, m = rs:flush()
return n + m
end, samples)
t:done()