Skip to content

Instantly share code, notes, and snippets.

@memish
Created December 7, 2018 18:23
Show Gist options
  • Save memish/a9009b4f0d44756878a8e940f9608379 to your computer and use it in GitHub Desktop.
Save memish/a9009b4f0d44756878a8e940f9608379 to your computer and use it in GitHub Desktop.
/**
* This sketch demonstrates how to use an FFT to analyze an
* AudioBuffer and draw the resulting spectrum. <br />
* It also allows you to turn windowing on and off,
* but you will see there is not much difference in the spectrum.<br />
* Press 'w' to turn on windowing, press 'e' to turn it off.
FIND the highest 100 -- there can be ties
*/
import ddf.minim.analysis.*;
import ddf.minim.*;
Minim minim;
AudioPlayer jingle;
AudioInput input;
FFT fft;
int[][] colo=new int[300][3];
//AudioIn in;
void setup()
{
size(480, 320);
//fullScreen();
noCursor();
for(int i = 0; i < 300; i++)
{
int ran = (int)random(255);
int ran2 = (int)random(255);
int ran3 = (int)random(255);
colo[i][0]=ran;
colo[i][1]=ran2;
colo[i][2]=ran3;
}
minim = new Minim(this);
input = minim.getLineIn();
// 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 512. see the online tutorial for more info.
fft = new FFT(input.bufferSize(), input.sampleRate());
// textFont(createFont("Arial", 16));
// windowName = "None";
}
void draw()
{
background(0);
stroke(255);
// perform a forward FFT on the samples in jingle's left buffer
// note that if jingle were a MONO file,
// this would be the same as using jingle.right or jingle.left
fft.forward(input.mix);
int spotx=0;
int spoty=10;
int spoter=0;
for(int i = 0; i < fft.specSize(); i++)
{
if(i%4==0){
fill(colo[spoter][0],colo[spoter][1],colo[spoter][2]);
rect(spotx, spoty, fft.getBand(i)*4, 15, 7);
if(spoter%16==0 && spoter!=0){
spotx+=40;
spoty=-20;
}
spoter++;
spoty+=20;
}
}
// keep us informed about the window being used
// text("The window being used is: " + windowName, 5, 20);
}
void mousePressed()
{
changeColor();
if ( key == 'w' )
{
// a Hamming window can be used to shape the sample buffer that is passed to the FFT
// this can reduce the amount of noise in the spectrum
fft.window(FFT.HAMMING);
// windowName = "Hamming";
}
if ( key == 'e' )
{
fft.window(FFT.NONE);
// windowName = "None";
}
}
void stop()
{
// always close Minim audio classes when you finish with them
input.close();
minim.stop();
super.stop();
}
public void changeColor(){
int ran = (int)random(4);
for(int i = 0; i < 300; i++)
{
if(ran==0){
colo[i][0]=255;
colo[i][1]=0;
colo[i][2]=0;
}
if(ran==1){
colo[i][0]=255;
colo[i][1]=255;
colo[i][2]=0;
}
if(ran==2){
colo[i][0]=0;
colo[i][1]=0;
colo[i][2]=255;
}
if(ran==3){
randColor();
}
}
}
public void randColor(){
for(int i = 0; i < 300; i++)
{
int ran = (int)random(255);
int ran2 = (int)random(255);
int ran3 = (int)random(255);
colo[i][0]=ran;
colo[i][1]=ran2;
colo[i][2]=ran3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment