"""Converts the captured references into flat binary files the C tests read. Keeping the test fixtures as plain little-endian float32 means the C side needs no parser and no dependency, and the conversion is the only place the two representations have to agree. python tools/make_golden.py chunks golden.npz build/golden.bin python tools/make_golden.py slide audio.wav build/slide_golden.bin """ import argparse import struct import sys import numpy as np from model import NUM_CLASSES, NUM_FRAMES, NUM_SAMPLES, SAMPLE_RATE MAGIC_CHUNKS = b"PSGG" MAGIC_SLIDE = b"PSGS" def chunks(args) -> int: """Per-window fixtures: waveform in, class probabilities out. The torch reference emits log probabilities. They are exponentiated here because that is what the engine returns, and because a difference of 2e-3 in a log probability of -17 is a difference of 4e-8 in the probability itself: comparing in log space would set the tolerance by the least significant outputs rather than the ones that decide a frame. """ data = np.load(args.npz) inputs = data["inputs"].astype(np.float32) probs = np.exp(data["outputs"].astype(np.float64)).astype(np.float32) n = inputs.shape[0] assert inputs.shape == (n, NUM_SAMPLES) assert probs.shape == (n, NUM_FRAMES, NUM_CLASSES) with open(args.out, "wb") as f: f.write(struct.pack("<4sIII", MAGIC_CHUNKS, n, NUM_SAMPLES, NUM_FRAMES)) f.write(inputs.tobytes()) f.write(np.ascontiguousarray(probs).tobytes()) print(f"wrote {args.out}: {n} windows") return 0 def slide(args) -> int: """Whole-recording fixture: the aggregated tracks pyannote's Inference produces, next to the samples that produced them.""" import soundfile as sf from pipeline import aggregate_tracks wav, rate = sf.read(args.audio, dtype="float32", always_2d=True) if rate != SAMPLE_RATE: raise SystemExit(f"{args.audio}: expected {SAMPLE_RATE} Hz, got {rate}") samples = np.ascontiguousarray(wav[: args.limit, 0]) tracks = aggregate_tracks(samples, step=args.step) with open(args.out, "wb") as f: f.write( struct.pack( "<4sIId", MAGIC_SLIDE, len(samples), tracks.shape[0], args.step ) ) f.write(samples.tobytes()) f.write(np.ascontiguousarray(tracks, dtype=np.float32).tobytes()) print(f"wrote {args.out}: {len(samples)} samples, {tracks.shape[0]} frames") return 0 def main() -> int: ap = argparse.ArgumentParser() sub = ap.add_subparsers(dest="cmd", required=True) c = sub.add_parser("chunks") c.add_argument("npz") c.add_argument("out") c.set_defaults(func=chunks) s = sub.add_parser("slide") s.add_argument("audio") s.add_argument("out") s.add_argument("--step", type=float, default=1.0) s.add_argument("--limit", type=int, default=45 * SAMPLE_RATE) s.set_defaults(func=slide) args = ap.parse_args() return args.func(args) if __name__ == "__main__": sys.exit(main())