5 ms·
One interesting thing to me about Tone.js has always been that their “Transport” API is a more reliable interval/timer tool than any of the native JS APIs or ev
by krrishd 8y ago
One interesting thing to me about Tone.js has always been that their “Transport” API is a more reliable interval/timer tool than any of the native JS APIs or even libraries/polyfills designed around more accurate interval timing. I’d almost be surprised if I wasn’t wrong about this but I searched and couldn’t find anything quite comparable.
- pierrec 8y agoIt has sample-level precision, just like the Web Audio API provided by the browser (which is what's used under the hood). It's pretty much independent of non-audio time APIs.
- krrishd 8y agoInteresting, makes sense. I wonder — is setInterval not as precise because it doesn’t really need to be (and thus it’d be a waste of complexity to be that precise)?
- fenomas 8y agoIt's not that Tone is more precise than setInterval, it's that the web audio APIs underneath are precisely scheduled, and Tone provides an interface to them. That is, if you set up a Tone transport to fire events every 100ms, it's not like your event handler gets called precisely 100ms later. Rather, it gets called early - say 85ms later - with a parameter saying "hey that event you wanted is precisely 14.7803ms away". Then you can call an API saying "okay, play a tone 14.7803ms from now", and Tone passes that along to web audio, and the end result is that your note plays with sample-level precision. But if you used Tone's transport to do something other than calling web audio APIs it wouldn't be any more precise than setInterval. (IIRC Tone actually uses setInterval to fire its events.)
- barryhoodlum 8y agohttps://www.html5rocks.com/en/tutorials/audio/scheduling/ https://www.html5rocks.com/en/tutorials/audio/scheduling/ is a deeper dive into how it works.