#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run the snes_spc referee (closed box) over .spc files and summarise each run. The referee is ``$EMU_TOOLS_GPL/tools/snes-spc/build/spc_dump``, built by that tool's ``build.sh`` (emu-tools contract.md, section 5); without it this exits with status 3. python -I scripts/dsp_make_spc.py reference/dsp/spc/synthetic python -I scripts/dsp_run_referee.py [--samples N] [--regs] [SPC ...] With no SPC arguments it runs every file in reference/dsp/spc/synthetic/. For each it prints the SHA-256 of the raw output (int16 LE, L/R interleaved, 32 kHz), the number of non-zero frames and the peak; with ``--regs`` also the SHA-256 of the per-sample DSP register dump. A second run of each file must give the same hash (the referee is deterministic). Outputs land in reference/dsp/runs/ (ignored). Every number printed is OBSERVED from snes_spc 0.9.0 run as a closed box. """ from __future__ import annotations import argparse import hashlib import struct import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) # run with -I: the script directory is not on the path import emu_tools #: The referee, ``$EMU_TOOLS_GPL/tools/snes-spc/build/spc_dump`` (None when not built). REFEREE = emu_tools.find(emu_tools.SPC_DUMP) SYNTHETIC = ROOT / "reference" / "dsp" / "spc" / "synthetic" RUNS = ROOT / "reference" / "dsp" / "runs" def run(spc: Path, samples: int, tag: str, regs: bool) -> tuple[bytes, bytes]: raw = RUNS / f"{spc.stem}.{tag}.raw" command = [str(REFEREE), str(spc), str(samples), str(raw)] regs_path = RUNS / f"{spc.stem}.{tag}.regs" if regs: command += ["--regs", str(regs_path)] subprocess.run(command, check=True) return raw.read_bytes(), regs_path.read_bytes() if regs else b"" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument( "--samples", type=int, default=64000, help="32 kHz frames per run (default 2 s)" ) parser.add_argument("--regs", action="store_true", help="also dump DSP registers per sample") parser.add_argument("spc", nargs="*", type=Path) args = parser.parse_args() emu_tools.require(emu_tools.SPC_DUMP) # exit 3, naming EMU_TOOLS_GPL and build.sh files = args.spc or sorted(SYNTHETIC.glob("*.spc")) RUNS.mkdir(parents=True, exist_ok=True) status = 0 for spc in files: data, regs = run(spc, args.samples, "a", args.regs) again, regs_again = run(spc, args.samples, "b", args.regs) frames = struct.unpack(f"<{len(data) // 2}h", data) pairs = list(zip(frames[0::2], frames[1::2])) nonzero = sum(1 for left, right in pairs if left or right) peak = max((max(abs(left), abs(right)) for left, right in pairs), default=0) same = again == data and regs_again == regs status |= not same line = ( f"{spc.name:20s} sha256 {hashlib.sha256(data).hexdigest()[:16]} frames {len(pairs)}" f" non-zero {nonzero} peak {peak}" ) if args.regs: line += f" regs {hashlib.sha256(regs).hexdigest()[:16]}" print(line + (" deterministic" if same else " NOT DETERMINISTIC")) return status if __name__ == "__main__": sys.exit(main())