OfflineAudioContext.OfflineAudioContext()

The OfflineAudioContext() constructor—part of the Web Audio API—creates and returns a new OfflineAudioContext object instance, which can then be used to render audio to an AudioBuffer rather than to an audio output device.

Syntax

offlineAudio = new OfflineAudioContext(numOfChannels, length, sampleRate);

offlineAudio = new OfflineAudioContext(options);

Parameters

You can specify the parameters for the OfflineAudioContext() constructor as either the same set of parameters as are inputs into the AudioContext.createBuffer() method, or by passing those parameters in an options object. Either way, the individual parameters are the same.

numOfChannels
An integer specifying the number of channels the resulting AudioBuffer should have.
length
An integer specifying the size of the buffer to create for the audio context, in sample-frames, where one sample-frame is a unit that can contain a single sample of audio data for every channel in the audio data. For example, a 512 sample-frame buffer for 8 channels of sound would be 512 * ((sizeof(float32) * 8) * 32) or 524,288 bytes.
sampleRate
The sample-rate of the linear audio data in sample-frames per second. All user agents are required to support a range of 22050kHz to 96000kHz, and may support a wider range than that. The most commonly-used rate is 44100kHz, which is the sample rate used by CD audio.

It is important to note that, whereas you can create a new AudioContext using the new AudioContext() constructor with no arguments, the OfflineAudioContext() constructor requires three arguments, since it needs to create an AudioBuffer. This works in exactly the same way as when you create a new AudioBuffer with the AudioContext.createBuffer method. For more detail, read Audio buffers: frames, samples and channels from our Basic concepts guide.

Return value

A new OfflineAudioContext object whose associated AudioBuffer is configured as requested.

Like a regular AudioContext, an OfflineAudioContext can be the target of events, therefore it implements the EventTarget interface.

Example

// define online and offline audio context

var audioCtx = new AudioContext();
var offlineCtx = new OfflineAudioContext(2,44100*40,44100);

source = offlineCtx.createBufferSource();

// etc...

For a full working example, see our offline-audio-context-promise Github repo (see the source code too.)

Specifications

Specification Status Comment
Web Audio API
The definition of 'OfflineAudioContext' in that specification.
Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes)
 
(Yes) No support

(Yes)

No support
Constructor accepts parameters in an object 62 57 (57) No support 49 No support
Feature Android Webview Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Yes)
 
(Yes)
 
(Yes) No support (Yes) No support
Constructor accepts parameters in an object 62 62 57 (57) No support 49 No support

Document Tags and Contributors

 Contributors to this page: jpmedley, Sheppy, chrisdavidmills
 Last updated by: jpmedley,