Web Audio & Interactive Sound Architecture
Use this skill when building interactive browser soundscapes, game audio, spatial 3D positioning, synthesizer sound effects, or audio-driven WebGL visualizers.
Core Rules & Architecture
1. AudioContext Lifecycle & User-Gesture Unlocking
- Modern browsers suspend new
AudioContextinstances until the user interacts with the page (click, tap, keypress). - Graceful Unlock Handler:
let audioCtx = null; export function getAudioContext() { if (!audioCtx) { const AudioContextClass = window.AudioContext || window.webkitAudioContext; audioCtx = new AudioContextClass(); } if (audioCtx.state === 'suspended') { const resume = () => { audioCtx.resume(); window.removeEventListener('pointerdown', resume); window.removeEventListener('keydown', resume); }; window.addEventListener('pointerdown', resume); window.addEventListener('keydown', resume); } return audioCtx; }
2. Spatial 3D Audio (PannerNode)
- Pair sound emitters with Three.js objects using
PannerNode:- Set
panningModel = 'HRTF'for realistic binaural audio rendering. - Set
distanceModel = 'inverse'with tunedrefDistanceandmaxDistance. - Continuously update the listener position from the active camera transform.
- Set
3. Audio-Reactive Visualizers (AnalyserNode)
- Connect audio sources to an
AnalyserNodewithfftSize = 512or1024. - Extract frequency bins into a
Uint8Arrayusinganalyser.getByteFrequencyData(). - Pass frequency intensities as uniforms (or data textures) into Three.js vertex/fragment shaders to deform meshes or drive particle colors in real time.
4. Accessibility & User Controls
- Global Mute Control: Always provide a prominent, accessible audio toggle button with
aria-labeland persistedlocalStoragestate. - Respect User Preferences: If
prefers-reduced-motionis enabled, tone down reactive flashing or disorienting low-frequency visual tremors.