Posts Tagged ‘sound energy’

Minim and Beat Detection

Wednesday, September 17th, 2008

While I was looking through the Minim documentation, I came across the built in BeatDetect class that analyses the audio input for beats. My understanding is that it looks for patterns in the rhythm to determine individual beats of a particular type, be it the bass drum, snare, cymbals etc. It has a couple of modes for detecting beats, one is sound energy mode and the other is frequency energy mode.

Sound energy mode just looks for spikes in the audio mix levels and returns a boolean response of true when a spike occurs.

Frequency energy mode allows you to look at particular frequency bands and use their output the same way you would the output of sound energy mode.

I decided to have a play with the sample code using frequency energy mode that is provided in the documentation to figure out how to control it and so on.

The sample code checks the frequency bands for the snare, kick/bass drum and high hats and then has text for each one that responds to the audio input for their particular band.

I didn’t do anything overly complex with this as of yet, just changed the objects that are manipulated to boxes rather than text so I could get an idea of of what does what.

Here is the code that I ended up with after my changes:

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

class BeatListener implements AudioListener
{
private BeatDetect beat;
private AudioPlayer source;

BeatListener(BeatDetect beat, AudioPlayer 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);
}
}

AudioPlayer song;
BeatDetect beat;
BeatListener bl;

float kickSize, snareSize, hatSize;

void setup()
{
size(512, 200);
smooth();
Minim.start(this);
song = Minim.loadFile("song.mp3", 1024);
song.play();
// a beat detection object that is FREQ_ENERGY mode that
// expects buffers the length of song's buffer size
// and samples captured at songs's sample rate
beat = new BeatDetect(song.bufferSize(), song.sampleRate());
beat.setSensitivity(300);
kickSize = snareSize = hatSize = 16;
// make a new beat listener, so that we won't miss any buffers for the analysis
bl = new BeatListener(beat, song);
}

void draw()
{
background(0);
fill(255);
if ( beat.isKick() ) kickSize = 32;
if ( beat.isSnare() ) snareSize = 32;
if ( beat.isHat() ) hatSize = 32;
stroke(255);
strokeWeight(kickSize);
rect(width/4, height/2, 20, 20);
stroke(255);
strokeWeight(snareSize);
rect(width/2, height/2, 20, 20);
stroke(255);
strokeWeight(hatSize);
rect(3*width/4, height/2, 20, 20);
kickSize = constrain(kickSize * 0.95, 16, 32);
snareSize = constrain(snareSize * 0.95, 16, 32);
hatSize = constrain(hatSize * 0.95, 16, 32);
}

void stop()
{
// always close Minim audio classes when you are finished with them
song.close();
// always stop Minim before exiting
Minim.stop();

// this closes the sketch
super.stop();
}

For this to work, make sure you have an MP3 or Wav file in a data folder within the sketch directory for this sketch and change song.mp3 to the filename of your song.