npm

npm’s 2019 JavaScript ecosystem survey analysis is now available!Get your copy here »

realtime-bpm-analyzer

1.1.1 • Public • Published

RealTime BPM/Tempo Analyzer

Greenkeeper badge Build Status Coverage Status npm Join the chat at https://gitter.im/realtime-bpm-analyzer/Lobby

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.

Usage / Requirements

  1. An AudioNode to analyze. So something like this :

    <audio src="./new_order-blue_monday.mp3" id="track"></audio>
  2. 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);
  3. 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.inputBuffer (AudioBuffer)
    scriptProcessorNode.onaudioprocess = function (e) {
        onAudioProcess.analyze(e);
    };

Example

You can find here a functionnal exemple of this tool.

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 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

Todo

  • Add controls

install

npm i realtime-bpm-analyzer

Downloadsweekly downloads

8

version

1.1.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability