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 & HelpPrograms › Need help optimizing some code
Page Index Toggle Pages: 1
Need help optimizing some code (Read 576 times)
Need help optimizing some code
Oct 20th, 2009, 5:00pm
 
Hi!

Im trying to do kind off an interactive music video. The idea is that you can paint some strokes across a moving canvas and some pre-animated animations (mov's) will trigger at certain times.

Im new to processing, but have worked a couple of weeks to get it done. However i could really use some help with the following:

1.
Optimizing the code to make the paint strokes more smooth (im using lines now and was thinking "Quad's" could be better but havnt had luck in getting it to work)

2. My "timer" that supposed to trigger videoclips as messed up. It calls a video clip repeadatly and doesnt work that well. I think this could be done in a much better way

I know the code is kind of messy - and im asking this to get some tips on doing this more elegantly and the right way.

the code:
[code]

//import processing.opengl.*;
import ddf.minim.analysis.*;
import ddf.minim.*;
import jmcvideo.*;
import processing.opengl.*;
import seltar.motion.*;


JMCMovie myMovie, bgMovie;
URL url;
Motion Pendel;
Minim minim;
AudioPlayer jingle;
FFT fft;

int num = 200;
float mx[] = new float[num];
float my[] = new float[num];
float mw[] = new float[num];
float ms[] = new float[num];

float p;
float setMyStroke;
float a,b = 0.0;

int offset;
int startingTime;
float inc = TWO_PI/250.0;


//------------------------------------------------------------------------------
--
// SETUP
//------------------------------------------------------------------------------
--

void setup()
{

 size(640, 360);
 
 //set starting time
 startingTime = millis();
 //fullscreen
 // size(screen.width, screen.height);
 
 Pendel = new Motion(width/2,height/2);
 Pendel.setDamping(0.90);

 
 bgMovie = movieFromDataPath("bg_2.mov");
 myMovie = movieFromDataPath("sun.mov");
 bgMovie.loop();
 myMovie.loop();
 myMovie.mask(myMovie);
 


 smooth();

 minim = new Minim(this);
 jingle = minim.loadFile("PlayingRecords.mp3", 2048);
 // loop the file
 jingle.loop();
 // create an FFT object that has a time-domain buffer the same size as jingle's sample buffer
 // and a sample rate that is the same as jingle's
 // note that this needs to be a power of two
 // and that it means the size of the spectrum will be 1024.
 // see the online tutorial for more info.
 fft = new FFT(jingle.bufferSize(), jingle.sampleRate());
 // use 128 averages.
 // the maximum number of averages we could ask for is half the spectrum size.
 fft.linAverages(1);


 //myMovie = new Movie(this, "bg_.mov");
 //myMovie.loop();

 //noStroke();
 //stroke(255);
 //frameRate(30);
}





JMCMovie movieFromDataPath(String filename)
{
 return new JMCMovie(this, filename, RGB);
}





//------------------------------------------------------------------------------
--
// DRAW
//------------------------------------------------------------------------------
--




void draw()
{



//------------------------------------------------------------------------------
--
// TIMER
//------------------------------------------------------------------------------
--

 if(mousePressed == false) {
   noCursor();
 }
 else {
   noCursor();
 }

 
 
 int seconds = (millis() - startingTime) /1000;
//println(seconds);


if (seconds == 5)
{
 
 //playVideo(1);
 
}
   

 

background(255);
image(bgMovie, 0,0 );
bgMovie.loop();
//image(myMovie, 250,0 );


 // perform a forward FFT on the samples in jingle's mix buffer
 // note that if jingle were a MONO file, this would be the same as using jingle.left or jingle.right
 fft.forward(jingle.mix);

 for(int i = 0; i < fft.avgSize(); i++)
 {
   // draw the line for frequency band i, scaling it by 4 so we can see it a bit better
   //line(i, height, i, height - fft.getBand(i)*4);
   p=2;
   setMyStroke = abs(p - fft.getAvg(i)*2);
   ellipseMode(CENTER);
   fill(0,abs(p - fft.getAvg(i)*25));
   noStroke();
   ellipse(pmouseX, pmouseY, 5- fft.getAvg(i)*25, 5 - fft.getAvg(i)*25);
 
 }


 // Reads throught the entire array
 // and shifts the values to the left
 for(int i=1; i<num; i++) {
   mx[i-1] = mx[i];
   my[i-1] = my[i];
   mw[i-1] = mw[i];
   ms[i-1] = ms[i];  

 }

 b = cos(a);

 Pendel.springTo(mouseX,mouseY);
 Pendel.move();

 fill(0);
 noStroke();
 //line(mouseX,mouseY,Pendel.getPX(),Pendel.getPY());
 ellipse(mouseX,mouseY,5+Pendel.getDistance()*4,5+Pendel.getDistance()*4);



 // Add the new values to the end of the array
 mx[num-1] = mouseX + Pendel.getDistance()/2;
 my[num-1] = mouseY + Pendel.getDistance()/2;
 mw[num-1] = setMyStroke + 5+abs(sin(a)*15);
 ms[num-1] = mouseX + sin(a)*25;

 for(int i=1; i<num; i++) {
   //println(ftt.getAvg(i));
   stroke(0);
   strokeWeight(mw[i-1]/2);
   //println(setMyStroke);
   //strokeWeight(setMyStroke);
   //strokeJoin(BEVEL);

 
   //MAIN STROKE - weee
   line(mx[i-1], my[i-1]++, mx[i], my[i]++);
   //ellipse(mx[i-1], my[i-1]++, 10, 10);

   //DUDLICATE STROKES - messy code
   //
   strokeWeight(mw[i-1]/4);
   stroke(50);

   offset = 50;
   line(ms[i-1]-offset, my[i-1]++ +20, ms[i]-offset, my[i]++ +20);
   line(ms[i-1]+offset, my[i-1]++ +20, ms[i]+offset, my[i]++ +20);
   //line(ms[i-1], my[i-1]++, ms[i], my[i]++);


    //line(mx[i-1]+sin(b)*25-50, my[i-1]++, mx[i]+sin(b)*25-50, my[i]++);
   //line(mx[i-1]+sin(b)*25+50, my[i-1]++, mx[i]+sin(b)*25+50, my[i]++);
   //strokeWeight(4);
}

 a = a + inc;

//------------------------------------------------------------------------------
--
// END DRAW

}

//------------------------------------------------------------------------------
--




//------------------------------------------------------------------------------
--
// BUTTON input goes here - randomize strokes
//------------------------------------------------------------------------------
--
void keyReleased()
{
 if ( key == 'w' )
 {
   a = random(25);
 }
}


void stop()
{
 // always close Minim audio classes when you finish with them
 jingle.close();
 minim.stop();
 super.stop();
}

Re: Need help optimizing some code
Reply #1 - Oct 20th, 2009, 5:01pm
 
and the final part of the script...
Code:

void playVideo(int i) {

//println("play new");
//myMovie.stop();
println(i);
myMovie.switchVideo(i+".mov");
myMovie.play();
myMovie.mask(myMovie);
//image(myMovie,200,0,);
//imageMode(CENTER);
}

Page Index Toggle Pages: 1