Path: blob/main/files/en-us/web/api/audiobuffer/index.md
6540 views
------{{APIRef("Web Audio API")}}
The AudioBuffer interface represents a short audio asset residing in memory, created from an audio file using the {{ domxref("BaseAudioContext/decodeAudioData", "AudioContext.decodeAudioData()") }} method, or from raw data using {{ domxref("BaseAudioContext/createBuffer", "AudioContext.createBuffer()") }}. Once put into an AudioBuffer, the audio can then be played by being passed into an {{ domxref("AudioBufferSourceNode") }}.
Objects of these types are designed to hold small audio snippets, typically less than 45 s. For longer sounds, objects implementing the {{domxref("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, a 32-bit floating point buffer, with each sample between -1.0 and 1.0. If the {{domxref("AudioBuffer")}} has multiple channels, they are stored in separate buffers.
Constructor
{{domxref("AudioBuffer.AudioBuffer", "AudioBuffer()")}}
: Creates and returns a new
AudioBufferobject instance.
Instance properties
{{domxref("AudioBuffer.sampleRate")}} {{ReadOnlyInline}}
: Returns a float representing the sample rate, in samples per second, of the PCM data stored in the buffer.
{{domxref("AudioBuffer.length")}} {{ReadOnlyInline}}
: Returns an integer representing the length, in sample-frames, of the PCM data stored in the buffer.
{{domxref("AudioBuffer.duration")}} {{ReadOnlyInline}}
: Returns a double representing the duration, in seconds, of the PCM data stored in the buffer.
{{domxref("AudioBuffer.numberOfChannels")}} {{ReadOnlyInline}}
: Returns an integer representing the number of discrete audio channels described by the PCM data stored in the buffer.
Instance methods
{{domxref("AudioBuffer.getChannelData()")}}
: Returns a {{jsxref("Float32Array")}} containing the PCM data associated with the channel, defined by the
channelparameter (with0representing the first channel).
{{domxref("AudioBuffer.copyFromChannel()")}}
: Copies the samples from the specified channel of the
AudioBufferto thedestinationarray.
{{domxref("AudioBuffer.copyToChannel()")}}
: Copies the samples to the specified channel of the
AudioBuffer, from thesourcearray.
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.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}