Scheduling Web Audio with precision
-
A Tale of Two Clocks
-
the two clocks: Web Audio Clock & JavaScript Clock
Web Audio Clock
- access to audio subsystem’s hardware clock
currentTimeproperty for audio clock, floating-point number of seconds- starting and stopping sounds, schedules changes to the sound (changing frequency or volume)
- schedules paramenters and audio events
- high-precision
- returns value in sound sample level (15 decimal digits of precision)
JavaScript Clock
Date.now()andsetTimeout()- useful callback
window.setTimeout()andwindow.setInterval()to call code back - not very precise
Date.now()returns value in milliseconds- sensitive to other things happening on main JavaScript thread, may cause in too-immediate or delayed playback of audio files
Scheduling methods and parameters
start()andstop()(previously known asnoteOnandnoteOff)- Web Audio Clock:
set*ValueAtTime()methods:linearRampToValueAtTime(),exponentialRampToValueAtTime(),setTargetAtTime(),setValueCurveAtTime(),cancelScheduledValues - JavaScript Clock:
setTimeout(),scheduleNote(),nextNote()
Synchronous vs Asynchronous
- Synchronous: happening at the same time (real-time)
- might have conflict in timing
- waits for one task to finsh before moving on to another task
- happens in a single series
- Asynchronous: not blocking the main JavaScript execution thread
- Happening at different times, fast-paced
- able to move on to another task before the previous one finishes
- different routines able to run in background
LINKS: