Most game audio tutorials tell you to download WAV files and play them. That works, but it means loading megabytes of audio assets, managing file formats across browsers, and having no control over the sound once it plays.
The Web Audio API lets you synthesize sounds in real time using oscillators, filters, and envelopes. This means zero audio files to load, infinite variations of each sound, and full runtime control over pitch, volume, and effects. For browser games where download size matters, procedural audio is a game-changer.
Everything starts with an AudioContext. This is your audio engine — it manages the audio graph (a network of connected nodes) and handles timing, mixing, and output.
Critical gotcha: browsers require a user interaction before audio can play. Create your AudioContext lazily — on the first click or keypress, not on page load.
A basic sound effect uses three components:
Connect them in order: Oscillator → Gain → Destination. Schedule the oscillator to start and stop, ramp the gain for a smooth envelope, and you have a sound effect.
A classic pickup sound is a quick ascending tone. Use a sine wave oscillator, frequency sweep from 600Hz to 1200Hz over 0.1 seconds, with a sharp attack and medium decay. The ascending pitch triggers a reward sensation in the player's brain.
A jump sound uses a sine wave with a quick frequency sweep from 200Hz to 400Hz over 0.15 seconds. Add a slight detune on a second oscillator for thickness. The upward pitch motion mirrors the visual upward movement.
Combine a low-frequency sawtooth (80Hz, quick decay) with white noise (very short, 0.05s). The sawtooth provides the "thump" while the noise adds the "crack." Filter the noise with a BandpassFilter around 2000Hz for a more natural sound.
Layer white noise through a lowpass filter (start at 2000Hz, sweep down to 200Hz over 0.5s) with a bass sine oscillator (60Hz, 0.3s decay). Add a second noise layer with a highpass filter for the "sizzle." Vary the filter frequencies randomly each time for unique explosions.
A short (0.02s) sine wave at 800Hz with instant attack and fast decay. Clean and non-intrusive. Vary the pitch slightly (780-820Hz) on each click to avoid it sounding robotic.
White noise through a bandpass filter (center 400Hz, Q 0.5) with slow random modulation of the filter frequency. Layer two instances at different center frequencies for depth. Crossfade between them with slow sine-wave gain modulation.
Don't scatter audio code throughout your game. Create a centralized audio manager that handles:
audio.play('coin') triggers the registered soundProcedural music is more complex than sound effects but surprisingly approachable. Start with a simple arpeggiator:
context.currentTime) to schedule notes ahead of timeLayer a bass note (fundamental of each chord, sine wave, low octave) under the arpeggio. Add a filtered noise pad for atmosphere. This gives you a basic but effective ambient soundtrack.
| Waveform | Sound Character | Best For |
|---|---|---|
| Sine | Pure, clean, flute-like | Melody, UI sounds, gentle tones |
| Square | Hollow, retro, 8-bit | Retro games, chip-tune music |
| Sawtooth | Buzzy, rich, brass-like | Bass, lead sounds, horns |
| Triangle | Soft, muted, woodwind-like | Gentle melody, background pads |
The Web Audio API includes a PannerNode that positions sounds in 3D space. This is powerful for 3D browser games — sounds get louder as you approach and quieter as you move away, with stereo panning based on direction.
Set up spatial audio:
AudioListener positioned at the camera/playerPannerNode with its 3D positiondistanceModel to 'inverse' and adjust refDistance and maxDistanceIn FarmHeart, animal sounds use spatial audio — cows moo louder when you're near the pasture, and crickets chirp from all around you at night. It adds tremendous immersion for minimal code.
Audio processing happens on a separate thread, so it rarely causes frame drops. But there are limits:
oscillator.onended) to prevent memory leaksAudio bugs are the hardest to automate. Build debug tools:
Test on real mobile devices — audio behavior differs significantly between Chrome on Android, Safari on iOS, and desktop browsers. Safari in particular has quirks with AudioContext resumption and oscillator scheduling.
A cozy 3D farming game that runs right in your browser. No download, no signup.
Play Now