I'm trying to setup a program that allows me to feed a musical score and get the Robot to sync up multiple key presses to play the song on a virtual piano. Everything is formatted correctly and the robot presses the right buttons in the right order, but it's not syncing up at all. Songs will have multiple notes played at the same time, so I'm having it read multiple bars one character at a time, but some will have rests when the others have notes and I think that's what's messing up the timing, I not entirely sure though and I also don't know how I would fix this.
Code snippet I'm dealing with:
//making robot press buttons cause I'm too prideful to just use a MIDI player
for (int o =0;o<alist.get(0).size();o++) {
for (int q=0;q<Pianos;q++) {
if (alist.get(q).get(o).equals("R")) {
System.out.println("rest here");
}else if (alist.get(q).get(o).equals("c2")) {
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
}else if (alist.get(q).get(o).equals("C2")) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_SHIFT);
} (... all the other notes in between C2 and c7...)
else if (alist.get(q).get(o).equals("c7")) {
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_M);
}
}
Thread.sleep(rest);
}
alist is an arraylists of arraylists, each arraylist is one bar of the song, with each string within being either a note to play or a rest. Pianos is the number of these bars. Rest is an arbitrary amount of ms.
I tried the code above and I thought the Thread.sleep(rest); at the end would make it so that whatever note the robot played would take the same amount of time, even if it was a rest. Instead, the timing is messed up everytime I use the program.