How to improve sketch performance

edited December 2017 in Library Questions

I have a sketch that's using a few long arrays and image processing.

Is there a way to make openGL render the sketch, or any other simple "trick" to improve performance?

Screenshot_13

Answers

  • P2D and P3D use opengl by default

  • I've added P2D to size() but it doesn't seem to affect performance. Is there a way to quickly make a working code P2D?

  • add P2D to size()...

    asking us to fix the improvement of code we can't see is kind of impossible. you could be doing ANYTHING.

  • edited December 2017

    Sorry :) I was wondering if there's any simple code I could add to make the the sketch render with P2D.

    here's the code (sorry about the mess):

    // Daniel Shiffman
    // http://codingtra.in
    // http://patreon.com/codingtrain
    // Code for: https://ySt-rokeoutu.be/QLHMtE5xsMs
    
    import processing.video.*;
    import processing.sound.*;
    
    SoundFile piano1;
    SoundFile piano2;
    SoundFile piano3;
    SoundFile celloop;
    
    float dist = 0;
    
    float jumpcut = 1200;
    
    float volume = 0;
    
    PShader blur;
    
    // ------- array vars ------- \\ 
    
    int Length = 800;
    
    int numStroke = Length;
    int[] x = new int[numStroke];
    int[] yStroke = new int[numStroke];
    
    int numDrop = Length;
    int numSizeDrop = 800;
    int[] xDrop = new int[numDrop];
    int[] yDrop = new int[numDrop];
    int[] sizeDrop = new int[numSizeDrop];
    
    int numLetter = Length;
    int[] xLetter = new int[numLetter];
    int[] yLetter = new int[numLetter];
    float[] letterSpeed = new float [numLetter];
    int[] rotLetter = new int[numLetter];
    float[] rotLetterSpeed = new float[numLetter];
    String[] letters = {"ה", "מ", "ש", "כ", "ן", "ל", "א", "ו", "מ", "נ", "ו", "ת", "ق", "س","م", "ا","ل","م","و","س","ي","ق","ى"};
    String[] letter = new String[numLetter];
    int letterPicker = 0;
    //letter[numLetter] = letters[letterPicker];
    
    int horDirection = 1;
    int vertDirection = 1;
    
    // ------- video  capture vars ------- \\ 
    
    Capture video;
    PImage prev;
    
    float threshold = 0.0001;
    
    float motionx = 0;
    float motiony = 0;
    
    float lerpx = 320;
    float lerpy = 240;
    
    float r3 = random(0, 255);
    float g3 = random(0, 255);
    float b3 = random(0, 255);
    
    float h = random(0, 255);
    
    // ------- text vars ------- \\
    
    PFont hebFont;
    float textx = 250;
    
    // ------- SETUP ------- \\
    
    void setup() {
     background(dist);
     //size(1280, 960, P2D);
     fullScreen();
     String[] cameras = Capture.list();
     printArray(cameras);
     video = new Capture(this, cameras[4]);
     video.start();
     prev = createImage(640, 480, RGB);
    
       blur = loadShader("blur.glsl"); 
    
      piano1 = new SoundFile(this, "Piano1.aiff");
      piano2 = new SoundFile(this, "Piano2.aiff");
      piano3 = new SoundFile(this, "Piano3.aiff");
      celloop = new SoundFile(this, "celloop-mono.wav");
    
      celloop.loop();
    
     hebFont = createFont("Times New Roman", 30);
     textFont(hebFont);
     fill(0);
    
     for (int i = 0; i < Length; i++){
       letter[i] = letters[letterPicker];
          if (letterPicker == letters.length-1){
          letterPicker = 0;
         }
         else {
          letterPicker++;
         }
     }
    
    }
    
    
    void mousePressed() {}
    
    void captureEvent(Capture video) {
     prev.copy(video, 0, 0, video.width, video.height, 0, 0, prev.width, prev.height);
     prev.updatePixels();
     video.read();
    }
    
    void draw() {
     video.loadPixels();
     prev.loadPixels();
     image(video, 0, 0);
     background(lerp(dist*1000, 0, 0.1));
     //threshold = map(mousex, 0, width, 0, 100);
     threshold = 100;
    
     int count = 0;
    
     float avgx = 640;
     float avgy = 480;
    
    
    
    
     loadPixels();
     // Begin loop to walk through everyStroke pixel
     for (int x = 0; x < video.width; x++) {
      if (h > 254) {
       h = 0;
      }
      h = h + 0.01;
      for (int yStroke = 0; yStroke < video.height; yStroke++) {
       int loc = x + yStroke * video.width;
       // What is current color
       color currentColor = video.pixels[loc];
       float r1 = red(currentColor);
       float g1 = green(currentColor);
       float b1 = blue(currentColor);
       color prevColor = prev.pixels[loc];
       float r2 = red(prevColor);
       float g2 = green(prevColor);
       float b2 = blue(prevColor);
    
       float d = distSq(r1, g1, b1, r2, g2, b2);
    
       if (d > threshold * threshold) {
        avgx += video.width-x;
        avgy += yStroke;
        count++;
       }
      }
     }
    
     updatePixels();
     if (count > 200) {
      motionx = avgx / count;
      motiony = avgy / count;
     }
    
     float lerpxold = lerpx;
     float lerpyold = lerpy;
    
     if (dist(motionx, motiony, lerpx, lerpy)<jumpcut){
        lerpx = lerp(lerpx, motionx, 0.1) * 1.05; 
        lerpy = lerp(lerpy, motiony, 0.1) * 1.05;
     } else {
        lerpx = motionx*1.05;
        lerpy = motiony*1.05;
     };
    
     println(dist(motionx, motiony, lerpx, lerpy));
    
    
     dist = sqrt((lerpx - lerpxold) * (lerpx - lerpxold) + (lerpy - lerpyold) * (lerpy - lerpyold));
    
     if (lerpx - lerpxold > 0){
       horDirection = 1;
     }
     if (lerpx - lerpxold < 0){
      horDirection = -1; 
     }
    // else{
    //  horDirection = 0; 
    // }
    
     float randx = random(-15, 15);
     float randyStroke = random(-15, 15);
    
     strokeWeight(dist / 1.5);
     line(lerpxold, lerpyold, lerpx, lerpy);
    
     fill(0, 0, 0);
     strokeWeight(0);
     stroke(0);
    
    /**
    if (dist>10 && dist<13){
     piano1.play();
    }
    if (dist>12 && dist<15){
      piano2.play();
    }
    if (dist>14 && dist<20){
      piano3.play();
    }
    **/
    
    
     //********** brush stroke **************\\
    
     for (int i = numStroke - 1; i > 0; i--) {
      x[i] = x[i - 1];
      yStroke[i] = yStroke[i - 1];
    
      xLetter[i] = xLetter[i - 1];
      yLetter[i] = yLetter[i - 1];
      letterSpeed[i] = letterSpeed[i-1];
      rotLetter[i] = rotLetter[i - 1];
      rotLetterSpeed[i] = rotLetterSpeed[i - 1];
    
      letter[i] = letter[i-1];
    
      if (dist < 5) {
    
         xDrop[i] = xDrop[i - 1];
         yDrop[i] = yDrop[i - 1];
         sizeDrop[i] = sizeDrop[i - 1];
    
      }
    
    //  if (dist > 10) {
    
    // }
     }
    
    
    volume = (lerp(volume, dist/2, 0.1));
    celloop.amp(volume);
    
     // Add the new values to the beginning of the array
    
     x[0] = int(lerpx);
     yStroke[0] = int(lerpy);
    
     // add new drop values
    
     xDrop[0] = int(lerpx + random(-20, 20));
     yDrop[0] = int(lerpy + random(-20, 20));
     sizeDrop[0] = int(random(0, 10));
    
     // and new letter values
    
         xLetter[0] = int(lerpx);
         yLetter[0] = int(lerpy);
    
    
         letter[0] = letters[letterPicker];
    
    
         letterPicker++;
    
         if (letterPicker == letters.length){
          letterPicker = 0; 
         }
    
    
    
         letterSpeed[0] = random(1, 4);
         rotLetter[0] = int(random(-40, 40));
         rotLetterSpeed[0] = random(-0.1, 0.1);
    
    
    
     // Stroke and Drops
    
     for (int i = numStroke - 1; i > 0; i--) {
    float dist2 = sqrt((x[i] - x[i - 1]) * (x[i] - x[i - 1]) + (yStroke[i] - yStroke[i - 1]) * (yStroke[i] - yStroke[i - 1]));
      if (i > 0) {
    
       strokeWeight(dist2 + i / 10);
       if (i <= 240) {
        stroke(i);
       } else {
        stroke(240 + (15 * 15 / (numStroke - i)));
       }
       if (dist2>jumpcut){
         strokeWeight(0);
         stroke(255, 0, 0);
       }
       line(x[i]+(640-x[0])*i/400, yStroke[i]+(480-yStroke[0])*i/400, x[i - 1]+(640-x[0])*i/400, yStroke[i - 1]+(480-yStroke[0])*i/400);
      }
    
      if (i <= 240) {
       fill(i);
      } else {
       fill(240 + (15 * 15 / (numStroke - i)));
      }
    
      noStroke();
    
      ellipse(float(xDrop[i])+(640-x[0])*i/400, float(yDrop[i]+(480-yStroke[0])*i/400), sizeDrop[i], sizeDrop[i]);
    
      stroke(0);
      translate(xLetter[i],yLetter[i]-i*letterSpeed[i]);  // Translate to the center
      rotate(rotLetterSpeed[i]*i);                // Rotate by i*speed
      textAlign(CENTER);   
      fill(0);
             if(dist2<20){
           letter[i] = ""; 
           }
      text(letter[i],0,0);
      rotate(-rotLetterSpeed[i]*i);
      translate(-xLetter[i],-yLetter[i]+i*letterSpeed[i]);
     }
    
    }
    
    float distSq(float x1, float yStroke1, float z1, float x2, float yStroke2, float z2) {
     float d = (x2 - x1) * (x2 - x1) + (yStroke2 - yStroke1) * (yStroke2 - yStroke1) + (z2 - z1) * (z2 - z1);
     return d;
    } 
    
  • Answer ✓

    edit post, highlight code, press ctrl-o to format.

    you'll probably need to undo whatever formatting you've already tried to apply.

  • Answer ✓

    red() green() and blue() are expensive. use bitshifting instead

  • blur = loadShader("blur.glsl"); 
    piano1 = new SoundFile(this, "Piano1.aiff");
    piano2 = new SoundFile(this, "Piano2.aiff");
    piano3 = new SoundFile(this, "Piano3.aiff");
    celloop = new SoundFile(this, "celloop-mono.wav");
    

    we have none of these files so we can't really run the code

    kill line 182.

    use dist() or distSq() on line 185. 288 also. you have a variable called dist which clashes with the standard dist() method. it's not an error but it makes things hard to follow.

  • edited December 2017

    ctrl-t in the pde editor will indent your code nicely.

    if you are only ever drawing grey images then you don't need to check the red green and blue for the pixels - all three channels will be the same so you only need to check one.

  • Hi

    Thank you so much for your answers! :) replaced red/green/blue with rightshift

    blur piano and celloop are for future functionality.

    the rgb is from the original motion detection code. Do you think it's redundant?

Sign In or Register to comment.