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 › [ESS] Lag with analysis
Page Index Toggle Pages: 1
[ESS] Lag with analysis (Read 339 times)
[ESS] Lag with analysis
Apr 2nd, 2008, 10:28pm
 
I am using the code below to just look at the FFT analysis of the song that is playing except that the analysis doesn't react to the music and its actually about a second late usually on my computer. Can anyone tell me how to sync this up?

I got this code originally from the Ess site and edited it a little to be able to get input from a song... Maybe I did it wrong(Most likely).
Thanks.

// Input FFT
// original code by Marius Watz <http://www.unlekker.net>
// modified by Krister Olsson <http://www.tree-axis.com>

// Showcase for new FFT processing options in Ess v2.
// Clicking and dragging changes FFT damping

// Created 27 May 2006

import krister.Ess.*;

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

FFT myFFT;
AudioFile myInput;
AudioStream myStream;

void setup () {
 size(700,221);

 // start up Ess
 Ess.start(this);  

 // set up our AudioInput
 bufferSize=512;
 myInput=new AudioFile("aw.mp3",0,Ess.READ);
 myStream = new AudioStream(256*1024);
 myStream.sampleRate(myInput.sampleRate);

 // 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);        

 myStream.start();
}

void draw() {
 background(0,0,255);
 myFFT.getSpectrum(myStream);
 // draw the waveform

 stroke(255,100);
 int interp=(int)max(0,(((millis()-myStream.bufferStartTime)/(float)myStream.duration
)*myStream.size));

 for (int i=0;i<bufferSize;i++) {
   float left=160;
   float right=160;

   if (i+interp+1<myStream.buffer2.length) {
     left-=myStream.buffer2[i+interp]*50.0;
     right-=myStream.buffer2[i+1+interp]*50.0;
   }

   line(10+i,left,11+i,right);
 }

 noStroke();
 fill(255,128);

 // draw the spectrum

 for (int i=0; i<bufferSize; i++) {
   rect(10+i,10,1,myFFT.spectrum[i]*200);
 }

 // draw our averages
 for(int i=0; i<numAverages; i++) {
   fill(255,128);
   rect(10+i*steps,10,steps,myFFT.averages[i]*200);
   fill(255);
   rect(10+i*steps,(int)(10+myFFT.maxAverages[i]*200),steps,1);
   rect(10+i*steps,10,1,200);
 }

 // complete the frame around our averages
 rect(10+numAverages*steps,10,1,201);
 rect(10,10,bufferSize,1);
 rect(10,210,bufferSize,1);

 // draw the range of normalization
 rect(600,10,50,1);
 rect(600,210,50,1);

 float percent=max(0,(myFFT.max-minLimit)/limitDiff);

 fill(255,128);
 rect(600,(int)(11+198*percent),50,1);
 rect(600,11,50,(int)(198*percent));

 // draw our damper slider
 fill(255);
 rect(660,10,30,1);
 rect(660,210,30,1);
 fill(255,128);
 rect(660,(int)(11+198*myDamp),30,1);
}

void mouseDragged() {
 mousePressed();
}

void mousePressed() {
 // set our damper
 myDamp=mouseY/(float)height;
 if (myDamp>1) myDamp=1;
 else if(myDamp<0) myDamp=0;

 myFFT.damp(myDamp);  
}

void audioStreamWrite(AudioStream theStream) {
 // read the next chunk

 int samplesRead=myInput.read(myStream);
 if (samplesRead==0) {
   // start over
   myInput.close();
   samplesRead=myInput.read(myStream);
 }
}

Re: [ESS] Lag with analysis
Reply #1 - Apr 3rd, 2008, 3:59am
 
I figured it out... It had to do with this line below...

myStream = new AudioStream(256*1024);
myStream.sampleRate(myInput.sampleRate);

I changed it to

myStream = new AudioStream();
myStream.sampleRate(myInput.sampleRate);

and now it works.
Page Index Toggle Pages: 1