Hi everyone ! So I thought my problem was solved once we got over this question of colored rectangles. ( https://forum.processing.org/two/discussion/comment/109296#Comment_109296 )
But not at all. This time I don't want random colors, and making an array isn't enough... So my question is the same as before: how to draw each rectangle so that its color stays the same, independently from the following rectangles? I want the color to change according to the frequency. Here is the code:
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer song;
FFT fft;
float highestAmp = 0;
int frequency;
float amplitude;
int rectX;
float time;
color[] colors = new color[3];
void setup(){
size(1500,500);
background(255);
// 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();
// define 3 colors
colors[0] = color(255,0,0);
colors[1] = color(255,255,0);
colors[2] = color(0,255,0);
}
void draw (){
highestAmp=0;
amplitude=0;
frequency=0;
fft.forward(song.left);
background(255);
// get frequency from 0Hz to 20000Hz
for(int i = 0; i < 20000; i++){
amplitude = fft.getFreq(i);
if (amplitude > highestAmp) {
highestAmp = amplitude;
frequency = i;
}
}
//make it move !
for (int x = 0; x < time; x = x+5) {
// color frequencies
if (frequency < 70){
fill(colors[0]);
}else if (frequency < 250){
fill(colors[1]);
}else{
fill(colors[2]);
}
rect(x,0,5, height);
}
time++;
}
I learned about classes and tried to use them (still a noob, haha). It looks like this :
Slot mySlot;
float x;
color[] colors = new color[3];
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer song;
FFT fft;
float highestAmp = 0;
int frequency;
float amplitude;
void setup() {
size(1500,500);
// 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();
mySlot = new Slot ();
colors[0] = color(255,0,0);
colors[1] = color(255,255,0);
colors[2] = color(0,255,0);
}
void draw() {
highestAmp=0;
amplitude=0;
frequency=0;
fft.forward(song.left);
// get frequency from 0Hz to 20000Hz
for(int i = 0; i < 20000; i++){
amplitude = fft.getFreq(i);
if (amplitude > highestAmp) {
highestAmp = amplitude;
frequency = i;
}
}
mySlot.display();
mySlot.avance();
x = 0;
if (frequency <70){
fill(colors[0]);
}else if (frequency < 250){
fill(colors[1]);
}else{
fill(colors[2]);
}
rect(x,0,5, height);
}
class Slot{
//Data
float x;
float time;
//Constructor
Slot(){
x = 0;
}
//Functionality
void display() {
rect(x,0,5,height);
}
void avance(){
x = 0;
for(int x = 0; x < time; x = x + 5){
rect(x,0,5,height);
}
time++;
if (x > width) {
x = 0;
}
}
}
But same problem: the "fill" color is inside the same draw function as the rectangle. I don't know how I can separate it from the above line of the shape, or make a series of shapes related to these conditions for colors... Any clue?