Skip to main content
Version: Next

OfflineAudioContext

The OfflineAudioContext interface inherits from BaseAudioContext. In contrast with a standard AudioContext, it doesn't render audio to the device hardware. Instead, it processes the audio as quickly as possible and outputs the result to an AudioBuffer.

Constructor​

OfflineAudioContext(options: OfflineAudioContextOptions)

interface OfflineAudioContextOptions {
numberOfChannels: number;
length: number; // The length of the rendered AudioBuffer, in sample-frames
sampleRate: number;
}

Properties​

OfflineAudioContext does not define any additional properties. Inherits all properties from BaseAudioContext.

Methods​

Inherits all methods from BaseAudioContext.

suspend​

Schedules a suspension of the time progression in audio context at the specified time.

ParameterTypeDescription
suspendTimenumberA floating-point number specifying the suspend time, in seconds.

Returns Promise<undefined>.​

resume​

Resume time progression in audio context when it has been suspended.

Returns Promise<undefined>​

startRendering​

Starts rendering the audio, taking into account the current connections and the current scheduled changes.

Returns Promise<AudioBuffer>.​

Events​

Inherits onstatechange from BaseAudioContext. It fires when rendering starts (running), when a scheduled suspend point is reached (suspended), after resume (running), and when rendering finishes (closed).

oncomplete​

Sets (or removes, when null is assigned) a callback fired when rendering has finished, in the same task that resolves the startRendering promise. state already reads closed inside the callback; the closed statechange itself arrives in a later task. The callback receives an event object:

FieldTypeDescription
typestringAlways 'complete'.
targetOfflineAudioContextThe context that finished rendering.
renderedBufferAudioBufferThe rendered audio.
const offlineContext = new OfflineAudioContext({
numberOfChannels: 2,
length: 44100,
sampleRate: 44100,
});

offlineContext.oncomplete = (event) => {
console.log(`rendered ${event.renderedBuffer.duration}s of audio`);
};

await offlineContext.startRendering();