AudioBufferSourceNode
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheAudioBufferSourceNode interface is anAudioScheduledSourceNode which represents an audio source consisting of in-memory audio data, stored in anAudioBuffer.
This interface is especially useful for playing back audio which has particularly stringent timing accuracy requirements, such as for sounds that must match a specific rhythm and can be kept in memory rather than being played from disk or the network. To play sounds which require accurate timing but must be streamed from the network or played from disk, use anAudioWorkletNode to implement its playback.
AnAudioBufferSourceNode has no inputs and exactly one output, which has the same number of channels as theAudioBuffer indicated by itsbuffer property. If there's no buffer set—that is, ifbuffer isnull—the output contains a single channel of silence (every sample is 0).
AnAudioBufferSourceNode can only be played once; after each call tostart(), you have to create a new node if you want to play the same sound again. Fortunately, these nodes are very inexpensive to create, and the actualAudioBuffers can be reused for multiple plays of the sound. Indeed, you can use these nodes in a "fire and forget" manner: create the node, callstart() to begin playing the sound, and don't even bother to hold a reference to it. It will automatically be garbage-collected at an appropriate time, which won't be until sometime after the sound has finished playing.
Multiple calls tostop() are allowed. The most recent call replaces the previous one, if theAudioBufferSourceNode has not already reached the end of the buffer.

| Number of inputs | 0 |
|---|---|
| Number of outputs | 1 |
| Channel count | defined by the associatedAudioBuffer |
In this article
Constructor
AudioBufferSourceNode()Creates and returns a new
AudioBufferSourceNodeobject. As an alternative, you can use theBaseAudioContext.createBufferSource()factory method; seeCreating an AudioNode.
Instance properties
Inherits properties from its parent,AudioScheduledSourceNode.
AudioBufferSourceNode.bufferAn
AudioBufferthat defines the audio asset to be played, or when set to the valuenull, defines a single channel of silence (in which every sample is 0.0).AudioBufferSourceNode.detuneAk-rate
AudioParamrepresenting detuning of playback incents. This value is compounded withplaybackRateto determine the speed at which the sound is played. Its default value is0(meaning no detuning), and its nominal range is -∞ to ∞.AudioBufferSourceNode.loopA Boolean attribute indicating if the audio asset must be replayed when the end of the
AudioBufferis reached. Its default value isfalse.AudioBufferSourceNode.loopStartOptionalA floating-point value indicating the time, in seconds, at which playback of the
AudioBuffermust begin whenloopistrue. Its default value is0(meaning that at the beginning of each loop, playback begins at the start of the audio buffer).AudioBufferSourceNode.loopEndOptionalA floating-point number indicating the time, in seconds, at which playback of the
AudioBufferstops and loops back to the time indicated byloopStart, ifloopistrue. The default value is0.AudioBufferSourceNode.playbackRateAk-rate
AudioParamthat defines the speed factor at which the audio asset will be played, where a value of 1.0 is the sound's natural sampling rate. Since no pitch correction is applied on the output, this can be used to change the pitch of the sample. This value is compounded withdetuneto determine the final playback rate.
Instance methods
Inherits methods from its parent,AudioScheduledSourceNode, and overrides the following method:.
start()Schedules playback of the audio data contained in the buffer, or begins playback immediately. Additionally allows the start offset and play duration to be set.
Examples
In this example, we create a two-second buffer, fill it with white noise, and then play it using anAudioBufferSourceNode. The comments should clearly explain what is going on.
Note:You can alsorun the code live, orview the source.
const audioCtx = new AudioContext();// Create an empty three-second stereo buffer at the sample rate of the AudioContextconst myArrayBuffer = audioCtx.createBuffer( 2, audioCtx.sampleRate * 3, audioCtx.sampleRate,);// Fill the buffer with white noise;// just random values between -1.0 and 1.0for (let channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) { // This gives us the actual ArrayBuffer that contains the data const nowBuffering = myArrayBuffer.getChannelData(channel); for (let i = 0; i < myArrayBuffer.length; i++) { // Math.random() is in [0; 1.0] // audio needs to be in [-1.0; 1.0] nowBuffering[i] = Math.random() * 2 - 1; }}// Get an AudioBufferSourceNode.// This is the AudioNode to use when we want to play an AudioBufferconst source = audioCtx.createBufferSource();// set the buffer in the AudioBufferSourceNodesource.buffer = myArrayBuffer;// connect the AudioBufferSourceNode to the// destination so we can hear the soundsource.connect(audioCtx.destination);// start the source playingsource.start();Note:For adecodeAudioData() example, see theAudioContext.decodeAudioData() page.
Specifications
| Specification |
|---|
| Web Audio API> # AudioBufferSourceNode> |