What is AudioBufferSourceNode?
AudioBufferSourceNode is a powerful interface within the Web Audio API that enables developers to play back audio data stored directly in memory. Unlike streaming sources that fetch audio from the network or disk, AudioBufferSourceNode provides ultra-low-latency playback perfect for applications requiring precise timing, such as games, interactive experiences, and audio production tools. This interface has been widely available across all major browsers since 2015, making it a reliable choice for modern web applications.
The node has no inputs and exactly one output, which has the same number of channels as the AudioBuffer indicated by its buffer property. An AudioBufferSourceNode can only be played once per node instance, but creating new nodes is extremely inexpensive, enabling a "fire and forget" pattern for audio playback. This makes it ideal for JavaScript-based applications that need responsive audio feedback without the overhead of complex audio management.
As part of the Web Audio API, AudioBufferSourceNode integrates seamlessly with other audio processing nodes like GainNode and AnalyserNode, allowing developers to build sophisticated audio experiences entirely in the browser.
Zero Inputs, One Output
Has no inputs and a single output that matches the AudioBuffer channel count
Single-Use Playback
Each node can only be played once - create new nodes for replay
Ultra Low Latency
Plays audio from memory for instant, precise playback
Efficient Creation
Extremely inexpensive to create and automatically garbage collected
Timing Control
Part of AudioScheduledSourceNode for precise scheduling
Loop Support
Built-in looping with customizable start and end points
Creating AudioBufferSourceNode
There are two approaches to creating AudioBufferSourceNode instances: using the constructor or the factory method. Both are valid, but the constructor offers more explicit control over options at instantiation time.
Constructor Approach
The AudioBufferSourceNode() constructor creates and returns a new AudioBufferSourceNode object instance:
const audioCtx = new AudioContext();
const source = new AudioBufferSourceNode(audioCtx, options);
Factory Method Approach
Using createBufferSource() is the traditional approach:
const source = audioCtx.createBufferSource();
Constructor Options
The options object supports several properties for configuring playback behavior during creation, including buffer, playbackRate, detune, loop, loopStart, and loopEnd. When building modern web applications, the constructor approach provides cleaner code organization and earlier error detection for configuration issues.
Core Properties
buffer
The AudioBuffer containing the audio data to play. When null, output is silent (all zeros). Setting this after playback starts has no effect - you must create a new node.
playbackRate (AudioParam)
Speed factor for playback. A value of 1.0 plays at natural speed. Higher values increase speed and pitch, lower values decrease both. Compounded with detune.
detune (AudioParam)
Pitch adjustment in cents (1200 cents = 1 octave). Negative values detune down, positive values detune up. Applied in addition to playbackRate.
loop
Boolean that enables looping when true. Playback jumps back to loopStart when reaching loopEnd.
loopStart / loopEnd
Floating-point values (in seconds) defining loop boundaries. Default is 0 for both. Must be set before playback starts.
These properties provide fine-grained control over audio playback, essential for creating polished interactive web experiences with professional audio quality. When implementing audio features that enhance user engagement, consider how SEO services can amplify your content's discoverability through optimized media delivery.
1const audioCtx = new AudioContext();2 3// Create a 2-second stereo buffer4const buffer = audioCtx.createBuffer(5 2, // number of channels6 audioCtx.sampleRate * 2, // duration in frames7 audioCtx.sampleRate // sample rate8);9 10// Fill with audio data11for (let channel = 0; channel < buffer.numberOfChannels; channel++) {12 const data = buffer.getChannelData(channel);13 for (let i = 0; i < data.length; i++) {14 data[i] = Math.random() * 2 - 1; // white noise15 }16}17 18// Create and configure source19const source = new AudioBufferSourceNode(audioCtx, {20 buffer: buffer,21 playbackRate: 1.0,22 loop: false23});24 25// Connect to output26source.connect(audioCtx.destination);27 28// Play immediately29source.start();30 31// Or schedule for later32// source.start(audioCtx.currentTime + 1);Playing Audio with start()
The start() method schedules playback with three optional parameters:
source.start(when, offset, duration);
Parameters
- when: When to start playing (seconds, relative to AudioContext.currentTime). 0 or omitted means immediate.
- offset: Where in the buffer to start (seconds). Defaults to 0.
- duration: How long to play (seconds). If omitted, plays until end or stop().
Examples
// Play immediately
source.start();
// Play in 1 second
source.start(audioCtx.currentTime + 1);
// Play from 2 seconds, for 3 seconds
source.start(0, 2.0, 3.0);
// Stop after 5 seconds
source.stop(audioCtx.currentTime + 5);
Important: One-Time Use
Each AudioBufferSourceNode can only be played once. After calling start(), create a new node to replay:
function playSound() {
const source = audioCtx.createBufferSource();
source.buffer = myBuffer;
source.connect(audioCtx.destination);
source.start();
// No need to keep reference - garbage collected after playback
}
This one-time-use pattern is actually a feature that simplifies memory management in long-running web applications, as developers don't need to manually track and dispose of nodes. For applications requiring intelligent audio responses, explore how AI automation can enhance user interactions through smart audio triggers and responses.
Fire and Forget
Create nodes, start playback, and let them be garbage collected
Buffer Reuse
Reuse AudioBuffer objects across multiple source nodes
Context State
Always resume AudioContext before playing (browsers suspend until interaction)
Pre-load Audio
Decode audio buffers during app initialization for instant playback
Use Gain Nodes
Insert GainNode for volume control instead of modifying buffer data
Disconnect When Done
Call disconnect() to free resources in long-running applications
Connecting to the Audio Graph
AudioBufferSourceNode must be connected to other nodes in the Web Audio API graph to produce audible output.
Basic Connection
const source = audioCtx.createBufferSource();
source.buffer = buffer;
source.connect(audioCtx.destination);
With Volume Control
const gainNode = audioCtx.createGain();
gainNode.gain.value = 0.5; // 50% volume
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
Multiple Destinations
A source can connect to multiple nodes simultaneously:
const gainNode = audioCtx.createGain();
const analyser = audioCtx.createAnalyser();
source.connect(gainNode);
source.connect(analyser);
gainNode.connect(audioCtx.destination);
This modular architecture is what makes the Web Audio API so powerful for interactive applications, allowing audio processing chains to be built dynamically at runtime.
Game Audio
Sound effects requiring precise timing - footsteps, gunshots, UI sounds
Interactive Apps
Audio feedback in educational software, music tools, accessibility features
Sampling
Drum machines, samplers, language learning apps with repeated playback
Presentations
Sound effects in interactive slideshows and web presentations
Notifications
Audio alerts and notifications with low latency response
Music Players
Playing stored audio clips with instant start capability
1// Initialize AudioContext2const audioCtx = new (window.AudioContext || window.webkitAudioContext)();3 4// Load an audio file5async function loadAudioFile(url) {6 const response = await fetch(url);7 const arrayBuffer = await response.arrayBuffer();8 const audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);9 return audioBuffer;10}11 12// Play audio with volume control13async function playAudio() {14 // Resume context if suspended (browser policy)15 if (audioCtx.state === 'suspended') {16 await audioCtx.resume();17 }18 19 const buffer = await loadAudioFile('example.mp3');20 21 const source = audioCtx.createBufferSource();22 source.buffer = buffer;23 24 // Add volume control25 const gainNode = audioCtx.createGain();26 gainNode.gain.value = 0.8;27 28 // Connect nodes29 source.connect(gainNode);30 gainNode.connect(audioCtx.destination);31 32 // Play33 source.start();34}35 36// Play with looping37async function playLoopingAudio() {38 const buffer = await loadAudioFile('background.mp3');39 40 const source = audioCtx.createBufferSource();41 source.buffer = buffer;42 source.loop = true;43 44 source.connect(audioCtx.destination);45 source.start();46}Frequently Asked Questions
Conclusion
AudioBufferSourceNode provides a straightforward yet powerful way to play back audio data stored in memory. Its key advantages include:
- Extremely low latency for responsive audio playback
- Precise timing control through the AudioScheduledSourceNode interface
- Efficient resource usage with automatic garbage collection
- Universal browser support since 2015
For most web applications requiring in-memory audio playback, AudioBufferSourceNode remains the standard and most accessible choice. When building with modern frameworks like Next.js, integrate AudioBufferSourceNode alongside your application state for seamless audio experiences. Optimizing audio delivery can also contribute to better user engagement metrics, which positively impacts your SEO performance.
Remember: create new nodes for each playback, reuse AudioBuffer objects across sources, and always handle the AudioContext state for browser autoplay compliance. Whether you're building interactive web applications, games, or audio production tools, AudioBufferSourceNode provides the foundation for professional-quality audio on the web.
Need help implementing advanced audio features in your web application? Our team of experienced developers can help you build immersive audio experiences that delight your users.