How the Metronome Keeps Accurate Time
AnyTool Metronome is built on the Web Audio API’s “two clocks” lookahead scheduler. A short setInterval timer fires about every 25 milliseconds and, on each wake-up, schedules every click that falls within the next ~100 milliseconds onto the AudioContext sample clock using oscillator.start(time) at an exact AudioContext.currentTime value. Because the audio hardware clock — not a JavaScript timer — decides the precise moment each click plays, the beat is sample-accurate and does not drift, even as the tempo is changed live. This is markedly more stable than a metronome that calls setTimeout once per beat, whose error accumulates with main-thread jitter.
- Lookahead scheduler: ~25 ms timer schedules clicks ~100 ms ahead
- Clicks are placed on the AudioContext sample clock (currentTime)
- Drift-free and stable across live tempo changes from 30 to 300 BPM
- Each click is a synthesized OscillatorNode + GainNode envelope — no audio files
- A queue of scheduled beats drives the visual indicator via requestAnimationFrame
