RealTime BPM/Tempo Analyzer
This tool allow to compute the BPM (Beats Per minutes) in real time, of a song on an <audio></audio>
or <video></video>
node thanks to the WebAudioAPI.
Please, note that the main use case for this tool, is to get the BPM during the video / audio play.
Installation
npm install realtime-bpm-analyzer -S
WebAudioAPI
The WebAudioAPI provides a powerful and versatile system for controlling audio on the Web, allowing developers to choose audio sources, add effects to audio, create audio visualizations, apply spatial effects (such as panning) and much more.
Example
You can find here a functionnal exemple of this tool with userMedia (microphone)
and audioNode
usage.
Usage / Requirements
-
An AudioNode to analyze. So something like this :
-
Connect the AudioNode to the AudioContext and create an AudioContext.createScriptProcessor().
// Create new instance of AudioContextconst audioContext = ;// Set the source with the HTML Audio Nodeconst source = audioContext;// Set the scriptProcessorNode to get PCM data in real timeconst scriptProcessorNode = audioContext;// Connect everythings togetherscriptProcessorNode;source;source; -
Now you have just to configure the tool and attach it to the audioprocess event like this :
;const onAudioProcess =scriptNode:bufferSize: 4096numberOfInputChannels: 1numberOfOutputChannels: 1pushTime: 2000{console;};// Attach realTime function to audioprocess event.inputBuffer (AudioBuffer)scriptProcessorNode {onAudioProcess;};
Technical approch
This tool has been largely inspired by the Tornqvist project.
His algorithm use an AudioBuffer in input. We apply a lowpass filter to get only bass frequencies.
Now, we extract brut data (PCM, Pulse Code Modulation, each points is between 1 and -1) to detect peaks.
Description | |
---|---|
![]() |
PCM Data are dots with value between the max/min amplitude (1/-1). Each dots have its own index |
To do this job, we start with a thresold setted to 0.9 (on the amplitude axis) and we search a minimal peak number (~15) by decrementing this thresold by 0.05 through all the AudioBuffer. When we find a peak, we jump 10000 peaks index (1/4 second) to ignore the descendant phase of the peak...
This tool is designed to detect BPM by detecting all peaks for all thresolds, because we work on chunks (AudioBuffer). So we can recompute the BPM with intervals, etc.. without recompute everything with a full AudioBuffer.
Credits
This library was been inspired from Tornqvist project which also based on Joe Sullivan's algorithm. Thank you to both of them