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 › saveImage and Framerate
Page Index Toggle Pages: 1
saveImage and Framerate (Read 596 times)
saveImage and Framerate
Aug 5th, 2008, 12:19am
 
hi there!
i guess my question has a pretty simple answer for most of you:

I did a small program that create something similar to the rain, my final aim is to relate the movements of the drops with a music track. Since I'm not yet enough skilled to do an algorithm that does that for me, i created some kinds of interaction that let me act as "VJ".

The problem is that if I add the saveImage function, the frame rate slow down too much, and i can't anymore interact in a fluent way.
Any ideas?

i post the code:




class goccia{
float vy;
float ay;
float px, py;
float kx;
float oldx,oldy;
float w,h;
float d,dx;
float ky = random (0,300);
float kBounce,kkx,kSw;

void update(){

 vy+=ay;
  py+=vy;

/*  d = dist (mouseX,mouseY,px,py);

 dx = (mouseX-px);

 if (dx<0){

 px+=pow((1/d)*100,4);
 }
 if(dx>0){
   px-=pow((1/d)*100,4);
 }



if(py > height-ky){

 vy = -(vy/5);
 //py=height-50;
 kx = random (-5,5);
 

 //for (int i=0; i< 5; i++){


 w = random(5,50);
 h = random(2,10);
 noFill();
 stroke (0,20);
 ellipse (px,py,w,h);


 // }
}
*/
 if(py > height-10){
       vy = -(vy/(1*kBounce));
     //  kx = random (-5,5);
   }
 
  if(vy <0){

       kx = random (-5,5);
   }
}
void render(){
 
   stroke (250);
   strokeWeight(1+kSw);  
   line (px-kkx-kx,py,px+kkx+kx,py);
   line (px-kkx-kx,py+5,px+kkx+kx,py+5);
}

void init (){
 oldy = py = 0;
 oldx = px = random (0,width);
 ay = random (0.3,0.9);
 kx=0;
   }
}

int nPioggia = 400;
int frame;
int kkx,kSw;
goccia[] pioggia = new goccia [nPioggia];

 


void setup (){
 


 size (1400,700);


 for (int i=0;i<nPioggia;i++){
   pioggia[i] = new goccia();
 }


 for (int i=0;i<nPioggia;i++){
   pioggia[i].init();
}

}



void draw(){
 


   if (frameCount % 1 == 0){
   frame++;
   int indice = frame % nPioggia;
   pioggia[indice].init();
   }
 


fill(0, 0, 0,30);
noStroke();
rect(0,0,width,height);

for (int i=0; i<nPioggia;i++){
pioggia[i].update();
pioggia[i].render();
}

//interazioni

if (keyPressed==true){
//incrementa lunghezza linee  
    if(key=='v'){
       for (int i=0; i<nPioggia;i++){
       pioggia[i].kkx+=2;
       }
     }
    if(key=='f'){
       for (int i=0; i<nPioggia;i++){
       pioggia[i].kkx=random(-1,20);
       pioggia[i].kSw=3;  
       }
     }      
    if(key=='p'){
      for (int i=0; i<nPioggia;i++){
         //if ((pioggia[i].py>300) && (pioggia[i].py<303)){
          noStroke();
          fill(255);
          rect(pioggia[i].px-10,pioggia[i].py-10,20,20);
          pioggia[i].kSw=0;
        //}
      }  
    }
    if(key=='d'){
      for (int i=0; i<nPioggia;i++){
          pioggia[i].kBounce=random(1,2);
         if (pioggia[i].vy<=0){
          pioggia[i].kSw=3;
         
          }
      }  
    }    
}
   else{
   for (int i=0; i<nPioggia;i++){
   pioggia[i].kkx=5;
   pioggia[i].kSw=0;
   pioggia[i].kBounce=5;
   }  
}

//smooth();
saveFrame("line-####.tif");
}

 void keyReleased() {
   if (keyCode == BACKSPACE) {
      for (int i=0; i<nPioggia;i++){
         pioggia[i].init();
         }
    }
     

 }
   

Re: saveImage and Framerate
Reply #1 - Aug 5th, 2008, 12:56am
 
It is normal that frame rate drops when you save each frame: it is a time consuming task, even more with the quite big size you set up.
Usually, you need real time, so no need for saveFrame, or you need a succession of images (like you) or a movie, and the speed isn't really important (when making a movie, the rate is slow but the result runs at full film speed, of course).
A possible way of improvement could be to make a virtual disk in RAM: writing there should be must faster! Or just save the frames in an array or an ArrayList, which is nearly the same thing (without the compression).
It depends on the need you have, ie. why you save these frames.
Re: saveImage and Framerate
Reply #2 - Aug 5th, 2008, 4:58am
 
First of all thanks for the reply. Actually it is pretty obvious that the saveFrame would affect the frame rate. Oh whatever!
I need to play "live" with the project I did on a track of music. Then, I was thinking of putting the sequence in some editing sw and adding the track. I don't know other ways to sync music and images.

I don't know yet the array technique, I'll try with that. The virtual memory solution I didn't really understand it. I know what is the virtual ram, but how do you set it?
Thanks again
Re: saveImage and Framerate
Reply #3 - Aug 5th, 2008, 6:31am
 
Different operating systems offer different ways of creating RAM disks. Linux and OS X offer text file configuration or command line methods. You may need additional software for Windows. Google for "create RAM disk".

However:

1. The alleged benefit presupposes the blocking time saveFrame takes comes from IO (input/ouput) delay writing to your persistent storage medium.
2. The extra blocking time could come from inefficiencies in the saveFrame algorithm in capturing the frame, not the IO performance of your persistent storage medium. If so, improving your IO performance with a RAM disk will not help.
3. Each of your frames currently creates a 2.8MB TIF file.
4. Even if your machine has 2GB of RAM and you create a 1.5GB RAM disk, recording 2.8MB TIF files into will give you only 22 seconds worth of recorded frames assuming a frame rate of 24 frames per second.

The TIF image format stores full information for every pixel, resulting in unnecessarily large files. You could store images in PNG format to dramatically decrease the size of the individual saved frames (test run shows roughly 65KB average file size) without losing any picture quality because the PNG algorithm losslessly compresses the image:

saveFrame("line-####.png");

This would extend the maximum time of frame capture to a limited-size RAM disk (about 15 minutes into 1.5GB at 24 frames per second). However, this may just replace IO time with compression time in slowing down the frame rate as it still runs slowly.

If you really need simultaneous live performance and recording, you may want to look at a screen capture tool like Fraps (Google it) that developers have optimized for low-utilization screen capture while an intensive application runs (such as a video game), rather than attempting to solve the problem in Processing code. If saveFrame does not block because of pure IO performance, you will find it difficult to address the issue without changing the source code of Processing itself.
Re: saveImage and Framerate
Reply #4 - Aug 5th, 2008, 10:37am
 
smith did a very good analysis, I will just tell virtual Ram is actually Ram simulated by writing on disk, so it is just the opposite: making believing you have lot of Ram by using a (very slow) medium we have aplenty.
Ram disk is a bunch of memory you allocate, which behaves like a regular disk, just must faster. So you lift (most of) the I/O latency, which isn't small for a 3MB file. But as pointed out, unless you have lot of memory and a good manager (32bit Windows XP is limited to 3GB), you will quickly hit a limit. And if you use a compressed format, lot of CPU time will be spent in the compression algorithm.
Page Index Toggle Pages: 1