Code Help
in
Core Library Questions
•
7 months ago
Why does my code use so much memory, and how can I fix that? It begins to cause my computer to lag and even sometimes freeze.
Here is the code:
import ddf.minim.analysis.*;
import ddf.minim.*;
Minim minim;
AudioInput in;
FFT fft; //fast fourier transform - splits up sound wave in a selection of bands
int w;
PImage fade;
int hVal;
float rWidth, rHeight;
void setup()
{
size(640, 480, P3D);
minim = new Minim(this); //initializing minim object
in = minim.getLineIn(Minim.STEREO, 512); //initializing in
fft = new FFT(in.bufferSize(), in.sampleRate()); //initializing fft
fft.logAverages(60, 7); //values tested and work to spread out frequencies
stroke(255);
w = width/fft.avgSize(); //going to divide the width of the sketch by the amount of the bands in the spectrum
strokeWeight(w);
strokeCap(SQUARE); //get rid of the curved caps
background(0);
fade = get(0, 0, width, height); //load fade image with the contents of the background
rWidth = width * 0.99; //makes 99% as wide as the sketch
rHeight = height * 0.99; // ^^ but height
hVal = 0;
}
void draw()
{
background(0); //clean screen each time
tint(255, 255, 255, 254); //everything drawn is tinted white
image(fade, (width - rWidth) / 2, (height - rHeight) / 2, rWidth, rHeight);
noTint();
fft.forward(in.mix); //mix from left to right and form fft on audio input
colorMode(HSB); // Hue Saturation and Brightness.. to cycle through a rainbow of color
stroke(hVal, 255, 255);
colorMode(RGB);
for(int i = 0; i < fft.avgSize(); i++) //pulling the values from each band from the fft object and putting them on the screen
{
line((i * w) + (w / 2), height, (i * w) + (w / 2), height - fft.getAvg(i) * 4);
}
fade = get(0, 0, width, height);
stroke(255);
hVal += 2;
if(hVal > 255) {
hVal = 0;
}
}
THANK YOU FOR THE HELP!!
1