BPM Analyzer
A real-time BPM (beats per minute) detection library and application for Rust, using wavelet decomposition and autocorrelation analysis.
Based on Audio Analysis using the Discrete Wavelet Transform by George Tzanetakis, Georg Essl and Perry Cook.
How It Works
The BPM analyzer uses a sophisticated multi-stage signal processing pipeline:
- Audio Capture: Captures audio from system input or loopback devices
- Resampling: Downsamples to 22.05 kHz for efficient processing
- Wavelet Decomposition: Applies 4-level Daubechies D4 wavelet transform to separate frequency bands
- Onset Detection: Extracts amplitude envelopes from each band using full-wave rectification and low-pass filtering
- Autocorrelation: Computes autocorrelation on the summed envelopes to find periodic patterns
- Peak Detection: Identifies BPM candidates based on autocorrelation peaks within the specified range
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
As a Library
use ;
As a Standalone Application
Run the GUI application:
With custom BPM range:
Command-Line Options
-m, --min-bpm <MIN_BPM>: Minimum BPM to detect (default: 40)-M, --max-bpm <MAX_BPM>: Maximum BPM to detect (default: 240)
Configuration
AnalyzerConfig
The analyzer behavior can be customized using AnalyzerConfig:
let config = builder
.min_bpm // Minimum BPM to detect
.max_bpm // Maximum BPM to detect
.window_size // Analysis window size (must be power of 2)
.queue_size // Audio queue size
.buffer_size // Audio capture buffer size
.build;
Parameters
- min_bpm / max_bpm: Define the range of tempos to detect. Narrowing this range can improve accuracy for specific genres.
- window_size: Size of the analysis window in samples. Larger windows provide better frequency resolution but slower response.
- queue_size: Size of the inter-thread audio queue. I don't think you need to change this.
- buffer_size: Audio capture buffer size. Smaller values reduce latency but may increase CPU usage. I'm also not convinced that this needs to be changed.
Audio Setup
macOS
For system audio capture, install BlackHole:
Then configure your system to route audio through BlackHole using a multi-output device.
Dependencies
This library builds on several excellent Rust crates:
- fundsp - Audio processing framework
- osclet - Wavelet transform library
- cpal - Cross-platform audio I/O
- resampler - Audio resampling
Limitations
- Tempo Changes: The analyzer works best with consistent tempo. Sudden tempo changes may take time to adapt.
- Polyrhythmic Music: Complex polyrhythmic patterns may produce multiple strong candidates.
- Very Slow/Fast Tempos: Accuracy may decrease outside the 40-240 BPM range.