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.
152 lines
7.0 KiB
Luau
152 lines
7.0 KiB
Luau
-- ops/AudioOps tests: toAudio from a Decoder, gain / gainDb / normalize, peak / RMS of known signals, remix matrices
|
|
-- (5.1 -> stereo -> mono, mono -> stereo / 5.1), resample through codecs/audio/Resample, trim, concat and mix with rate /
|
|
-- channel conversion, fades, bad arguments (Err.ARG, never a throw), and the F.ops facade.
|
|
local T = require("@tests/T")
|
|
local AudioOps = require("@formats/ops/AudioOps")
|
|
local Types = require("@formats/Types")
|
|
local Pcm = require("@formats/codecs/audio/Pcm")
|
|
local Err = require("@formats/core/Err")
|
|
local F = require("@formats")
|
|
|
|
local t = T.suite("audioops")
|
|
|
|
local function audio(rate: number, ch: number, frames: number, fn: (f: number, c: number) -> number): Types.Audio
|
|
local a = Types.Audio.new(rate, ch, frames)
|
|
for f = 0, frames - 1 do
|
|
for c = 0, ch - 1 do
|
|
buffer.writef32(a.samples, (f * ch + c) * 4, fn(f, c))
|
|
end
|
|
end
|
|
a.tags = { title = "x" }
|
|
return a
|
|
end
|
|
local function at(a: Types.Audio, f: number, c: number): number
|
|
return buffer.readf32(a.samples, (f * a.channels + c) * 4)
|
|
end
|
|
local function near(x: number, y: number, eps: number?): boolean
|
|
return math.abs(x - y) <= (eps or 1e-6)
|
|
end
|
|
|
|
local sine = audio(8000, 2, 8000, function(f, c)
|
|
return (if c == 0 then 0.5 else 0.25) * math.sin(2 * math.pi * 440 * f / 8000)
|
|
end)
|
|
|
|
-- toAudio
|
|
do
|
|
local dec = Pcm.audioDecoder(sine)
|
|
local a = AudioOps.toAudio(dec) :: Types.Audio
|
|
t:check(a and a.frames == 8000 and a.channels == 2 and buffer.tostring(a.samples) == buffer.tostring(sine.samples), "toAudio(Decoder)")
|
|
t:check(AudioOps.toAudio(sine) == sine, "toAudio(Audio) is the Audio")
|
|
local _, e2 = AudioOps.toAudio(Pcm.audioDecoder(sine), { maxFrames = 10 })
|
|
t:check(e2 and e2.code == Err.LIMIT, "maxFrames -> LIMIT")
|
|
end
|
|
|
|
-- levels
|
|
do
|
|
local p, per = AudioOps.peak(sine)
|
|
t:check(near(p :: number, 0.5, 1e-3) and near((per :: any)[2], 0.25, 1e-3), "peak")
|
|
local r, rper = AudioOps.rms(sine)
|
|
t:check(near((rper :: any)[1], 0.5 / math.sqrt(2), 1e-3) and near((rper :: any)[2], 0.25 / math.sqrt(2), 1e-3), "rms per channel")
|
|
t:check(near(r :: number, math.sqrt((0.125 + 0.03125) / 2), 1e-3), "rms")
|
|
local g = AudioOps.gainDb(sine, -6.0206) :: Types.Audio
|
|
t:check(near(at(g, 100, 0), at(sine, 100, 0) / 2, 1e-4), "gainDb -6 dB halves")
|
|
t:eq(g.tags.title, "x", "tags copied")
|
|
local n = AudioOps.normalize(sine) :: Types.Audio
|
|
t:check(near((AudioOps.peak(n)) :: number, 1, 1e-5), "normalize to 1")
|
|
local z = AudioOps.normalize(Types.Audio.new(8000, 1, 10)) :: Types.Audio
|
|
t:check(z and z.frames == 10, "normalize silence")
|
|
end
|
|
|
|
-- channels
|
|
do
|
|
-- 5.1 (WAVE order FL FR C LFE BL BR) with distinct constants
|
|
local five = audio(8000, 6, 4, function(_, c)
|
|
return ({ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6 })[c + 1]
|
|
end)
|
|
local st = AudioOps.downmix(five) :: Types.Audio
|
|
t:check(st and st.channels == 2, "5.1 -> stereo")
|
|
local h = math.sqrt(0.5)
|
|
local norm = 1 / (1 + 2 * h)
|
|
t:check(near(at(st, 0, 0), (0.1 + h * 0.3 + h * 0.5) * norm, 1e-6), "stereo L = FL + C / sqrt2 + BL / sqrt2 (normalised)")
|
|
t:check(near(at(st, 0, 1), (0.2 + h * 0.3 + h * 0.6) * norm, 1e-6), "stereo R (LFE dropped)")
|
|
local mono = AudioOps.downmix(sine, 1) :: Types.Audio
|
|
t:check(near(at(mono, 100, 0), (at(sine, 100, 0) + at(sine, 100, 1)) / 2, 1e-6), "stereo -> mono averages")
|
|
local up = AudioOps.upmix(mono, 2) :: Types.Audio
|
|
t:check(up.channels == 2 and at(up, 100, 0) == at(mono, 100, 0) and at(up, 100, 1) == at(mono, 100, 0), "mono -> stereo copies")
|
|
local up6 = AudioOps.upmix(sine, 6) :: Types.Audio
|
|
t:check(up6.channels == 6 and at(up6, 7, 0) == at(sine, 7, 0) and at(up6, 7, 1) == at(sine, 7, 1) and at(up6, 7, 2) == 0, "stereo -> 5.1 keeps FL / FR")
|
|
local swapped = AudioOps.remix(sine, 2, { { 0, 1 }, { 1, 0 } }) :: Types.Audio
|
|
t:check(at(swapped, 9, 0) == at(sine, 9, 1), "explicit matrix")
|
|
local _, e = AudioOps.downmix(mono, 2)
|
|
t:check(e and e.code == Err.ARG, "downmix to more channels: ARG")
|
|
end
|
|
|
|
-- time
|
|
do
|
|
local r = AudioOps.resample(sine, 16000) :: Types.Audio
|
|
t:check(r.sampleRate == 16000 and r.frames == 16000, "resample 8 -> 16 kHz")
|
|
local err = 0
|
|
for f = 2000, 14000 do
|
|
err = math.max(err, math.abs(at(r, f, 0) - 0.5 * math.sin(2 * math.pi * 440 * f / 16000)))
|
|
end
|
|
t:check(err < 1e-3, string.format("resampled sine (max err %.1e)", err))
|
|
local c = AudioOps.trim(sine, 0.25, 0.5) :: Types.Audio
|
|
t:check(c.frames == 2000 and at(c, 0, 0) == at(sine, 2000, 0), "trim [0.25, 0.5) s")
|
|
t:eq((AudioOps.trim(sine, 0.9, 5) :: Types.Audio).frames, 800, "trim clamps")
|
|
local cat = AudioOps.concat({ sine, c }) :: Types.Audio
|
|
t:check(cat.frames == 10000 and at(cat, 8000, 1) == at(c, 0, 1), "concat")
|
|
local monoHi = audio(16000, 1, 1600, function()
|
|
return 0.1
|
|
end)
|
|
local cat2 = AudioOps.concat({ sine, monoHi }) :: Types.Audio
|
|
t:check(cat2.channels == 2 and cat2.sampleRate == 8000 and math.abs(cat2.frames - 8800) <= 1, "concat converts rate and channels")
|
|
local m = AudioOps.mix({ sine, sine }, { 0.5, 0.25 }) :: Types.Audio
|
|
t:check(near(at(m, 50, 0), at(sine, 50, 0) * 0.75, 1e-6), "mix with weights")
|
|
local m2 = AudioOps.mix({ c, sine }) :: Types.Audio
|
|
t:check(m2.frames == 8000, "mix is as long as the longest")
|
|
local fd = AudioOps.fade(sine, 0.5, 0.5) :: Types.Audio
|
|
t:check(at(fd, 0, 0) == 0 and near(at(fd, 2000, 0), at(sine, 2000, 0) * 0.5, 1e-6) and near(at(fd, 7999, 0), 0), "fades")
|
|
end
|
|
|
|
-- bad arguments: Err.ARG, never a throw
|
|
do
|
|
local junk: { any } = { nil, 5, "x", {}, { kind = "audio" }, Types.Image.new(2, 2) }
|
|
local calls = {
|
|
function(v: any) return AudioOps.gain(v, 2) end,
|
|
function(v: any) return AudioOps.remix(v, 2) end,
|
|
function(v: any) return AudioOps.resample(v, 44100) end,
|
|
function(v: any) return AudioOps.trim(v, 0) end,
|
|
function(v: any) return AudioOps.concat({ v }) end,
|
|
function(v: any) return AudioOps.mix(v) end,
|
|
function(v: any) return AudioOps.peak(v) end,
|
|
function(v: any) return AudioOps.fade(v, 1) end,
|
|
}
|
|
local ok = true
|
|
for _, fn in calls do
|
|
for i = 1, #junk + 1 do
|
|
local s, r, e = pcall(fn, junk[i])
|
|
ok = ok and s and r == nil and type(e) == "table" and e.code ~= nil
|
|
end
|
|
end
|
|
t:check(ok, "bad values return nil, Err")
|
|
t:check(AudioOps.gain(sine, 0 / 0) == nil, "NaN gain")
|
|
t:check(AudioOps.resample(sine, 0) == nil, "zero rate")
|
|
t:check(AudioOps.remix(sine, 0) == nil, "zero channels")
|
|
end
|
|
|
|
-- the F.ops facade
|
|
do
|
|
t:check(F.ops.audio == AudioOps, "F.ops.audio")
|
|
t:check(F.ops.image ~= nil and type(F.ops.image.resize) == "function", "F.ops.image")
|
|
t:check(type(F.ops.mesh.merge) == "function", "F.ops.mesh")
|
|
t:check(type(F.ops.layers.flatten) == "function", "F.ops.layers")
|
|
local v, err = F.load('<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8" fill="red"/></svg>')
|
|
t:check(v, "svg loads: " .. tostring(err))
|
|
local img, rerr = F.ops.vector.rasterize(v, 8, 8)
|
|
t:check(img and img.width == 8 and buffer.readu8(img.pixels, 0) == 255 and buffer.readu8(img.pixels, 1) == 0, "F.ops.vector.rasterize: " .. tostring(rerr))
|
|
t:check(F.ops.nothing == nil, "unknown ops: nil")
|
|
end
|
|
|
|
-- (no decoder here: the no-throw pass is the bad-arguments block above)
|
|
t:done()
|