"""Score a megadrive-python DAC dump the way tools/blastem_audio.py scores a VGM. check_audio says how *much* of a run is wrong, within a jitter tolerance, and reports drift. It does not say where a fault started or what the driver played instead, so a "+8 samples of drift" verdict leaves the cause open. The lag analysis built for BlastEm answers both, and the fault it names - a run of samples explained by a small positive lag - is the signature of the ring's underrun guard rewinding and replaying a group, which is invisible to an interval histogram. /python tools/dump_dac.py ROM 1600 out/dac python tools/oracle_audio.py out/dac.u8 out/full """ from __future__ import annotations import argparse import struct import sys from pathlib import Path import numpy as np ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tools")) from blastem_audio import ( # noqa: E402 PAGE, SAMPLE_RATE, describe, faults, find_base, lag_timeline, place, ) def main(argv: list[str] | None = None) -> int: ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("dump", type=Path, help="out/dac.u8 from tools/dump_dac.py") ap.add_argument("out", type=Path, help="build directory holding audio.u8") ap.add_argument("--audio-page", type=int, default=21) ap.add_argument("--window", type=int, default=4096) ap.add_argument("--clean", type=float, default=0.9) ap.add_argument("--min-run", type=int, default=4) ap.add_argument("--merge-gap", type=int, default=16) ap.add_argument("--top", type=int, default=20) args = ap.parse_args(argv) dac = np.frombuffer(args.dump.read_bytes(), dtype=np.uint8) source = np.frombuffer((args.out / "audio.u8").read_bytes(), dtype=np.uint8) print(f"{args.dump.name}: {len(dac):,} DAC writes ({len(dac) / SAMPLE_RATE:.2f} s)") print(f"stream: {len(source):,} samples") if not len(dac): raise SystemExit("empty dump") base, agree, votes = find_base(dac, source, args.window) print(f"dump starts at stream sample {base:,}, agreed by {agree} of {votes} probe windows") segments = lag_timeline(dac, source, base, args.window, args.clean, True) found = faults(dac, source, base, segments, args.clean, args.min_run, args.merge_gap, args.window) bad = sum(n for _, n, _, _ in found) print(f"\n{len(found)} faults, {bad:,} samples ({1000 * bad / SAMPLE_RATE:.1f} ms) of wrong data " f"out of {len(dac):,} ({100 * bad / len(dac):.3f}%)") if not found: print("every sample played in order from the right place: PASS") return 0 print(f"\n{'start':>11} {'length':>9} {'came from':>22} match where it started") for s0, n, lag, f in sorted(found, key=lambda r: -r[1])[: args.top]: print(f" {(base + s0) / SAMPLE_RATE:9.3f}s {1000 * n / SAMPLE_RATE:7.1f}ms " f"{describe(lag):>22} {100 * f:3.0f}% {place(base + s0, args.audio_page)}") # A positive lag that persists is data replayed: the ring underran and the i4 guard # rewound. A page-multiple lag is a stale window. Separating them names the cause. # A negative lag means the data played is *behind* the stream: samples were # inserted or replayed, so everything after runs late. A positive lag means data # ahead of the stream: samples were dropped. A page multiple is a stale window. stale = [r for r in found if r[2] and r[2] % PAGE == 0] replay = [r for r in found if r not in stale and -1024 <= r[2] < 0] dropped = [r for r in found if r not in stale and 0 < r[2] <= 1024] print(f"\nby kind: {len(replay)} replayed or spliced in (negative lag: the stream " f"runs late from here), {len(dropped)} dropped (positive lag), " f"{len(stale)} stale page, {len(found) - len(replay) - len(dropped) - len(stale)} other") near_page = [r for r in found if (base + r[0]) % PAGE < PAGE // 8] print(f"{len(near_page)} of {len(found)} faults start in the first eighth of a 512 KB page " f"(the crossings, where the gate is shut waiting for the 68000)") return 1 if __name__ == "__main__": raise SystemExit(main())