The AudioContext()
constructor creates a new AudioContext
object which represents an audio-processing graph, built from audio modules linked together, each represented by an AudioNode
.
var audioCtx = new AudioContext(); var audioCtx = new AudioContext(options);
options
Optional
AudioContextOptions
dictionary that contains zero or more optional properties to configure the new context. Available properties are as follows: latencyHint
Optional
AudioContextLatencyCategory
enum or a double-precision floating-point value indicating the preferred maximum latency of the context in seconds. The user agent may or may not choose to meet this request; check the value of AudioContext.baseLatency
to determine the true latency after creating the context.sampleRate
Optional
sampleRate
to be used by the AudioContext
, specified in samples per second. The value may be any value supported by AudioBuffer
. If not specified, the preferred sample rate for the context's output device is used by default.The newly constructed AudioContext
instance.
NotSupportedError
sampleRate
isn't supported by the context.The specification doesn't go into a lot of detail about things like how many audio contexts a user agent should support, or minimum or maximum latency requirements (if any), so these details can vary from browser to browser. Be sure to check the values if they matter to you.
In particular, the specification doesn't indicate a maximum or minimum number of audio contexts that must be able to be open at the same time, so this is left up to the browser implementations to decide.
Google Chrome only supports up to six audio contexts per tab at a time, because each context spawns a new thread. This limitation is intended to help protect overhead problems that could arise if too many audio contexts are created (along with their threads). If you try to create more than six contexts, AudioContext()
will throw a DOMException
with the message "The number of hardware contexts provided (6) is greater than or equal to the maximum bound (6)".
You should try to reuse existing contexts rather than creating a new one for each sound you play, and be sure to call close()
on each context as you finish using it. Once close()
's Promise
is resolved, it's safe to create a new context.
If the value of the latencyHint
property isn't valid, Chrome throws a TypeError
exception with the message "The provided value '...' is not a valid enum value of type AudioContextLatencyCategory".
This example creates a new AudioContext
for interactive audio (optimizing for latency) and a sample rate of 44.1kHz.
var AudioContext = window.AudioContext || window.webkitAudioContext; var audioCtx = new AudioContext({ latencyHint: 'interactive', sampleRate: 44100, });
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'AudioContext()' in that specification. | Working Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 55
|
Yes | 25 | No | 42 | Yes
|
latencyHint option
|
60 | No | No | No | 47 | ? |
sampleRate option
|
No
|
No | No | No | No | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 55 | 55
|
? | 25 | 42 | ? | 6.0 |
latencyHint option
|
60 | 60 | ? | No | 47 | ? | No |
sampleRate option
|
? | ? | ? | ? | ? | ? | No |
new OfflineAudioContext()
constructor
© 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/AudioContext/AudioContext