Web/API/AudioBuffer

From Get docs

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 data in the following format: non-interleaved IEEE754 32-bit linear PCM with a nominal range between -1 and +1, that is, 32bits floating point buffer, with each samples between -1.0 and 1.0. If the AudioBuffer has multiple channels, they are stored in separate buffer.

Constructor

AudioBuffer()
Creates and returns a new AudioBuffer object 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.

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 [[../../../../../../mdn.github.io/webaudio-examples/audio-buffer/index|running live]] version is also available.

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();

// Create an empty three-second stereo buffer at the sample rate of the AudioContext
var myArrayBuffer = audioCtx.createBuffer(2, audioCtx.sampleRate * 3, audioCtx.sampleRate);

// Fill the buffer with white noise;
// just random values between -1.0 and 1.0
for (var channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) {
  // This gives us the actual array that contains the data
  var nowBuffering = myArrayBuffer.getChannelData(channel);
  for (var i = 0; i < myArrayBuffer.length; i++) {
    // Math.random() is in [0; 1.0]
    // audio needs to be in [-1.0; 1.0]
    nowBuffering[i] = Math.random() * 2 - 1;
  }
}

// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
var source = audioCtx.createBufferSource();

// set the buffer in the AudioBufferSourceNode
source.buffer = myArrayBuffer;

// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);

// start the source playing
source.start();

Specifications

Specification Status Comment
Web Audio APIThe definition of 'AudioBuffer' in that specification. Working Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
AudioBuffer Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

AudioBuffer() constructor

Chrome Full support 55

Notes'

Full support 55

Notes'

Notes' The context parameter was supported up until version 57, but has now been removed.

Edge

Full support 79

Firefox

Full support 53

IE

No support No

Opera Full support 42

Notes'

Full support 42

Notes'

Notes' The context parameter was supported up until version 44, but has now been removed.

Safari

No support No

WebView Android Full support 55

Notes'

Full support 55

Notes'

Notes' The context parameter was supported up until version 57, but has now been removed.

Chrome Android Full support 55

Notes'

Full support 55

Notes'

Notes' The context parameter was supported up until version 57, but has now been removed.

Firefox Android

Full support 53

Opera Android Full support 42

Notes'

Full support 42

Notes'

Notes' The context parameter was supported up until version 44, but has now been removed.

Safari iOS

No support No

Samsung Internet Android Full support 6.0

Notes'

Full support 6.0

Notes'

Notes' The context parameter was supported up until Samsung Internet 7.0, but has now been removed.

copyFromChannel Chrome

Full support 43

Edge

Full support 13

Firefox

Full support 25

IE

No support No

Opera

Full support 30

Safari

No support No

WebView Android

Full support 43

Chrome Android

Full support 43

Firefox Android

Full support 26

Opera Android

Full support 30

Safari iOS

No support No

Samsung Internet Android

Full support 4.0

copyToChannel Chrome

Full support 43

Edge

Full support 13

Firefox

Full support 25

IE

No support No

Opera

Full support 30

Safari

No support No

WebView Android

Full support 43

Chrome Android

Full support 43

Firefox Android

Full support 26

Opera Android

Full support 30

Safari iOS

No support No

Samsung Internet Android

Full support 4.0

duration Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

getChannelData Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

length Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

numberOfChannels Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

sampleRate Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 25

IE

No support No

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support Yes

Chrome Android

Full support 18

Firefox Android

Full support 26

Opera Android

Full support 14

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.'
See implementation notes.


See also