Skip to content

Instantly share code, notes, and snippets.

@cwilso
Created September 15, 2012 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwilso/3728539 to your computer and use it in GitHub Desktop.
Save cwilso/3728539 to your computer and use it in GitHub Desktop.
setTimeout-based example
var timer = null;
var domhrtTimeAtStartOfPerformance;
var msgIndex;
var PREQUEUE = 0; // with a conformant sendMIDIMessage w/ timestamps, could be set to a larger number like 200.
function tick() {
var msg, delay;
var domhrtRelativeTime = Math.round(window.performance.webkitNow() -
domhrtTimeAtStartOfPerformance);
if (msgIndex=sequenceLength)
return; // we've hit the end of the sequence. This shouldn't be hit, except for an empty initial sequence.
msg = sequence[msgIndex];
delay = msg.timestamp - domhrtRelativeTime;
while (delay <= PREQUEUE ) { // send all messages that are due now.
output.sendMIDIMessage(msg);
logMessage("timestamp: " + msg.timestamp + ", domhrtTime: " + domhrtRelativeTime +
", scheduling deviation: " + (domhrtRelativeTime - msg.timestamp));
msgIndex++;
if (msgIndex == sequenceLength)
return; // we've hit the end of the sequence.
msg = sequence[msgIndex];
delay = msg.timestamp - domhrtRelativeTime;
}
window.setTimeout(tick, delay); // this will schedule the next tick.
}
function sendMIDISequence() {
domhrtTimeAtStartOfPerformance = window.performance.webkitNow();
msgIndex = 0;
tick();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment