"""Compare two mono WAV renderings window by window (the board's against MAME's recording, as a detector: outputs only). python scripts/compare_audio.py OURS.wav THEIRS.wav [--window 735] [--tones 661.9,525.2,391.1] Per window (default 1/60 s at 44,100 Hz): the AC RMS of each, whether each is sounding (RMS above 1 % of the louder file's peak window), and the Goertzel magnitude at each listed frequency. Prints: windows compared, windows where both agree on sounding/silent, the correlation of the two RMS envelopes, and, over windows where both sound, how often the strongest listed tone is the same. """ from __future__ import annotations import argparse import math import struct import sys import wave def read(path: str) -> tuple[int, list[float]]: with wave.open(path) as w: if w.getnchannels() != 1 or w.getsampwidth() != 2: raise SystemExit(f"{path}: need 16-bit mono") data = w.readframes(w.getnframes()) return w.getframerate(), [v / 32768 for (v,) in struct.iter_unpack(" float: k = 2 * math.cos(2 * math.pi * freq / rate) s1 = s2 = 0.0 for v in x: s1, s2 = v + k * s1 - s2, s1 return math.sqrt(max(0.0, s1 * s1 + s2 * s2 - k * s1 * s2)) / len(x) def rms(x: list[float]) -> float: mean = sum(x) / len(x) return math.sqrt(sum((v - mean) ** 2 for v in x) / len(x)) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("ours") parser.add_argument("theirs") parser.add_argument("--window", type=int, default=735) parser.add_argument("--tones", default="") args = parser.parse_args() rate_a, a = read(args.ours) rate_b, b = read(args.theirs) if rate_a != rate_b: raise SystemExit("sample rates differ") n = min(len(a), len(b)) // args.window tones = [float(t) for t in args.tones.split(",") if t] ra = [rms(a[i * args.window : (i + 1) * args.window]) for i in range(n)] rb = [rms(b[i * args.window : (i + 1) * args.window]) for i in range(n)] ta, tb = max(ra) * 0.01, max(rb) * 0.01 agree = sum((x > ta) == (y > tb) for x, y in zip(ra, rb, strict=True)) both = [i for i in range(n) if ra[i] > ta and rb[i] > tb] ma, mb = sum(ra) / n, sum(rb) / n cov = sum((x - ma) * (y - mb) for x, y in zip(ra, rb, strict=True)) var = math.sqrt(sum((x - ma) ** 2 for x in ra) * sum((y - mb) ** 2 for y in rb)) print( f"{n} windows of {args.window} samples; sounding in ours {sum(x > ta for x in ra)}, " f"theirs {sum(y > tb for y in rb)}; agree on sounding/silent {agree} of {n}; " f"RMS envelope correlation {cov / var if var else 0.0:.4f}" ) if tones and both: same = 0 for i in both: wa = a[i * args.window : (i + 1) * args.window] wb = b[i * args.window : (i + 1) * args.window] pa = max(tones, key=lambda f: goertzel(wa, f, rate_a)) pb = max(tones, key=lambda f: goertzel(wb, f, rate_b)) same += pa == pb print(f"strongest of {tones} Hz the same in {same} of {len(both)} windows where both sound") if both: # Spectral shape: Goertzel magnitudes on 48 log-spaced frequencies, 100-8,000 Hz, # compared by cosine similarity per window where both sound. grid = [100 * (80 ** (k / 47)) for k in range(48)] sims = [] for i in both: wa = a[i * args.window : (i + 1) * args.window] wb = b[i * args.window : (i + 1) * args.window] va = [goertzel(wa, f, rate_a) for f in grid] vb = [goertzel(wb, f, rate_b) for f in grid] dot = sum(x * y for x, y in zip(va, vb, strict=True)) norm = math.sqrt(sum(x * x for x in va) * sum(y * y for y in vb)) sims.append(dot / norm if norm else 0.0) sims.sort() print( f"spectral cosine similarity over {len(sims)} windows: median {sims[len(sims) // 2]:.3f}, " f"10th percentile {sims[len(sims) // 10]:.3f}, minimum {sims[0]:.3f}" ) return 0 if __name__ == "__main__": sys.exit(main())