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);
});Constructor
Section titled “Constructor”new OfflineContext(channels: number, duration: number, sampleRate: number)Parameters
-
channels
numberThe number of channels to render
-
duration
numberThe duration to render in seconds
-
sampleRate
numberthe sample rate to render at
new OfflineContext(context: OfflineAudioContext)Parameters
- context
OfflineAudioContext
Properties
Section titled “Properties”isOffline
Section titled “isOffline”read-only
isOffline: booleanIndicates if the context is an OfflineAudioContext or an AudioContext
Default: true
clockSource
Section titled “clockSource”↳ inherited from Context
clockSource: TickerClockSourceWhat the source of the clock is, either “worker” (default), “timeout”, or “offline” (none).
destination
Section titled “destination”↳ inherited from Context
destination: DestinationClassA reference to the Context’s destination node.
↳ inherited from Context
draw: DrawClassThis is the Draw object for the context which is useful for synchronizing the draw frame with the Tone.js clock.
latencyHint
Section titled “latencyHint”read-only ↳ inherited from Context
latencyHint: number | AudioContextLatencyCategoryThe 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);lookAhead
Section titled “lookAhead”↳ inherited from Context
lookAhead: numberThe 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.
rawContext
Section titled “rawContext”read-only ↳ inherited from Context
rawContext: AnyAudioContextThe unwrapped AudioContext or OfflineAudioContext
sampleRate
Section titled “sampleRate”read-only ↳ inherited from Context
sampleRate: numberThe current time in seconds of the AudioContext.
read-only ↳ inherited from Context
state: AudioContextStateThe current time in seconds of the AudioContext.
transport
Section titled “transport”↳ inherited from Context
transport: TransportClassThere is only one Transport per Context. It is created on initialization.
updateInterval
Section titled “updateInterval”↳ inherited from Context
updateInterval: numberHow 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.
Methods
Section titled “Methods”addAudioWorkletModule()
Section titled “addAudioWorkletModule()”↳ inherited from Context
addAudioWorkletModule(url: string): Promise<void>Add an AudioWorkletProcessor module
Parameters
-
url
stringThe url of the module
Returns Promise<void>
clearInterval()
Section titled “clearInterval()”↳ inherited from Context
clearInterval(id: number): thisClear the function scheduled by setInterval
Parameters
- id
number
Returns this
clearTimeout()
Section titled “clearTimeout()”↳ inherited from Context
clearTimeout(id: number): thisClears a previously scheduled timeout with Tone.context.setTimeout
Parameters
-
id
numberThe ID returned from setTimeout
Returns this
createAnalyser()
Section titled “createAnalyser()”↳ inherited from Context
createAnalyser(): AnalyserNodeThe createAnalyser() method of the BaseAudioContext interface creates an AnalyserNode, which can be used to expose audio time and frequency data and create data visualizations.
Returns AnalyserNode
createAudioWorkletNode()
Section titled “createAudioWorkletNode()”↳ inherited from Context
createAudioWorkletNode(name: string, options?: Partial<AudioWorkletNodeOptions>): AudioWorkletNodeCreate 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
createBiquadFilter()
Section titled “createBiquadFilter()”↳ inherited from Context
createBiquadFilter(): BiquadFilterNodeThe createBiquadFilter() method of the BaseAudioContext interface creates a BiquadFilterNode, which represents a second order filter configurable as several different common filter types.
Returns BiquadFilterNode
createBuffer()
Section titled “createBuffer()”↳ inherited from Context
createBuffer(numberOfChannels: number, length: number, sampleRate: number): AudioBufferThe 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.
Parameters
- numberOfChannels
number - length
number - sampleRate
number
Returns AudioBuffer
createBufferSource()
Section titled “createBufferSource()”↳ inherited from Context
createBufferSource(): AudioBufferSourceNodeThe 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.
Returns AudioBufferSourceNode
createChannelMerger()
Section titled “createChannelMerger()”↳ inherited from Context
createChannelMerger(numberOfInputs?: number): ChannelMergerNodeThe createChannelMerger() method of the BaseAudioContext interface creates a ChannelMergerNode, which combines channels from multiple audio streams into a single audio stream.
Parameters
- numberOfInputs
numberoptional
Returns ChannelMergerNode
createChannelSplitter()
Section titled “createChannelSplitter()”↳ inherited from Context
createChannelSplitter(numberOfOutputs?: number): ChannelSplitterNodeThe 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.
Parameters
- numberOfOutputs
numberoptional
Returns ChannelSplitterNode
createConstantSource()
Section titled “createConstantSource()”↳ inherited from Context
createConstantSource(): ConstantSourceNodeThe 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.
Returns ConstantSourceNode
createConvolver()
Section titled “createConvolver()”↳ inherited from Context
createConvolver(): ConvolverNodeThe 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.
Returns ConvolverNode
createDelay()
Section titled “createDelay()”↳ inherited from Context
createDelay(maxDelayTime?: number): DelayNodeThe 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.
Parameters
- maxDelayTime
numberoptional
Returns DelayNode
createDynamicsCompressor()
Section titled “createDynamicsCompressor()”↳ inherited from Context
createDynamicsCompressor(): DynamicsCompressorNodeThe createDynamicsCompressor() method of the BaseAudioContext Interface is used to create a DynamicsCompressorNode, which can be used to apply compression to an audio signal.
Returns DynamicsCompressorNode
createGain()
Section titled “createGain()”↳ inherited from Context
createGain(): GainNodeThe createGain() method of the BaseAudioContext interface creates a GainNode, which can be used to control the overall gain (or volume) of the audio graph.
Returns GainNode
createIIRFilter()
Section titled “createIIRFilter()”↳ inherited from Context
createIIRFilter(feedForward: number[] | Float32Array<ArrayBuffer>, feedback: number[] | Float32Array<ArrayBuffer>): IIRFilterNodeThe 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.
Parameters
- feedForward
number[] | Float32Array<ArrayBuffer> - feedback
number[] | Float32Array<ArrayBuffer>
Returns IIRFilterNode
createMediaElementSource()
Section titled “createMediaElementSource()”↳ inherited from Context
createMediaElementSource(element: HTMLMediaElement): MediaElementAudioSourceNodeParameters
- element
HTMLMediaElement
Returns MediaElementAudioSourceNode
createMediaStreamDestination()
Section titled “createMediaStreamDestination()”↳ inherited from Context
createMediaStreamDestination(): MediaStreamAudioDestinationNodeReturns MediaStreamAudioDestinationNode
createMediaStreamSource()
Section titled “createMediaStreamSource()”↳ inherited from Context
createMediaStreamSource(stream: MediaStream): MediaStreamAudioSourceNodeParameters
- stream
MediaStream
Returns MediaStreamAudioSourceNode
createOscillator()
Section titled “createOscillator()”↳ inherited from Context
createOscillator(): OscillatorNodeThe createOscillator() method of the BaseAudioContext interface creates an OscillatorNode, a source representing a periodic waveform. It basically generates a constant tone.
Returns OscillatorNode
createPanner()
Section titled “createPanner()”↳ inherited from Context
createPanner(): PannerNodeThe 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.
Returns PannerNode
createPeriodicWave()
Section titled “createPeriodicWave()”↳ inherited from Context
createPeriodicWave(real: number[] | Float32Array<ArrayBuffer>, imag: number[] | Float32Array<ArrayBuffer>, constraints?: PeriodicWaveConstraints): PeriodicWaveThe 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.
Parameters
- real
number[] | Float32Array<ArrayBuffer> - imag
number[] | Float32Array<ArrayBuffer> - constraints
PeriodicWaveConstraintsoptional
Returns PeriodicWave
createStereoPanner()
Section titled “createStereoPanner()”↳ inherited from Context
createStereoPanner(): StereoPannerNodeThe 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.
Returns StereoPannerNode
createWaveShaper()
Section titled “createWaveShaper()”↳ inherited from Context
createWaveShaper(): WaveShaperNodeThe 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.
Returns WaveShaperNode
decodeAudioData()
Section titled “decodeAudioData()”↳ 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.
Parameters
- audioData
ArrayBuffer
Returns Promise<AudioBuffer>
dispose()
Section titled “dispose()”↳ inherited from Context
dispose(): thisClean up. Also closes the audio context.
Returns this
emit()
Section titled “emit()”↳ inherited from Emitter
emit(event: "statechange" | "tick", ...args: any[]): thisInvoke 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
getConstant()
Section titled “getConstant()”↳ inherited from Context
getConstant(val: number): AudioBufferSourceNodeInternal Generate a looped buffer at some constant value.
Parameters
- val
number
Returns AudioBufferSourceNode
immediate()
Section titled “immediate()”↳ inherited from Context
immediate(): numberThe 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(): numberOverride the now method to point to the internal clock time
Returns number
↳ inherited from Emitter
off(event: "statechange" | "tick", callback?: (...args: any[]) => void): thisRemove the event listener.
Parameters
-
event
"statechange" | "tick"The event to stop listening to.
-
callback
(…args: any[]) => voidoptionalThe 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): thisBind a callback to a specific event.
Parameters
-
event
"statechange" | "tick"The name of the event to listen for.
-
callback
(…args: any[]) => voidThe callback to invoke when the event is emitted
Returns this
once()
Section titled “once()”↳ inherited from Emitter
once(event: "statechange" | "tick", callback: (...args: any[]) => void): thisBind a callback which is only invoked once
Parameters
-
event
"statechange" | "tick"The name of the event to listen for.
-
callback
(…args: any[]) => voidThe callback to invoke when the event is emitted
Returns this
render()
Section titled “render()”render(asynchronous?: boolean): Promise<ToneAudioBuffer>Render the output of the OfflineContext
Parameters
-
asynchronous
booleanoptional defaulttrueIf the clock should be rendered asynchronously, which will not block the main thread, but be slightly slower.
Returns Promise<ToneAudioBuffer>
resume()
Section titled “resume()”↳ 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>
setInterval()
Section titled “setInterval()”↳ inherited from Context
setInterval(fn: (...args: any[]) => void, interval: number): numberAdds a repeating event to the context’s callback clock
Parameters
- fn
(…args: any[]) => void - interval
number
Returns number
setTimeout()
Section titled “setTimeout()”↳ inherited from Context
setTimeout(fn: (...args: any[]) => void, timeout: number): numberA setTimeout which is guaranteed by the clock source. Also runs in the offline context.
Parameters
-
fn
(…args: any[]) => voidThe callback to invoke
-
timeout
numberThe timeout in seconds
Returns number ID to use when invoking Context.clearTimeout
toJSON()
Section titled “toJSON()”↳ inherited from BaseContext
toJSON(): Record<string, any>Returns Record<string, any>
getDefaults()
Section titled “getDefaults()”static ↳ inherited from Context
getDefaults(): ContextOptionsReturns all of the default options belonging to the class.
Returns ContextOptions
mixin()
Section titled “mixin()”static ↳ inherited from Emitter
mixin(constr: any): voidAdd Emitter functions (on/off/emit) to the object
Parameters
- constr
any
Inherited members
Section titled “Inherited members”- From
Tone: debug, version, disposed, toString()