Transport for timing musical events.
Supports tempo curves and time changes. Unlike browser-based timing (setInterval, requestAnimationFrame)
Transport timing events pass in the exact time of the scheduled event
in the argument of the callback function. Pass that time value to the object
you're scheduling.
A single transport is created for you when the library is initialized.
The transport emits the events: "start", "stop", "pause", and "loop" which are
called with the time of that event as the argument.
The Transport object belonging to the global Tone.js Context.
See Transport
const osc = new Tone.Oscillator().toDestination();
// repeated event every 8th note
Tone.Transport.scheduleRepeat((time) => {
// use the callback time to schedule events
osc.start(time).stop(time + 0.1);
}, "8n");
// transport must be started before it starts invoking events
Tone.Transport.start();
The number of seconds of 1 processing block (128 samples)
console.log(Tone.Destination.blockTime);
The Beats Per Minute of the Transport.
const osc = new Tone.Oscillator().toDestination();
Tone.Transport.bpm.value = 80;
// start/stop the oscillator every quarter note
Tone.Transport.scheduleRepeat(time => {
osc.start(time).stop(time + 0.1);
}, "4n");
Tone.Transport.start();
// ramp the bpm to 120 over 10 seconds
Tone.Transport.bpm.rampTo(120, 10);
The context belonging to the node.
Set this debug flag to log all events that happen in this class.
Indicates if the instance was disposed. 'Disposing' an instance means that all of the Web Audio nodes that were created for the instance are disconnected and freed for garbage collection.
When the Transport.loop = true, this is the ending position of the loop.
When the Transport.loop = true, this is the starting position of the loop.
The Transport's position in Bars:Beats:Sixteenths. Setting the value will jump to that position right away.
Pulses Per Quarter note. This is the smallest resolution the Transport timing supports. This should be set once on initialization and not set again. Changing this value after other objects have been created can cause problems.
The Transport's loop position as a normalized value. Always returns 0 if the transport if loop is not true.
The duration in seconds of one sample.
console.log(Tone.Transport.sampleTime);
The Transport's position in seconds Setting the value will jump to that position right away.
Returns the playback state of the source, either "started", "stopped", or "paused"
The swing value. Between 0-1 where 1 equal to the note + half the subdivision.
Set the subdivision which the swing will be applied to. The default value is an 8th note. Value must be less than a quarter note.
The time signature as just the numerator over 4. For example 4/4 would be just 4 and 6/8 would be 3.
// common time
Tone.Transport.timeSignature = 4;
// 7/8
Tone.Transport.timeSignature = [7, 8];
// this will be reduced to a single number
Tone.Transport.timeSignature; // returns 3.5
The version number semver
Remove scheduled events from the timeline after the given time. Repeated events will be removed if their startTime is after the given time
Clear the passed in event id from the timeline
The id of the event.
Get the object's attributes.
const osc = new Tone.Oscillator();
console.log(osc.get());
Returns all of the default options belonging to the class.
Return the elapsed seconds at the given time.
Return the current time of the Context clock without any lookAhead.
setInterval(() => {
console.log(Tone.immediate());
}, 100);
Returns the time aligned to the next subdivision of the Transport. If the Transport is not started, it will return 0. Note: this will not work precisely during tempo ramps.
// the transport must be started, otherwise returns 0
Tone.Transport.start();
Tone.Transport.nextSubdivision("4n");
Return the current time of the Context clock plus the lookAhead.
setInterval(() => {
console.log(Tone.now());
}, 100);
Pause the transport and all sources synced to the transport.
Schedule an event along the timeline.
// schedule an event on the 16th measure
Tone.Transport.schedule((time) => {
// invoked on measure 16
console.log("measure 16!");
}, "16:0:0");
Schedule an event that will be removed after it is invoked.
Schedule a repeated event along the timeline. The event will fire
at the interval
starting at the startTime
and for the specified
duration
.
const osc = new Tone.Oscillator().toDestination().start();
// a callback invoked every eighth note after the first measure
Tone.Transport.scheduleRepeat((time) => {
osc.start(time).stop(time + 0.1);
}, "8n", "1m");
When along the timeline the events should start being invoked.
Set multiple properties at once with an object.
const filter = new Tone.Filter().toDestination();
// set values using an object
filter.set({
frequency: "C6",
type: "highpass"
});
const player = new Tone.Player("https://tonejs.github.io/audio/berklee/Analogsynth_octaves_highmid.mp3").connect(filter);
player.autostart = true;
Set the loop start and stop at the same time.
// loop over the first measure
Tone.Transport.setLoopPoints(0, "1m");
Tone.Transport.loop = true;
Start the transport and all sources synced to the transport.
// start the transport in one second starting at beginning of the 5th measure.
Tone.Transport.start("+1", "4:0:0");
Stop the transport and all sources synced to the transport.
Tone.Transport.stop();
Attaches the signal to the tempo control signal so that any changes in the tempo will change the signal in the same ratio.
Optionally pass in the ratio between the two signals.Otherwise it will be computed based on their current values.
Convert the input to a frequency number
const gain = new Tone.Gain();
console.log(gain.toFrequency("4n"));
Toggle the current state of the transport. If it is started, it will stop it, otherwise it will start the Transport.
Convert the incoming time to seconds. This is calculated against the current Tone.Transport bpm
const gain = new Tone.Gain();
setInterval(() => console.log(gain.toSeconds("4n")), 100);
// ramp the tempo to 60 bpm over 30 seconds
Tone.getTransport().bpm.rampTo(60, 30);
Convert the class to a string
const osc = new Tone.Oscillator();
console.log(osc.toString());
Convert the input time into ticks
const gain = new Tone.Gain();
console.log(gain.toTicks("4n"));
Unsyncs a previously synced signal from the transport's control. See Transport.syncSignal.