Files
rosetta/tools/fixtures/opus_celt.py
T

49 lines
2.7 KiB
Python

# The CELT encoder reference check (tools/fixtures.py): runs tools/fixtures/_opus_encode.luau (our encoder over
# tests/audio/_opus_cases.luau), then the reference tools through nix: opusdec --float (libopus), opusinfo (stream
# structure), ffmpeg (an independent Ogg / Opus reader).
# tests/gen/opus_celt.luau { { name, crc (of our bytes), opusdec (f32le 48 kHz decode), opusinfo (exit 0),
# ffmpeg (decoded), ffmpegFrames, comments (opusinfo's user comments) } }
import os, re, shutil, subprocess, tempfile, zlib
INPUTS = ["src/Formats/audio/opus", "src/Formats/codecs/Mdct", "src/Formats/containers/Ogg",
"tests/audio/_opus_cases.luau", "tools/fixtures/_opus_encode.luau"]
def generate(ctx):
luau = os.environ.get("LUAU") or shutil.which("luau")
script = os.path.join(ctx.root, "tools/fixtures/_opus_encode.luau")
if luau:
out = subprocess.run([luau, script], capture_output=True, check=True, cwd=ctx.root).stdout
else:
out = ctx.nix("luau", ["luau", script])
d = tempfile.mkdtemp(prefix="opus-celt-fx-")
try:
cases = []
for line in out.decode().split("\n"):
if not line.strip():
continue
name, hx = line.split(" ", 1)
if hx.startswith("ERROR"):
raise RuntimeError("opus_celt fixtures: our encoder failed on %s: %s" % (name, hx))
data = bytes.fromhex(hx)
path = os.path.join(d, name + ".opus")
with open(path, "wb") as f:
f.write(data)
raw = os.path.join(d, name + ".f32")
ctx.nix("opus-tools", ["opusdec", "--quiet", "--float", "--rate", "48000", path, raw])
with open(raw, "rb") as f:
pcm = f.read()
info = subprocess.run(["nix", "shell", "nixpkgs#opus-tools", "-c", "opusinfo", path], capture_output=True)
text = info.stdout.decode(errors="replace")
comments = {}
for m in re.finditer(r"^\s*([A-Za-z]+)=(.*)$", text, re.M):
comments[m.group(1).lower()] = m.group(2)
ok = info.returncode == 0 and "WARNING" not in text and "ERROR" not in text.upper().replace("ERRORS", "")
ff = subprocess.run(["nix", "shell", "nixpkgs#ffmpeg", "-c", "ffmpeg", "-v", "error", "-i", path, "-f", "f32le", "-ac", "1", "-"], capture_output=True)
cases.append({"name": name, "crc": zlib.crc32(data), "opusdec": pcm, "opusinfo": ok,
"ffmpeg": ff.returncode == 0 and len(ff.stderr) == 0, "ffmpegFrames": len(ff.stdout) // 4,
"comments": comments})
ctx.emit("opus_celt", cases)
finally:
shutil.rmtree(d, ignore_errors=True)