Hi PhiLho,
Sorry about that. I've pasted the code again below but
this time without using the midibus library. Instead, the keyboard keys
'a, 's,' and 'd' create the Note objects. I also simplified it to one
linked list of Note objects to try and isolate the problem. Would you be able to move the post back to Programming Questions?
When
running this program, hitting a, s, and d keys will create smoothly
rotating squares that fade away most of the time. It gets tripped up
sometimes though. The rotation looks jerky. Just wondered if someone
might have a suggestion.
Thanks,
Dave
- /* A note object is created when the user hits an a, s, or d key on their keyboard.
A LinkedList stores the 5 most recent Note objects. Each Note's display method draws a square
and updates its angle (rotation) and opacity values for the next time display is called. */
int numNotes = 5; //Maximum number of notes shown at any given time
LinkedList<Note> theNoteStream = new LinkedList<Note>();
Note newNote = null;
void setup() {
size(854, 480);
noStroke();
smooth();
rectMode(CENTER);
}
void draw() {
background(0);
for (Note y : theNoteStream) {
y.display(); //display each Note and update its rotation and opacity
}
if (newNote != null) { //if note events have happened since last time through draw, update the linked list
theNoteStream.add(newNote);
while (theNoteStream.size() > numNotes) {
theNoteStream.remove();
}
newNote = null;
}
}
void keyPressed() { //using keyboard keys a, s, and d in this version
int channel = 0;
int pitch = 0;
int velocity = 90;
switch (key) {
case 'a':
pitch = 30;
newNote = new Note(pitch, velocity);
break;
case 's':
pitch = 50;
newNote = new Note(pitch, velocity);
break;
case 'd':
pitch = 70;
newNote = new Note(pitch, velocity);
break;
}
}
class Note {
float opacity;
float pitch, velocity;
float xpos, ypos;
float angle;
Note(float pitchIn, float velocityIn) {
opacity = 255;
pitch = pitchIn;
velocity = velocityIn;
xpos = 400;
ypos = pitch*5;
angle = 0;
}
void display() {
fill(255, opacity);
pushMatrix();
translate(xpos, ypos);
rotate(radians(angle)); // not necessary for circles
rect(0, 0, velocity, velocity);
popMatrix();
if (opacity > 0) {
angle += (.04 * velocity) + 1;
opacity -= 5;
} else {
opacity = 0;
}
}
}