Class Context

Wrapper around the native AudioContext.

Hierarchy (view full)

Constructors

Properties

debug: boolean = false

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

isOffline: boolean = false

Indicates if the context is an OfflineAudioContext or an AudioContext

name: string = "Context"
version: string = version

The version number semver

Accessors

  • get currentTime(): number
  • The current time in seconds of the AudioContext.

    Returns number

  • get destination(): DestinationClass
  • A reference to the Context's destination node.

    Returns DestinationClass

  • set destination(d): void
  • Parameters

    • d: DestinationClass

    Returns void

  • 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 draw(): DrawClass
  • This is the Draw object for the context which is useful for synchronizing the draw frame with the Tone.js clock.

    Returns DrawClass

  • set draw(d): void
  • Parameters

    • d: DrawClass

    Returns void

  • get latencyHint(): number | AudioContextLatencyCategory
  • The type of playback, which affects tradeoffs between audio output latency and responsiveness. In addition to setting the value in seconds, the latencyHint also accepts the strings "interactive" (prioritizes low latency), "playback" (prioritizes sustained playback), "balanced" (balances latency and performance).

    Returns number | AudioContextLatencyCategory

    Example

    // prioritize sustained playback
    const context = new Tone.Context({ latencyHint: "playback" });
    // set this context as the global Context
    Tone.setContext(context);
    // the global context is gettable with Tone.getContext()
    console.log(Tone.getContext().latencyHint);
  • get lookAhead(): number
  • The amount of time into the future events are scheduled. Giving Web Audio a short amount of time into the future to schedule events can reduce clicks and improve performance. This value can be set to 0 to get the lowest latency. Adjusting this value also affects the updateInterval.

    Returns number

  • set lookAhead(time): void
  • Parameters

    • time: number

    Returns void

  • get rawContext(): AnyAudioContext
  • The unwrapped AudioContext or OfflineAudioContext

    Returns AnyAudioContext

  • get sampleRate(): number
  • The current time in seconds of the AudioContext.

    Returns number

  • get state(): AudioContextState
  • The current time in seconds of the AudioContext.

    Returns AudioContextState

  • get transport(): TransportClass
  • There is only one Transport per Context. It is created on initialization.

    Returns TransportClass

  • set transport(t): void
  • Parameters

    • t: TransportClass

    Returns void

  • get updateInterval(): number
  • How often the interval callback is invoked. This number corresponds to how responsive the scheduling can be. Setting to 0 will result in the lowest practial interval based on context properties. context.updateInterval + context.lookAhead gives you the total latency between scheduling an event and hearing it.

    Returns number

  • set updateInterval(interval): void
  • Parameters

    • interval: number

    Returns void

Methods

  • Close the context. Once closed, the context can no longer be used and any AudioNodes created from the context will be silent.

    Returns Promise<void>

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

    Parameters

    • event: "statechange" | "tick"

      The name of the event.

    • Rest ...args: any[]

      The arguments to pass to the functions listening.

    Returns this

  • Remove the event listener.

    Parameters

    • event: "statechange" | "tick"

      The event to stop listening to.

    • Optional callback: ((...args) => void)

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

        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

  • Bind a callback to a specific event.

    Parameters

    • event: "statechange" | "tick"

      The name of the event to listen for.

    • callback: ((...args) => void)

      The callback to invoke when the event is emitted

        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

  • Bind a callback which is only invoked once

    Parameters

    • event: "statechange" | "tick"

      The name of the event to listen for.

    • callback: ((...args) => void)

      The callback to invoke when the event is emitted

        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

  • Adds a repeating event to the context's callback clock

    Parameters

    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • interval: number

    Returns number

  • A setTimeout which is guaranteed by the clock source. Also runs in the offline context.

    Parameters

    • fn: ((...args) => void)

      The callback to invoke

        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • timeout: number

      The timeout in seconds

    Returns number

    ID to use when invoking Context.clearTimeout

  • Convert the class to a string

    Returns string

    Example

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