Context
class Core ↳ extends BaseContext
Wraps the native AudioContext.
Constructor
Section titled “Constructor”new Context(context?: AnyAudioContext)Parameters
- context
AnyAudioContextoptional
new Context(options?: Partial<ContextOptions>)Parameters
- options
Partial<ContextOptions>optional
Properties
Section titled “Properties”isOffline
Section titled “isOffline”read-only
isOffline: booleanIndicates if the context is an OfflineAudioContext or an AudioContext
Default: false
clockSource
Section titled “clockSource”clockSource: TickerClockSourceWhat the source of the clock is, either “worker” (default), “timeout”, or “offline” (none).
currentTime
Section titled “currentTime”read-only
currentTime: numberThe current time in seconds of the AudioContext.
destination
Section titled “destination”destination: DestinationInstanceA reference to the Context’s destination node.
draw: DrawInstanceThis 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
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”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
rawContext: AnyAudioContextThe unwrapped AudioContext or OfflineAudioContext
sampleRate
Section titled “sampleRate”read-only
sampleRate: numberThe current time in seconds of the AudioContext.
read-only
state: AudioContextStateThe current time in seconds of the AudioContext.
transport
Section titled “transport”transport: TransportInstanceThere is only one Transport per Context. It is created on initialization.
updateInterval
Section titled “updateInterval”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 practical 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()”addAudioWorkletModule(url: string): Promise<void>Add an AudioWorkletProcessor module
Parameters
-
url
stringThe url of the module
Returns Promise<void>
clearInterval()
Section titled “clearInterval()”clearInterval(id: number): thisClear the function scheduled by setInterval.
Parameters
-
id
numberThe ID returned from setInterval.
Returns this
clearTimeout()
Section titled “clearTimeout()”clearTimeout(id: number): thisClears a previously scheduled timeout with setTimeout.
Parameters
-
id
numberThe ID returned from setTimeout.
Returns this
close()
Section titled “close()”close(): Promise<void>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>
createAnalyser()
Section titled “createAnalyser()”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()”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()”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()”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()”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()”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()”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()”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()”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()”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()”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()”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()”createIIRFilter(feedForward: Float32Array<ArrayBuffer> | number[], feedback: Float32Array<ArrayBuffer> | number[]): 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
Float32Array<ArrayBuffer> | number[] - feedback
Float32Array<ArrayBuffer> | number[]
Returns IIRFilterNode
createMediaElementSource()
Section titled “createMediaElementSource()”createMediaElementSource(element: HTMLMediaElement): MediaElementAudioSourceNodeParameters
- element
HTMLMediaElement
Returns MediaElementAudioSourceNode
createMediaStreamDestination()
Section titled “createMediaStreamDestination()”createMediaStreamDestination(): MediaStreamAudioDestinationNodeReturns MediaStreamAudioDestinationNode
createMediaStreamSource()
Section titled “createMediaStreamSource()”createMediaStreamSource(stream: MediaStream): MediaStreamAudioSourceNodeParameters
- stream
MediaStream
Returns MediaStreamAudioSourceNode
createOscillator()
Section titled “createOscillator()”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()”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()”createPeriodicWave(real: Float32Array<ArrayBuffer> | number[], imag: Float32Array<ArrayBuffer> | number[], 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
Float32Array<ArrayBuffer> | number[] - imag
Float32Array<ArrayBuffer> | number[] - constraints
PeriodicWaveConstraintsoptional
Returns PeriodicWave
createStereoPanner()
Section titled “createStereoPanner()”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()”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()”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()”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
immediate()
Section titled “immediate()”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(): numberThe current audio context time plus a short lookAhead.
setInterval(() => {
console.log("now", Tone.now());
}, 100);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
resume()
Section titled “resume()”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()”setInterval(fn: (...args: any[]) => void, interval: number): numberAdds a repeating event to the context’s callback clock.
Parameters
-
fn
(…args: any[]) => voidThe callback to invoke.
-
interval
numberThe timeout in seconds.
Returns number ID to use when invoking clearInterval.
setTimeout()
Section titled “setTimeout()”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 clearTimeout.
toJSON()
Section titled “toJSON()”↳ inherited from BaseContext
toJSON(): Record<string, any>Returns Record<string, any>
getDefaults()
Section titled “getDefaults()”static
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
getConstant()
Section titled “getConstant()”getConstant(val: number): AudioBufferSourceNodeInternal Generate a looped buffer at some constant value.
Parameters
- val
number
Returns AudioBufferSourceNode
Inherited members
Section titled “Inherited members”- From
Tone: debug, version, disposed, toString()