Download BeatDetect
See BeatDetect in action.
BeatDetect is a Processing object for use with the Ess sound library. It is a modified implementation of the sound energy algorithm outlined in Beat Detection Algorithms by Frédéric Patin. You can use it to detect onsets (beats) in an AudioInput. It has two modes: sound energy detection and frequency energy detection. Sound enery detection determines if there has been an onset (beat) in a frame by tracking the level of the signal and registering peaks. Frequency energy detection uses the same algorithm but tracks frequency bands, giving more detailed information about where in the spectrum the onset occurred. This is useful for tracking just the kick drum, for instance. Keep in mind that frequency energy detection requires a lot more computation than sound energy does.
BeatDetect()
BeatDetect(int size, int sampleRate)
By default, BeatDetect assumes you are using an an input with a size of 1024 and a sample rate of 44100. If you are using a different size or sample rate you must specify both when calling the constructor.
detectMode(String mode)
The two modes are "SOUND_ENERGY" and "FREQ_ENERGY". If detectMode is not called, "SOUND_ENERGY" will be used. The mode can be changed at any time.
detectDetail(int numBands)
This method sets how many frequency bands are used by the "FREQ_ENERGY" mode. If detectDetail is not called, "FREQ_ENERGY" will use 64 bands. The number of frequency bands can be changed at any time.
detect(AudioInput in)
To use this object, its detect method must be called every time the active AudioInput's buffer is filled. This can be done quite simply:
public void audioInputData(AudioInput in)
{
detector.detect(in);
}
The following methods can be used to determine whether an onset has occurred and what kind of sound it is:
isOnset()
Returns true if there has been an onset (beat) in this frame. It will work with either mode.
isOnset(int freqBand)
Returns true if freqBand registers onset. freqBand can range from 0 to numBands - 1. If detectDetail is not explicitly called, numBands is 64.
isKick()
Returns true if the onset is a kick drum. Technically, it returns isOnset(0), which is simply the lowest frequency band. This means that bass guitars and other loud sounds in the lower end of the spectrum might cause this to return true. In the case of dance music, it pretty reliably tracks the kick drum.
isSnare()
Returns true if the onset is a snare hit. Technically, it is testing a range of frequency bands, which means that it is sometimes triggered by sounds other than a snare.
isHat()
Returns true is the onset is a hi-hat hit. This is also testing a range of bands and may not always respond to hi-hat hits, particularly if they are not loud in the mix.isRange(int low, int high, int threshold)
Returns true if at least threshold bands register an onset between low and high, inclusive. low must not be less than 0 and high must not be greater than numBands - 1. If you know what music you will be analysing, you will want to use this method to more accurately detect the sounds you want to use as triggers.
drawGraph()
This is a diagnostic function that draws different things depending on which mode you are in. In "FREQ_ENERGY" mode it draws bars along the bottom of the screen showing which frequency bands are registering an onset. This is useful for determining your own ranges to test. I used this visualization to decide on the ranges for isSnare() and isHat(). I leave the understanding of the two graphs drawn in "SOUND_ENERGY" mode as an exercise for the reader.