The AudioBufferSourceNode
interface is an AudioScheduledSourceNode
which represents an audio source consisting of in-memory audio data, stored in an AudioBuffer
. It's 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 a AudioWorkletNode
to implement its playback.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 23.333333333333332%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 140" preserveAspectRatio="xMinYMin meet"><a xlink:href="/docs/Web/API/EventTarget" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="/docs/Web/API/AudioNode" target="_top"><rect x="151" y="1" width="90" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="196" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">AudioNode</text></a><polyline points="241,25 251,20 251,30 241,25" stroke="#D4DDE4" fill="none"/><line x1="251" y1="25" x2="281" y2="25" stroke="#D4DDE4"/><a xlink:href="/docs/Web/API/AudioScheduledSourceNode" target="_top"><rect x="281" y="1" width="240" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="401" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">AudioScheduledSourceNode</text></a><polyline points="521,25 531,20 531,30 521,25" stroke="#D4DDE4" fill="none"/><line x1="531" y1="25" x2="561" y2="25" stroke="#D4DDE4"/><a xlink:href="/docs/Web/API/AudioBufferSourceNode" target="_top"><rect x="561" y="1" width="210" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="666" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">AudioBufferSourceNode</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
An AudioBufferSourceNode
has no inputs and exactly one output, which has the same number of channels as the AudioBuffer
indicated by its buffer
property. If there's no buffer set—that is, if buffer
is null
—the output contains a single channel of silence (every sample is 0).
An AudioBufferSourceNode
can only be played once; after each call to start()
, 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 actual AudioBuffer
s can be reused for multiple plays of the sound. Indeed, you can use these nodes in a "fire and forget" manner: create the node, call start()
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 to stop()
are allowed. The most recent call replaces the previous one, if the AudioBufferSourceNode
has not already reached the end of the buffer.
Number of inputs | 0 |
---|---|
Number of outputs | 1 |
Channel count | defined by the associated AudioBuffer |
Constructor
AudioBufferSourceNode()
- Creates and returns a new
AudioBufferSourceNode
object. AnAudioBufferSourceNode
can be instantiated using theAudioContext.createBufferSource()
method.
Properties
Inherits properties from its parent, AudioNode
.
AudioBufferSourceNode.buffer
- An
AudioBuffer
that 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.detune
- Is a k-rate
AudioParam
representing detuning of playback in cents. This value is compounded withplaybackRate
to determine the speed at which the sound is played. Its default value is0
(meaning no detuning), and its nominal range is -∞ to ∞. AudioBufferSourceNode.loop
- A Boolean attribute indicating if the audio asset must be replayed when the end of the
AudioBuffer
is reached. Its default value isfalse
. AudioBufferSourceNode.loopStart
Optional- A floating-point value indicating the time, in seconds, at which playback of the
AudioBuffer
must begin whenloop
istrue
. Its default value is0
(meaning that at the beginning of each loop, playback begins at the start of the audio buffer). AudioBufferSourceNode.loopEnd
Optional- A floating-point number indicating the time, in seconds, at which playback of the
AudioBuffer
stops and loops back to the time indicated byloopStart
, ifloop
istrue
. The default value is0
. AudioBufferSourceNode.playbackRate
- An a-rate
AudioParam
that 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 withdetune
to determine the final playback rate.
Event handlers
Inherits event handlers from its parent, AudioScheduledSourceNode
.
Methods
Inherits methods from its parent, AudioScheduledSourceNode
.
AudioBufferSourceNode.start()
- Used to schedule playback of the audio data contained in the buffer, or to begin playback immediately.
Examples
In this example, we create a two-second buffer, fill it with white noise, and then play it using an AudioBufferSourceNode
. The comments should clearly explain what is going on.
You can also run the code live, or view the source.
var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // Create an empty three-second stereo buffer at the sample rate of the AudioContext var myArrayBuffer = audioCtx.createBuffer(2, audioCtx.sampleRate * 3, audioCtx.sampleRate); // Fill the buffer with white noise; //just random values between -1.0 and 1.0 for (var channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) { // This gives us the actual ArrayBuffer that contains the data var nowBuffering = myArrayBuffer.getChannelData(channel); for (var 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 AudioBuffer var source = audioCtx.createBufferSource(); // set the buffer in the AudioBufferSourceNode source.buffer = myArrayBuffer; // connect the AudioBufferSourceNode to the // destination so we can hear the sound source.connect(audioCtx.destination); // start the source playing source.start();
For a decodeAudioData()
example, see the AudioContext.decodeAudioData()
page.
Specifications
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'AudioBufferSourceNode' in that specification. |
Working Draft |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
AudioBufferSourceNode | Chrome Full support 14 | Edge Full support ≤18 | Firefox Full support 25 | IE No support No | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 26 | Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 |
AudioBufferSourceNode() constructor | Chrome
Full support
55
| Edge Full support ≤79 | Firefox Full support 53 | IE No support No | Opera Full support 42 | Safari ? | WebView Android
Full support
55
| Chrome Android
Full support
55
| Firefox Android Full support 53 | Opera Android Full support 42 | Safari iOS ? | Samsung Internet Android
Full support
6.0
|
buffer | Chrome Full support 14 | Edge Full support 12 | Firefox
Full support
25
| IE No support No | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android
Full support
26
| Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 |
detune | Chrome Full support 44 | Edge Full support 13 | Firefox Full support 40 | IE No support No | Opera Full support 31 | Safari No support No | WebView Android Full support 44 | Chrome Android Full support 44 | Firefox Android Full support 40 | Opera Android Full support 32 | Safari iOS ? | Samsung Internet Android Full support 4.0 |
loop | Chrome Full support 14 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 26 | Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 |
loopEnd | Chrome Full support 14 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 26 | Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 |
loopStart | Chrome Full support 14 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 26 | Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 |
onended | Chrome Full support 14 | Edge Full support 12 | Firefox ? | IE No support No | Opera No support ? — 44 | Safari ? | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android ? | Opera Android No support ? — 43 | Safari iOS ? | Samsung Internet Android Full support 1.0 |
playbackRate | Chrome Full support 14 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 26 | Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 |
start | Chrome No support 14 — 57 | Edge No support 12 — 79 | Firefox Full support 25 | IE No support No | Opera Full support 15 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 26 | Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.