8000 fix(tempo): remove frame-grid BPM bias + robust long-form aggregation by johannboehme · Pull Request #55 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content

fix(tempo): remove frame-grid BPM bias + robust long-form aggregation - #55

Merged
johannboehme merged 1 commit into
mainfrom
claude/hungry-pascal-a13d90
Jun 14, 2026
Merged

johannboehme merged 1 commit into
mainfrom
claude/hungry-pascal-a13d90

Conversation

@johannboehme
Copy link
Copy Markdown
Owner

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).
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, 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").
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 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 in analyzeAudio and threaded per-chunk (detectedBpmConfidence/detectedBpmStability) 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 and free passages). Exposed and unit-tested; not yet wired into the bar-grid UI — that's the intended follow-up.

Side effects.

  • ANALYSIS_VERSION 3→4 invalidates cached tempos so stale 90.x values recompute.
  • snapChunkEndToBar now 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

  • The chunk-detect.browser.test.ts "splits tone/silence/tone" assertion was already failing on main (verified by reverting analyze.ts/chunk-bar-grid.ts to 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.
  • Octave folding means 60 BPM and 120 BPM chunks now vote as the same pulse (half/double-time), consistent with the existing per-chunk bpmOctaveShift override model. Two old pickGlobalBpm tests encoding the old integer-mode semantics were updated.

Tests

  • ✅ 1094 unit tests, 13 browser tests, tsc typecheck clean
  • New: sub-BPM precision sweep, rubato stability, tempo segmentation, and a discriminating pickGlobalBpm aggregation suite (each test fails on the old code).

🤖 Generated with Claude Code

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>
@johannboehme
johannboehme merged commit 9d49645 into main Jun 14, 2026
1 check passed
@johannboehme
johannboehme deleted the claude/hungry-pascal-a13d90 branch June 14, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0