Realtime BPM Analyzer
Platforms
The Python, Java, C# and Swift ports run the offline analysis on samples you decode. They give the same readings as the JS library, proven by shared conformance vectors. Live input from a microphone or a stream is the browser's.
Features
- Zero dependencies - Uses native Web Audio API
- Real-time analysis - Analyze audio/video as it plays
- Two engines, one reading - The tempogram by default,
engine: 'peaks'for the 5.x engine - Multiple sources - Works with files, streams, microphone, or any audio/video node
- Typed events - Full TypeScript support with autocomplete
- Runs locally - The library analyzes audio on the device, makes no network call of its own and collects nothing
- Any decoded audio - The library reads decoded audio: in the browser, whatever
decodeAudioDatasupports; in Node.js, samples you decode. The CLI reads MP3, WAV, FLAC, OGG and Opus files, and other formats through ffmpeg
Installation
npm install realtime-bpm-analyzer
Quick Example
import { createRealtimeBpmAnalyzer } from 'realtime-bpm-analyzer';
// In a click handler: browsers start audio only after a user gesture
const audioContext = new AudioContext();
const analyzer = await createRealtimeBpmAnalyzer(audioContext);
// An <audio> element as the source. Connect it to the analyzer's input, which feeds the engine you picked,
// and to the speakers: a media element source plays only through the nodes it is connected to
const audioElement = document.querySelector('audio')!;
const source = audioContext.createMediaElementSource(audioElement);
source.connect(analyzer.input);
source.connect(audioContext.destination);
// A reading on each analysis step outside listening: twice a second with the default
// tempogram engine, about ten times a second with peaks
analyzer.on('reading', (reading) => {
console.log('BPM:', reading.bpm, 'state:', analyzer.state.status);
});
// The state moves through listening, settling, locked and stopped
analyzer.on('statechange', ({ to }) => {
if (to.status === 'locked') {
console.log('Locked on', to.reading.bpm);
}
});
While locked, keep showing the latest reading: the tempo can still move without a statechange.
A whole file
import { analyzeFullBuffer } from 'realtime-bpm-analyzer';
// A File from an <input type="file">, or a Blob from fetch(). Call it from a user gesture, such as the input's
// change event, so the AudioContext starts without an autoplay warning
async function bpmOf(file: Blob) {
const audioContext = new AudioContext();
const audioBuffer = await audioContext.decodeAudioData(await file.arrayBuffer());
await audioContext.close(); // the decoded buffer outlives its context
// The same reading as realtime, or null when no tempo was found
const reading = await analyzeFullBuffer(audioBuffer);
return reading?.bpm ?? null;
}
How it works, and its limits
The default engine, the tempogram, measures how the sound changes from moment to moment across several frequency bands, and reads the tempo as the spacing that repeats most in those changes. engine: 'peaks', the 5.x engine, low-passes the audio and counts the intervals between its loudest peaks. Both report the same reading, with a confidence that is the chance the reading is right, calibrated on a labelled benchmark from 90 to 180 BPM. Outside that range, or on a custom tempoRange, the confidence is null: unmeasured.
Every tempo is reported in a range, 90 to 180 BPM by default. A track outside it reads at double or half its tempo: 70 BPM reads 140, and a track at about 180 BPM or faster can read at half. The candidates mark the half and double readings, and tempoRange moves the range. Music without a steady beat is the hard case: expect a lower confidence, a wrong reading or none. The benchmark describes the data, the method and what was measured.
Strict CSP and browser extensions
The worklet loads from a blob: URL by default. Pages whose Content Security Policy blocks blob: scripts, and Manifest V3 extensions, serve the published processor file (realtime-bpm-analyzer/processor, i.e. dist/realtime-bpm-processor.js) from their own origin and pass its URL:
const analyzer = await createRealtimeBpmAnalyzer(audioContext, {
processorUrl: chrome.runtime.getURL('realtime-bpm-processor.js'), // or '/realtime-bpm-processor.js'
});
See the strict CSP guide for Vite and extension setups.
Node.js
realtime-bpm-analyzer/node analyzes decoded PCM samples without Web Audio, in Node.js, Bun or Deno, with no added dependency. It returns the same reading as analyzeFullBuffer for the same samples, or null when it finds no tempo. Decoding is up to you (a WAV reader, ffmpeg, ...):
import {analyzeChannelData} from 'realtime-bpm-analyzer/node';
const reading = await analyzeChannelData(channelData, 44100); // Float32Array of one channel
console.log(reading?.bpm, reading?.candidates);
See the Node.js guide.
Command line
A separate package, realtime-bpm-analyzer-cli, detects the BPM of audio files from a terminal, so this library stays dependency-free:
npx realtime-bpm-analyzer-cli song.mp3 # 128 BPM, the engine, its confidence and the top candidates
npx realtime-bpm-analyzer-cli --json ./music # every audio file of a folder, as JSON
See the command-line guide.
AI assistants (MCP)
realtime-bpm-analyzer-mcp is a local MCP server, so Claude Desktop, Claude Code or any MCP client can detect the BPM of files on your disk. It runs on your machine over stdio: the audio never leaves it.
claude mcp add bpm -- npx -y realtime-bpm-analyzer-mcp # Claude Code
See the MCP guide for Claude Desktop and the two tools, analyze_file and analyze_folder.
Documentation
📖 Full Documentation - Complete guides, API reference, and tutorials
Key sections:
- Getting Started - Get up and running
- API Reference - Complete API documentation
- Examples - Live demos and code samples
- Migration to v6 - Upgrading from 5.x
- Migration to v5 - Upgrading from v4.x
Use Cases
- Audio Players - Display BPM while playing tracks
- Live Streams - Continuous BPM detection from radio/streaming
- File Analysis - Whole-file BPM detection from local files
- Microphone Input - Real-time analysis from live audio
- DJ Applications - Beat matching and tempo detection
Running Examples Locally
From a clean clone, at the repository root:
# Install the workspace from the lockfile
npm ci
# Build the library: the examples import it from its dist/ folder
npm run build -w realtime-bpm-analyzer
# Run one example
npm run dev -w examples/01-vanilla-basic
Each folder in examples/ is one app, in vanilla JavaScript, React or Vue. examples/readme.md gives each example's port, npm run examples:dev to start all nine at once, and why the microphone examples need localhost or HTTPS.
Privacy & Security
These points cover the library, the CLI and the MCP server:
- Local processing - audio is analyzed on the device and never leaves it
- No data collection or transmission - no network call of their own, no telemetry
- No audio recording - audio is analyzed, not kept
- Open source - fully auditable code
The website that presents them, realtime-bpm-analyzer.com, is a separate thing. Its tools analyze your audio in your browser, and the website measures its own pages with Google Analytics, as Website Privacy describes.
See privacy.md and security.md for details.
Live BPM Detector for Chrome and Edge
Want the tempo without writing code? Live BPM Detector is a free extension for Chrome and Edge, built on this library. It shows the live tempo of the music playing in the current tab, on YouTube, SoundCloud, Bandcamp and more. Your audio never leaves your device. This extension is independent, and is not affiliated with or endorsed by any of these services.
Contributing
Contributions are welcome! See contributing.md for guidelines.
License
Apache-2.0 License - See license.md for details.
Sponsors
If your product depends on this library, consider sponsoring its maintenance via GitHub Sponsors.
Credits
Inspired by Tornqvist's bpm-detective and Joe Sullivan's algorithm.