Reference p5.PeakDetect

p5.PeakDetect

PeakDetect works in conjunction with p5.FFT to look for onsets in some or all of the frequency spectrum.

To use p5.PeakDetect, call update in the draw loop and pass in a p5.FFT object.

You can listen for a specific part of the frequency spectrum by setting the range between freq1 and freq2.

threshold is the threshold for detecting a peak, scaled between 0 and 1. It is logarithmic, so 0.1 is half as loud as 1.0.

The update method is meant to be run in the draw loop, and frames determines how many loops must pass before another peak can be detected. For example, if the frameRate() = 60, you could detect the beat of a 120 beat-per-minute song with this equation: framesPerPeak = 60 / (estimatedBPM / 60 );

Based on example contribtued by @b2renger, and a simple beat detection explanation by Felix Turner.

Examples

Syntax

p5.PeakDetect([freq1], [freq2], [threshold], [framesPerPeak])

Parameters

freq1
Number:

lowFrequency - defaults to 20Hz

freq2
Number:

highFrequency - defaults to 20000 Hz

threshold
Number:

Threshold for detecting a beat between 0 and 1 scaled logarithmically where 0.1 is 1/2 the loudness of 1.0. Defaults to 0.35.

framesPerPeak
Number:

Defaults to 20.

Methods

update

The update method is run in the draw loop.

Accepts an FFT object. You must call .analyze() on the FFT object prior to updating the peakDetect because it relies on a completed FFT analysis.

onPeak

onPeak accepts two arguments: a function to call when a peak is detected. The value of the peak, between 0.0 and 1.0, is passed to the callback.

Notice any errors or typos? Please let us know. Please feel free to edit lib/addons/p5.sound.js and open a pull request!

Related References