Sound effects from a few lines of code
No audio files, nothing to preload, and no third-party library — just oscillators and envelopes, synthesised as they play.
Sound is the part of a small game most often skipped, and it is the cheapest part to add. A satisfying click, a rising tone for a pickup and a descending buzz for a failure can each be built from an oscillator and a volume envelope in a handful of lines, with no files to download and nothing to preload.
The shape of a synthesised sound
A note has three parts: a waveform, a frequency, and a volume envelope that describes how the sound starts and stops. The Web Audio API exposes all three directly. An oscillator provides the waveform — a sine for something soft, a triangle for something bright, a sawtooth for something harsh. A gain node provides the volume, and the envelope is written by scheduling changes to that gain over time.
The whole thing is created, connected, started and stopped inside the function that plays it, and then thrown away. A short sound costs a fraction of a millisecond to set up, which is why it is practical to build one from scratch on every event rather than keeping a pool of voices.
Always ramp from silence
The one detail that turns a synthesiser from a novelty into something usable is the attack. Starting an oscillator at full volume produces an audible click, because the waveform jumps instantly from nothing to a non-zero value. Ramping the gain up over about ten milliseconds removes it entirely.
The release matters for the same reason. Letting a sound stop abruptly clicks again; ramping it down to a very small value before stopping it does not. Every sound in these games uses the same shape: a fast ramp up, then an exponential ramp down to near-silence.
The browser will not let you make noise until it is asked
Audio contexts begin suspended, because browsers refuse to play sound before a user has interacted with the page. The first tap that starts a game is also the moment the audio context can be resumed, and doing that inside the handler is what makes the very first sound audible rather than swallowed.
Inside a host environment there is a second gate: the platform itself can have sound switched off, and it reports that through its own API. The polite behaviour is to respect it and stay silent until the host says otherwise, rather than making noise the moment the player taps.
What short sounds should do
A game sound has a job. A successful action gets a short, bright, rising tone; a failure gets a lower one and a little more duration; a piece of punctuation — a level cleared, a record beaten — gets a small sequence rather than a single note. The volume matters more than the waveform: game sounds should sit under the music a player might have on, which means quiet, and it is much easier to make a sound quieter at the start than to apologise for it later.
When a file is the right answer
Synthesis is not always correct. A game that needs music, a realistic sound effect, or a very specific recorded voice should ship a file, accept the download, and manage the preload. Synthesis wins when the sounds are short, abstract and functional, which describes essentially every sound in these games — and when the alternative is a download several times larger than the game itself.
Want to go deeper?
Building something for the browser? Send us a link and we will tell you honestly whether it fits.