Skip to content

API Reference


API Reference / BpmAnalyzer

Class: BpmAnalyzer

Defined in: core/bpm-analyzer.ts:85

Real-time BPM analyzer with typed event listeners.

This class wraps an AudioWorkletNode and provides a clean, type-safe API for analyzing beats per minute in real-time audio streams.

Remarks

This class extends native EventTarget to provide typed event listeners with full TypeScript autocomplete support. It implements the AudioNode interface for seamless integration into Web Audio API graphs.

Examples

Basic Usage

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

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

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

// Connect to audio source - use .node for audio connections
const source = audioContext.createMediaElementSource(audioElement);
source.connect(bpmAnalyzer.node);
bpmAnalyzer.node.connect(audioContext.destination);

One-time Listeners

typescript
analyzer.once('bpmStable', (data) => {
  console.log('First stable detection:', data.bpm[0].tempo);
  // This listener will be automatically removed after firing
});

Removing Listeners

typescript
// Store handler reference for cleanup
const handleBPM = (event) => {
  console.log('BPM:', event.detail.bpm);
};

// Add listener using native EventTarget API
analyzer.addEventListener('bpm', handleBPM);

// Remove with same reference
analyzer.removeEventListener('bpm', handleBPM);

Controlling the Analyzer

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

// Stop analysis
analyzer.stop();

// Disconnect from audio graph
analyzer.disconnect();

Extends

  • EventTarget

Accessors

context

Get Signature

ts
get context(): BaseAudioContext;

Defined in: core/bpm-analyzer.ts:309

Get the audio context associated with this analyzer

Returns

BaseAudioContext


numberOfInputs

Get Signature

ts
get numberOfInputs(): number;

Defined in: core/bpm-analyzer.ts:316

Get the number of inputs

Returns

number


numberOfOutputs

Get Signature

ts
get numberOfOutputs(): number;

Defined in: core/bpm-analyzer.ts:323

Get the number of outputs

Returns

number


channelCount

Get Signature

ts
get channelCount(): number;

Defined in: core/bpm-analyzer.ts:330

Get the channel count

Returns

number

Set Signature

ts
set channelCount(value): void;

Defined in: core/bpm-analyzer.ts:334

Parameters
ParameterType
valuenumber
Returns

void


channelCountMode

Get Signature

ts
get channelCountMode(): ChannelCountMode;

Defined in: core/bpm-analyzer.ts:341

Get the channel count mode

Returns

ChannelCountMode

Set Signature

ts
set channelCountMode(value): void;

Defined in: core/bpm-analyzer.ts:345

Parameters
ParameterType
valueChannelCountMode
Returns

void


channelInterpretation

Get Signature

ts
get channelInterpretation(): ChannelInterpretation;

Defined in: core/bpm-analyzer.ts:352

Get the channel interpretation

Returns

ChannelInterpretation

Set Signature

ts
set channelInterpretation(value): void;

Defined in: core/bpm-analyzer.ts:356

Parameters
ParameterType
valueChannelInterpretation
Returns

void

Constructors

Constructor

ts
new BpmAnalyzer(workletNode): BpmAnalyzer;

Defined in: core/bpm-analyzer.ts:98

Creates a new BpmAnalyzer instance

Parameters

ParameterTypeDescription
workletNodeAudioWorkletNodeThe AudioWorkletNode to wrap

Returns

BpmAnalyzer

Overrides

ts
EventTarget.constructor

Methods

reset()

ts
reset(): void;

Defined in: core/bpm-analyzer.ts:118

Reset the analyzer state to start fresh analysis

Returns

void

Remarks

This clears all internal state including detected peaks and intervals, allowing the analyzer to start analyzing as if it were newly created.

Example

typescript
// When switching to a different audio source
audioElement.src = 'new-song.mp3';
analyzer.reset();

stop()

ts
stop(): void;

Defined in: core/bpm-analyzer.ts:134

Stop the analyzer

Returns

void

Remarks

This stops the analysis and resets the internal state. The analyzer will no longer emit events until analysis is restarted.

Example

typescript
analyzer.stop();

on()

ts
on<K>(event, listener): void;

Defined in: core/bpm-analyzer.ts:151

Add an event listener for a specific event type

Type Parameters

Type Parameter
K extends keyof BpmAnalyzerEventMap

Parameters

ParameterTypeDescription
eventKThe event name to listen for
listener(data) => voidThe callback function to invoke when the event is emitted

Returns

void

Example

typescript
analyzer.on('bpm', (data) => {
  console.log('Current BPM:', data.bpm[0].tempo);
});

once()

ts
once<K>(event, listener): void;

Defined in: core/bpm-analyzer.ts:173

Add a one-time event listener that will be removed after being called once

Type Parameters

Type Parameter
K extends keyof BpmAnalyzerEventMap

Parameters

ParameterTypeDescription
eventKThe event name to listen for
listener(data) => voidThe callback function to invoke when the event is emitted

Returns

void

Example

typescript
analyzer.once('bpmStable', (data) => {
  console.log('First stable BPM detected:', data.bpm[0].tempo);
});

connect()

Call Signature

ts
connect(
   destinationNode, 
   outputIndex?, 
   inputIndex?): AudioNode;

Defined in: core/bpm-analyzer.ts:198

Connect the analyzer to an audio destination

Parameters
ParameterTypeDescription
destinationNodeAudioNodeThe destination node to connect to
outputIndex?numberThe output index (default: 0)
inputIndex?numberThe input index on the destination (default: 0)
Returns

AudioNode

The destination node for chaining

Example
typescript
analyzer.connect(audioContext.destination);

Call Signature

ts
connect(destinationParameter, outputIndex?): void;

Defined in: core/bpm-analyzer.ts:199

Connect the analyzer to an audio destination

Parameters
ParameterTypeDescription
destinationParameterAudioParam-
outputIndex?numberThe output index (default: 0)
Returns

void

The destination node for chaining

Example
typescript
analyzer.connect(audioContext.destination);

disconnect()

Call Signature

ts
disconnect(
   destinationNode?, 
   outputIndex?, 
   inputIndex?): void;

Defined in: core/bpm-analyzer.ts:224

Disconnect the analyzer from all destinations or a specific destination

Parameters
ParameterTypeDescription
destinationNode?AudioNodeOptional destination node to disconnect from
outputIndex?numberThe output index (default: 0)
inputIndex?numberThe input index on the destination (default: 0)
Returns

void

Example
typescript
// Disconnect from all destinations
analyzer.disconnect();

// Disconnect from a specific destination
analyzer.disconnect(audioContext.destination);

Call Signature

ts
disconnect(destinationParameter?, outputIndex?): void;

Defined in: core/bpm-analyzer.ts:225

Disconnect the analyzer from all destinations or a specific destination

Parameters
ParameterTypeDescription
destinationParameter?AudioParam-
outputIndex?numberThe output index (default: 0)
Returns

void

Example
typescript
// Disconnect from all destinations
analyzer.disconnect();

// Disconnect from a specific destination
analyzer.disconnect(audioContext.destination);

Properties

PropertyModifierTypeDescriptionDefined in
nodereadonlyAudioWorkletNodeThe underlying AudioWorkletNode Remarks Exposed for advanced use cases. Most users should interact with the BpmAnalyzer API instead of directly accessing the worklet node.core/bpm-analyzer.ts:92

Released under the Apache License 2.0