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
Name | Type | Description | |
---|---|---|---|
sampleRate | number | Float value representing sample rate of the PCM data stored in the buffer. | Read only |
length | length | Integer value representing length of the PCM data stored in the buffer. | Read only |
duration | number | Double value representing duration, in seconds, of the PCM data stored in the buffer. | Read only |
numberOfChannels | number | Integer 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.
Parameters | Type | Description |
---|---|---|
channel | number | Index of the AudioBuffer's channel from which data will be returned. |
Errors:
Error type | Description |
---|---|
IndexSizeError | channel specifies unexisting audio channel. |
Returns Float32Array
.
copyFromChannel
The above method copies data from given channel of the AudioBuffer
to an array.
Parameters | Type | Description |
---|---|---|
destination | Float32Array | The array to which data will be copied. |
channelNumber | number | Index of the AudioBuffer's channel from which data will be copied. |
startInChannel Optional | number | Channel's offset from which to start copying data. |
Errors:
Error type | Description |
---|---|
IndexSizeError | channelNumber specifies unexisting audio channel. |
IndexSizeError | startInChannel is greater then the AudioBuffer length. |
Returns undefined
.
copyToChannel
The above method copies data from given array to specified channel of the AudioBuffer
.
Parameters | Type | Description |
---|---|---|
source | Float32Array | The array from which data will be copied. |
channelNumber | number | Index of the AudioBuffer's channel to which data will be copied. |
startInChannel Optional | number | Channel's offset from which to start copying data. |
Errors:
Error type | Description |
---|---|
IndexSizeError | channelNumber specifies unexisting audio channel. |
IndexSizeError | startInChannel is greater then the AudioBuffer length. |