From c0a30762b9781057c7e3959ad1b283699e89f536 Mon Sep 17 00:00:00 2001 From: Squatch Stack <321440025+squatchlr@users.noreply.github.com> Date: Fri, 9 Oct 2026 14:24:59 -0500 Subject: [PATCH] Add tools/compare_renders.py to compare two render builds For reviewing a speed-up: renders every model with a base and a candidate tools/render binary and prints EXACT, NEAR or DIFFERENT per model, with the first differing sample. NEAR means an error-to-signal ratio below -100 dB and no sample off by more than 1e-4, so one wrong sample in a long render is DIFFERENT; so is a NaN. - The test signal is generated at each model's own sample rate (48 kHz when a model declares none; a .wav impulse response at its file's rate), and its length is odd, so the last block of any power-of-two size is short. - Renders shorter than the input are DIFFERENT even when they agree. - Exits 0 if no model differs, 1 if one does, 2 if a render fails. - render writes output channel 0 only, so only that channel is compared. Standard library only; --self-test checks the comparison itself. --- tools/compare_renders.py | 260 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 tools/compare_renders.py diff --git a/tools/compare_renders.py b/tools/compare_renders.py new file mode 100644 index 00000000..25c3ca87 --- /dev/null +++ b/tools/compare_renders.py @@ -0,0 +1,260 @@ +#!/usr/bin/env python3 +"""Compare two builds of tools/render on the same models and audio, for reviewing a speed-up: + + python3 tools/compare_renders.py build-base/tools/render build-candidate/tools/render + python3 tools/compare_renders.py BASE CANDIDATE --models example_models/A2.nam my_ir.wav + +Every model (default: example_models/*.nam) is rendered by both binaries and reported as EXACT, NEAR (error-to-signal +ratio below -100 dB and no sample off by more than 1e-4) or DIFFERENT, with the first differing sample. Look at speed numbers only when every model is +EXACT or NEAR. Exits 0 if none is DIFFERENT, 1 if one is, 2 if a render fails. Standard library only; +--self-test checks the comparison itself. + +render writes output channel 0 only, so only that channel is compared. +""" + +import argparse +import json +import math +from pathlib import Path +import struct +import subprocess +import sys +import tempfile +import wave + +# Float32 differences between code paths measure -117 to -140 dB; real bugs are tens of dB. +NEAR_DB = -100.0 +# ...and no single sample may differ by more than this (full scale is 1.0). Float32 path differences measured here +# stay below 2e-5; a whole-file ratio alone would let one wrong sample in a long render through. +NEAR_MAX_ABS = 1e-4 + + +def read_float_wav(path): + blob = path.read_bytes() + if len(blob) < 12 or blob[:4] != b"RIFF" or blob[8:12] != b"WAVE": + raise ValueError(f"{path}: expected a RIFF/WAVE file") + fmt = audio = None + pos = 12 + while pos + 8 <= len(blob): + kind, size = struct.unpack_from("<4sI", blob, pos) + pos += 8 + if pos + size > len(blob): + raise ValueError(f"{path}: truncated WAV chunk") + if kind == b"fmt ": + fmt = blob[pos:pos + size] + elif kind == b"data": + audio = blob[pos:pos + size] + pos += size + (size & 1) + if fmt is None or audio is None or len(fmt) < 16: + raise ValueError(f"{path}: missing WAV format or audio data") + tag, channels, rate, _, align, bits = struct.unpack_from("= 40: + if fmt[24:40] == bytes.fromhex("0300000000001000800000aa00389b71"): + tag = 3 # WAVE_FORMAT_EXTENSIBLE with IEEE float subtype + if (tag, channels, bits, align) != (3, 1, 32, 4) or rate == 0: + raise ValueError(f"{path}: unsupported WAV format (need mono 32-bit IEEE float)") + if len(audio) % 4: + raise ValueError(f"{path}: incomplete float sample") + samples = [item[0] for item in struct.iter_unpack("= 8: + return struct.unpack_from(" 0 else 48000 + + +def write_test_input(path, rate): + samples = [] + duration = 0.5 + slope = math.log(20000 / 20) + for i in range(int(rate * duration)): + t = i / rate + phase = 2 * math.pi * 20 * duration * math.expm1(slope * t / duration) / slope + samples.append(int(round(0.8 * math.sin(phase) * 32767))) + seed = 0x12345678 + for _ in range(rate // 2): + seed = (1664525 * seed + 1013904223) & 0xffffffff + samples.append(int(((seed / 0xffffffff) * 2 - 1) * 16384)) + samples.extend([0] * (rate // 4)) + samples.extend(32767 if (i // 24) % 2 == 0 else -32768 for i in range(rate // 4)) + # An odd number of extra samples: no power-of-two block size above 1 divides the length, so the last block is short + samples.extend(int(16384 * math.sin(0.05 * i)) for i in range(37)) + with wave.open(str(path), "wb") as out: + out.setparams((1, 2, rate, 0, "NONE", "not compressed")) + out.writeframes(struct.pack(f"<{len(samples)}h", *samples)) + + +def write_float_wav(path, samples, rate=48000): + audio = struct.pack(f"<{len(samples)}f", *samples) + fmt = struct.pack(" 0.01: + raise AssertionError(f"incorrect ESR or first sample: {result}") + # One wrong sample in a long render, and a NaN, are DIFFERENT (the whole-file ratio alone would say NEAR). + long_base = [math.sin(0.01 * i) for i in range(60037)] + write_float_wav(base, long_base) + for i, bad in ((30000, long_base[30000] + 0.002), (17, float("nan"))): + write_float_wav(candidate, long_base[:i] + [bad] + long_base[i + 1:]) + result = compare(base, candidate) + if result["verdict"] != "DIFFERENT" or result["first_differing_sample"] != i: + raise AssertionError(f"one sample changed at {i} to {bad}: {result}") + # Equally empty or truncated renders are not EXACT. + write_float_wav(candidate, [1.0, 0.0]) + for frames in ([], [1.0]): + write_float_wav(base, frames) + write_float_wav(candidate, frames) + result = compare(base, candidate, expected_frames=2) + if result["verdict"] != "DIFFERENT": + raise AssertionError(f"{len(frames)}-sample renders of a 2-sample input: {result}") + # The generated input follows the model's rate; render refuses a mismatched one. + model = Path(directory) / "model.nam" + for written, expected in (({"sample_rate": 44100}, 44100), ({"sample_rate": 48000.0}, 48000), ({}, 48000), + ({"sample_rate": -1}, 48000)): + model.write_text(json.dumps(written)) + rate = model_sample_rate(model) + write_test_input(Path(directory) / "input.wav", rate) + with wave.open(str(Path(directory) / "input.wav"), "rb") as wav: + if (rate, wav.getframerate()) != (expected, expected): + raise AssertionError(f"{written}: model rate {rate}, input rate {wav.getframerate()}") + if wav.getnframes() % 2 == 0: + raise AssertionError(f"input length {wav.getnframes()} is even; the last block would not be short") + ir = Path(directory) / "ir.wav" + write_float_wav(ir, [1.0, 0.5], rate=44100) + if model_sample_rate(ir) != 44100: + raise AssertionError(f"a .wav model's rate: {model_sample_rate(ir)}") + + +def main(): + parser = argparse.ArgumentParser(description="Compare audio from two render builds") + parser.add_argument("base_render_bin", nargs="?", type=Path) + parser.add_argument("candidate_render_bin", nargs="?", type=Path) + parser.add_argument("--models", nargs="+", type=Path, + default=sorted((Path(__file__).resolve().parent.parent / "example_models").glob("*.nam")), + help="model files (.nam, or .wav impulse responses); default: example_models/*.nam") + parser.add_argument("--input", type=Path) + parser.add_argument("--json", action="store_true") + parser.add_argument("--self-test", action="store_true") + args = parser.parse_args() + if args.self_test: + self_test() + return 0 + if not args.base_render_bin or not args.candidate_render_bin: + parser.error("BASE_RENDER_BIN and CANDIDATE_RENDER_BIN are required") + if not args.models: + parser.error("no models found; pass --models") + results = [] + with tempfile.TemporaryDirectory() as directory: + temp = Path(directory) + for index, model in enumerate(args.models): + if args.input: + input_path = args.input.resolve() + else: + # The generated input is at the model's own rate, so a 44.1 kHz model is not fed 48 kHz audio. + rate = model_sample_rate(model) + input_path = temp / f"input-{rate}.wav" + if not input_path.exists(): + write_test_input(input_path, rate) + outputs = [temp / f"{index}-base.wav", temp / f"{index}-candidate.wav"] + for binary, output in zip((args.base_render_bin, args.candidate_render_bin), outputs): + run = subprocess.run([str(binary.resolve()), str(model.resolve()), + str(input_path), str(output)], capture_output=True, text=True) + if run.returncode: + raise RuntimeError(f"{binary} failed on {model}: {run.stderr.strip() or run.stdout.strip()}") + results.append({"model": str(model), **compare(*outputs, input_frames(input_path))}) + if args.json: + print(json.dumps(results, indent=2)) + else: + for result in results: + line = f"{result['model']}: {result['verdict']}" + if result.get("reason"): + line += f" ({result['reason']})" + elif not result["identical"]: + line += (f" max_abs_diff={result['max_abs_diff']:.9g}" + f" first_differing_sample={result['first_differing_sample']}" + f" ESR={result['esr_db']} dB") + print(line) + return int(any(result["verdict"] == "DIFFERENT" for result in results)) + + +if __name__ == "__main__": + try: + sys.exit(main()) + except (OSError, ValueError, RuntimeError, wave.Error) as error: + print(f"compare_renders: {error}", file=sys.stderr) + sys.exit(2)