TransportInstance
interface Core ↳ extends ToneWithContext<TransportOptions>
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.
A single transport is created for you when the library is initialized.
The transport emits “start”, “stop”, “pause”, and “loop” events.
const osc = new Tone.Oscillator().toDestination();
// Repeated event every 8th note.
Tone.getTransport().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.getTransport().start();Properties
Section titled “Properties”bpm: TickParam<"bpm">The Beats Per Minute of the Transport.
const osc = new Tone.Oscillator().toDestination();
Tone.getTransport().bpm.value = 80;
// start/stop the oscillator every quarter note
Tone.getTransport().scheduleRepeat(time => {
osc.start(time).stop(time + 0.1);
}, "4n");
Tone.getTransport().start();
// ramp the bpm to 120 over 10 seconds
Tone.getTransport().bpm.rampTo(120, 10);emit: (event: any, ...args: any[]) => thisInvoke all of the callbacks bound to the event with any arguments passed in.
off: (event: TransportEventNames, callback?: (...args: any[]) => void) => thisRemove the event listener.
on: (event: TransportEventNames, callback: (...args: any[]) => void) => thisBind a callback to a specific event.
once: (event: TransportEventNames, callback: (...args: any[]) => void) => thisBind a callback which is only invoked once
loopEnd
Section titled “loopEnd”loopEnd: TimeWhen the Transport.loop = true, this is the ending position of the loop.
loopStart
Section titled “loopStart”loopStart: TimeWhen the Transport.loop = true, this is the starting position of the loop.
position
Section titled “position”position: TimeThe Transport’s position in Bars:Beats:Sixteenths. Setting the value will jump to that position right away.
PPQ: numberPulses 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.
progress
Section titled “progress”read-only
progress: numberThe Transport’s loop position as a normalized value. Always returns 0 if the Transport.loop = false.
seconds
Section titled “seconds”seconds: numberThe Transport’s position in seconds. Setting the value will jump to that position right away.
read-only
state: PlaybackStateThe playback state of the transport, either “started”, “stopped”, or “paused”.
swing: numberThe swing value. Between 0-1 where 1 equal to the note + half the subdivision.
swingSubdivision
Section titled “swingSubdivision”swingSubdivision: SubdivisionSet the subdivision which the swing will be applied to. The default value is an 8th note. Value must be less than a quarter note.
timeSignature
Section titled “timeSignature”timeSignature: TimeSignatureThe 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.getTransport().timeSignature = 4; // 7/8 Tone.getTransport().timeSignature = [7, 8]; // this will be reduced to a single number Tone.getTransport().timeSignature; // returns 3.5
Methods
Section titled “Methods”cancel()
Section titled “cancel()”cancel(after?: Time): thisRemove scheduled events from the timeline after the given time. Repeated events will be removed if their startTime is after the given time
Parameters
-
after
Timeoptional default0Clear all events after this time.
Returns this
clear()
Section titled “clear()”clear(eventId: number): thisClear the passed in event id from the timeline
Parameters
-
eventId
numberThe id of the event.
Returns this
getSecondsAtTime()
Section titled “getSecondsAtTime()”getSecondsAtTime(time: Time): numberReturn the elapsed seconds at the given time.
Parameters
-
time
TimeWhen to get the elapsed seconds
Returns number The number of elapsed seconds
getTicksAtTime()
Section titled “getTicksAtTime()”getTicksAtTime(time?: Time): numberGet the clock’s ticks at the given time.
Parameters
-
time
TimeoptionalWhen to get the tick value
Returns number The tick value at the given time.
nextSubdivision()
Section titled “nextSubdivision()”nextSubdivision(subdivision?: Time): numberReturns 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.getTransport().start();
Tone.getTransport().nextSubdivision("4n");Parameters
-
subdivision
TimeoptionalThe subdivision to quantize to
Returns number The context time of the next subdivision.
pause()
Section titled “pause()”pause(time?: Time): thisPause the transport and all sources synced to the transport.
Parameters
- time
Timeoptional
Returns this
schedule()
Section titled “schedule()”schedule(callback: TransportCallback, time: Time | TransportTimeClass<number>): numberSchedule an event to be invoked at a specific time.
// Schedule an event on the 16th measure.
Tone.getTransport().schedule((time) => {
// Invoked on measure 16.
console.log("measure 16!");
}, "16:0:0");Parameters
-
callback
TransportCallbackThe callback to invoke at the given time.
-
time
Time | TransportTimeClass<number>The time to invoke the callback at.
Returns number The ID of the event, which can be used to cancel the event.
scheduleOnce()
Section titled “scheduleOnce()”scheduleOnce(callback: TransportCallback, time: Time | TransportTimeClass<number>): numberSchedule an event that will be removed after it is invoked.
Parameters
-
callback
TransportCallbackThe callback to invoke once.
-
time
Time | TransportTimeClass<number>The time the callback should be invoked.
Returns number The ID of the scheduled event.
scheduleRepeat()
Section titled “scheduleRepeat()”scheduleRepeat(callback: TransportCallback, interval: Time | TimeClass<number, TimeBaseUnit>, startTime?: Time | TransportTimeClass<number>, duration?: Time): numberSchedule a repeated event.
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.getTransport().scheduleRepeat((time) => {
osc.start(time).stop(time + 0.1);
}, "8n", "1m");Parameters
-
callback
TransportCallbackThe callback to invoke.
-
interval
Time | TimeClass<number, TimeBaseUnit>The duration between successive callbacks.
-
startTime
Time | TransportTimeClass<number>optionalWhen the event should start.
-
duration
Timeoptional defaultInfinityHow long the event should repeat.
Returns number The ID of the scheduled event. Use this to cancel the event.
setLoopPoints()
Section titled “setLoopPoints()”Set the loop start and stop at the same time.
// loop over the first measure Tone.getTransport().setLoopPoints(0, "1m"); Tone.getTransport().loop = true;
Returns this
setSecondsAtTime()
Section titled “setSecondsAtTime()”setSecondsAtTime(seconds: number, time: Time): thisSet the Transport’s seconds value at the given time.
Parameters
-
seconds
numberThe seconds value to set
-
time
TimeThe Context time at which to set the seconds value
Returns this
setTicksAtTime()
Section titled “setTicksAtTime()”setTicksAtTime(ticks: number, time: Time): thisSet the Transport’s ticks value at the given time
Parameters
-
ticks
numberThe tick value to set
-
time
TimeThe Context time at which to set the seconds value
Returns this
start()
Section titled “start()”Start the transport and all sources synced to the transport.
// Start the transport in one second, beginning at the start of the 5th measure.
Tone.getTransport().start("+1", "4:0:0");Parameters
Returns this
stop()
Section titled “stop()”stop(time?: Time): thisStop the transport and all sources synced to the transport.
Tone.getTransport().stop();
Parameters
-
time
TimeoptionalThe time when the transport should stop.
Returns this
syncSignal()
Section titled “syncSignal()”syncSignal(signal: Signal<any>, ratio?: number): thisAttaches the signal to the tempo control signal so that any changes in the tempo will change the signal in the same ratio.
Parameters
-
signal
Signal<any> -
ratio
numberoptionalOptionally pass in the ratio between the two signals. Otherwise it will be computed based on their current values.
Returns this
toggle()
Section titled “toggle()”toggle(time?: Time): thisToggle the current state of the transport.
If it is started, it will stop it. If it is stopped, it will start it.
Parameters
-
time
TimeoptionalThe time of the event.
Returns this
unsyncSignal()
Section titled “unsyncSignal()”unsyncSignal(signal: Signal<any>): thisUnsyncs a previously synced signal from the transport’s control.
See also: syncSignal.
Parameters
- signal
Signal<any>
Returns this
Inherited members
Section titled “Inherited members”- From
ToneWithContext: context, blockTime, sampleTime, get(), immediate(), now(), set(), toFrequency(), toSeconds(), toTicks() - From
Tone: debug, disposed, toString()