Why is my program lagging?

edited October 2016 in Library Questions

I'm having a difficult time figuring out why my code is lagging. Can anyone help? Here's my code:

//minim imports
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim minim;
AudioPlayer songL;
AudioPlayer songR;
FFT fft;
BeatDetect beat;

//tablet import
import codeanticode.tablet.*;

Tablet tablet;

boolean music = true;

public void setup() {
  size (640, 640, P2D);

  minim = new Minim(this);
  songL = minim.loadFile("Sailing on the Wind.mp3", 2048);
  beat = new BeatDetect();
  fft = new FFT (songL.bufferSize(), songL.sampleRate());


  minim = new Minim(this);
  songR = minim.loadFile("Through the Fire and Flames.mp3", 2048);
  beat = new BeatDetect();
  fft = new FFT (songR.bufferSize(), songR.sampleRate());

  tablet = new Tablet (this);
}

public void draw () {
  background (0);
  smooth();

  songL.play();
  songR.play();

  if (music == false) {
    songL.mute();
    songR.unmute();
  }

  if (music == true) {
    songL.unmute();
    songR.mute();
  }

  for (int i = 0; i < songL.bufferSize()-1; i++) {


    music = false;

    //Blue square
    noStroke();
    fill (135, 206, 250, 50);

    if (mousePressed && (mouseButton == LEFT)) {
    } else {
      music = true;

      background (135, 206, 250);

      noStroke();
      fill (255, 0, 0, 5);

      ellipse (mouseX, mouseY, 50, 50);
    }
  }

  for (int i = 0; i < songR.bufferSize()-1; i++) {

    if (mousePressed && (mouseButton == RIGHT)) {

      fft.forward (songR.mix);

      background (0);
    }

    //white square
    noStroke();
    fill (248, 248, 255, 4);
    rect (220, 220, 200, 200, 75);

    //black line
    stroke (0);
    line (i, 310+ songL.left.get(i)*50, i+1, 310 + songL.left.get(i+1)*50);
    //blue line
    stroke (135, 206, 250);
    line (i, 330+ songR.left.get(i)*50, i+1, 330 + songR.left.get(i+1)*50);

    //flower/blood
    noStroke();
    fill (255, 0, 0, 5);
    ellipse (mouseX, mouseY, 25, 25);
    ellipse (mouseX-10, mouseY-10, 10, 10);
    ellipse (mouseX-10, mouseY+10, 10, 10);
    ellipse (mouseX+10, mouseY-10, 10, 10);
    ellipse (mouseX+10, mouseY+10, 10, 10);  
    ellipse (mouseX, mouseY-15, 10, 10);
    ellipse (mouseX-15, mouseY, 10, 10);
    ellipse (mouseX, mouseY+15, 10, 10);
    ellipse (mouseX+15, mouseY, 10, 10);
  }
}
Tagged:

Answers

  • Answer ✓

    Edit post, highlight code, press Ctrl-o to format.

    You are drawing 10 ellipses for each sample. Maybe that should be outside the loop.

  • edited October 2016

    That did help for when my mouse is pressed, but it still lags when my mouse isn't pressed.

  • Can't tell what you've changed. But the white square doesn't need to be inside the loop either. And certainly not the fft.forward.

Sign In or Register to comment.