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, decodeAudioDataSource or from raw data using constructor. Once you have data in AudioBuffer, audio can be played by passing it to AudioBufferSourceNode.

Constructor

BaseAudioContext.createBuffer(numChannels, length, sampleRate)

Decoding

BaseAudioContext.decodeAudioData

const audioBuffer = await fetch(URL)
.then((response) => response.arrayBuffer())
.then((arrayBuffer) =>
audioContextRef.current!.decodeAudioData(arrayBuffer)
)

BaseAudioContext.decodeAudioDataSource

import * as FileSystem from 'expo-file-system';

const audioBuffer = await FileSystem.downloadAsync(
URL,
`${FileSystem.documentDirectory}/example-music-01.mp3`
).then(({ uri }) => audioContext.decodeAudioDataSource(uri));

Properties

NameTypeDescription
sampleRatenumberFloat value representing sample rate of the PCM data stored in the buffer.
Read only
lengthlengthInteger 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

The above method returns modifiable array with PCM data from given channel.

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

Errors:

Error typeDescription
IndexSizeErrorchannel specifies unexisting audio channel.

Returns Float32Array.

copyFromChannel

The above method copies data from given channel of the AudioBuffer to an array.

ParametersTypeDescription
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

The above method copies data from given array to specified channel of the AudioBuffer.

ParametersTypeDescription
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.