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.
var offlineAudioCtx = new OfflineAudioContext(numberOfChannels, length, sampleRate); var offlineAudioCtx = new OfflineAudioContext(options);
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
AudioBuffer
should have.length
512 * ((sizeof(float32) * 8) * 32)
or 524,288 bytes.sampleRate
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.
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.
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.)
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'OfflineAudioContext' in that specification. | Working Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 551 | ? | 53 | No | 42 | ? |
Parameters accepted as a single object, as well as being passed in individually | 62 | ? | 57 | No | 49 | No |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | 551 | 551 | ? | 53 | No | 42 | ? |
Parameters accepted as a single object, as well as being passed in individually | 62 | 62 | ? | 57 | No | 49 | No |
1. Before Chrome 59, the default values were not supported.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/OfflineAudioContext/OfflineAudioContext