8000 fix(tempo): Rayleigh prior on tempo detection — kill octave-doubling by johannboehme · Pull Request #26 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions frontend/src/local/render/audio-analysis/analyze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,45 @@ describe("analyzeAudio — pure pipeline", () => {
expect(a.tempo!.bpm).toBeLessThan(92);
});

it("picks the half-time tempo when both 85 and 170 BPM autocorrelate", () => {
// Lo-fi hip-hop / swing pattern: a strong kick on every beat at
// 85 BPM, plus a softer hat on every off-beat (twice the rate =
// 170 BPM events). The autocorrelation surface has peaks at
// BOTH lags and the unprior'd picker used to land on 170/180
// because it sums the on-beat-AC into the doubled-tempo lag's
// vote. The Rayleigh prior at ~110 BPM pulls the answer back
// toward the perceptually-correct slower octave.
const seconds = 16;
const total = Math.round(SR * seconds);
const pcm = new Float32Array(total);
for (let i = 0; i < total; i++) pcm[i] = (Math.random() - 0.5) * 0.01;
const beatStride = Math.round((60 / 85) * SR);
const halfStride = Math.round((60 / 170) * SR);
const clickLen = Math.round(0.005 * SR);
// Strong kick on every beat (85 BPM).
for (let beat = 0; beat * beatStride + clickLen < total; beat++) {
const start = beat * beatStride;
for (let k = 0; k < clickLen; k++) {
const env = Math.exp((-k / clickLen) * 4);
pcm[start + k] += 0.9 * env * Math.sin((2 * Math.PI * 80 * k) / SR);
}
}
// Softer hat on every off-beat (= every position at 170 BPM that
// ISN'T already a kick).
for (let h = 0; h * halfStride + clickLen < total; h++) {
if (h % 2 === 0) continue; // skip kicks
const start = h * halfStride;
for (let k = 0; k < clickLen; k++) {
const env = Math.exp((-k / clickLen) * 4);
pcm[start + k] += 0.4 * env * Math.sin((2 * Math.PI * 6000 * k) / SR);
}
}
const a = analyzeAudio(pcm, SR);
expect(a.tempo).not.toBeNull();
expect(a.tempo!.bpm).toBeGreaterThan(80);
expect(a.tempo!.bpm).toBeLessThan(95);
});

it("emits beats roughly every beat-period (count near duration*bpm/60)", () => {
const pcm = buildClickTrack(120, 10);
const a = analyzeAudio(pcm, SR);
Expand Down
25 changes: 24 additions & 1 deletion frontend/src/local/render/audio-analysis/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ const MAX_BPM = 200;
// Beat-tracking weight on the period prior (higher = more rigid tempo).
const BEAT_PRIOR_ALPHA = 100;

// Perceptual-tempo prior centre (BPM). Autocorrelation peaks tend to
// alias across tempo octaves: a track at 85 BPM shows a strong peak at
// 170 BPM too (every × 85), and the unweighted picker
// frequently lands on the wrong octave. A Rayleigh prior centred near
// the typical music range (~100–110 BPM) softly biases the picker
// toward perceptually-natural tempos without hard-cutting any region
// of the search space — drum'n'bass at 170 still wins if its AC is
// strong enough, but a lo-fi-hip-hop track that quietly autocorrelates
// at 180 lands on its true 90 BPM. Klapuri / Davies-style construction.
const TEMPO_PRIOR_SIGMA_BPM = 110;
const TEMPO_PRIOR_NORM = Math.exp(0.5); // so prior(σ) = 1.

/** Rayleigh-shaped weighting in [0, 1]; peaks at σ. */
function tempoPrior(bpm: number): number {
const sigma = TEMPO_PRIOR_SIGMA_BPM;
const x = bpm / sigma;
return TEMPO_PRIOR_NORM * x * Math.exp(-(x * x) / 2);
}

export interface AnalyzeOptions {
/** Override frames-per-second target (default 30). Mostly for tests. */
framesPerSec?: number;
Expand Down Expand Up @@ -485,12 +504,16 @@ function detectTempo(strength: number[], framesPerSec: number): Tempo | null {

// Octave voting: combine ac[lag] with ac[2*lag] and ac[lag/2] (downscaled)
// to bias toward the perceptual tempo and avoid tempo-half/double errors.
// Then multiply by a Rayleigh-shaped tempo prior so the picker softly
// prefers musically-typical tempos without hard-cutting fast or slow
// material — see TEMPO_PRIOR_SIGMA_BPM rationale at top of file.
const voted = new Array<number>(maxLag + 1).fill(0);
for (let lag = minLag; lag <= maxLag; lag++) {
let v = ac[lag];
if (2 * lag <= maxLag) v += 0.5 * ac[2 * lag];
if (lag % 2 === 0) v += 0.3 * ac[lag / 2];
voted[lag] = v;
const bpm = (60 / lag) * framesPerSec;
voted[lag] = v * tempoPrior(bpm);
}

// Pick the peak.
Expand Down
Loading
0