out-of-memoryprocessingminim

Out of memory error. What can I do to fix this?


My current project heavy on the code thus far. I would like to add music to it using minim (I'm using a pc) but I keep getting hit with the out of memory error. The song will run but extremely choppy and it makes the program unusable. I changed the max memory in preferences but after trying 4gb with no luck, I figured this just wasn't going to work. Can anyone please look at my code and tell me a way to maybe compress things to save space? or a way to make my program run the way it is?

here is all the code:

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 player;

final int stateMenu = 0;
final int RedBox   = 1;
final int BlueBox = 2;
final int GreenBox = 3;
int state = stateMenu;
  float x, y, r, g, b, radius;
int timer; 

//
// font
PFont font;
PFont Amatic;
//

// ----------------------------------------------------------------------
// main functions

void setup()
{
  // runs only once
  //
  size(800, 700);
  smooth();
  minim = new Minim (this);
  font = createFont("ARCARTER-78.vlw", 14);
  textFont(font);
  //Amatic = createFont("Amatic-Bold.ttf",60);
  //textFont(Amatic);
  frameRate(15);
} // func
//
void draw()
{
  // the main routine. It handels the states.
  // runs again and again
  switch (state) {
  case stateMenu:
    showMenu();
    break;
  case RedBox:
    handleRedBox();
     player = minim.loadFile("SuperLove.mp3");
     player.play();
    break;
  case BlueBox:
    handleBlueBox();
    break;
  case GreenBox:
  handleGreenBox();
  break;
  default:
    println ("Unknown state (in draw) "
      + state
      + " ++++++++++++++++++++++");
    exit();
    break;
  } // switch
  //
} // func
// ----------------------------------------------------------------
// keyboard functions

void keyPressed() {
  // keyboard. Also different depending on the state.
  switch (state) {
  case stateMenu:
    keyPressedForStateMenu();
    break;
  case RedBox:
    keyPressedForRedBox();
    break;
  case BlueBox:
    keyPressedForBlueBox();
    break;
  case GreenBox:
    keyPressedForGreenBox();
    break;
  default:
    println ("Unknown state (in keypressed) "
      + state
      + " ++++++++++++++++++++++");
    exit();
    break;
  } // switch
  //
} // func
void keyPressedForStateMenu() {
  //
  switch(key) {
  case '1':
    state = RedBox;
    break;
  case '2':
    state = BlueBox;
        background(255);
    break;
    case '3':
    state = GreenBox;
    break;
  case 'x':
  case 'X':
    // quit
    exit();
    break;
  default:
    // do nothing
    break;
  }// switch
  //
} // func
void keyPressedForRedBox() {
  // any key is possible
  switch(key) {
  default:
    state = stateMenu;
    break;
  } // switch
  //
} // func
void keyPressedForBlueBox() {
  // any key is possible
  switch(key) {
  default:
    state = stateMenu;
    break;
  } // switch
}//
  void keyPressedForGreenBox(){
      switch(key) {
  default:
    state = stateMenu;
    break;
      }
} // func

// ----------------------------------------------------------------
// functions to show the menu and functions that are called from the menu.
// They depend on the states and are called by draw().

void showMenu() {
  background(255);
  fill(0);
  textSize(45);
  //textFont(Amatic);
  text(" Music Box ", 330, 250, 3);
  textFont(font);
  textSize(14);
  text("Press 1 for Red ", 350, 300);
  textSize(14);
  text("Press 2 for Blue ", 350, 325);
  textSize(14);
  text("Press 3 for Green", 350, 350);
  textSize(14);
  // exit button
  text("Press x to quit ", 350, 375);
} 

void handleRedBox() {
  background(255);
  stroke(255,0,0);
  color from = color(100,random(255),2);
  color to = color(0,200,0);
  color interA = lerpColor (to,from,.44);
  int x = 100;
  while (x < width/2 || x> width/2 ) {
    int y = 100;
    while (y <height/2 || y > height/2) {
      blendMode(DIFFERENCE);
      noStroke();
      fill(interA);
      quadstuff(); 
      strokeWeight(5);
      stroke(0,random(255),0);
      line(width/2,height/2,mouseY,mouseX);
      translate(width, height);
      rotate(radians(frameCount));
      y = y + 50;
    }
    x = x + 50;
  }
ghostcirc();
ghostcirc2();
}

void ghostcirc(){
  int w = 0;
  while (w < width) {
    int q = 0;
    while (q <height) {
      blendMode(ADD);
      fill(random(61), random(90), random(250));
      ellipse(255,255,100,100);; 
      noStroke();
      translate(width, height);
      rotate(radians(frameCount));
      q = q + 100;
    }
    w = w + 50;
  }
}

void ghostcirc2(){
  for (int w= 0; w < width; w+=10) {    
      blendMode(ADD);
      fill(random(61), random(90), random(250));
      ellipse(50,50,75,75);; 
      noStroke();
      translate(width, height);
      rotate(radians(frameCount));
      //if (keyPressed == true){
      //    fill(random(100), random(90), random(250));
      }
    }

void quadstuff() {
    int rad = 60;      // Width of the shape
  float xpos, ypos;    // Starting position of shape    

  float xspeed = 2.8;  // Speed of the shape
  float yspeed = 2.2;  // Speed of the shape

  xpos = width/2;
  ypos = height/2; 
  //ellipse(mouseX+x, mouseY+y, 100,100);
  quad(xpos, ypos, rad, rad, mouseX+rad, mouseY+rad, xspeed, yspeed);
  stroke(0);
  strokeWeight(5);
}

void handleBlueBox() {
  Zon();
} 

void Zon(){
    blendMode(REPLACE);
  frameRate(75);
  noStroke();
  smooth();
  color from = color(2,217,227);
  color to = color(0,127,193);
  color interA = lerpColor (from,to,.24);
   // use frameCount to move x, use modulo to keep it within bounds
  x = frameCount % width;

  // use millis() and a timer to change the y every 2 seconds
  if (millis() - timer >= 8000) {
    y = random(height);
    timer = millis();
  }

  // use frameCount and noise to change the red color component
  r = noise(frameCount * 0.01) * 60;

  // use frameCount and modulo to change the green color component
  g = frameCount % 1;

  // use frameCount and noise to change the blue color component
  b = 255 - noise(1 + frameCount * 0.025) * 255;

  // use frameCount and noise to change the radius
  radius = noise(frameCount * 0.01) * mouseX;

  color c = color(r, g, b);
  fill(c);
  ellipse(x, y, radius, radius);
}

void Vert(){
  frameRate(45);
   // use frameCount to move x, use modulo to keep it within bounds
  x = frameCount % width;

  // use millis() and a timer to change the y every 2 seconds
  if (millis() - timer >= 8000) {
    y = random(height);
    timer = millis();
  }

  // use frameCount and noise to change the red color component
  r = noise(frameCount * 0.01) * 255;

  // use frameCount and modulo to change the green color component
  g = frameCount % 1;

  // use frameCount and noise to change the blue color component
  b = 255 - noise(1 + frameCount * 0.025) * 255;

  // use frameCount and noise to change the radius
  radius = noise(frameCount * 0.01) * mouseX;

  color c = color(r, g, b);
  fill(c);
  ellipse(y, x, radius, radius);
}

void handleGreenBox() {
    background(255);
  fill(0);
  textSize(32);
  text(" Some stuff", 150, 100, 3);
  textSize(14);
  text("..... some text ", 100, 200);
} 

Solution

  • You should only load your sound files once!

    The draw() function is called 60 times per second, and inside of that you have this code:

    case RedBox:
        handleRedBox();
         player = minim.loadFile("SuperLove.mp3");
    

    In this case, that sound file is going to be called 60 times per second. That's not what you want to do.

    Instead, load all of your sound files from the setup() function, and then refer to them inside the draw() function.