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
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
analyzer.once('bpmStable', (data) => {
console.log('First stable detection:', data.bpm[0].tempo);
// This listener will be automatically removed after firing
});Removing Listeners
// 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
// Reset the analyzer state
analyzer.reset();
// Stop analysis
analyzer.stop();
// Disconnect from audio graph
analyzer.disconnect();Extends
EventTarget
Accessors
context
Get Signature
get context(): BaseAudioContext;Defined in: core/bpm-analyzer.ts:309
Get the audio context associated with this analyzer
Returns
BaseAudioContext
numberOfInputs
Get Signature
get numberOfInputs(): number;Defined in: core/bpm-analyzer.ts:316
Get the number of inputs
Returns
number
numberOfOutputs
Get Signature
get numberOfOutputs(): number;Defined in: core/bpm-analyzer.ts:323
Get the number of outputs
Returns
number
channelCount
Get Signature
get channelCount(): number;Defined in: core/bpm-analyzer.ts:330
Get the channel count
Returns
number
Set Signature
set channelCount(value): void;Defined in: core/bpm-analyzer.ts:334
Parameters
| Parameter | Type |
|---|---|
value | number |
Returns
void
channelCountMode
Get Signature
get channelCountMode(): ChannelCountMode;Defined in: core/bpm-analyzer.ts:341
Get the channel count mode
Returns
ChannelCountMode
Set Signature
set channelCountMode(value): void;Defined in: core/bpm-analyzer.ts:345
Parameters
| Parameter | Type |
|---|---|
value | ChannelCountMode |
Returns
void
channelInterpretation
Get Signature
get channelInterpretation(): ChannelInterpretation;Defined in: core/bpm-analyzer.ts:352
Get the channel interpretation
Returns
ChannelInterpretation
Set Signature
set channelInterpretation(value): void;Defined in: core/bpm-analyzer.ts:356
Parameters
| Parameter | Type |
|---|---|
value | ChannelInterpretation |
Returns
void
Constructors
Constructor
new BpmAnalyzer(workletNode): BpmAnalyzer;Defined in: core/bpm-analyzer.ts:98
Creates a new BpmAnalyzer instance
Parameters
| Parameter | Type | Description |
|---|---|---|
workletNode | AudioWorkletNode | The AudioWorkletNode to wrap |
Returns
BpmAnalyzer
Overrides
EventTarget.constructorMethods
reset()
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
// When switching to a different audio source
audioElement.src = 'new-song.mp3';
analyzer.reset();stop()
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
analyzer.stop();on()
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
| Parameter | Type | Description |
|---|---|---|
event | K | The event name to listen for |
listener | (data) => void | The callback function to invoke when the event is emitted |
Returns
void
Example
analyzer.on('bpm', (data) => {
console.log('Current BPM:', data.bpm[0].tempo);
});once()
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
| Parameter | Type | Description |
|---|---|---|
event | K | The event name to listen for |
listener | (data) => void | The callback function to invoke when the event is emitted |
Returns
void
Example
analyzer.once('bpmStable', (data) => {
console.log('First stable BPM detected:', data.bpm[0].tempo);
});connect()
Call Signature
connect(
destinationNode,
outputIndex?,
inputIndex?): AudioNode;Defined in: core/bpm-analyzer.ts:198
Connect the analyzer to an audio destination
Parameters
| Parameter | Type | Description |
|---|---|---|
destinationNode | AudioNode | The destination node to connect to |
outputIndex? | number | The output index (default: 0) |
inputIndex? | number | The input index on the destination (default: 0) |
Returns
AudioNode
The destination node for chaining
Example
analyzer.connect(audioContext.destination);Call Signature
connect(destinationParameter, outputIndex?): void;Defined in: core/bpm-analyzer.ts:199
Connect the analyzer to an audio destination
Parameters
| Parameter | Type | Description |
|---|---|---|
destinationParameter | AudioParam | - |
outputIndex? | number | The output index (default: 0) |
Returns
void
The destination node for chaining
Example
analyzer.connect(audioContext.destination);disconnect()
Call Signature
disconnect(
destinationNode?,
outputIndex?,
inputIndex?): void;Defined in: core/bpm-analyzer.ts:224
Disconnect the analyzer from all destinations or a specific destination
Parameters
| Parameter | Type | Description |
|---|---|---|
destinationNode? | AudioNode | Optional destination node to disconnect from |
outputIndex? | number | The output index (default: 0) |
inputIndex? | number | The input index on the destination (default: 0) |
Returns
void
Example
// Disconnect from all destinations
analyzer.disconnect();
// Disconnect from a specific destination
analyzer.disconnect(audioContext.destination);Call Signature
disconnect(destinationParameter?, outputIndex?): void;Defined in: core/bpm-analyzer.ts:225
Disconnect the analyzer from all destinations or a specific destination
Parameters
| Parameter | Type | Description |
|---|---|---|
destinationParameter? | AudioParam | - |
outputIndex? | number | The output index (default: 0) |
Returns
void
Example
// Disconnect from all destinations
analyzer.disconnect();
// Disconnect from a specific destination
analyzer.disconnect(audioContext.destination);Properties
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
node | readonly | AudioWorkletNode | The 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 |