Files
rosetta/tools/fixtures/qoa.py
T

49 lines
2.4 KiB
Python

# QOA fixtures (tools/fixtures.py): audio/qoa and codecs/audio/Qoa. The reference is qoa.h itself
# (tests/fixtures/qoa/qoa.h, MIT, compiled here with nix gcc through tests/fixtures/qoa/qoa_ref.c) plus ffmpeg's decoder.
# tests/gen/qoa_cases.luau { name, data (qoa.h's encoding of the signal), rate, ch, ref (ffmpeg s16le),
# qoaref (qoa.h's decode, s16le), sig (the signal module name) }
# tests/gen/qoa_sig_*.luau the signals (our encoder must write the same bytes as qoa.h)
import importlib.util, os, shutil, struct, subprocess, tempfile
_dir = os.path.dirname(os.path.abspath(__file__))
def _load(name):
spec = importlib.util.spec_from_file_location(name, os.path.join(_dir, name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
A = _load("_audio")
D = _load("_adpcm")
INPUTS = ["tests/fixtures/qoa", "src/Rosetta/codecs/audio/Qoa"]
def generate(ctx):
work = tempfile.mkdtemp(prefix="fmt-qoa-")
try:
exe = os.path.join(work, "qoa_ref")
ctx.nix("gcc", ["gcc", "-O2", "-o", exe, os.path.join(ctx.root, "tests/fixtures/qoa/qoa_ref.c")])
cases = []
for name, kind, rate, ch, frames in (("stereo-44k", "music", 44100, 2, 12000), ("mono-22k", "sweep", 22050, 1, 5120),
("6ch-48k", "sine", 48000, 6, 700), ("loud-mono", "loud", 8000, 1, 5121),
("square-stereo", "square", 32000, 2, 6000)):
sig = A.signal(kind, rate, ch, frames, seed=len(cases) + 40)
mod = "qoa_sig_" + name.replace("-", "_")
A.emit_signal(ctx, mod, sig, rate, ch)
raw = os.path.join(work, "in.raw")
with open(raw, "wb") as f:
f.write(struct.pack("<%dh" % len(sig), *D.s16(sig)))
qoa = os.path.join(work, "out.qoa")
subprocess.run([exe, "enc", str(ch), str(rate), raw, qoa], check=True)
data = open(qoa, "rb").read()
dec = os.path.join(work, "dec.raw")
subprocess.run([exe, "dec", qoa, dec], check=True, capture_output=True)
ref = D.ffs16(ctx, data, "qoa")
cases.append(dict(name=name, data=data, rate=rate, ch=ch, ref=ref, qoaref=open(dec, "rb").read(), sig=mod))
ctx.emit("qoa_cases", cases)
finally:
shutil.rmtree(work, ignore_errors=True)