Skip to content
Detects the BPM of a song or audio sample
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Refactored API May 19, 2016
src
test
.eslintignore
.eslintrc.json Refactored API May 19, 2016
.gitignore Refactored API May 19, 2016
.npmignore
README.md Update link in readme, resolves #2 Sep 6, 2017
package.json

README.md

bpm-detective

Detects the BPM of a song or audio sample

This module uses the Web Audio API to try and detect the BPM of a given sound. You can find more on the implementation and how it works by reading the blog post Beat Detection Using JavaScript and the Web Audio API which happens to be where I got most of the code.

Install

$ npm install --save bpm-detective

Usage

The module exports one function. The function takes an AudioBuffer as its only argument. It returns the detected BPM. If the sample was too short or if, for any other reason, the detection failed, the method throws an error.

import detect from 'bpm-detective';

const AudioContext = window.AudioContext || window.webkitAudioContext;
let context = new AudioContext();

// Fetch some audio file
fetch('some/audio/file.wav')
  // Get response as ArrayBuffer
  .then(response => response.arrayBuffer())
  .then(buffer => {
    // Decode audio into an AudioBuffer
    return new Promise((resolve, reject) => {
      context.decodeAudioData(buffer, resolve, reject);
    });
  })
  // Run detection
  .then(buffer => {
    try {
      const bpm = detect(buffer);
      alert(`Detected BPM: ${ bpm }`);
    } catch (err) {
      console.error(err);
    }
  );

Disclaimer

The detection presumes you are working with dance(-ish) kind of music and as so looks only in the 90-180 BPM spectrum.

License

MIT

You can’t perform that action at this time.