Skip to content
Tone.js

OfflineContext

class Core ↳ extends Context

Wrapper around the OfflineAudioContext

// generate a single channel, 0.5 second buffer
const context = new Tone.OfflineContext(1, 0.5, 44100);
const osc = new Tone.Oscillator({ context });
context.render().then(buffer => {
	console.log(buffer.numberOfChannels, buffer.duration);
});
new OfflineContext(channels: number, duration: number, sampleRate: number)

Parameters

  • channels number

    The number of channels to render

  • duration number

    The duration to render in seconds

  • sampleRate number

    the sample rate to render at

new OfflineContext(context: OfflineAudioContext)

Parameters

  • context OfflineAudioContext

read-only

isOffline: boolean

Indicates if the context is an OfflineAudioContext or an AudioContext

Default: true

read-only

name: string

The name of the class

Default: "OfflineContext"

↳ inherited from Context

clockSource: TickerClockSource

What the source of the clock is, either “worker” (default), “timeout”, or “offline” (none).

read-only

currentTime: number

Same as this.now()

↳ inherited from Context

destination: DestinationInstance

A reference to the Context’s destination node.

↳ inherited from Context

This is the Draw object for the context which is useful for synchronizing the draw frame with the Tone.js clock.

read-only ↳ inherited from Context

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).

// 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);

↳ inherited from Context

The listener

↳ inherited from Context

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.

read-only ↳ inherited from Context

rawContext: AnyAudioContext

The unwrapped AudioContext or OfflineAudioContext

read-only ↳ inherited from Context

sampleRate: number

The current time in seconds of the AudioContext.

read-only ↳ inherited from Context

state: AudioContextState

The current time in seconds of the AudioContext.

↳ inherited from Context

There is only one Transport per Context. It is created on initialization.

↳ inherited from Context

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 practical interval based on context properties. context.updateInterval + context.lookAhead gives you the total latency between scheduling an event and hearing it.

↳ inherited from Context

addAudioWorkletModule(url: string): Promise<void>

Add an AudioWorkletProcessor module

Parameters

  • url string

    The url of the module

Returns Promise<void>

↳ inherited from Context

clearInterval(id: number): this

Clear the function scheduled by setInterval.

Parameters

Returns this

↳ inherited from Context

clearTimeout(id: number): this

Clears a previously scheduled timeout with setTimeout.

Parameters

Returns this

close(): Promise<void>

Close the context

Returns Promise<void>

↳ inherited from Context

createAnalyser(): AnalyserNode

The createAnalyser() method of the BaseAudioContext interface creates an AnalyserNode, which can be used to expose audio time and frequency data and create data visualizations.

MDN Reference

Returns AnalyserNode

↳ inherited from Context

createAudioWorkletNode(name: string, options?: Partial<AudioWorkletNodeOptions>): AudioWorkletNode

Create an audio worklet node from a name and options. The module must first be loaded using addAudioWorkletModule.

Parameters

  • name string
  • options Partial<AudioWorkletNodeOptions> optional

Returns AudioWorkletNode

↳ inherited from Context

createBiquadFilter(): BiquadFilterNode

The createBiquadFilter() method of the BaseAudioContext interface creates a BiquadFilterNode, which represents a second order filter configurable as several different common filter types.

MDN Reference

Returns BiquadFilterNode

↳ inherited from Context

createBuffer(numberOfChannels: number, length: number, sampleRate: number): AudioBuffer

The createBuffer() method of the BaseAudioContext Interface is used to create a new, empty AudioBuffer object, which can then be populated by data, and played via an AudioBufferSourceNode.

MDN Reference

Parameters

  • numberOfChannels number
  • length number
  • sampleRate number

Returns AudioBuffer

↳ inherited from Context

createBufferSource(): AudioBufferSourceNode

The createBufferSource() method of the BaseAudioContext Interface is used to create a new AudioBufferSourceNode, which can be used to play audio data contained within an AudioBuffer object. AudioBuffers are created using BaseAudioContext.createBuffer or returned by BaseAudioContext.decodeAudioData when it successfully decodes an audio track.

MDN Reference

Returns AudioBufferSourceNode

↳ inherited from Context

createChannelMerger(numberOfInputs?: number): ChannelMergerNode

The createChannelMerger() method of the BaseAudioContext interface creates a ChannelMergerNode, which combines channels from multiple audio streams into a single audio stream.

MDN Reference

Parameters

  • numberOfInputs number optional

Returns ChannelMergerNode

↳ inherited from Context

createChannelSplitter(numberOfOutputs?: number): ChannelSplitterNode

The createChannelSplitter() method of the BaseAudioContext Interface is used to create a ChannelSplitterNode, which is used to access the individual channels of an audio stream and process them separately.

MDN Reference

Parameters

  • numberOfOutputs number optional

Returns ChannelSplitterNode

↳ inherited from Context

createConstantSource(): ConstantSourceNode

The createConstantSource() property of the BaseAudioContext interface creates a ConstantSourceNode object, which is an audio source that continuously outputs a monaural (one-channel) sound signal whose samples all have the same value.

MDN Reference

Returns ConstantSourceNode

↳ inherited from Context

createConvolver(): ConvolverNode

The createConvolver() method of the BaseAudioContext interface creates a ConvolverNode, which is commonly used to apply reverb effects to your audio. See the spec definition of Convolution for more information.

MDN Reference

Returns ConvolverNode

↳ inherited from Context

createDelay(maxDelayTime?: number): DelayNode

The createDelay() method of the BaseAudioContext Interface is used to create a DelayNode, which is used to delay the incoming audio signal by a certain amount of time.

MDN Reference

Parameters

  • maxDelayTime number optional

Returns DelayNode

↳ inherited from Context

createDynamicsCompressor(): DynamicsCompressorNode

The createDynamicsCompressor() method of the BaseAudioContext Interface is used to create a DynamicsCompressorNode, which can be used to apply compression to an audio signal.

MDN Reference

Returns DynamicsCompressorNode

↳ inherited from Context

createGain(): GainNode

The createGain() method of the BaseAudioContext interface creates a GainNode, which can be used to control the overall gain (or volume) of the audio graph.

MDN Reference

Returns GainNode

↳ inherited from Context

createIIRFilter(feedForward: Float32Array<ArrayBuffer> | number[], feedback: Float32Array<ArrayBuffer> | number[]): IIRFilterNode

The createIIRFilter() method of the BaseAudioContext interface creates an IIRFilterNode, which represents a general infinite impulse response (IIR) filter which can be configured to serve as various types of filter.

MDN Reference

Parameters

  • feedForward Float32Array<ArrayBuffer> | number[]
  • feedback Float32Array<ArrayBuffer> | number[]

Returns IIRFilterNode

↳ inherited from Context

createMediaElementSource(element: HTMLMediaElement): MediaElementAudioSourceNode

Parameters

  • element HTMLMediaElement

Returns MediaElementAudioSourceNode

↳ inherited from Context

createMediaStreamDestination(): MediaStreamAudioDestinationNode

Returns MediaStreamAudioDestinationNode

↳ inherited from Context

createMediaStreamSource(stream: MediaStream): MediaStreamAudioSourceNode

Parameters

  • stream MediaStream

Returns MediaStreamAudioSourceNode

↳ inherited from Context

createOscillator(): OscillatorNode

The createOscillator() method of the BaseAudioContext interface creates an OscillatorNode, a source representing a periodic waveform. It basically generates a constant tone.

MDN Reference

Returns OscillatorNode

↳ inherited from Context

createPanner(): PannerNode

The createPanner() method of the BaseAudioContext Interface is used to create a new PannerNode, which is used to spatialize an incoming audio stream in 3D space.

MDN Reference

Returns PannerNode

↳ inherited from Context

createPeriodicWave(real: Float32Array<ArrayBuffer> | number[], imag: Float32Array<ArrayBuffer> | number[], constraints?: PeriodicWaveConstraints): PeriodicWave

The createPeriodicWave() method of the BaseAudioContext interface is used to create a PeriodicWave. This wave is used to define a periodic waveform that can be used to shape the output of an OscillatorNode.

MDN Reference

Parameters

  • real Float32Array<ArrayBuffer> | number[]
  • imag Float32Array<ArrayBuffer> | number[]
  • constraints PeriodicWaveConstraints optional

Returns PeriodicWave

↳ inherited from Context

createStereoPanner(): StereoPannerNode

The createStereoPanner() method of the BaseAudioContext interface creates a StereoPannerNode, which can be used to apply stereo panning to an audio source. It positions an incoming audio stream in a stereo image using a low-cost panning algorithm.

MDN Reference

Returns StereoPannerNode

↳ inherited from Context

createWaveShaper(): WaveShaperNode

The createWaveShaper() method of the BaseAudioContext interface creates a WaveShaperNode, which represents a non-linear distortion. It is used to apply distortion effects to your audio.

MDN Reference

Returns WaveShaperNode

↳ inherited from Context

decodeAudioData(audioData: ArrayBuffer): Promise<AudioBuffer>

The decodeAudioData() method of the BaseAudioContext Interface is used to asynchronously decode audio file data contained in an ArrayBuffer that is loaded from fetch(), XMLHttpRequest, or FileReader. The decoded AudioBuffer is resampled to the AudioContext’s sampling rate, then passed to a callback or promise.

MDN Reference

Parameters

  • audioData ArrayBuffer

Returns Promise<AudioBuffer>

↳ inherited from Context

dispose(): this

Clean up. Also closes the audio context.

Returns this

↳ inherited from Emitter

emit(event: "statechange" | "tick", ...args: any[]): this

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

Parameters

  • event "statechange" | "tick"

    The name of the event.

  • args any[]

    The arguments to pass to the functions listening.

Returns this

↳ inherited from Context

immediate(): number

The current audio context time without the lookAhead. In most cases it is better to use now instead of immediate since with now the lookAhead is applied equally to all components including internal components, to making sure that everything is scheduled in sync. Mixing now and immediate can cause some timing issues. If no lookAhead is desired, you can set the lookAhead to 0.

Returns number

now(): number

Override the now method to point to the internal clock time

Returns number

↳ inherited from Emitter

off(event: "statechange" | "tick", callback?: (...args: any[]) => void): this

Remove the event listener.

Parameters

  • event "statechange" | "tick"

    The event to stop listening to.

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

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

Returns this

↳ inherited from Emitter

on(event: "statechange" | "tick", callback: (...args: any[]) => void): this

Bind a callback to a specific event.

Parameters

  • event "statechange" | "tick"

    The name of the event to listen for.

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

    The callback to invoke when the event is emitted

Returns this

↳ inherited from Emitter

once(event: "statechange" | "tick", callback: (...args: any[]) => void): this

Bind a callback which is only invoked once

Parameters

  • event "statechange" | "tick"

    The name of the event to listen for.

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

    The callback to invoke when the event is emitted

Returns this

render(asynchronous?: boolean): Promise<ToneAudioBuffer>

Render the output of the OfflineContext

Parameters

  • asynchronous boolean optional default true

    If the clock should be rendered asynchronously, which will not block the main thread, but be slightly slower.

Returns Promise<ToneAudioBuffer>

↳ inherited from Context

resume(): Promise<void>

Starts the audio context from a suspended state. This is required to initially start the AudioContext.

See also: start

Returns Promise<void>

↳ inherited from Context

setInterval(fn: (...args: any[]) => void, interval: number): number

Adds a repeating event to the context’s callback clock.

Parameters

  • fn (…args: any[]) => void

    The callback to invoke.

  • interval number

    The timeout in seconds.

Returns number ID to use when invoking clearInterval.

↳ inherited from Context

setTimeout(fn: (...args: any[]) => void, timeout: number): number

A setTimeout which is guaranteed by the clock source.

Also runs in the offline context.

Parameters

  • fn (…args: any[]) => void

    The callback to invoke.

  • timeout number

    The timeout in seconds.

Returns number ID to use when invoking clearTimeout.

↳ inherited from BaseContext

toJSON(): Record<string, any>

Returns Record<string, any>

static ↳ inherited from Context

getDefaults(): ContextOptions

Returns all of the default options belonging to the class.

Returns ContextOptions

static ↳ inherited from Emitter

mixin(constr: any): void

Add Emitter functions (on/off/emit) to the object

Parameters

  • constr any

↳ inherited from Context

getConstant(val: number): AudioBufferSourceNode

Internal Generate a looped buffer at some constant value.

Parameters

  • val number

Returns AudioBufferSourceNode