We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSound,  Music Libraries › Realtime audio Spectrum Analysis
Page Index Toggle Pages: 1
Realtime audio Spectrum Analysis (Read 1310 times)
Realtime audio Spectrum Analysis
Mar 18th, 2008, 10:07pm
 
Hi,
I'm in need of some expert help. I'm currently working on a project that visualizes sound in realtime. what I'm trying to do is inform people of the range of sound that they are hearing. In order to do this, i've split the frequency spectrum up into low, mid, and high ranges noted by blue, red, and yellow. I'm trying to make the form of these frequency lines so the low range is made up of curves, the middle straight, and the high is sharper edged shapes. and i want all three ranges to form in the center of the screen, like a floating organism. I am having trouble creating the appropriate forms and need some assistance. I want the amplitude to determine the y axis of the form, so it grows vertically from the center(both upwards and downwards), and i want the form to represent a sort of oscilloscope looking thing. Can anyone help? P.s. I'm an amateur processor and thank you in advance for any assistance. I realize my code is a disaster. Thanks.....


import krister.Ess.*;

int bufferSize;
int steps;
float limitDiff;
int numAverages=32;
float myDamp=.1f;
float maxLimit,minLimit;
SoundLayer lowLayer;
SoundLayer midLayer;
SoundLayer highLayer;

//render type switch
boolean block = true;

FFT myFFT;
AudioInput myInput;

void setup () {
 size(1024,768);
 smooth();
 // start up Ess
 Ess.start(this);  

 // set up our AudioInput
 bufferSize=512;
 myInput=new AudioInput(bufferSize);

 // set up our FFT
 myFFT=new FFT(bufferSize*2);
 myFFT.equalizer(true);

 // set up our FFT normalization/dampening
 minLimit=.005;
 maxLimit=.05;
 myFFT.limits(minLimit,maxLimit);
 myFFT.damp(myDamp);
 myFFT.averages(numAverages);

 // get the number of bins per average
 steps=bufferSize/numAverages;

 // get the distance of travel between minimum and maximum limits
 limitDiff=maxLimit-minLimit;

 frameRate(25);      

 lowLayer = new SoundLayer();
 lowLayer.lowPoint = 0;
 lowLayer.highPoint = 180;
 lowLayer.x = 100;
 lowLayer.y = 100;
 lowLayer.objectID = 0;

 midLayer = new SoundLayer();
 midLayer.lowPoint = 180;
 midLayer.highPoint = 256;
 midLayer.x = 100;
 midLayer.y = 100;
 midLayer.objectID = 1;

 highLayer = new SoundLayer();
 highLayer.lowPoint = 256;
 highLayer.highPoint = 512;
 highLayer.x = 100;          //offets the line
 highLayer.y = 100;
 highLayer.objectID = 2;
 myInput.start();

 myInput.start();
}

void draw()
{
 //background(0);
 fill(0, 100); //TRAILS
 rect(0, 0, width, height);
 translate (200,100);
 //noStroke();
 lowLayer.update();
 midLayer.update();
 highLayer.update();

}

void mousePressed()
{
 block = false;
}
void mouseReleased()
{
 block = true;
}

public void audioInputData(AudioInput theInput)
{
 myFFT.getSpectrum(myInput);
}

class SoundLayer
{
 int lowPoint = 0;
 int highPoint = 512;
 int edginess = 0;
 int x = 0;
 int y = 0;
 int objectID = 0;
 boolean show = true;
 float threshold = 0.03;


 void update()
 {
   pushMatrix();
   translate(x, y);

   //looping through only this layer's portion of the sound buffer
   for (int i= lowPoint; i<highPoint; i++)
   {
     float left=160;
     float right=160;
     float amplitude = myFFT.spectrum[i]; //amp at this frequency
     float r = random(-50, 255);
     left-=amplitude*2050.0; //650 changes the size growth of the spectrum lines
     right-=amplitude*2050.0;

     if(frameCount % 30 == 0)
     {
       println(amplitude);
       println("LEFT : " + left);
       println("RIGHT: " + right);
     }


     if(amplitude < threshold)
     {
       show = false;
     }
     else
     {
       show = true;
     }

     //DRAWS THE VISUALIZATION
     if(show)
     {
       if (objectID == 0)
       {
         //DRAW THE LOW
         stroke(i,50,i+100, i);
         //line(i,i,50+i,left*i);
         //line(left,i, 50+i, left);
         //line(left,i,i,right);
         //line(left,i,2*i,right);
         //bezier(i, r, amplitude+r, r, left*r, r,right+i, r);
         line(1000+i,i-100,-800+i,r);

       }
       else if (objectID == 1)
       {
         //DRAW THE MID
         stroke(100+i, 0, i*r, amplitude+100);
         noFill();
         //curve(i/i, i-r, 10*r, i, r, i, left*r, i);
         //line(left,i,2*i,right);
         //line(left,left,50+i,right);
        //bezier(i, r, amplitude+r, r, left*r, r,right+i, r);
       }
       else if(objectID == 2)
       {
         //DRAW THE HIGH
         stroke(i, i, 0, right);
         //rect(i, left*i, i, right*i);
         //line(i, left,i,left);
         //line(sin(left),cos(i),i*r,sin(right));
         //bezier(i, r, amplitude+r, r, left*r, r,right+i, r);
         //curve(i/r,sin(r), amplitude*r, r, i, i, left*r, i);
         //line(sin(i),i,i*r,r);
       }

     }
   }

   popMatrix();
 }
}
Page Index Toggle Pages: 1