Schedule audio in JavaScript
Start or resume your audio context from a user gesture. Read currentTime in seconds, then schedule each note against that audio clock. A timer can refill a short scheduling window for long performances. Audio events already queued inside that window keep their timing if rendering briefly stalls.
const secondsPerStep = 60 / bpm / 4;
const start = context.currentTime + 0.03;
oscillator.start(start + stepIndex * secondsPerStep);
oscillator.stop(start + (stepIndex + 0.75) * secondsPerStep);At 120 BPM a quarter takes half a second and a sixteenth takes 125 ms. Use one time anchor for the whole bar to avoid adding callback delay every step. A short gain ramp prevents sharp clicks when a tone starts or ends.
Implement stop
Route every voice for a playback run through one gain node. Disconnect that node on Stop so future notes that are already scheduled cannot reach the output. Keep the notes short and release references once the run ends.
Try the sequencer at two tempos to hear the difference.