I think your problem is a bit more involved and there're several things to try out...
If you have Java 5 or newer installed on your machine you could try using System.nanoTime() instead since the old System.currentTimeMillis() of earlier versions has a notoriously bad timing resolution...
Code:int numIterations=100000;
double min=Integer.MAX_VALUE;
double max=-min;
double avg=0;
for(int i=0; i<numIterations; i++) {
long now = System.nanoTime();
long delta = System.nanoTime()-now;
avg+=delta;
if (delta<min) min=delta;
if (delta>max) max=delta;
}
avg/=numIterations;
// scale nanoseconds back into milliseconds
avg/=1e6;
min/=1e6;
max/=1e6;
println("average: "+avg+"ms");
println("min: "+min+"ms / max: "+max+"ms");
Am getting these results here (resulotion between 1-2 MICRO seconds)
average: 0.00189156105ms
min: 0.001396ms / max: 0.844241ms
That itself is not the final answer though. You should run place your MIDI event sending into a separate thread to make it independent from the main framerate of the Processing app:
Code:MidiThread midi;
void setup() {
size(100,100);
// create new thread running at 160bpm, bit of D'n'B
midi=new MidiThread(160);
midi.start();
}
void draw() {
// do whatever
}
// also shutdown the midi thread when the applet is stopped
public void stop() {
if (midi!=null) midi.isActive=false;
super.stop();
}
class MidiThread extends Thread {
long previousTime;
boolean isActive=true;
double interval;
MidiThread(double bpm) {
// interval currently hard coded to quarter beats
interval = 1000.0 / (bpm / 60.0);
previousTime=System.nanoTime();
}
void run() {
try {
while(isActive) {
// calculate time difference since last beat & wait if necessary
double timePassed=(System.nanoTime()-previousTime)*1.0e-6;
while(timePassed<interval) {
timePassed=(System.nanoTime()-previousTime)*1.0e-6;
}
// insert your midi event sending code here
println("midi out: "+timePassed+"ms");
// calculate real time until next beat
long delay=(long)(interval-(System.nanoTime()-previousTime)*1.0e-6);
previousTime=System.nanoTime();
Thread.sleep(delay);
}
}
catch(InterruptedException e) {
println("force quit...");
}
}
}
The output from that example (at 160bpm) should be something like:
midi out: 375.0018615722656ms
midi out: 375.0001525878906ms
midi out: 375.0001525878906ms
midi out: 375.0015563964844ms
midi out: 375.00103759765625ms
midi out: 375.0004577636719ms
midi out: 375.0004577636719ms
...
So you can see the difference in timing will be negligible (at least for musical purposes)...
Hope that helps!