This page is designed to test the timing accuracy of the HTML5 WindowTimers.setInterval() method.
Is the timing stable enough to play a rhythm with acceptable accuracy? You be the judge.
The script in this window plays a sequence of pitches in a loop at a rate of 125ms per note.
Does it sound steady? Does it stay steady even when you do other things in your browser?
In JavaScript, you can schedule events to take place an exact amount of time in the future, using the global function setTimeout(). And you can schedule events to occur periodically at regular time intervals, using the global function setInterval(). Both setTimeout() and setInterval() are ostensibly precise to the nearest millisecond or better. In actual practice, the timing precision is not totally reliable, and might also be limited by the browser itself, such that the timing might be (usually is) off by a few milliseconds. That might or might not be a significant imprecision for musical purposes. In most circumstances it won't be a problem, but it's worth bearing in mind when very precise timing is needed.
The WindowTimers.setInterval() method is not explicitly linked to the sample-rate timing of the Web Audio API, yet in many cases it can be sufficiently accurate for timing of audio events. In this example we use setInterval() to schedule a recurring pattern of note events in Web Audio API. Try it and see if it is adequately accurate on your computer.
The script creates an array of sixteen pitches and an array of sixteen loudnesses, and uses those arrays to play a recurring pattern of sixteen synthesized notes in a loop, with each note scheduled by setInterval(). Note that the duration of each note is not quite as long as the time interval between note onsets. That produces a very slightly staccato or détaché articulation of the notes, but more importantly it avoids any potential disparity between the timing of the notes' amplitude envelope and the imperfect timing of the setInterval() method.
This script also includes handy functions mtof() for converting MIDI-style pitch numbers to their corresponding frequency, and dbtoa() for converting loudness in decibels into their corresponding linear amplitude.
This page was last modified January 21, 2024
by Christopher Dobrian, dobrian@uci.edu