Skip to main content

AudioBufferSourceNode

The AudioBufferSourceNode is an AudioBufferBaseSourceNode which represents audio source with in-memory audio data, stored in AudioBuffer. You can use it for audio playback, including standard pause and resume functionalities.

An AudioBufferSourceNode can be started only once, so if you want to play the same sound again you have to create a new one. However, this node is very inexpensive to create, and what is crucial you can reuse same AudioBuffer.

AudioNode properties

Number of inputs0
Number of outputs1
Channel countdefined by associated buffer
Channel count modemax
Channel interpretationspeakers

Constructor

BaseAudioContext.createBufferSource(options)

Example

import React, { useEffect, useRef, FC } from 'react';
import {
AudioContext,
AudioBufferSourceNode,
} from 'react-native-audio-api';

function App() {
const audioContextRef = useRef<AudioContext | null>(null);
if (!audioContextRef.current) {
audioContextRef.current = new AudioContext();
}
const audioBufferSource = audioContextRef.current.createBufferSource();
const buffer = ...; // Load your audio buffer here
audioBufferSource.buffer = buffer;
audioBufferSource.connect(audioContextRef.current.destination);
audioBufferSource.start(audioContextRef.current.currentTime);
}

Properties

NameTypeDescription
bufferAudioBufferAssociated AudioBuffer.
loopbooleanBoolean indicating if audio data must be replayed after when end of the associated AudioBuffer is reached.
loopSkipbooleanBoolean indicating if upon setting up loopStart we want to skip immediately to the loop start.
loopStartnumberFloat value indicating the time, in seconds, at which playback of the audio must begin, if loop is true.
loopEndnumberFloat value indicating the time, in seconds, at which playback of the audio must end and loop back to loopStart, if loop is true.

Methods

start
Overridden

The above method schedules AudioBufferSourceNode to start playback of audio data contained in the associated AudioBuffer, or starts to play immediately.

ParametersTypeDescription
whennumberThe time, in seconds, at which the node will start to play.
offset
Optional
numberAn offset, in seconds, determines the starting point for audio playback.
duration
Optional
numberThe duration, in seconds, of audio playback.

Errors:

Error typeDescription
RangeErrorwhen is negative number.
RangeErroroffset is negative number.
RangeErrorduration is negative number.
InvalidStateErrorIf node has already been started once.

Returns undefined.

Remarks

buffer

  • If is null, it outputs a single channel of silence (all samples are equal to 0).

detune

  • Default value is 0.0.
  • Nominal range is -∞ to ∞.
  • For example value of 100 detune the source up by one semitone, whereas -1200 down by one octave.
  • When createBufferSource(true) it is clamped to range -1200 to 1200.

loop

  • Default value is false.

loopStart

  • Default value is 0.

loopEnd

  • Default value is buffer.duration.

playbackRate

  • Default value is 1.0.
  • Nominal range is -∞ to ∞.
  • For example value of 1.0 plays audio at normal speed, whereas value of 2.0 plays audio twice as fast as normal speed.
  • When created with createBufferSource(true) it is clamped to range 0 to 3 and uses pitch correction algorithm.