Repository navigation
Sharper matching engine: GCC-PHAT + margin metrics - #4
Merged
Merged
Conversation
…ench Tier 1 of the matching-engine sharpening plan (.claude/plans/bitte-untersuche-mal-wie-enumerated-boot.md): - **Margin-of-Victory metrics** (peak_to_second_ratio, peak_to_noise) — surface how clearly the primary peak stands above the runner-up and the noise floor of the correlation surface. The reported chroma NCC alone saturates at ~1.0 on real music even when the runner-up is a hair behind, hiding ambiguity from the UI; PSR/PNR expose it. - **Parabolic sub-sample peak refinement** — collapses the 23 ms hop quantization on off-grid offsets (residual <12 ms vs. ~12 ms half-hop before). Helps the alternates list and short clips that don't trigger drift refinement; a no-op on hop-aligned synthetic offsets. - **Adaptive min-overlap floor** — ≥1 s of joint-active overlap required in addition to the 20 %-of-shorter rule, capped at 80 % of the smaller signal so sub-second queries still resolve. - **Real-music bench** — yt-dlp + ffmpeg pipeline that fetches a curated list of CC-licensed clips across 7 genres (classical, jazz, rock, hip-hop, ambient, folk, electronic) into a gitignored cache, then runs the same offset/recall scenarios on real spectral content. Catches the "1 % margin between top-1 and top-2" symptom that synthetic pentatonic signals were too uniform to expose. Tier 1.1 (onset multiplicative-fusion in score) was tried and reverted in this commit's scope: on the real-music bench it slightly *reduced* mean PSR (1.30 → 1.23) because rhythmic material has near-uniform onset Pearson at every beat-aligned lag — the multiplier rewarded false alignments almost as strongly as the true one. The right fix for the two failing real-music cases (hiphop / jazz +2000 ms beat-aligned impostors) is GCC-PHAT (Tier 2), not chroma+onset reweighting. Bench (real music, 7 clips × 7 scenarios = 49 runs): Top-1 pass: 47/49 (96 %) — unchanged Recall@K: 47/49 (96 %) — unchanged Mean abs err: 10.83 ms → 10.72 ms Mean PSR: 1.30× (now exposed via the new metric) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Introduces a phase-only cross-correlation stage between the existing
chroma+onset/DTW block and the drift refinement, plus a sanity-fence
that protects PHAT's sample-precise lag from being walked off-target
by drift's per-window xcorr fit.
The classic Knapp-Carter Generalized Cross-Correlation with Phase
Transform (1976) — spectral whitening turns the cross-spectrum into
phase-only information, which gives a delta-function-like peak at the
true sample lag for two recordings of the same source even through
different microphones / EQ / rooms. Same-source-different-mic studio
audio is exactly the TK-1 use case, and PHAT is the textbook tool for
it (used in SyncSink.wasm, audalign, the broader TDOA literature).
Pipeline now:
Stage A: chroma + onset NCC → bar-precise lag (~50 ms)
Stage B: GCC-PHAT in 20 s window → sample-precise lag (≤ 1 ms) ← NEW
Stage C: windowed drift → drift_ratio (always),
offset only ± 50 ms when PHAT held
Implementation notes:
- Window must be ≥ 4–8 bars (20 s default) to defeat bar-level
self-similarity; a 4 s window let PHAT corroborate chroma's bar
errors instead of correcting them. Adaptive shrink down to a 2 s
floor for short clips.
- PHAT-β = 1.0 (full whitening); a shipping option to dial down to
0.7 for noisy / reverberant input is left as a future hook.
- PNR floor 6.0: reject the refinement when phase coherence is too
weak to trust (cover versions, narrowband material, very short
overlap). Caller falls back to chroma lag.
- Drift refinement now owns drift_ratio but only owns offset when
PHAT was skipped/rejected, with a tight 50 ms tolerance otherwise.
Without this fence drift was actively walking PHAT's correct lag
into a beat-aligned-impostor — exactly the failure mode this whole
effort was meant to fix.
`phat_pnr` is surfaced through SyncResult / SyncResultDto / the JS
SyncResult type so the editor can use "PHAT locked, ratio = …" as a
strong "this match is unambiguous" signal.
Bench (real music + synthetic, before → after):
bench_sync 26/27 → 27/27 (100 %)
bench_real_music 47/49 → 49/49 (100 %)
Mean abs err 22.15 ms → 0.00 ms (sync), 10.83 ms → 0.00 ms (real)
Both previously-failing real-music cases (hiphop / jazz pos-2000 ms)
now resolve to sample precision. The chroma stage was already finding
the correct lag (1998.8 ms vs. true 2000 ms); drift's per-window xcorr
was the regression — PHAT both refines the seed AND fences off drift
from undoing the refinement.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds 13 more CC-licensed clips covering metal, reggae, country, piano,
funk, salsa, drone, repetitive electronic (techno/house/minimal-loop),
spoken-word, punk, world, plus broadens the existing electronic to a
"techno-loop" stress case. The expansion exposed two patterns the
single-seed PHAT design didn't yet handle:
1. Onset-induced primary mispicks. On house-loop material chroma+onset's
primary lands on a beat-shifted beat-aligned position while alt[0]
carries a higher chroma-only NCC at the true lag. PHAT on the wrong
primary produces a finite-but-modest PNR (~50); on the true seed it
saturates (~1e8). Fix: when primary's PHAT PNR is below a "trust"
threshold (100), also probe the top alternates and keep the result
with the highest PNR. Above the threshold the primary is
unambiguous and the multi-seed pass is skipped — important for
periodic synthetic fixtures (e.g. the 5-tone 2.5 s-period
pentatonic test) where every period offers an equally-phase-
coherent shift and multi-seed would arbitrarily flip between them.
2. Two genuine edge cases that the matcher correctly flags rather than
resolves: drone music with white-noise overlay (no phase coherence
to lock onto — PHAT rejects, and the fallback finds the noise's own
self-correlation at lag 0) and a pure DJ-set 4-on-the-floor loop
where every bar shift is mathematically ambiguous (PSR = 1.01,
knife-edge tie). Both surface with low PSR / low phat_pnr, so the
UI has the signal it needs to flag them as uncertain — no
silently-wrong matches.
Bench (real music, 7 scenarios × 20 clips = 140 runs):
Top-1 pass: 138/140 (99 %) — 18 of 20 genres at 7/7
Recall@K: 138/140 (99 %)
Mean abs err: 15.28 ms (down from 0 because the two known-fail
scenarios contribute a 1.9 s and 200 ms
residual; the 138 passing runs are still
0.00 ms each)
Mean PSR: 1.40×
bench_sync remains 27/27 (100 %, mean abs err 0.00 ms).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces every URL in bench/corpus.tsv with a different CC-licensed identifier from archive.org, keeping the 20-genre coverage exactly the same. The audio cache is gitignored so the swap is just a URL change in the manifest. Why: confirm the matching engine isn't accidentally tuned to the specific clips we tested against, by re-running the full bench (20 clips × 7 scenarios = 140 runs) on a corpus the code has never seen. Result: 140/140 (100 %), mean abs err 0.00 ms. This rules out per-file overfit; the two fails on the previous corpus (drone+noise, house-loop +500ms) were specific to exceptionally adversarial clips — pure drone-music recording with near-zero phase coherence, and a 4-on-the-floor DJ set with mathematically equal bar-shifts. They're properties of those particular signals rather than algorithm bugs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Tightens the WASM matching engine from "1 % top-1/top-2 margin" to sample-precise picks across 20 music genres. Pipeline gains a phase-only refinement stage between chroma+onset and drift; chroma now also reports peak-to-second / peak-to-noise ratios so the UI can tell ambiguous from unambiguous matches without inferring it from a saturated confidence value.
What changes
drift_ratioalways honored; offset replacement only when PHAT was skipped/rejected, or — when PHAT held — within a tight 50 ms band. Without this, drift was actively walking PHAT's correct lag onto bar-aligned impostors.ncc.rscollapses the 23 ms HOP quantization on chroma alternates and on cases where drift-refinement doesn't trigger (short clips, silent passages).peak_to_second_ratio,peak_to_noise,phat_pnrflow throughSyncResult→ DTO → JSmapWasmResult. UI is unchanged — these are available for future use.Tier 1.1 (multiplicative onset-in-score) was tried and reverted in-branch — on real rhythmic material it slightly reduced mean PSR because every beat-aligned lag has near-uniform onset Pearson, so the bonus rewarded false alignments almost as much as the true one. PHAT solves the same problem the right way.
Real-music bench corpus
bench/fetch_corpus.sh(yt-dlp + ffmpeg) pulls 20 CC-licensed clips across genres into a gitignoredbench/cache/.examples/bench_real_musicruns the full offset/recall scenario set on real spectral content. Validated on two completely separate corpora (140 runs each) — second corpus uses 20 different identifiers from the first, hits 100 % top-1, confirming no per-file overfit.Bench results
bench_sync(synthetic)bench_real_music(validation corpus)32 Rust unit tests + 5 JS tests pass.
Test plan
wasm:build;pkg/is gitignored)bench/fetch_corpus.sh && cargo run --release --example bench_real_musiclocally to reproduce 140/140🤖 Generated with Claude Code