W3cubDocs

/Web APIs

AudioContext: AudioContext() constructor

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.

Syntax

js

new AudioContext()
new AudioContext(options)

Parameters

options Optional

An object used to configure the context. The available properties are:

latencyHint Optional

The type of playback that the context will be used for, as a predefined string ("balanced", "interactive" or "playback") 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.

  • "balanced": The browser balances audio output latency and power consumption when selecting a latency value.
  • "interactive" (default value): The audio is involved in interactive elements, such as responding to user actions or needing to coincide with visual cues such as a video or game action. The browser selects the lowest possible latency that doesn't cause glitches in the audio. This is likely to require increased power consumption.
  • "playback": The browser selects a latency that will maximize playback time by minimizing power consumption at the expense of latency. Useful for non-interactive playback, such as playing music.
sampleRate Optional

Indicates the sample rate to use for the new context. The value must be a floating-point value indicating the sample rate, in samples per second, for which to configure the new context; additionally, the value must be one which is supported by AudioBuffer.sampleRate. The value will typically be between 8,000 Hz and 96,000 Hz; the default will vary depending on the output device, but the sample rate 44,100 Hz is the most common. If the sampleRate property is not included in the options, or the options are not specified when creating the audio context, the new context's output device's preferred sample rate is used by default.

sinkId Optional Experimental

Specifies the sink ID of the audio output device to use for the AudioContext. This can take one of the following value types:

  • A string representing the sink ID, retrieved for example via the deviceId property of the MediaDeviceInfo objects returned by MediaDevices.enumerateDevices().
  • An object representing different options for a sink ID. Currently, this takes a single property, type, with a value of none. Setting this parameter causes the audio to be processed without being played through any audio output device.

Return value

A new AudioContext instance.

Exceptions

NotSupportedError DOMException

Thrown if the specified sampleRate isn't supported by the context.

Usage notes

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

Per-tab audio context limitation in Chrome

Prior to version 66 Google Chrome only supported up to six audio contexts per tab at a time.

Non-standard exceptions in Chrome

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".

Example

This example creates a new AudioContext for interactive audio (optimizing for latency), with a sample rate of 44.1kHz and a specific audio output.

js

const audioCtx = new AudioContext({
  latencyHint: "interactive",
  sampleRate: 44100,
  sinkId: "bb04fea9a8318c96de0bd...", // truncated for brevity
});

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
AudioContext
35["Before Chrome 66, each tab is limited to 6 audio contexts in Chrome; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Chrome throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
14–57
12 25 No
22["Before Opera 53, each tab is limited to 6 audio contexts in Opera; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Opera throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
15–44
14.16
37["Before WebView 66, each tab is limited to 6 audio contexts in WebView; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, WebView throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
≤37–57
35["Before Chrome 66, each tab is limited to 6 audio contexts in Chrome; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Chrome throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
18–57
25
22["Before Opera Android 47, each tab is limited to 6 audio contexts in Opera; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Opera throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
14–43
14.56
3.0["Before Samsung Internet 9.0, each tab is limited to 6 audio contexts in Samsung Internet; attempting to create more will throw a DOMException. For details see Per-tab audio context limitation in Chrome.", "If latencyHint isn't valid, Samsung Internet throws a TypeError exception. See Non-standard exceptions in Chrome for details."]
1.0–7.0
options_latencyHint_parameter 58 79 No No 45 14.1 58 58 No 43 14.5 7.0
options_sampleRate_parameter 74 79 61 No 62 14.1 74 74 61 53 14.5 11.0
options_sinkId_parameter 110 110 No No 96 No 110 110 No 74 No 21.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/AudioContext