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:
class BeatListener implements AudioListener
private BeatDetect beat;
private AudioPlayer source;
BeatListener(BeatDetect beat, AudioPlayer source)
this.source = source;
this.beat = beat;
void samples(float samps)
void samples(float sampsL, float sampsR)
float kickSize, snareSize, hatSize;
song = Minim.loadFile("song.mp3", 1024);
// 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());
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);
if ( beat.isKick() ) kickSize = 32;
if ( beat.isSnare() ) snareSize = 32;
if ( beat.isHat() ) hatSize = 32;
rect(width/4, height/2, 20, 20);
rect(width/2, height/2, 20, 20);
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);
// always close Minim audio classes when you are finished with them
// always stop Minim before exiting
// this closes the sketch
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.