Repository navigation
fix(tempo): remove frame-grid BPM bias + robust long-form aggregation - #55
Merged
Merged
Conversation
Our beat analyser reported a true 91 BPM track (brother's Lago.mp3, set
to 91 in the DAW) as global 90. Two compounding causes, both fixed.
Säule 1 — sub-BPM precision (the 91→90 snap).
`refineTempoFromBeats` derived the period from the MEDIAN of adjacent
beat intervals. Beats are quantized to the ~33 ms analysis-frame grid, so
each interval carries a full ±½-frame error; near the grid magnets
(90 BPM = exactly 20 frames) a true 91 (19.78 frames) snapped toward 20
and read ~90.6, then rounded to 90. Replaced with a long-baseline
Theil-Sen slope: the median of (beats[j]−beats[i])/(j−i) over long spans
divides the endpoint quantization across the whole baseline, recovering
±0.05 BPM regardless of grid position, while the median keeps the
robustness to dropped/doubled beats that motivated the original median.
Clean click tracks at 90/90.5/91/91.5/92/120/121 now land within 0.15;
Lago whole-file goes 90.44 → 91.02.
Säule 2 — robust global aggregation ("orient on the biggest part").
`pickGlobalBpm` bucketed by ROUNDED BPM and voted by chunk count, so a
swarm of short free-played fragments could outvote a long steady core,
sub-BPM precision was thrown away at the round, and an octave-flipped
chunk (182) competed against the 91 it should reinforce. Rewrote it to
fold tempi into a canonical octave, weight each chunk by
duration × confidence × stability, then cluster continuously and pick the
heaviest cluster. The long stable core now dominates the free intro.
Säule 1.5 — tempo stability + local-tempo curve.
The DP tracker regularizes its own beats (it fits a near-constant grid to
a rubato take), so steadiness is measured from a pre-tracking
sliding-window autocorrelation curve instead. `Tempo.stability` (0..1) is
attached in analyzeAudio and threaded per-chunk into the global vote.
Säule 3 — tempo segmentation primitives.
`localTempoCurve`, `segmentTempo`, `dominantTempo` split a take into
maximal stable-tempo runs (handles mid-song tempo changes / free
passages). Exposed + tested; not yet wired into the bar-grid UI.
ANALYSIS_VERSION 3→4 invalidates cached tempos so stale 90.x values
recompute. `snapChunkEndToBar` now absorbs the ~46 ms window-center
anchor offset (time-based slop) so an N-bar chunk no longer sheds its
last bar.
Co-Authored-By: Claude Opus 4.8 (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.
Why
A track known to be 91 BPM (Lago.mp3, set to 91 in the DAW) was reported as global 90. Investigation found two compounding causes — a per-chunk detection bias and a brittle global vote — plus a need for the analyser to handle long-form material that starts/stops, is played freely, or changes tempo mid-take.
Measured impact: Lago whole-file goes 90.44 → 91.02; synthetic click tracks at 90/90.5/91/91.5/92/120/121 now land within ±0.15 BPM of truth (previously off by up to 0.7, biased toward the frame-grid magnets at 90/120).
What changed
Säule 1 — sub-BPM precision (the 91→90 snap).
refineTempoFromBeatsderived the period from the median of adjacent beat intervals. Beats are quantized to the ~33 ms analysis-frame grid, so each interval carries a full ±½-frame error; near the grid magnets (90 BPM = exactly 20 frames) a true 91 (19.78 frames) snapped toward 20, read ~90.6, and rounded to 90. Replaced with a long-baseline Theil-Sen slope — the median of(beats[j]−beats[i])/(j−i)over long spans divides the endpoint quantization across the whole baseline (sub-0.1 BPM accuracy regardless of grid position) while keeping robustness to dropped/doubled beats.Säule 2 — robust global aggregation ("orient on the biggest part").
pickGlobalBpmbucketed by rounded BPM and voted by chunk count, so a swarm of short free-played fragments could outvote a long steady core, sub-BPM precision was lost at the round, and an octave-flipped chunk (182) competed against the 91 it should reinforce. Rewrote it to fold tempi into a canonical octave, weight each chunk by duration × confidence × stability, then cluster continuously and pick the heaviest cluster.Säule 1.5 — tempo stability + local-tempo curve.
The DP beat-tracker regularizes its own beats (fits a near-constant grid even to a rubato take), so steadiness is measured from a pre-tracking sliding-window autocorrelation curve instead.
Tempo.stability(0..1) is attached inanalyzeAudioand threaded per-chunk (detectedBpmConfidence/detectedBpmStability) into the global vote.Säule 3 — tempo segmentation primitives.
localTempoCurve/segmentTempo/dominantTemposplit a take into maximal stable-tempo runs (handles mid-song tempo changes and free passages). Exposed and unit-tested; not yet wired into the bar-grid UI — that's the intended follow-up.Side effects.
ANALYSIS_VERSION3→4 invalidates cached tempos so stale 90.x values recompute.snapChunkEndToBarnow absorbs the ~46 ms STFT window-center anchor offset (time-based slop) so an N-bar chunk no longer sheds its last bar.Reviewer notes
chunk-detect.browser.test.ts"splits tone/silence/tone" assertion was already failing onmain(verified by revertinganalyze.ts/chunk-bar-grid.tsto HEAD — same 4046 result): its first tone's content ends at ~5.6 s = 2.78 bars, which correctly snaps down to 2 whole bars, not the raw 6 s it expected. Updated to assert correct bar-snap-down behaviour.bpmOctaveShiftoverride model. Two oldpickGlobalBpmtests encoding the old integer-mode semantics were updated.Tests
tsctypecheck cleanpickGlobalBpmaggregation suite (each test fails on the old code).🤖 Generated with Claude Code