The AudioBuffer
interface represents a short audio asset residing in memory, created from an audio file using the AudioContext.decodeAudioData()
method, or from raw data using AudioContext.createBuffer()
. Once put into an AudioBuffer, the audio can then be played by being passed into an AudioBufferSourceNode
.
Objects of these types are designed to hold small audio snippets, typically less than 45 s. For longer sounds, objects implementing the MediaElementAudioSourceNode
are more suitable. The buffer contains the audio signal waveform encoded as a series of amplitudes in the following format: non-interleaved IEEE754 32-bit linear PCM with a nominal range between -1
and +1
, that is, a 32-bit floating point buffer, with each sample between -1.0 and 1.0. If the AudioBuffer
has multiple channels, they are stored in separate buffers.
Constructor
-
AudioBuffer()
-
Creates and returns a new AudioBuffer
object instance.
Instance properties
AudioBuffer.sampleRate
Read only
-
Returns a float representing the sample rate, in samples per second, of the PCM data stored in the buffer.
AudioBuffer.length
Read only
-
Returns an integer representing the length, in sample-frames, of the PCM data stored in the buffer.
AudioBuffer.duration
Read only
-
Returns a double representing the duration, in seconds, of the PCM data stored in the buffer.
AudioBuffer.numberOfChannels
Read only
-
Returns an integer representing the number of discrete audio channels described by the PCM data stored in the buffer.
Instance methods
-
AudioBuffer.getChannelData()
-
Returns a Float32Array
containing the PCM data associated with the channel, defined by the channel
parameter (with 0
representing the first channel).
-
AudioBuffer.copyFromChannel()
-
Copies the samples from the specified channel of the AudioBuffer
to the destination
array.
-
AudioBuffer.copyToChannel()
-
Copies the samples to the specified channel of the AudioBuffer
, from the source
array.
Example
The following simple example shows how to create an AudioBuffer
and fill it with random white noise. You can find the full source code at our webaudio-examples repository; a running live version is also available.
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const myArrayBuffer = audioCtx.createBuffer(
2,
audioCtx.sampleRate * 3,
audioCtx.sampleRate,
);
for (let channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) {
const nowBuffering = myArrayBuffer.getChannelData(channel);
for (let i = 0; i < myArrayBuffer.length; i++) {
nowBuffering[i] = Math.random() * 2 - 1;
}
}
const source = audioCtx.createBufferSource();
source.buffer = myArrayBuffer;
source.connect(audioCtx.destination);
source.start();
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 |
AudioBuffer |
55The context parameter was supported up until version 57, but has now been removed.
|
79 |
53 |
No |
42The context parameter was supported up until version 44, but has now been removed.
|
14.1 |
55The context parameter was supported up until version 57, but has now been removed.
|
55The context parameter was supported up until version 57, but has now been removed.
|
53 |
42The context parameter was supported up until version 44, but has now been removed.
|
14.5 |
6.0The context parameter was supported up until Samsung Internet 7.0, but has now been removed.
|
AudioBuffer |
14 |
12 |
25 |
No |
15 |
6 |
4.4.3 |
18 |
25 |
14 |
6 |
1.0 |
copyFromChannel |
43 |
13 |
27 |
No |
30 |
14.1 |
43 |
43 |
27 |
30 |
14.5 |
4.0 |
copyToChannel |
43 |
13 |
27 |
No |
30 |
14.1 |
43 |
43 |
27 |
30 |
14.5 |
4.0 |
duration |
14 |
12 |
25 |
No |
15 |
6 |
4.4.3 |
18 |
25 |
14 |
6 |
1.0 |
getChannelData |
14 |
12 |
25 |
No |
15 |
6 |
4.4.3 |
18 |
25 |
14 |
6 |
1.0 |
length |
14 |
12 |
25 |
No |
15 |
6 |
4.4.3 |
18 |
25 |
14 |
6 |
1.0 |
numberOfChannels |
14 |
12 |
25 |
No |
15 |
6 |
4.4.3 |
18 |
25 |
14 |
6 |
1.0 |
sampleRate |
14 |
12 |
25 |
No |
15 |
6 |
4.4.3 |
18 |
25 |
14 |
6 |
1.0 |
See also