"""Check tools/blastem_audio.py against a VGM whose faults are known. Builds a synthetic stream and a synthetic log of it carrying one stale-page read, one stale-bank read and one stall, then asserts the analyser recovers each one's position, length and origin. Keeps the VGM parse and the lag attribution honest without needing BlastEm or the real soundtrack: run it after touching either. python tools/blastem_audio_test.py """ from __future__ import annotations import struct import sys import tempfile from pathlib import Path import numpy as np sys.path.insert(0, str(Path(__file__).resolve().parent)) from blastem_audio import BANK, PAGE, SAMPLE_RATE, VGM_RATE, faults, lag_timeline, read_vgm PAGE_FAULT_AT, PAGE_FAULT_LEN = PAGE, 30_000 BANK_FAULT_AT, BANK_FAULT_LEN = 900_000, 600 STALL_AT, STALL_VGM_SAMPLES = 700_000, 265 N = 1_200_000 def make_source(n: int) -> np.ndarray: """A random walk: no long runs of equal bytes, so every lag is unambiguous.""" rng = np.random.default_rng(11) walk = np.cumsum(rng.integers(-9, 10, n + PAGE)) return (np.abs(walk) % 256).astype(np.uint8) def make_vgm(played: np.ndarray, stall_at: int, stall_len: int, path: Path) -> None: body = bytearray() acc = 0.0 step = VGM_RATE / SAMPLE_RATE for i, v in enumerate(played): body += bytes((0x52, 0x2A, int(v))) acc += step wait = int(acc) acc -= wait if i == stall_at: wait += stall_len while wait > 0: if wait <= 16: body += bytes((0x70 + wait - 1,)) wait = 0 else: chunk = min(wait, 65535) body += bytes((0x61,)) + struct.pack(" int: source = make_source(N) played = source[:N].copy() played[PAGE_FAULT_AT : PAGE_FAULT_AT + PAGE_FAULT_LEN] = \ source[PAGE_FAULT_AT - PAGE : PAGE_FAULT_AT - PAGE + PAGE_FAULT_LEN] played[BANK_FAULT_AT : BANK_FAULT_AT + BANK_FAULT_LEN] = \ source[BANK_FAULT_AT - BANK : BANK_FAULT_AT - BANK + BANK_FAULT_LEN] with tempfile.TemporaryDirectory() as tmp: vgm = Path(tmp) / "test.vgm" make_vgm(played, STALL_AT, STALL_VGM_SAMPLES, vgm) dac, times, info = read_vgm(vgm) assert len(dac) == N, f"parsed {len(dac)} DAC writes, expected {N}" assert (dac == played).all(), "parsed DAC values differ from what was written" print(f"VGM parse: {len(dac):,} writes, values exact, {info['vgm_seconds']:.1f} s") window = 4096 segments = lag_timeline(dac, source, 0, window, 0.9, True) found = faults(dac, source, 0, segments, 0.9, 8, 16, window) for at, length, lag in ((PAGE_FAULT_AT, PAGE_FAULT_LEN, -PAGE), (BANK_FAULT_AT, BANK_FAULT_LEN, -BANK)): hits = [f for f in found if f[2] == lag and abs(f[0] - at) <= 8] assert hits, f"no fault with lag {lag:+,} near sample {at:,}; got {found}" s0, n, _, frac = hits[0] assert abs(n - length) <= 2 * 8, f"fault at {at:,}: length {n}, expected {length}" assert frac > 0.95, f"fault at {at:,}: only {100 * frac:.0f}% explained by lag {lag:+,}" print(f"fault at {s0:,} ({s0 / SAMPLE_RATE:.3f} s): {n:,} samples from lag {lag:+,}, " f"{100 * frac:.0f}% explained") assert len(found) == 2, f"expected exactly 2 faults, got {len(found)}: {found}" gaps = np.diff(times) stalls = np.flatnonzero(gaps > 4 * VGM_RATE / SAMPLE_RATE) assert len(stalls) == 1, f"expected 1 stall, got {len(stalls)}" assert stalls[0] == STALL_AT, f"stall at write {stalls[0]}, expected {STALL_AT}" print(f"stall at {STALL_AT:,} ({STALL_AT / SAMPLE_RATE:.3f} s): " f"{1000 * gaps[stalls[0]] / VGM_RATE:.2f} ms gap") print("PASS") return 0 if __name__ == "__main__": raise SystemExit(main())