BeatDetect(optionsopt)

new BeatDetect(optionsopt)

Beat detection library

This library provides an audio analyser to retrieve a track beat per minute value. It is also made to guess the time offset before the first significant sound (relative to the BPM), and the time offset of the estimated first bar. All its parameters can be updated when constructed, so it can adapt to any type of audio input. The analysis method is based on the work of Joe Sullivan and José M. Pérez, and is describe in the README.md file of its repository. Remarks and pull requests are welcome!
Parameters:
Name Type Attributes Description
options object <optional>

The configuration object of this library

Properties
Name Type Attributes Default Description
log boolean <optional>
false

Log debug information during the analysis process

perf boolean <optional>
false

Log ellapsed time of each analysis step

sampleRate number <optional>
44100

The sample rate to use during the audio analysis. Can be changed its setter

round boolean <optional>
false

Allow the output rounding to remove floating point

float number <optional>
8

The floating precision for the output. Disabled if round is at true

lowPassFreq number <optional>
150

The low pass filter cut frequency

highPassFreq number <optional>
100

The high pass filter cut frequency

bpmRange Array.<number> <optional>
[90, 180]

The BPM range to output the result in

timeSignature number <optional>
4

The analysed audio time signature

Since:
  • November 2020
Author:
  • Arthur Beaulieu

Members

private _bpmRange :array

The BPM range to display the output in

private _float :number

The number of floating point for the output

private _highPassFreq :number

The high pass filter cut frequency

private _log :boolean

Log debug information in the console when set to true

private _lowPassFreq :number

The low pass filter cut frequency

private _perf :boolean

Log elapsed times during the analysis in the console when set to true

private _round :boolean

Remove any floating point from output when set to true

private _sampleRate :number

The sample rate used for analysis. Must match the analysed audio sample rate

private _tapResetId :number

Reset tap timeout ID

private _timeSignature :number

The studied track time signature

private _ts :object

Contains timestamp used to determine manual BPM

private count :number

The amount of time a click is trigerred to compute BPM

float

Set the output floating precision.

highPassFreq

Set the high pass filter cut frequency.

log

Set logging in console.

lowPassFreq

Set the low pass filter cut frequency.

perf

Set performance timings in console.

round

Set output rounding.

sampleRate

Set sample rate for analysis.

VERSION :string

The BeatDetect version number

Methods

private, static _buildOfflineCtx(options) → {promise}

This method will build and connect all required nodes to perform the BPM analysis.
Parameters:
Name Type Description
options object

The option object sent to the _fetchRawTrack method, augmented with track array buffer

Returns:
promise -

A Promise that is resolved when analysis is done, of will be rejected otherwise

private, static _fetchRawTrack(options) → {promise}

This method will perform a fetch on the given URL to retrieve the track to analyse.
Parameters:
Name Type Description
options object

The option object sent to the getBeatInfo method, augmented with performance marks

Returns:
promise -

A Promise that is resolved when analysis is done, of will be rejected otherwise

private, static _floatRound(value, precision) → {number}

This method will return a rounded floating value to a given precision.
Parameters:
Name Type Description
value number

The value to round at a given floating point

precision number

The amount of numbers after the floating point

Returns:
number -

The rounded value with its given floating point

private, static _getIntervals(peaks) → {array}

This method will then compute time interval between peak, in order to spot the interval that is the most represented. See implementation for further details.
Parameters:
Name Type Description
peaks Array.<object>

The peaks for a given track. Returned from _getPeaks method

Returns:
array -

An array of time intervals

private, static _getLowestTimeOffset(position, bpm) → {object}

This method will search for the smallest time in track for a beat ; using the estimated bpm, we rewind from time signature to get the closest from the track beginning. See implementation for further details.
Parameters:
Name Type Description
position Array.<object>

The beat position for beat to lower

bpm number

The most credible BPM, computed after the most frequent time interval

Returns:
object -

The beat offset and the offset to the first bar

private, static _getOffsets(data, bpm) → {object}

This method will finally compute time offset from song start to first bar, or first significant beat. See implementation for further details.
Parameters:
Name Type Description
data Array.<object>

Array containg L audio data (no important to stereo this)

bpm number

The most credible BPM, computed after the most frequent time interval

Returns:
object -

The beat offset and the offset to the first bar

private, static _getPeaks(data) → {array}

This method will extract peak value from given channel data. See implementation for further details.
Parameters:
Name Type Description
data Array.<array>

Array containg L/R audio data arrays

Returns:
array -

An array filled with peaks value

private, static _getPerfDuration(perf) → {object}

This method will format performance mark to be readable as times
Parameters:
Name Type Description
perf Array.<object>

The performance mark to format

Returns:
object -

The ellapsed times for different beat detection steps

private, static _logEvent(level, string)

This method will display a given console output if the logging is allowed.
Parameters:
Name Type Description
level string

The console method to call in info, log, warn, error, trace etc.

string string

The text to display in the console

private, static _processRenderedBuffer(options) → {promise}

This method will process the audio buffer to extract its peak and guess the track BPM and offset.
Parameters:
Name Type Description
options object

The option object sent to the _buildOfflineCtx method, augmented with track audio buffer

Returns:
promise -

A Promise that is resolved when analysis is done, of will be rejected otherwise

private, static _tapBpm(options, precision, callback)

Internal method to determine manual BPM
Parameters:
Name Type Description
options object

The internal options object

precision number

The floating point for result

callback function

The callback function to call each click

static getBeatInfo(options) → {promise}

Perform a beat detection on a given track and return the analysis result trhough the Promise resolution. Any exception will be thrown in the Promise catch method.
Parameters:
Name Type Description
options object

The beat detection option

Properties
Name Type Attributes Description
url string

The url to the audio file to analyse

name string <optional>

The track name, only useful for logging

Returns:
promise -

A Promise that is resolved when analysis is done, of will be rejected otherwise

static tapBpm(options)

Providing a DOM element and a callback to manually determine a bpm, using a click. After 5 seconds, the result will be reset.
Parameters:
Name Type Description
options objects

Manual bpm determinitation options

Properties
Name Type Description
element object

The DOM element to listen to

precision number

The floating point for result

callback function

The callback function to call each click