Skip to content

API Reference


API Reference / createRealtimeBpmAnalyzer

Function: createRealtimeBpmAnalyzer()

ts
function createRealtimeBpmAnalyzer(audioContext, processorOptions?): Promise<BpmAnalyzer>;

Defined in: index.ts:131

Creates a real-time BPM analyzer for live audio streams.

This function sets up a BPM analyzer that processes audio in real-time to detect the beats-per-minute (tempo) of streaming audio sources like microphones, audio elements, or live streams.

Parameters

ParameterTypeDescription
audioContextAudioContextThe Web Audio API AudioContext to use for processing
processorOptions?RealTimeBpmAnalyzerParametersOptional configuration parameters for the analyzer

Returns

Promise<BpmAnalyzer>

A promise that resolves to a BpmAnalyzer instance (event emitter wrapping AudioWorkletNode)

Remarks

The returned BpmAnalyzer is an event emitter that wraps an AudioWorkletNode.

  • Use the analyzer directly for event listening: analyzer.on('bpm', ...)
  • Use .node property for Web Audio API connections: source.connect(analyzer.node)

Examples

Basic Usage with Audio Element

typescript
const audioContext = new AudioContext();
const bpmAnalyzer = await createRealtimeBpmAnalyzer(audioContext);

// Connect audio graph - use .node for AudioWorkletNode connections
const audioElement = document.querySelector('audio');
const source = audioContext.createMediaElementSource(audioElement);
source.connect(bpmAnalyzer.node);
bpmAnalyzer.node.connect(audioContext.destination);

// Listen for BPM events with full type safety
bpmAnalyzer.on('bpm', (data) => {
  console.log('Current BPM:', data.bpm[0].tempo);
  console.log('Confidence:', data.bpm[0].count);
});

bpmAnalyzer.on('bpmStable', (data) => {
  console.log('Stable BPM detected:', data.bpm[0].tempo);
});

With Custom Configuration

typescript
const audioContext = new AudioContext();
const bpmAnalyzer = await createRealtimeBpmAnalyzer(audioContext, {
  continuousAnalysis: true,    // Keep analyzing after stable BPM found
  stabilizationTime: 20000,    // Time in ms to consider BPM stable
  muteTimeInIndexes: 10000,    // Audio indexes to skip between peaks
  debug: false                 // Enable debug logging
});

With Microphone

typescript
const audioContext = new AudioContext();
const bpmAnalyzer = await createRealtimeBpmAnalyzer(audioContext);

// Connect microphone - use .node for audio connections
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const source = audioContext.createMediaStreamSource(stream);
source.connect(bpmAnalyzer.node);
// Note: Don't connect to destination to avoid feedback

// Handle different event types
bpmAnalyzer.on('bpm', (data) => {
  console.log('Analyzing:', data.bpm[0].tempo);
});

bpmAnalyzer.on('bpmStable', (data) => {
  console.log('Stable BPM:', data.bpm[0].tempo);
  console.log('Confidence:', data.bpm[0].count);
});

// One-time listener
bpmAnalyzer.once('bpmStable', (data) => {
  console.log('First stable detection!');
});

Controlling the Analyzer

typescript
// Reset the analyzer state
bpmAnalyzer.reset();

// Listen for reset events
bpmAnalyzer.on('analyzerReset', () => {
  console.log('Analyzer was reset');
});

// Stop the analyzer
bpmAnalyzer.stop();

// Remove listeners with native API
const handler = (event) => console.log(event.detail);
bpmAnalyzer.addEventListener('bpm', handler);
bpmAnalyzer.removeEventListener('bpm', handler);

// Or use once() for automatic cleanup
bpmAnalyzer.once('bpmStable', (data) => console.log('First detection'));

// Disconnect and cleanup
bpmAnalyzer.disconnect();

See

Released under the Apache License 2.0