• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    JavaScript
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Detects the BPM of a song or audio sample

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
const context = new AudioContext()

// Fetch some audio file
fetch('some/audio/file.wav').then(async function (response) {
  // Get response as ArrayBuffer
  const buffer = await response.arrayBuffer()

  // Decode audio into an AudioBuffer
  const data = await new Promise(function (resolve, reject) {
    context.decodeAudioData(buffer, resolve, reject)
  });

  // Run detection
  const bpm = detect(data)
  alert(`Detected BPM: ${bpm}`)
}).catch(console.error)

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