ws-audio: audio/aiff (AIFF/AIFC incl. sowt/fl32/fl64/ulaw/alaw), audio/au, audio/voc: decode/open/encode; codecs/Pcm folder (+ Convert: raw sample formats <-> f32, G.711, rawDecoder/audioDecoder/encode); fixtures vs ffmpeg/sox, encoder outputs decoded by ffmpeg in the fixture step

This commit is contained in:
4DBug committed 2026-10-31 08:38:56 -05:00
1 parent 66df4a2254
commit 315e819b01
14 files changed
+1940 -6

No files matched your search

+80
View File
@@ -192,3 +192,83 @@ def luau(ctx, code):
return out[:-1] if out.endswith(b"\n") else out
finally:
os.unlink(path)
def emit_signal(ctx, name, samples, rate, ch):
"""emit tests/gen/<name>.luau = { rate, channels, frames, data = f32le bytes } (the input encoders are tested on)"""
ctx.emit(name, {"rate": rate, "channels": ch, "frames": len(samples) // ch, "data": f32le(samples)})
def encode_luau(ctx, module, sigmod, opts_lua="nil", source="audio"):
"""encode the emitted signal module `sigmod` with require("@formats/<module>").encode(audio, opts) in Luau; returns
the encoded bytes. source = "decoder" feeds a streaming Decoder (codecs/Pcm.audioDecoder) instead of the Audio."""
code = """
local S = require("@tests/gen/%s")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/Pcm")
local M = require("@formats/%s")
local a = Types.Audio.new(S.rate, S.channels, S.frames)
buffer.writestring(a.samples, 0, S.data)
local src: any = a
if "%s" == "decoder" then src = Pcm.audioDecoder(a) end
local out, err = M.encode(src, %s)
if not out then error("encode failed: " .. tostring(err)) end
print(buffer.tostring(out))
""" % (sigmod, module, source, opts_lua)
return luau(ctx, code)
def max_diff(a, b):
if len(a) != len(b):
return float("inf")
return max((abs(x - y) for x, y in zip(a, b)), default=0.0)
def pcm_fixtures(ctx, fmt, made, encodes, sig_rate=22050, sig_frames=3000):
"""The fixtures of an uncompressed container (aiff, au, voc):
made = [(name, packages, argv)]: reference tools write files from {in} (a stereo / mono 16-bit or float WAV of
the test signal; argv may use {in_mono}, {in_float}) into {out}; each file is decoded by ffmpeg as the
reference (f32le)
encodes = [(name, opts_lua, channels)]: our encoder's output (in the fixture step) decoded by ffmpeg, compared
against the f32 input (max abs difference, which the test bounds by the format's quantization step)
Emits <fmt>_cases (files + references), <fmt>_sig / <fmt>_sig1 (the inputs, stereo / mono) and <fmt>_enc."""
stereo = signal("music", sig_rate, 2, sig_frames, seed=3)
mono = signal("sweep", sig_rate, 1, sig_frames, seed=4)
emit_signal(ctx, fmt + "_sig", stereo, sig_rate, 2)
emit_signal(ctx, fmt + "_sig1", mono, sig_rate, 1)
inputs = {"{in}": wav(stereo, sig_rate, 2, 16), "{in_mono}": wav(mono, sig_rate, 1, 16),
"{in_float}": wav(stereo, sig_rate, 2, float_=True)}
cases = []
for name, packages, argv in made:
d = tempfile.mkdtemp(prefix="fmt-audio-")
try:
paths = {}
for key, data in inputs.items():
paths[key] = os.path.join(d, key.strip("{}") + ".wav")
with open(paths[key], "wb") as f:
f.write(data)
out = os.path.join(d, "out." + fmt)
args = []
for a in argv:
for key, p in paths.items():
a = a.replace(key, p)
args.append(a.replace("{out}", out))
ctx.nix(packages, args)
with open(out, "rb") as f:
data = f.read()
finally:
shutil.rmtree(d, ignore_errors=True)
rate, ch, ref = ffdecode(ctx, data, ext=fmt)
cases.append({"name": name, "data": data, "rate": rate, "channels": ch, "frames": len(ref) // ch, "ref": f32le(ref)})
ctx.emit(fmt + "_cases", cases)
results = []
for name, opts_lua, ch in encodes:
sig = stereo if ch == 2 else mono
out = encode_luau(ctx, "audio/" + fmt, fmt + ("_sig" if ch == 2 else "_sig1"), opts_lua)
streamed = encode_luau(ctx, "audio/" + fmt, fmt + ("_sig" if ch == 2 else "_sig1"), opts_lua, source="decoder")
rate, dch, dec = ffdecode(ctx, out, ext=fmt)
errs = tool(ctx, "ffmpeg", ["ffmpeg", "-v", "error", "-i", "{in}", "-f", "null", "-"], out, fmt)
results.append({"name": name, "opts": opts_lua, "channels": dch, "rate": rate, "frames": len(dec) // max(1, dch),
"maxDiff": max_diff(sig, dec) if dch == ch else 99.0, "ffmpegErrors": errs.decode(errors="replace"),
"bytes": len(out), "sameStreamed": out == streamed, "data": out})
ctx.emit(fmt + "_enc", results)