Hi! i cant resolve a problem here. Im trying to do a music visualizer. The idea is very simple; when i press a Key in my midiController add a new Particle, and when i release, delete that Particle. but when i release a key, all particles are delete. What i was looking for is that only delete the Particle what was correspond that key that i was release. here the code:
import themidibus.*;
MidiBus myBus;
int pitchs;
float cc [] =new float [256];
boolean note;
ArrayList<Particle>part;
void setup () {
fullScreen();
background(0);
note = true;
myBus= new MidiBus(this, "visuals", "visuals");
part = new ArrayList <Particle>();
}
void draw() {
background(0);
for (int i = part.size()-1; i>= 0; i--) {
Particle p = part.get(i);
p.update();
p.display();
if(p.isDead()){
part.remove(i);
}
}
}
void noteOff(int channel, int pitch, int velocity) {
println();
println("Note Off:");
println("--------");
println("Channel:"+channel);
println("Pitch:"+pitch);
println("Velocity:"+velocity);
note=false;
pitchs = pitch;
}
void noteOn(int channel, int pitch, int velocity) {
println();
println("Note On:");
println("--------");
println("Channel:"+channel);
println("Pitch:"+pitch);
println("Velocity:"+velocity);
pitchs= pitch;
note = true;
part.add(new Particle(random(width), random(height), pitchs, note));
}
void controllerChange(int channel, int number, int value) {
// Receive a controllerChange
println();
println("Controller Change:");
println("--------");
println("Channel:"+channel);
println("Number:"+number);
println("Value:"+value);
cc[number]=map(value, 0, 127, 0, 1);
}
class Particle {
PVector pos, vel, acel;
float pitch;
float life;
boolean notes;
Particle (float x, float y, float pitchs) {
pos = new PVector(x, y);
vel = new PVector(random(-1, 1), random(-1, 1));
acel = new PVector(0, 0);
pitch = pitchs;
life = 255;
}
void update() {
pos.add(vel);
vel.add(acel);
vel.limit(1);
life-=1;
}
void display() {
float r = map(pitchs, 0, 127, 0, 200);
fill(255);
noStroke();
ellipse(pos.x, pos.y, r, r);
}
boolean isDead() {
if (note==false || life < 0) {
return true;
} else {
return false;
}
}
}