Install web-audio-beat-detector via npm
masterInstall the package using npm to use the beat detection utility in your project.
npm install web-audio-beat-detectorrepository·master·Indexed 20 days ago
https://github.com/chrisguttandin/web-audio-beat-detectorA beat detection utility for the browser using the Web Audio API to extract BPM and beat offsets from AudioBuffers. Optimized for electronic music, it provides lightweight analysis via the analyze() and guess() functions, supporting custom tempo range configurations through tempoSettings.
Install the package using npm to use the beat detection utility in your project.
npm install web-audio-beat-detectorBy default, the library expects the BPM to be between 90 and 180. You can adjust this expectation by passing a tempoSettings object as the last argument to analyze() or guess(). This is useful if you know the genre or characteristics of the audio.
Supported keys in tempoSettings:
minTempo: The minimum expected BPM.maxTempo: The maximum expected BPM.With offset and duration:
analyze(audioBuffer, 1, 10, { maxTempo: 120, minTempo: 60 });With only tempo settings:
analyze(audioBuffer, { maxTempo: 120, minTempo: 60 });// with an offset and duration
analyze(audioBuffer, 1, 10, { maxTempo: 120, minTempo: 60 });
// with no other arguments
analyze(audioBuffer, { maxTempo: 120, minTempo: 60 });The guess() function provides a more detailed estimation than analyze(). It returns a Promise that resolves to an object containing the estimated BPM, the tempo, and the offset of the first beat.
bpm: The rounded tempo (integer).tempo: The precise tempo (same as analyze() return value).offset: The offset of the first beat in seconds.audioBuffer: The AudioBuffer to be analyzed.offset (optional): The time in seconds to start analysis.duration (optional): The duration in seconds to analyze.tempoSettings (optional): An object to constrain the expected BPM range.import { guess } from 'web-audio-beat-detector';
guess(audioBuffer)
.then(({ bpm, offset, tempo }) => {
// bpm, offset, and tempo are available
})
.catch((err) => {
// handle error
});import { guess } from 'web-audio-beat-detector';
guess(audioBuffer)
.then(({ bpm, offset, tempo }) => {
// the bpm and offset could be guessed
// the tempo is the same as the one returned by analyze()
})
.catch((err) => {
// something went wrong
});The analyze() function retrieves the BPM (beats per minute) of a given AudioBuffer. It returns a Promise that resolves to a number representing the tempo.
audioBuffer: The AudioBuffer to be analyzed.offset (optional): The time in seconds to start analysis.duration (optional): The duration in seconds to analyze.tempoSettings (optional): An object to constrain the expected BPM range.import { analyze } from 'web-audio-beat-detector';
analyze(audioBuffer)
.then((tempo) => {
// tempo is a number
})
.catch((err) => {
// handle error
});import { analyze } from 'web-audio-beat-detector';
analyze(audioBuffer)
.then((tempo) => {
// the tempo could be analyzed
})
.catch((err) => {
// something went wrong
});The guess function provides a way to quickly estimate the tempo (BPM) of an audio source. Like analyze, it utilizes a broker to handle the computation, ensuring the process is handled efficiently (often via a Web Worker).
import { guess } from 'web-audio-beat-detector';
// Usage depends on the underlying broker's load implementation
const bpm = await guess(audioBufferOrFile);The analyze function is the primary entrypoint for performing beat detection on audio data. It uses an underlying broker to process the audio, typically offloading work to a Web Worker to avoid blocking the main thread. It accepts arguments compatible with the load function from web-audio-beat-detector-broker.
import { analyze } from 'web-audio-beat-detector';
// Usage depends on the underlying broker's load implementation
const results = await analyze(audioBufferOrFile);