We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone ! I'm trying to let appear a color rectangle in my processing window, and change color according to the frequencies of a song. There is only 3 different scales for the moment; red under 70, orange above 71 and yellow above 250. I used "if" and "else if" to order these 3 different conditions. However, when I run the script, only the first "if" appears. I get the red one when low frequencies are playing, and only a black screen when it's above... Why ?
Here is the code:
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer song;
FFT fft;
float highestAmp = 0;
int frequency;
float amplitude;
void setup () {
size(500,250);
background(0);
// initialize Minim and catching the output
minim = new Minim(this);
song = minim.loadFile("altj.mp3",1024);
//better result with *8
fft = new FFT(song.left.size(), 44100);
song.play();
}
void draw() {
highestAmp=0;
amplitude=0;
frequency = 0;
fft.forward(song.left);
//searching from 0Hz to 20000Hz. gettingthe band, and from the band the frequency
for(int i = 0; i < 20000; i++){
amplitude = fft.getFreq(i);
if (amplitude > highestAmp) {
highestAmp = amplitude;
frequency = i;
}
}
//frequency ellipse color
fill(255);
background(0);
if (frequency <= 70){
fill(255,0,0);
} else if (frequency >= 71){
fill(255,102,0);
rect(245,0,10,height);
} else if (frequency >= 250){
fill(255,255,0);
ellipse(width/2,height/2,frequency/2,frequency/2);
}
}
Answers
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
I suspect the issue is that you aren't aware about the value stored in your
frequency
variable. First, realize that (since it is set to the value ofi
) it can range from 0 to 20000 (since these are the values that you iterate over fori
in your for loop).Also, your if-else statements are all doing slightly different things when it comes to drawing... and your logic is off just a little bit.
The first case (
frequency <= 70
) doesn't actually draw anything. It sets the fill color to red and that's it. You get a black background.The second case (
frequency >= 71
), is ALWAYS going to happen if the first case doesn't happen. That is, either the value of frequency is less that 70 or it is greater than 70. One or the other must be true. In this case, you draw the mostly red rectangle.The third case never happens! Since one of the first two cases evaluated to true, the else here ensures that this case isn't even checked.
The issue really is your check for the second case. Since you know that
frequency
is greater than 70 if your first case isn't true, then you need to check the UPPER bound onfrequency
- not the LOWER bound. And then, for your third case, there's really no need to check for a boundry at all - since the third case deals with everything above 250. Try this:Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Kf
Thank you very much TfGuy44 ! It does work this way. I'm a total beginner in coding, now I understand the processing logic a little better.
And thanks kfrajer for the tip, it's edited now ;)