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
var offlineAudioCtx = new OfflineAudioContext(numberOfChannels, length, sampleRate); var offlineAudioCtx = 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.
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 of5 * 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 22050Hz 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 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
const offlineCtx = new OfflineAudioContext({ numberOfChannels: 2, length: 44100 * 40, sampleRate: 44100, }); const 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
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
OfflineAudioContext() constructor | Chrome
Full support
55
| Edge Full support ≤79 | Firefox Full support 53 | IE No support No | Opera Full support 42 | Safari ? | WebView Android
Full support
55
| Chrome Android
Full support
55
| Firefox Android Full support 53 | Opera Android Full support 42 | Safari iOS ? | Samsung Internet Android
Full support
6.0
|
Parameters accepted as a single object, as well as being passed in individually | Chrome Full support 62 | Edge Full support ≤79 | Firefox Full support 57 | IE No support No | Opera Full support 49 | Safari No support No | WebView Android Full support 62 | Chrome Android Full support 62 | Firefox Android Full support 57 | Opera Android Full support 46 | Safari iOS No support No | Samsung Internet Android Full support 8.0 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.