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)
+51
View File
@@ -0,0 +1,51 @@
# AIFF / AIFF-C fixtures (tools/fixtures.py): files written by ffmpeg and sox in every sample format we read, each
# with ffmpeg's f32 decode as the reference; our encoder's output for every sample format, decoded by ffmpeg in the
# fixture step (tools/fixtures/_audio.py pcm_fixtures).
# tests/gen/aiff_cases.luau { name, data, rate, channels, frames, ref (f32le) }
# tests/gen/aiff_enc.luau { name, opts, channels, rate, frames, maxDiff, ffmpegErrors, bytes, sameStreamed, data }
# tests/gen/aiff_sig*.luau the encoder inputs
import importlib.util, os
_spec = importlib.util.spec_from_file_location("_audio", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_audio.py"))
A = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(A)
INPUTS = ["src/Formats/audio/aiff", "src/Formats/codecs/Pcm"]
def ff(codec, src="{in}", extra=()):
return ["ffmpeg", "-v", "error", "-i", src] + list(extra) + ["-c:a", codec, "-f", "aiff", "-y", "{out}"]
def generate(ctx):
made = [
("ffmpeg-s16be", "ffmpeg", ff("pcm_s16be")),
("ffmpeg-s16be-mono", "ffmpeg", ff("pcm_s16be", "{in_mono}")),
("ffmpeg-s8", "ffmpeg", ff("pcm_s8")),
("ffmpeg-s24be", "ffmpeg", ff("pcm_s24be", "{in_float}")),
("ffmpeg-s32be", "ffmpeg", ff("pcm_s32be", "{in_float}")),
("ffmpeg-sowt", "ffmpeg", ff("pcm_s16le")),
("ffmpeg-fl32", "ffmpeg", ff("pcm_f32be", "{in_float}")),
("ffmpeg-fl64", "ffmpeg", ff("pcm_f64be", "{in_float}")),
("ffmpeg-ulaw", "ffmpeg", ff("pcm_mulaw")),
("ffmpeg-alaw", "ffmpeg", ff("pcm_alaw")),
("ffmpeg-48k-6ch", "ffmpeg", ["ffmpeg", "-v", "error", "-i", "{in}", "-af", "aresample=48000,pan=5.1|c0=c0|c1=c1|c2=c0|c3=c1|c4=c0|c5=c1", "-c:a", "pcm_s16be", "-f", "aiff", "-y", "{out}"]),
("ffmpeg-tags", "ffmpeg", ff("pcm_s16be", extra=("-metadata", "title=Song Name", "-metadata", "author=Some One", "-metadata", "comment=hello"))),
("sox-aiff-s16", "sox", ["sox", "{in}", "-t", "aiff", "{out}"]),
("sox-aiff-s24-mono", "sox", ["sox", "{in_mono}", "-b", "24", "-t", "aiff", "{out}"]),
("sox-aifc-s16", "sox", ["sox", "{in}", "-t", "aifc", "{out}"]),
]
encodes = [
("s16", "nil", 2),
("s16-mono", "nil", 1),
("s8", '{ sampleFormat = "s8" }', 2),
("s24", '{ sampleFormat = "s24" }', 2),
("s32", '{ sampleFormat = "s32" }', 1),
("f32", '{ sampleFormat = "f32" }', 2),
("f64", '{ sampleFormat = "f64" }', 2),
("ulaw", '{ sampleFormat = "ulaw" }', 2),
("alaw", '{ sampleFormat = "alaw" }', 1),
("sowt16", '{ littleEndian = true }', 2),
("aifc-s16", '{ aifc = true }', 2),
]
A.pcm_fixtures(ctx, "aiff", made, encodes)
+47
View File
@@ -0,0 +1,47 @@
# Sun AU fixtures (tools/fixtures.py): files written by ffmpeg and sox in every encoding we read, each with ffmpeg's
# f32 decode as the reference; our encoder's output for every encoding, decoded by ffmpeg in the fixture step
# (tools/fixtures/_audio.py pcm_fixtures).
# tests/gen/au_cases.luau { name, data, rate, channels, frames, ref (f32le) }
# tests/gen/au_enc.luau { name, opts, channels, rate, frames, maxDiff, ffmpegErrors, bytes, sameStreamed, data }
# tests/gen/au_sig*.luau the encoder inputs
import importlib.util, os
_spec = importlib.util.spec_from_file_location("_audio", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_audio.py"))
A = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(A)
INPUTS = ["src/Formats/audio/au", "src/Formats/codecs/Pcm"]
def ff(codec, src="{in}", extra=()):
return ["ffmpeg", "-v", "error", "-i", src] + list(extra) + ["-c:a", codec, "-f", "au", "-y", "{out}"]
def generate(ctx):
made = [
("ffmpeg-s16be", "ffmpeg", ff("pcm_s16be")),
("ffmpeg-s16be-mono", "ffmpeg", ff("pcm_s16be", "{in_mono}")),
("ffmpeg-s8", "ffmpeg", ff("pcm_s8")),
("ffmpeg-s24be", "ffmpeg", ff("pcm_s24be", "{in_float}")),
("ffmpeg-s32be", "ffmpeg", ff("pcm_s32be", "{in_float}")),
("ffmpeg-f32be", "ffmpeg", ff("pcm_f32be", "{in_float}")),
("ffmpeg-f64be", "ffmpeg", ff("pcm_f64be", "{in_float}")),
("ffmpeg-ulaw", "ffmpeg", ff("pcm_mulaw")),
("ffmpeg-alaw", "ffmpeg", ff("pcm_alaw")),
("sox-comment", "sox", ["sox", "{in}", "--comment", "hello sun", "-t", "au", "{out}"]),
("sox-ulaw-8k", "sox", ["sox", "{in_mono}", "-r", "8000", "-e", "mu-law", "-t", "au", "{out}"]),
("sox-s16", "sox", ["sox", "{in}", "-t", "au", "{out}"]),
("sox-f32", "sox", ["sox", "{in_float}", "-e", "floating-point", "-b", "32", "-t", "au", "{out}"]),
]
encodes = [
("s16", "nil", 2),
("s16-mono", "nil", 1),
("s8", '{ sampleFormat = "s8" }', 2),
("s24", '{ sampleFormat = "s24" }', 2),
("s32", '{ sampleFormat = "s32" }', 1),
("f32", '{ sampleFormat = "f32" }', 2),
("f64", '{ sampleFormat = "f64" }', 2),
("ulaw", '{ sampleFormat = "ulaw" }', 2),
("alaw", '{ sampleFormat = "alaw" }', 1),
]
A.pcm_fixtures(ctx, "au", made, encodes)
+39
View File
@@ -0,0 +1,39 @@
# Creative VOC fixtures (tools/fixtures.py): files written by ffmpeg (type 1 + type 8 blocks for 8-bit, type 9 for the
# rest) and sox, each with ffmpeg's f32 decode as the reference; our encoder's output for every sample format, decoded
# by ffmpeg in the fixture step (tools/fixtures/_audio.py pcm_fixtures).
# tests/gen/voc_cases.luau { name, data, rate, channels, frames, ref (f32le) }
# tests/gen/voc_enc.luau { name, opts, channels, rate, frames, maxDiff, ffmpegErrors, bytes, sameStreamed, data }
# tests/gen/voc_sig*.luau the encoder inputs
import importlib.util, os
_spec = importlib.util.spec_from_file_location("_audio", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_audio.py"))
A = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(A)
INPUTS = ["src/Formats/audio/voc", "src/Formats/codecs/Pcm"]
def ff(codec, src="{in}", extra=()):
return ["ffmpeg", "-v", "error", "-i", src] + list(extra) + ["-c:a", codec, "-f", "voc", "-y", "{out}"]
def generate(ctx):
made = [
("ffmpeg-u8-stereo", "ffmpeg", ff("pcm_u8")),
("ffmpeg-u8-mono", "ffmpeg", ff("pcm_u8", "{in_mono}")),
("ffmpeg-s16", "ffmpeg", ff("pcm_s16le")),
("ffmpeg-s16-mono-44k", "ffmpeg", ff("pcm_s16le", "{in_mono}", ("-ar", "44100"))),
("ffmpeg-alaw", "ffmpeg", ff("pcm_alaw")),
("ffmpeg-ulaw", "ffmpeg", ff("pcm_mulaw", "{in_mono}")),
("sox-u8-mono-11k", "sox", ["sox", "{in_mono}", "-r", "11025", "-b", "8", "-t", "voc", "{out}"]),
("sox-u8-stereo", "sox", ["sox", "{in}", "-b", "8", "-t", "voc", "{out}"]),
("sox-s16-stereo", "sox", ["sox", "{in}", "-b", "16", "-t", "voc", "{out}"]),
]
encodes = [
("s16", "nil", 2),
("s16-mono", "nil", 1),
("u8", '{ sampleFormat = "u8" }', 2),
("alaw", '{ sampleFormat = "alaw" }', 2),
("ulaw", '{ sampleFormat = "ulaw" }', 1),
]
A.pcm_fixtures(ctx, "voc", made, encodes)