import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
FFT fft;
AudioInput in;
float amp = 20; // used to make signal stronger/weaker
float ampWave = 20*amp;
float avgAudio; // store avg volume globally
float bass;
float high;
void setup() {
background(255);
size(500, 500);
noStroke();
smooth();
colorMode(HSB, 360, 100, 100);
minim = new Minim(this); // initalize in setup
in = minim.getLineIn(Minim.STEREO, 512); // audio in + bufferSize 512 or 1024
fft = new FFT(in.bufferSize(), in.sampleRate());
fft.logAverages(22, 3); // 3 = 30, 4 = 40, slices of frequency
}
void draw() {
if (keyPressed) {
fft.forward(in.mix); // IMPORTANT! -update for FFT anaylsis
bass = fft.calcAvg(20, 300)*amp; // fft.calcAvg(minFreq, maxFreq)
high = fft.calcAvg(300, 20000)*amp;
}
if (bass > high) {
background(255);
noStroke();
fill(random(180,360), 100, 100);
int n_vertex = int(random(7, 20));
beginShape();
for (int i = 0; i < n_vertex; i++) {
vertex(random(width), random(height));
}
endShape(CLOSE);
} else if (high > bass) {
background(255);
noStroke();
fill(random(1,180), 100, 100);
int n_vertex = int(random(3, 7));
beginShape();
for (int i = 0; i < n_vertex; i++) {
vertex(random(width), random(height));
}
endShape(CLOSE);
}
println(bass+" / "+high);
}
and the problem basically is that after I stop to press the keyboard it continues to draw shapes and I cannot “freeze” it…also the background is not still (255) but it become grey.
Any idea of this two problems?
Hello there!
Is there any way you can post your full code? It’s difficult to figure out the initial setup without knowing the rest of the program. If for some reason you don’t want to post your full code (it is highly recommended that you do), I would say look at the standalone methods keyPressed(){} and keyReleased{}. Also, if you want the background to be white regardless, try putting the background(255) just after the “void draw(){” and not in any parenthetical statements. This will set the background to 255 every time the draw loop iterates, as the first thing in the loop.
Rioma,
The background turning gray has to do with the fact that you are in Color Mode HSB, not RGB. HSB stands for Hue, Saturation, and Brightness, and so the way to get white would be background(0, 0, 100). RGB stands for Red, Green Blue, so you get colors by combining those. Try background(0, 0, 100) if you want to stay in HSB.
About that. What exactly is it that you want the program to do? Do you want to draw shapes while a key is being pressed, and then when the user stops pressing that key, do you want the screen to go blank, or do you want to display the last shape that was displayed before the keys stopped being pressed?
If you want the screen to just go blank, I recommend putting the polygon drawing sections in the same loop as the keyPressed statement.
Alright, why don’t you try using an array of PVectors instead of vertices? Both vertex() and PVector() hold two values, one for x, one for y. Try creating a PVector[] vertices = new PVector[0]; (before the setup(){} )then use
int n_vertex = int(random(7, 20));
vertices = new PVector[n_vertex];
beginShape();
for (int i = 0; i < n_vertex; i++) {
vertices[i] = new PVector(random(width), random(height));
}
instead of
int n_vertex = int(random(7, 20));
beginShape();
for (int i = 0; i < n_vertex; i++) {
vertex(random(width), random(height));
}
endShape(CLOSE);
and for the other condition (high> bass) as well.
Then, right before the end of the draw loop, use
This should work, because every time a new shape is created, its vertices are stored in an array, in the form of PVectors. This way, you can draw it anytime. Hope this helps.