web-audio-beat-detector

repository·master·Indexed 20 days ago

https://github.com/chrisguttandin/web-audio-beat-detector

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

Tokens
1.2K
Snippets
6
Records
6
Agent score
22%

What's inside web-audio-beat-detector

  1. Configure tempo range with tempoSettings

    master

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

    Usage Patterns

    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 });
  2. Use the guess() function to get BPM and offset

    master

    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.

    Return Object Shape

    • bpm: The rounded tempo (integer).
    • tempo: The precise tempo (same as analyze() return value).
    • offset: The offset of the first beat in seconds.

    Parameters

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

    Example

    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
        });
  3. Use the analyze() function to get tempo

    master

    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.

    Parameters

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

    Example

    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
        });
  4. Estimate tempo with guess()

    master

    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);
  5. Analyze audio with analyze()

    master

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