Skip to main content

AudioBuffer

The AudioBuffer interface represents a short audio asset, commonly shorter then one minute. It can consists of one or more channels, each one appearing to be 32-bit floating-point linear PCM values with a nominal range of [−1, 1] (but not limited to that range), specific sample rate which is the quantity of frames that will play in one second and length.

It can be created from audio file using decodeAudioData or from raw data using constructor. Once you have data in AudioBuffer, audio can be played by passing it to AudioBufferSourceNode.

Constructor

constructor(context: BaseAudioContext, options: AudioBufferOptions)

AudioBufferOptions

ParameterTypeDefaultDescription
numberOfChannels
Optional
number1.0Number of channels in buffer
lengthnumber-Length of the buffer
sampleRatenumber-Sample rate of the buffer in Hz

Or by using BaseAudioContext factory method: BaseAudioContext.createBuffer(numChannels, length, sampleRate) that creates buffer with default values.

Decoding

See example implementations in BaseAudioContext on how to decode data in various ways.

Properties

NameTypeDescription
sampleRatenumberFloat value representing sample rate of the PCM data stored in the buffer.
Read only
lengthnumberInteger value representing length of the PCM data stored in the buffer.
Read only
durationnumberDouble value representing duration, in seconds, of the PCM data stored in the buffer.
Read only
numberOfChannelsnumberInteger value representing the number of audio channels of the PCM data stored in the buffer.
Read only

Methods

getChannelData

Gets modifiable array with PCM data from given channel.

ParameterTypeDescription
channelnumberIndex of the AudioBuffer's channel, from which data will be returned.

Errors:

Error typeDescription
IndexSizeErrorchannel specifies unexisting audio channel.

Returns Float32Array.

copyFromChannel

Copies data from given channel of the AudioBuffer to an array.

ParameterTypeDescription
destinationFloat32ArrayThe array to which data will be copied.
channelNumbernumberIndex of the AudioBuffer's channel, from which data will be copied.
startInChannel
Optional
numberChannel's offset from which to start copying data.

Errors:

Error typeDescription
IndexSizeErrorchannelNumber specifies unexisting audio channel.
IndexSizeErrorstartInChannel is greater then the AudioBuffer length.

Returns undefined.

copyToChannel

Copies data from given array to specified channel of the AudioBuffer.

ParameterTypeDescription
sourceFloat32ArrayThe array from which data will be copied.
channelNumbernumberIndex of the AudioBuffer's channel to which data will be copied.
startInChannel
Optional
numberChannel's offset from which to start copying data.

Errors:

Error typeDescription
IndexSizeErrorchannelNumber specifies unexisting audio channel.
IndexSizeErrorstartInChannel is greater then the AudioBuffer length.

Returns undefined.