Skip to content

Instantly share code, notes, and snippets.

@mudcube
Created August 8, 2015 08:41
Show Gist options
  • Select an option

  • Save mudcube/184caae137186ff779cf to your computer and use it in GitHub Desktop.

Select an option

Save mudcube/184caae137186ff779cf to your computer and use it in GitHub Desktop.
Compare window.setTimeout() vs. using Audio BufferSource shim
var timeout = .50;
///
var channels = 1;
var audioCtx = new AudioContext;
var sampleRate = audioCtx.sampleRate;
var audioDuration = 0.01;
var audioBufferSize = sampleRate * audioDuration;
var audioBuffer = audioCtx.createBuffer(channels, audioBufferSize, sampleRate);
var date = Date.now();
var source = audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.start(timeout - audioDuration);
source.onended = function() {
console.log('shim', date - Date.now())
};
setTimeout(function() {
console.log('setTimeout', date - Date.now())
}, timeout * 1000);
source.connect(audioCtx.destination);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment