Tone.js
    Preparing search index...

    Interface TransportInstance

    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.

    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();
    interface TransportInstance {
        bpm: TickParam<"bpm">;
        context: BaseContext;
        debug: boolean;
        emit: (event: any, ...args: any[]) => this;
        name: string;
        off: (
            event: TransportEventNames,
            callback?: (...args: any[]) => void,
        ) => this;
        on: (
            event: TransportEventNames,
            callback: (...args: any[]) => void,
        ) => this;
        once: (
            event: TransportEventNames,
            callback: (...args: any[]) => void,
        ) => this;
        get blockTime(): number;
        get disposed(): boolean;
        get loop(): boolean;
        set loop(loop: boolean): void;
        get loopEnd(): Unit.Time;
        set loopEnd(endPosition: Unit.Time): void;
        get loopStart(): Unit.Time;
        set loopStart(startPosition: Unit.Time): void;
        get position(): Unit.Time;
        set position(progress: Unit.Time): void;
        get PPQ(): number;
        set PPQ(ppq: number): void;
        get progress(): number;
        get sampleTime(): number;
        get seconds(): number;
        set seconds(s: number): void;
        get state(): PlaybackState;
        get swing(): number;
        set swing(amount: number): void;
        get swingSubdivision(): Subdivision;
        set swingSubdivision(subdivision: Subdivision): void;
        get ticks(): number;
        set ticks(t: number): void;
        get timeSignature(): TimeSignature;
        set timeSignature(timeSig: TimeSignature): void;
        cancel(after?: Unit.Time): this;
        clear(eventId: number): this;
        dispose(): this;
        get(): TransportOptions;
        getSecondsAtTime(time: Unit.Time): number;
        getTicksAtTime(time?: Unit.Time): number;
        immediate(): number;
        nextSubdivision(subdivision?: Unit.Time): number;
        now(): number;
        pause(time?: Unit.Time): this;
        schedule(
            callback: TransportCallback,
            time: Unit.Time | TransportTimeClass<number>,
        ): number;
        scheduleOnce(
            callback: TransportCallback,
            time: Unit.Time | TransportTimeClass<number>,
        ): number;
        scheduleRepeat(
            callback: TransportCallback,
            interval: Unit.Time | TimeClass<number, TimeBaseUnit>,
            startTime?: Unit.Time | TransportTimeClass<number>,
            duration?: Unit.Time,
        ): number;
        set(props: RecursivePartial<Options>): this;
        setLoopPoints(startPosition: Unit.Time, endPosition: Unit.Time): this;
        start(time?: Unit.Time, offset?: Unit.Time): this;
        stop(time?: Unit.Time): this;
        syncSignal(signal: Signal<any>, ratio?: number): this;
        toFrequency(freq: Unit.Frequency): number;
        toggle(time?: Unit.Time): this;
        toSeconds(time?: Unit.Time): number;
        toString(): string;
        toTicks(time?: Unit.Time | TimeClass<number, TimeBaseUnit>): number;
        unsyncSignal(signal: Signal<any>): this;
    }

    Hierarchy

    • ToneWithContext<TransportOptions>
      • TransportInstance

    Implements

    Index

    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);
    context: BaseContext

    The context belonging to the node.

    debug: boolean = false

    Set this debug flag to log all events that happen in this class.

    emit: (event: any, ...args: any[]) => this

    Invoke all of the callbacks bound to the event with any arguments passed in.

    Type Declaration

      • (event: any, ...args: any[]): this
      • Parameters

        • event: any

          The name of the event.

        • ...args: any[]

          The arguments to pass to the functions listening.

        Returns this

    name: string = "Transport"

    The name of the class

    off: (event: TransportEventNames, callback?: (...args: any[]) => void) => this

    Remove the event listener.

    Type Declaration

      • (event: TransportEventNames, callback?: (...args: any[]) => void): this
      • Parameters

        • event: TransportEventNames

          The event to stop listening to.

        • Optionalcallback: (...args: any[]) => void

          The callback which was bound to the event with Emitter.on. If no callback is given, all callbacks events are removed.

        Returns this

    on: (event: TransportEventNames, callback: (...args: any[]) => void) => this

    Bind a callback to a specific event.

    Type Declaration

      • (event: TransportEventNames, callback: (...args: any[]) => void): this
      • Parameters

        • event: TransportEventNames

          The name of the event to listen for.

        • callback: (...args: any[]) => void

          The callback to invoke when the event is emitted

        Returns this

    once: (event: TransportEventNames, callback: (...args: any[]) => void) => this

    Bind a callback which is only invoked once

    Type Declaration

      • (event: TransportEventNames, callback: (...args: any[]) => void): this
      • Parameters

        • event: TransportEventNames

          The name of the event to listen for.

        • callback: (...args: any[]) => void

          The callback to invoke when the event is emitted

        Returns this

    Accessors

    • get blockTime(): number

      The number of seconds of 1 processing block (128 samples)

      Returns number

      console.log(Tone.Destination.blockTime);
      
    • get disposed(): boolean

      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.

      Returns boolean

    • get PPQ(): number

      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.

      Returns number

    • set PPQ(ppq: number): void

      Parameters

      • ppq: number

      Returns void

    • get progress(): number

      The Transport's loop position as a normalized value. Always returns 0 if the Transport.loop = false.

      Returns number

    Methods

    • Remove scheduled events from the timeline after the given time. Repeated events will be removed if their startTime is after the given time

      Parameters

      • after: Unit.Time = 0

        Clear all events after this time.

      Returns this

    • Clear the passed in event id from the timeline

      Parameters

      • eventId: number

        The id of the event.

      Returns this

    • Get the object's attributes.

      Returns TransportOptions

      const osc = new Tone.Oscillator();
      console.log(osc.get());
    • Return the current time of the Context clock without any lookAhead.

      Returns number

      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.

      Parameters

      • Optionalsubdivision: Unit.Time

        The subdivision to quantize to

      Returns number

      The context time of the next subdivision.

      // the transport must be started, otherwise returns 0
      Tone.getTransport().start();
      Tone.getTransport().nextSubdivision("4n");
    • Return the current time of the Context clock plus the lookAhead.

      Returns number

      setInterval(() => {
      console.log(Tone.now());
      }, 100);
    • Schedule an event along the timeline.

      Parameters

      • callback: TransportCallback

        The callback to be invoked at the time.

      • time: Unit.Time | TransportTimeClass<number>

        The time to invoke the callback at.

      Returns number

      The id of the event which can be used for canceling the event.

      // schedule an event on the 16th measure
      Tone.getTransport().schedule((time) => {
      // invoked on measure 16
      console.log("measure 16!");
      }, "16:0:0");
    • Schedule a repeated event along the timeline. The event will fire at the interval starting at the startTime and for the specified duration.

      Parameters

      • callback: TransportCallback

        The callback to invoke.

      • interval: Unit.Time | TimeClass<number, TimeBaseUnit>

        The duration between successive callbacks. Must be a positive number.

      • OptionalstartTime: Unit.Time | TransportTimeClass<number>

        When along the timeline the events should start being invoked.

      • duration: Unit.Time = Infinity

        How long the event should repeat.

      Returns number

      The ID of the scheduled event. Use this to cancel the event.

      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");
    • Set multiple properties at once with an object.

      Parameters

      • props: RecursivePartial<Options>

      Returns this

      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.

      Parameters

      Returns this

      // loop over the first measure
      Tone.getTransport().setLoopPoints(0, "1m");
      Tone.getTransport().loop = true;
    • Start the transport and all sources synced to the transport.

      Parameters

      • Optionaltime: Unit.Time

        The time when the transport should start.

      • Optionaloffset: Unit.Time

        The timeline offset to start the transport.

      Returns this

      // start the transport in one second starting at beginning of the 5th measure.
      Tone.getTransport().start("+1", "4:0:0");
    • Stop the transport and all sources synced to the transport.

      Parameters

      • Optionaltime: Unit.Time

        The time when the transport should stop.

      Returns this

      Tone.getTransport().stop();
      
    • Attaches 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>
      • Optionalratio: number

        Optionally pass in the ratio between the two signals. Otherwise it will be computed based on their current values.

      Returns this

    • Toggle the current state of the transport. If it is started, it will stop it, otherwise it will start the Transport.

      Parameters

      • Optionaltime: Unit.Time

        The time of the event

      Returns this

    • Convert the incoming time to seconds. This is calculated against the current TransportInstance bpm

      Parameters

      Returns number

      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

      Returns string

      const osc = new Tone.Oscillator();
      console.log(osc.toString());