OfflineAudioContext: OfflineAudioContext() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

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

js
new OfflineAudioContext(options)

new OfflineAudioContext(numberOfChannels, length, sampleRate)

Parameters

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

numberOfChannels

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 5-second buffer with a sampleRate of 48000Hz would have a length of 5 * 48000 = 240000 sample-frames.

sampleRate

The sample-rate of the linear audio data in sample-frames per second. All user agents are required to support a range of 8000Hz to 96000Hz, and may support a wider range than that. The most commonly-used rate is 44100Hz, which is the sample rate used by CD audio.

It is important to note that, whereas you can create a new AudioContext using the 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 BaseAudioContext.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.

Examples

js
const offlineCtx = new OfflineAudioContext({
  numberOfChannels: 2,
  length: 44100 * 40,
  sampleRate: 44100,
});
const source = offlineCtx.createBufferSource();
// …

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

Specifications

Specification
Web Audio API
# dom-offlineaudiocontext-offlineaudiocontext

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
OfflineAudioContext() constructor
Chrome – No support
Chrome 25 – 56 (Release date: 2013-02-21)
prefix
footnote Removed in 57 and later
prefix Implemented with the vendor prefix: webkit
Chrome – Full support
Chrome 35 (Release date: 2014-05-20)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 25 (Release date: 2013-10-29)
footnote Full support
Opera – No support
Opera 15 – 43 (Release date: 2013-07-02)
prefix
footnote Removed in 44 and later
prefix Implemented with the vendor prefix: webkit
Opera – Full support
Opera 22 (Release date: 2014-06-03)
footnote Full support
Safari – No support
Safari 7 – 14 (Release date: 2013-10-22)
prefix
prefix Implemented with the vendor prefix: webkit
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – No support
Chrome Android 25 – 56 (Release date: 2013-02-27)
prefix
footnote Removed in 57 and later
prefix Implemented with the vendor prefix: webkit
Chrome Android – Full support
Chrome Android 35 (Release date: 2014-05-20)
footnote Full support
Firefox for Android – Full support
Firefox for Android 25 (Release date: 2013-10-29)
footnote Full support
Opera Android – No support
Opera Android 14 – 42 (Release date: 2013-05-21)
prefix
footnote Removed in 43 and later
prefix Implemented with the vendor prefix: webkit
Opera Android – Full support
Opera Android 22 (Release date: 2014-06-17)
footnote Full support
Safari on iOS – No support
Safari on iOS 7 – 14 (Release date: 2013-09-18)
prefix
prefix Implemented with the vendor prefix: webkit
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – No support
Samsung Internet 1.5 – 6.4 (Release date: 2013-09-25)
prefix
footnote Removed in 7 and later
prefix Implemented with the vendor prefix: webkit
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – No support
WebView Android 4.4 – 56 (Release date: 2013-12-09)
prefix
footnote Removed in 57 and later
prefix Implemented with the vendor prefix: webkit
WebView Android – Full support
WebView Android 37 (Release date: 2014-09-03)
footnote Full support
WebView on iOS – No support
WebView on iOS 7 – 14 (Release date: 2013-09-18)
prefix
prefix Implemented with the vendor prefix: webkit
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
options parameter (accepts all parameters as a single dictionary)
Chrome – Full support
Chrome 62 (Release date: 2017-10-17)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 57 (Release date: 2017-11-14)
footnote Full support
Opera – Full support
Opera 49 (Release date: 2017-11-08)
footnote Full support
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – Full support
Chrome Android 62 (Release date: 2017-10-24)
footnote Full support
Firefox for Android – Full support
Firefox for Android 57 (Release date: 2017-11-28)
footnote Full support
Opera Android – Full support
Opera Android 46 (Release date: 2018-05-14)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – Full support
Samsung Internet 8 (Release date: 2018-07-18)
footnote Full support
WebView Android – Full support
WebView Android 62 (Release date: 2017-10-24)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Requires a vendor prefix or different name for use.
Has more compatibility info.