Files
rosetta/tools/fixtures/adx.py
T

70 lines
2.9 KiB
Python

# CRI ADX fixtures (tools/fixtures.py): audio/adx. vgmstream is the reference decoder (CRI's scale + 1 and version 3
# rounding; ffmpeg's ADX decoder differs a little on both).
# tests/gen/adx_cases.luau { name, data, rate, ch, frames, ref (vgmstream s16le), loopStart?, loopEnd? }
# ffmpeg -f adx files (version 3, mono / stereo), and a version 4 file rebuilt around ffmpeg's frames with an
# initial history and loop points (vgmstream -m reports the loop)
# tests/gen/adx_ours.luau our encoder's files (run in this step) with vgmstream's decode and loop report
# tests/gen/adx_sig.luau the encoder input
import importlib.util, os, re, struct
_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 = ["tools/fixtures/_adpcm.py", "src/Rosetta/codecs/audio/Adpcm", "src/Rosetta/audio/adx"]
def loops(info):
m = re.search(r"loop start: (\d+) samples.*?loop end: (\d+) samples", info, re.S)
return (int(m.group(1)), int(m.group(2))) if m else None
def case(ctx, name, data):
rate, ch, ref = D.vgdecode(ctx, data, "adx")
e = dict(name=name, data=data, rate=rate, ch=ch, frames=len(ref) // (2 * ch), ref=ref)
lp = loops(D.vginfo(ctx, data, "adx"))
if lp:
e["loopStart"], e["loopEnd"] = lp
return e
def generate(ctx):
cases = []
for name, rate, ch, kind in (("ffmpeg-mono-22k", 22050, 1, "sweep"), ("ffmpeg-stereo-44k", 44100, 2, "music"),
("ffmpeg-stereo-32k", 32000, 2, "sine")):
sig = A.signal(kind, rate, ch, 7000, seed=ch)
f = D.ffencode(ctx, A.wav(sig, rate, ch), ["-c:a", "adpcm_adx", "-f", "adx"], "adx")
cases.append(case(ctx, name, f))
# version 4 with history and loops, around the stereo frames
f = cases[1]["data"]
start = struct.unpack_from(">H", f, 2)[0] + 4
frames_ = f[start:]
rate, n = struct.unpack_from(">II", f, 8)
head = bytearray(f[:0x14])
struct.pack_into(">H", head, 0x12, 0x0400)
head += b"\0" * 4 + struct.pack(">hhhh", 300, -200, -150, 90) # 0x14 flags, 0x18 history (2 channels)
head += struct.pack(">HHIIIII", 0, 1, 1, 1024, 0, 6000, 0)
newstart = len(head) + 6
struct.pack_into(">H", head, 2, newstart - 4)
cases.append(case(ctx, "v4-hist-loop", bytes(head) + b"(c)CRI" + frames_))
ctx.emit("adx_cases", cases)
sig = A.signal("music", 32000, 2, 9000, seed=11)
A.emit_signal(ctx, "adx_sig", sig, 32000, 2)
ours = []
for name, opts in (("plain", "nil"), ("loop", "{ loop = { start = 1000, stop = 8000 } }")):
data = A.encode_luau(ctx, "audio/adx", "adx_sig", opts)
e = case(ctx, name, data)
e["opts"] = opts
ours.append(e)
ctx.emit("adx_ours", ours)