Package Exports
- realtime-bpm-analyzer
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (realtime-bpm-analyzer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
RealTime BPM/Tempo Analyzer
This tool allow to compute the BPM (Beats Per minutes) in real time, of a song on an or 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. In fact, it pre-compute datas instead to store the entire AudioBuffer in memory. So it can quickly return BPM.
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.
Usage / Requirements
An AudioNode to analyze. So something like this :
<audio src="./new_order-blue_monday.mp3" id="track"></audio>
Connect the AudioNode to the AudioContext and create an AudioContext.createScriptProcessor().
// Create new instance of AudioContext var audioContext = new AudioContext(); // Set the source with the HTML Audio Node var source = audioContext.createMediaElementSource(document.getElementById('track')); // Set the scriptProcessorNode to get PCM data in real time var scriptProcessorNode = audioContext.createScriptProcessor(4096, 1, 1); // Connect everythings together scriptProcessorNode.connect(audioContext.destination); source.connect(scriptProcessorNode); source.connect(audioContext.destination);
Now you have just to configure the tool and attach it to the audioprocess event like this :
var RealTimeBPMAnalyzer = include('realtime-bpm-analyzer'); var onAudioProcess = new RealTimeBPMAnalyzer({ scriptNode: { bufferSize: 4096, numberOfInputChannels: 1, numberOfOutputChannels: 1 }, pushTime: 2000, pushCallback: function (err, bpm) { console.log('bpm', bpm); } }); // Attach realTime function to audioprocess event scriptProcessorNode.onaudioprocess = function (e) { onAudioProcess.analyze(e); };
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
