Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created January 26, 2013 08:54
Show Gist options
  • Save srikumarks/4641156 to your computer and use it in GitHub Desktop.
Save srikumarks/4641156 to your computer and use it in GitHub Desktop.
(Web Audio API) A test to see if accumulating sample time drifts away from AudioContext.currentTime.
<script>
var ac = new webkitAudioContext;
var j = ac.createScriptProcessor(1024, 1, 1);
var startTime, startTimeGrabbed = false, samples = 0, sampleCountTime = 0, frames = 0;
j.onaudioprocess = function (event) {
if (!startTimeGrabbed) {
startTime = ac.currentTime;
samples = 0;
sampleCountTime = ac.currentTime;
frames = 0;
startTimeGrabbed = true;
}
var drift_ms = Math.round(1000 * (sampleCountTime - ac.currentTime));
if ((frames++) % 100 === 0) {
console.log("Frame stats = " + JSON.stringify({
startTime: startTime,
samples: samples,
sampleCountTime: sampleCountTime,
currentTime: ac.currentTime,
drift_ms: drift_ms
}));
}
if (Math.abs(sampleCountTime - ac.currentTime) > 0.1) {
console.log("Drift = " + drift_ms + " ms");
}
samples += event.inputBuffer.length;
sampleCountTime += event.inputBuffer.duration;
};
j.connect(ac.destination);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment