AudioContext time vs. Transport time
Tone.js has two clocks: the AudioContext’s time, which every sound is ultimately scheduled with, and the Transport’s time, a musical timeline built on top of it. This page describes how they relate.
AudioContext time
Section titled “AudioContext time”The AudioContext’s currentTime starts at 0 when the AudioContext is created and counts up in seconds while it’s running. It never stops, jumps or goes backwards. Sounds are scheduled at positions on this clock.
By the time anything happens, the clock has already been counting for a while, so times are usually relative to the current time, Tone.now(). For example, to play a sound for two seconds when a button is clicked:
const sine = new Tone.Oscillator(440, "sine").toDestination();
document.querySelector("button").addEventListener("click", async () => { // start the AudioContext, which needs a click because of the autoplay policy await Tone.start(); const now = Tone.now(); sine.start(now); sine.stop(now + 2);});This works well for a single sound. But say we scheduled a whole song this way when the button was clicked. To pause it, jump ahead to a particular moment, or go back to the beginning, we’d have to cancel and reschedule every sound, because the AudioContext time can’t be paused or moved.
Transport time
Section titled “Transport time”The Transport is a second clock that can. It’s a timeline that you can start, stop, pause, loop and seek, and whose tempo can change, while everything scheduled on it stays in sync. Positions on it are called TransportTime, and they can be written in seconds or in musical values like "1m" or "4n".
The event classes like Tone.Loop are scheduled with TransportTime. This loop plays a short tone every 2 seconds, starting at the beginning of the Transport’s timeline:
const sine = new Tone.Oscillator(440, "sine").toDestination();const loop = new Tone.Loop((time) => { sine.start(time); sine.stop(time + 0.5);}, 2);loop.start(0);Tone.getTransport().start();Nothing plays until the Transport is started, because the loop is waiting for the Transport to reach 0. When it does, the callback is passed time, which is the AudioContext time of the event. Under the hood all Web Audio scheduling uses the AudioContext’s clock, so that’s the time to pass to start and stop.
The two clocks meet at Tone.getTransport().start(). Its first argument is the AudioContext time to start at (now, if you leave it out), and the optional second argument is the TransportTime to start from. The event classes’ start methods are the other way around: they take a TransportTime.
Both clocks at once
Section titled “Both clocks at once”This example starts, stops and restarts the Transport, with a loop and a single event scheduled on it. Each callback logs its time on both clocks, in seconds since the code ran:
const transport = Tone.getTransport();const now = Tone.now();const log = (name, time) => console.log( `${name}: context ${(time - now).toFixed(1)}s, transport ${transport.getSecondsAtTime(time).toFixed(1)}s`, );
// a loop every second, from 0 to 2.5 on the Transport's timelinenew Tone.Loop((time) => log("loop", time), 1).start(0).stop(2.5);// a single event at 1.5 on the Transport's timelinenew Tone.ToneEvent((time) => log("event", time)).start(1.5);
// start the Transport after 1 second, stop it, and start it againtransport.start(now + 1).stop(now + 3.5).start(now + 4.5);Here’s what that looks like on the two clocks:
While the Transport is stopped, its clock doesn’t move. Stopping also returns it to 0, so when it’s started again, the loop and the event play again from the beginning, at new AudioContext times. Pausing (transport.pause()) would keep its position instead.
Why callbacks are passed a time
Section titled “Why callbacks are passed a time”Callbacks don’t run exactly at the time they’re scheduled for. JavaScript timers like setTimeout are only roughly on time, and can be delayed by whatever else the page is doing, which isn’t precise enough for music. The AudioContext, on the other hand, can start and stop sounds at an exact time, down to the sample.
So Tone.js checks for upcoming events every 50 milliseconds or so, and runs each callback a little ahead of its time, passing in the exact AudioContext time it’s for. The callback then schedules its sounds at that time, and the AudioContext plays them precisely when they’re due. How far ahead is called the lookAhead, and it’s 0.1 seconds by default:
That’s why it’s so important to pass time along to whatever you trigger in a callback (see Callback Timing). If you used Tone.now() instead, or nothing, the sound would play when the callback happened to run, up to 0.1 seconds early, and the timing would wobble.