🌾 FarmHeart
Web AudioSound DesignGame Dev

Browser Game Sound Design with the Web Audio API

2026-09-14 · 10 min read

Why Procedural Audio?

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.

Web Audio API Basics

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.

Tip: Create a single AudioContext for your entire game and reuse it. Creating multiple contexts wastes resources and can cause issues on some browsers.

Building Your First Sound Effect

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.

Essential Game Sound Recipes

Coin Collect / Pickup

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.

Jump

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.

Impact / Hit

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.

Explosion

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.

UI Click

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.

Ambient Wind

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.

Building an Audio Manager

Don't scatter audio code throughout your game. Create a centralized audio manager that handles:

Music Without Files

Procedural music is more complex than sound effects but surprisingly approachable. Start with a simple arpeggiator:

  1. Define a chord progression (e.g., C major → A minor → F major → G major)
  2. For each chord, define the notes (frequencies)
  3. Schedule oscillators to play each note in sequence with a fixed interval (e.g., 0.2 seconds)
  4. Use the AudioContext's precise timing (context.currentTime) to schedule notes ahead of time

Layer 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.

WaveformSound CharacterBest For
SinePure, clean, flute-likeMelody, UI sounds, gentle tones
SquareHollow, retro, 8-bitRetro games, chip-tune music
SawtoothBuzzy, rich, brass-likeBass, lead sounds, horns
TriangleSoft, muted, woodwind-likeGentle melody, background pads

Spatial Audio for 3D Games

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:

In 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.

Performance Considerations

Audio processing happens on a separate thread, so it rarely causes frame drops. But there are limits:

Mobile tip: Always provide a mute button. Mobile players often play in public or while listening to their own music. Respect their audio space.

Testing Audio

Audio 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.

Play FarmHeart

A cozy 3D farming game that runs right in your browser. No download, no signup.

Play Now