Skip to content

Instantly share code, notes, and snippets.

@bilalsavas
Created April 15, 2013 12:29
Show Gist options
  • Save bilalsavas/5387703 to your computer and use it in GitHub Desktop.
Save bilalsavas/5387703 to your computer and use it in GitHub Desktop.
/**
* This sketch demonstrates how to use an FFT to analyze
* the audio being generated by an AudioPlayer.
* <p>
* FFT stands for Fast Fourier Transform, which is a
* method of analyzing audio that allows you to visualize
* the frequency content of a signal. You've seen
* visualizations like this before in music players
* and car stereos.
*/
import ddf.minim.analysis.*;
import ddf.minim.*;
Minim minim;
AudioPlayer jingle;
FFT fft;
int meh = 0;
void setup()
{
size(1024, 800, P3D);
minim = new Minim(this);
// specify that we want the audio buffers of the AudioPlayer
// to be 1024 samples long because our FFT needs to have
// a power-of-two buffer size and this is a good size.
jingle = minim.loadFile("aroundtheworld.mp3", 1024);
// loop the file indefinitely
jingle.loop();
// create an FFT object that has a time-domain buffer
// the same size as jingle's sample buffer
// note that this needs to be a power of two
// and that it means the size of the spectrum will be half as large.
fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
}
void draw()
{
pushMatrix();
translate(0,meh,0);
fill(255,255,255,10);
rect(0,0,width,height);
stroke(20);
// perform a forward FFT on the samples in jingle's mix buffer,
// which contains the mix of both the left and right channels of the file
fft.forward( jingle.mix );
for(int i = 0; i < fft.specSize(); i++)
{
stroke(10);
// draw the line for frequency band i, scaling it up a bit so we can see it
ellipse( i+512, 400, i, fft.getBand(i)*30 );
ellipse( 1024-(i+512), 400, i, fft.getBand(i)*30 );
}
popMatrix();
// meh++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment