/** * @module bpm * @description BPM detection for audio analysis */ /** * Estimate the tempo (beats per minute) of an audio buffer. * * Analyses the first (left/mono) channel by detecting onsets, measuring the * time between successive onsets, converting each interval to a tempo, and * histogramming those tempos into 3-BPM buckets (60-200 BPM) to find the most * common one. Octave errors are corrected by doubling very slow results and * halving very fast ones when a strong half/double bucket also exists, then a * fixed -1 BPM calibration offset is applied. Returns null when there is no * tempo to report — too few onsets (silence, a pad, speech), no interval in the * 60-200 BPM range — and when analysis throws. It used to return a 120 (or, * after the offset, 119) fallback there, which `showBPM` displayed as if it * had been measured. * * @param buffer - Decoded audio buffer to analyse; only channel 0 is read. * @returns Detected tempo in BPM, or null when none was detected. */ export function detectBPM(buffer: AudioBuffer): number | null { try { const channelData = buffer.getChannelData(0); const sampleRate = buffer.sampleRate; const onsets = detectOnsets(channelData, sampleRate); if (onsets.length < 2) return null; // Calculate intervals const intervals: number[] = []; for (let i = 1; i < onsets.length; i++) { intervals.push((onsets[i] - onsets[i - 1]) / sampleRate); } // Convert to tempos and group const tempoGroups: Record = {}; intervals.forEach(interval => { const tempo = 60 / interval; const bucket = Math.round(tempo / 3) * 3; if (bucket > 60 && bucket < 200) { tempoGroups[bucket] = (tempoGroups[bucket] || 0) + 1; } }); // Find most common let maxCount = 0; let detectedBPM: number | null = null; for (const [tempo, count] of Object.entries(tempoGroups)) { if (count > maxCount) { maxCount = count; detectedBPM = parseInt(tempo); } } // No interval landed in the tempo range: nothing was detected. if (detectedBPM === null) return null; // Handle tempo ambiguity if (detectedBPM < 70 && tempoGroups[detectedBPM * 2]) { detectedBPM *= 2; } else if (detectedBPM > 160 && tempoGroups[Math.round(detectedBPM / 2)]) { detectedBPM = Math.round(detectedBPM / 2); } return detectedBPM - 1; // Calibration offset } catch (e) { console.warn('[WaveformPlayer] BPM detection failed:', e); return null; } } /** * Detect onset sample positions (transients/beats) within a channel of audio. * * Slides a 2048-sample window (50% overlap via a half-window hop) across the * signal, computing the mean squared energy of each window. An onset is flagged * when the energy rise over the previous (smoothed) energy exceeds an adaptive * threshold and the window energy is above a noise floor, subject to a minimum * spacing of 150 ms so a single transient is not counted twice. The running * previousEnergy is exponentially smoothed (0.8 new / 0.2 old) to track the * local energy envelope. * * @param channelData - PCM samples (normalised -1..1) for a single channel. * @param sampleRate - Sample rate in Hz, used to derive the minimum onset spacing. * @returns Ascending sample indices at which onsets were detected. */ function detectOnsets(channelData: Float32Array, sampleRate: number): number[] { const windowSize = 2048; const hopSize = windowSize / 2; const onsets: number[] = []; let previousEnergy = 0; for (let i = 0; i < channelData.length - windowSize; i += hopSize) { let energy = 0; for (let j = i; j < i + windowSize; j++) { energy += channelData[j] * channelData[j]; } energy = energy / windowSize; const energyDiff = energy - previousEnergy; const threshold = previousEnergy * 1.8 + 0.01; if (energyDiff > threshold && energy > 0.01) { const lastOnset = onsets[onsets.length - 1] || 0; const minDistance = sampleRate * 0.15; if (i - lastOnset > minDistance) { onsets.push(i); } } previousEnergy = energy * 0.8 + previousEnergy * 0.2; } return onsets; }