Audio Overview
Web Engine uses the Web Audio API for high-performance spatial audio with channel-based mixing, effects, and voice pooling.
The audio system in Web Engine is built on the Web Audio API, providing professional-grade 3D spatial audio, advanced channel routing, and zero-GC voice pooling for optimal performance.
Audio Features#
AudioSource
Play sounds with volume, pitch, and looping controls. Supports both 2D and 3D audio.
Spatial Audio
3D positional audio with HRTF panning, distance attenuation, and AudioListener.
Audio Channels
Channel-based mixing for SFX, Music, and Voice with independent volume control.
Scripting API
Play sounds, control playback, and handle audio events from scripts.
Audio Architecture#
Web Engine's audio system consists of several key components working together:
SoundManager#
The SoundManager handles one-shot sound effects with a fixed 64-voice pool. It provides:
- Zero-GC voice pooling for predictable performance
- Priority-based voice stealing when pool is full
- 3D spatial audio with HRTF panning
- Automatic spatial culling beyond max distance
- Fade in/out support for smooth transitions
AudioSystem#
The AudioSystem manages AudioSource components for persistent, looping sounds. It integrates with Three.js Audio for positional audio and handles:
- AudioSource entity lifecycle and playback
- Spatial positioning synchronized with entity transforms
- Audio LOD (Level of Detail) based on distance
- Channel routing and mixer integration
- Automatic cleanup of finished sounds
AudioMixer#
The AudioMixer provides centralized volume and mute control across all audio sources:
- Four channels: Master, SFX, Music, Voice
- Per-channel volume and mute controls
- Rate-limited updates to prevent excessive API calls
- Synchronized state across SoundManager and AudioSystem
Audio Context Management#
Web Engine creates a single global AudioContext that's shared across the entire audio system. The context is automatically resumed on user interaction to comply with browser autoplay policies.
// The audio context is initialized automatically// when you play your first sound import { SoundManager } from '@web-engine-dev/core'; const soundManager = SoundManager.getInstance();soundManager.init(); // Creates and configures AudioContext // Access the context if neededconst ctx = soundManager.getContext();console.log('Sample rate:', ctx.sampleRate);console.log('State:', ctx.state); // 'running', 'suspended', or 'closed'Autoplay Policy
Modern browsers require user interaction before playing audio. The audio system automatically resumes the AudioContext on the first click or keypress.
Supported Audio Formats#
Web Engine supports all audio formats that the Web Audio API can decode:
- MP3 — Best compatibility, good compression. Recommended for music.
- OGG/Vorbis — Free format, excellent compression. Great for sound effects.
- WAV — Uncompressed, highest quality. Large file size.
- AAC/M4A — Good quality and compression. Widely supported.
- WebM — Modern format with excellent compression.
Format Recommendations
Use MP3 or OGG for music tracks (longer files with good compression). Use OGG or short MP3 files for sound effects. Avoid WAV in production due to large file sizes unless quality is critical.
Quick Start#
To add audio to your game:
- Import an audio file into your project's Assets panel.
- Add an
AudioSourcecomponent to an entity. - Assign the audio asset to the Asset ID field.
- Configure volume, pitch, and loop settings.
- Check Playing to start playback.
- For 3D audio, enable Spatial and adjust distance settings.
Voice Pooling System#
For maximum performance, one-shot sounds use a pre-allocated pool of 64 voices. When the pool is full, the system uses priority-based voice stealing:
- Higher priority sounds (0-255) are less likely to be stolen
- Among equal priority, quieter sounds are stolen first
- Spatial culling automatically stops sounds beyond max distance
- Fade out on stop prevents audio pops and clicks
// Play a high-priority sound effectconst handle = soundManager.play(explosionAssetId, { volume: 0.8, spatial: true, priority: 200, // High priority - won't be stolen easily fadeIn: 0.05, // 50ms fade in fadeOut: 0.2, // 200ms fade out on stop}); // Stop with fade outsoundManager.stop(handle); // Uses configured fadeOut durationsoundManager.stop(handle, true); // Immediate stop, no fadePerformance Optimization#
The audio system is designed for zero-GC performance in the hot path:
- Voice Pool — Pre-allocated 64 voices, no runtime allocation
- Spatial Culling — Automatically stops sounds beyond 1.5× max distance
- Audio LOD — Reduces quality/volume for distant sounds
- Rate Limiting — Prevents excessive WebAudio API calls
- Dirty Flags — Only updates when values change
- Typed Arrays — Pre-allocated buffers for zero-GC calculations
Performance Monitoring
Use the audio profiler to monitor voice usage, pool statistics, and performance metrics. Check the browser console for pool exhaustion warnings if you need to adjust the pool size.