Manual: BeatDetect

[ javadoc | examples ]

The BeatDetect class allows you to analyze an audio stream for beats (rhythmic onsets). Beat Detection Algorithms by Frédéric Patin describes beats in the following way:

The human listening system determines the rhythm of music by detecting a pseudo-periodical succession of beats. The signal which is intercepted by the ear contains a certain energy, this energy is converted into an electrical signal which the brain interprets. Obviously, The more energy the sound transports, the louder the sound will seem. But a sound will be heard as a beat only if his energy is largely superior to the sound’s energy history, that is to say if the brain detects a brutal variation in sound energy. Therefore if the ear intercepts a monotonous sound with sometimes big energy peaks it will detect beats, however, if you play a continuous loud sound you will not perceive any beats. Thus, the beats are big variations of sound energy.

The two algorithms in BeatDetect are based on two algorithms described in that paper.

Beat Detection

[snip java]
// Create a BeatDetect object that is in SOUND_ENERGY mode.
BeatDetect()
// Create a BeatDetect object that is in FREQ_ENERGY mode
// and expects a sample buffer with the requested attributes.
BeatDetect(int timeSize, float sampleRate)
// Analyze the samples in ab.
void detect(AudioBuffer ab)
// Constant used to request frequency energy tracking mode.
static int FREQ_ENERGY
// Constant used to request sound energy tracking mode.
static int SOUND_ENERY
// Set the detection mode to use
void detectMode(int algo)
// Sets the sensitivity of the algorithm (in milliseconds)
void setSensitivity(int s)
[/snip]

To use BeatDetect, inside of draw() you must call the detect method, passing the AudioBuffer you want to analyze. BeatDetect has two modes: sound energy tracking and frequency energy tracking. In sound energy mode, the level of the buffer, as returned by level(), is used as the instant energy in each frame. Beats, then, are spikes in this value, relative to the previous one second of sound. In frequency energy mode, the same process is used but instead of tracking the level of the buffer, an FFT is used to obtain a spectrum, which is then divided into average bands, and each of these bands is tracked individually. The result is that it is possible to track sounds that occur in different parts of the frequency spectrum independently (like the kick drum and snare drum). The setSensitivity method is used to put a damper on the analysis. If you set the sensitivity to 200, every time the algorithm detects a beat it will wait 200 milliseconds before testing for a beat again, returning false in the meantime. You can use this to dampen the algorithm if it is giving too many false-positives. The default value is 10, which is essentially no damping. If you try to set the sensitivity to a negative value, an error will be reported and it will be set to 10 instead.

Sound Energy Mode

[snip java]
// returns true when a beat has been detected.
boolean isOnset()
[/snip]

In sound energy mode you use isOnset() to query the algorithm. This will return true if a “beat” has been detected. But remember, because it is simply the level of the audio being analyzed, this merely represents significant spikes in the level. If you are analyzing dance music, this will track the kick drum pretty well.

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/BeatDetect/SoundEnergy/SoundEnergy.pde[/snip]

Frequency Energy Mode

[snip java]
// returns true when a beat has been
// detected in the ith frequency band.
boolean isOnset(int i)
// returns true if a beat corresponding to the
// frequency range of a kick drum has been detected.
boolean isKick()
// returns true if a beat corresponding to the
// frequency range of a snare drum has been detected.
boolean isSnare()
// returns true if a beat corresponding to the
// frequency range of a hi hat has been detected.
boolean isHat()
// returns true if at least threshold bands of the
// bands included in the range [low, high] have registered a beat.
boolean isRange(int low, int high, int threshold)
[/snip]

In frequency energy mode you use the above methods to query particular frequency bands or ranges of frequency bands. It should be noted that isKick(), isSnare(), and isHat() merely call isRange() with values determined by testing the algorithm against music with a heavy beat and they may not be appropriate for all kinds of music. If you find they are performing poorly with your music, you should use isRange() directly to locate the bands that provide the most meaningful information for you. The example below also demonstrates how you might write an AudioListener class that catches buffers for you so that you don’t have to call the detect method in draw().

Code Sample (online example)

[snip code_sample]http://code.compartmental.net/minim/examples/BeatDetect/FrequencyEnergy/FrequencyEnergy.pde[/snip]

17 thoughts on “Manual: BeatDetect

  1. Pingback: Minim and Beat Detection | matthewbrown.net.au

  2. Pingback: z3219928’s blog » Blog Archive » soooundd - multiWeek7

  3. Pingback: Sabrina’s External Brain… | Week 7: Sound and Sound Input

  4. Hmmm, getting: Cannot find a class or type named “BeatListener” in the example and my own code.

  5. I am not clear on how the isOnset(int i) method works.

    What is the maximum number of frequency bands? I assumed that the maximum is 32 since the paper you cite uses 32.

  6. This is how the array accessed by isOnset(int) is created:

    spect = new FFT(timeSize, sampleRate);
    spect.logAverages(60, 3);
    int numAvg = spect.avgSize();
    fIsOnset = new boolean[numAvg];
    

    The logAverages function is asking the FFT to compute the averages with a minimum bandwidth of 60 Hz and 3 bands per octave. I can’t do the math in my head right at the moment, but you should be able to figure out how many slots that is, considering the highest frequency band in the FFT is likely to be 22050 Hz.

    Regardless, you bring up a good point: there is no way to query for how many onset slots there are. That should be fixed.

  7. I’m getting “Cannot find a class or type named “BeatListener” in the example and my own code too.

    Can anyone help?!

  8. Hi, i am trying to use the beatdetection with an audio live input from the linein. The line in Example works fine, and the Beat detection with an mp3 file works as well. But is it possible to use the beat detection with the linein? I cant get it to work.

  9. Pingback: XX :: Heart Skipped a Beat « mediadump

  10. cerr,

    I just wrote beat detection that uses the the line-in as input. The hardest part i found was configing my computers settings to get the right input, but if you got line-in example to work then you should be fine. Here’s parts of my code that my help:

    minim = new Minim(this);
    in = minim.getLineIn();
    //bpf filter helps beatDetection algo when bass beat detection is only //wanted
    bpf = new BandPass(55, 12, in.sampleRate());
    in.addEffect(bpf);
    beat.detect(in.mix);
    if ( beat.isOnset() )
    {
    //what you want it to do
    }

    hope this helps.

  11. forgot 2 line safter in.addEffect(bpf);
    add in:

    beat = new BeatDetect();
    beat.setSensitivity(275);

  12. Pingback: kellective» Sound visualisation

  13. “It should be noted that isKick(), isSnare(), and isHat() merely call isRange() with values determined by testing the algorithm against music with a heavy beat and they may not be appropriate for all kinds of music.”

    What exactly would those values be?

  14. just wanted to thank cal for that fft from input code! I’ve been learning processing for about a week now and that just made things so much clearer.

    And thank you to everyone else who has contributed to the excellent code examples and helpful forum comments. I’m really excited about what i can do with processing.

  15. Hmnn also trying to combine it with line in using the code posted but having no luck.

    import ddf.minim.*;
    import ddf.minim.effects.*;
    import ddf.minim.analysis.*;

    Minim minim;
    AudioInput in;
    BandPass bpf;
    BeatDetect beat;
    BeatListener bl;

    void setup()
    {
    minim = new Minim(this);
    in = minim.getLineIn();
    bpf = new BandPass(55, 12, in.sampleRate());
    in.addEffect(bpf);
    beat = new BeatDetect(in.bufferSize(), in.sampleRate());
    beat.setSensitivity(300);
    bl = new BeatListener(beat, in);

    }
    void draw(){

    if ( beat.isOnset() )
    {
    println(“beat!”);
    }
    }

    void stop()
    {
    // always close Minim audio classes when you are finished with them
    in.close();
    // always stop Minim before exiting
    minim.stop();
    // this closes the sketch
    super.stop();
    }
    class BeatListener implements AudioListener
    {
    private BeatDetect beat;
    private AudioInput source;

    BeatListener(BeatDetect beat, AudioInput source)
    {
    this.source = source;
    this.source.addListener(this);
    this.beat = beat;
    }

    void samples(float[] samps)
    {
    beat.detect(source.mix);
    }

    void samples(float[] sampsL, float[] sampsR)
    {
    beat.detect(source.mix);
    }
    }