Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): octave correction for half-tempo errors on high-BPM tracks

[._.]/ Adam Eivy32433cbbe4ac
diff --git a/server/services/musicVideo/audioAnalysis.js b/server/services/musicVideo/audioAnalysis.js
index 04c761156f..6b419daf7c 100644
--- a/server/services/musicVideo/audioAnalysis.js
+++ b/server/services/musicVideo/audioAnalysis.js
@@ -189,13 +189,28 @@ function estimateTempo(onset, fps) {
}
if (bestLag < 0 || bestScore <= 0) return { bpm: null, lag: null };
- // Parabolic interpolation around the integer-lag peak for sub-frame tempo
- // precision (raw integer lags quantize BPM coarsely at high tempo). Reuse the
- // stored autocorrelation; only the rare boundary neighbor needs a fresh pass.
const acAt = (lag) => {
if (lag < 1 || lag >= n) return 0;
return lag >= minLag && lag <= maxLag ? ac[lag] : acLag(lag);
};
+
+ // Octave correction (half-tempo fold-down). Autocorrelation peaks at EVERY
+ // multiple of the true period, so the half-tempo lag (2× the true period) is
+ // itself a valid peak — and frame quantization smears a non-integer true
+ // period across two integer lags, dropping its value below the sharper
+ // half-tempo peak. The weighted score then picks half tempo (e.g. 140 BPM
+ // read as ~70). Fold down while half the chosen lag is still a strong peak:
+ // if `bestLag` were the true fundamental, its half-lag would sit on a trough
+ // (low/negative), so a strong peak there means the shorter period is the real
+ // one. The inverse error (picking double tempo) can't occur from AC alone.
+ const peakNear = (lag) => Math.max(acAt(Math.floor(lag)), acAt(Math.ceil(lag)));
+ while (bestLag / 2 >= minLag && peakNear(bestLag / 2) >= 0.5 * ac[bestLag]) {
+ bestLag = Math.round(bestLag / 2);
+ }
+
+ // Parabolic interpolation around the integer-lag peak for sub-frame tempo
+ // precision (raw integer lags quantize BPM coarsely at high tempo). Reuse the
+ // stored autocorrelation; only the rare boundary neighbor needs a fresh pass.
const ym = acAt(bestLag - 1);
const y0 = ac[bestLag];
const yp = acAt(bestLag + 1);
diff --git a/server/services/musicVideo/audioAnalysis.test.js b/server/services/musicVideo/audioAnalysis.test.js
index f93c8b07d9..d854c601fd 100644
--- a/server/services/musicVideo/audioAnalysis.test.js
+++ b/server/services/musicVideo/audioAnalysis.test.js
@@ -90,6 +90,16 @@ describe('analyzePcm', () => {
expect(result.bpm).toBeLessThan(92);
});
+ it.each([140, 160, 180])('recovers a high-BPM (%i) click track without half-tempo error', (bpm) => {
+ // High tempos whose beat period falls between onset frames are prone to a
+ // half-tempo octave error (the 2× lag scores higher than the smeared
+ // fundamental). The detected tempo must stay near the true BPM, not ~half.
+ const samples = clickTrack({ bpm, durationSec: 18 });
+ const result = analyzePcm(samples, ANALYSIS_SAMPLE_RATE);
+ expect(result.bpm).toBeGreaterThan(bpm - 3);
+ expect(result.bpm).toBeLessThan(bpm + 3);
+ });
+
it('emits 4/4 downbeats as a quarter of the beats', () => {
const samples = clickTrack({ bpm: 120, durationSec: 16 });
const { beats, downbeats } = analyzePcm(samples, ANALYSIS_SAMPLE_RATE);