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.
AudioBuffer()
AudioBuffer
object instance.AudioBuffer.sampleRate
Read only
AudioBuffer.length
Read only
AudioBuffer.duration
Read only
AudioBuffer.numberOfChannels
Read only
AudioBuffer.getChannelData()
Float32Array
containing the PCM data associated with the channel, defined by the channel
parameter (with 0
representing the first channel).AudioBuffer.copyFromChannel()
AudioBuffer
to the destination
array.AudioBuffer.copyToChannel()
AudioBuffer
, from the source
array.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.
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();
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'AudioBuffer' in that specification. | Working Draft | Initial definition. |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 14 | Yes | 25 | No | 15 | 6 |
AudioBuffer() constructor |
551 | ? | 53 | No | 422 | ? |
duration |
14 | Yes | 25 | No | 15 | 6 |
length |
14 | Yes | 25 | No | 15 | 6 |
numberOfChannels |
14 | Yes | 25 | No | 15 | 6 |
sampleRate |
14 | Yes | 25 | No | 15 | 6 |
copyFromChannel |
14 | Yes | 25 | No | 15 | 6 |
copyToChannel |
14 | Yes | 25 | No | 15 | 6 |
getChannelData |
14 | Yes | 25 | No | 15 | 6 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | Yes | 14 | Yes | 26 | No | 15 | ? |
AudioBuffer() constructor |
551 | 551 | ? | 53 | No | 422 | ? |
duration |
Yes | 14 | Yes | 26 | No | 15 | ? |
length |
Yes | 14 | Yes | 26 | No | 15 | ? |
numberOfChannels |
Yes | 14 | Yes | 26 | No | 15 | ? |
sampleRate |
Yes | 14 | Yes | 26 | No | 15 | ? |
copyFromChannel |
Yes | 14 | Yes | 26 | No | 15 | ? |
copyToChannel |
Yes | 14 | Yes | 26 | No | 15 | ? |
getChannelData |
Yes | 14 | Yes | 26 | No | 15 | ? |
1. The context
parameter was supported up until version 57, but has now been removed.
2. The context
parameter was supported up until version 44, but has now been removed.
© 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/AudioBuffer