Craft·5 min read

Getting a Game's Volume Right

Every sound in our games passes through a single gain set to about a third. Everything else is decided against that.

The Fighting Chess interface with its war log of moves beside the slain pieces panel.

Every sound our games make passes through one control, set to roughly a third of maximum. Every individual sound's level is chosen relative to that.

That sounds like an arbitrary implementation detail. It is actually the thing that decides whether a game sounds considered or sounds like a phone alarm, and it is worth understanding why the number is a third rather than one.

Everything through one place

The structure is simple. Each sound is created, given a level, and connected to a single master control, which is connected to the speakers. Nothing connects to the speakers directly.

Two things fall out of that arrangement and both matter.

Relative levels stay fixed. A jump is set to 0.32, a landing to 0.10, a pickup to 0.28. Those numbers are relative to each other, and because they all pass through the same master, changing the master changes all of them together. Nothing drifts out of balance.

There is one place to intervene. Muting is setting the master to zero. Fading out for a cutscene is ramping it down. Ducking the music while a line of narration plays is lowering one branch. None of that requires touching individual sounds.

Without a master, muting means finding every sound in the game and setting each to zero, and forgetting one. That bug ships more often than you would think.

Why a third and not full volume

This is headroom, and it is the least intuitive part of audio for people coming from anywhere else.

Digital audio has a hard ceiling. Sound is a number per sample, and there is a maximum that number can be. Ask for louder and it cannot go louder; it clips, meaning the peaks are flattened, and flattened peaks sound like harsh crackling distortion rather than like something loud.

The trap is that sounds add. One sound at full volume is fine. Two sounds at full volume happening at the same moment are asking for twice the maximum, and what comes out is distorted. In a game, several sounds happening at once is not an edge case, it is Tuesday: you land, collect something and get hit within the same fifth of a second.

So the master sits well below the ceiling, leaving room for several sounds to stack without ever reaching it. A third is a comfortable setting for a game with a handful of simultaneous sounds. It means the game is quieter than it could be, and quieter is completely fine, because the person listening has a volume control and no way to undo distortion.

The rule is: it is always better to be too quiet than to clip. Too quiet is solved by turning it up.

The Fighting Chess interface with its war log of moves beside the slain pieces panel.
A capture here fires several sounds within a fraction of a second. That is what headroom is for.

The silence problem

Browsers do not allow a page to make noise until somebody has interacted with it. This is a good rule and it is not going away.

The practical effect is that a game cannot set up its audio when the page loads, because at that point it is not allowed to. If it tries, the audio system is created in a suspended state and every sound played into it goes nowhere, silently, with no error.

The fix in our builds has two halves. The audio system is created lazily, the first time a sound is actually requested, which in practice is after the player has pressed something. And every time a sound is requested, the code checks whether the system has been suspended and resumes it if so.

That second check catches the other version of the same problem: a browser will suspend audio when a tab goes to the background, and returning to the tab does not reliably resume it. Without the check, a player who switched apps mid-game comes back to a silent game and has no idea why. It is three lines and it is the difference between working and mysteriously not.

When to split the controls

A small game needs one control and a mute. Our platformer has exactly that, toggled with a key.

A larger one needs more. The adventure build keeps music, effects and ambience on separate levels, plus a choice of music mode and a switch for narration, and it stores all of that separately from the game's progress so that starting a new chapter does not reset somebody's volume preferences. That separation is described in how a game remembers where you got to.

The test for whether a split is needed is whether anyone would plausibly want one thing quieter and another louder. Music and effects, yes, always, because people play games with their own music on. Two kinds of effect, almost never.

The thing that is not in the mix

One decision worth flagging because it is a mix decision that looks like a design decision.

A sound that plays constantly, an engine hum or a wind loop, has to sit far lower than feels right when you audition it alone. Anything continuous becomes fatiguing at a level that seems perfectly reasonable in isolation, because in isolation you are listening to it and in the game you are living with it.

The reliable method is to set it while doing something else, then set it lower than that. This is the same category of judgement as knowing which sounds need variation, which is covered in why the same sound twice sounds wrong.

What this means for a commission

Audio is quick to add and slow to get right. The system is an afternoon. The levels are a week of noticing things.

A mute switch is not optional. Somebody will play this in a room where they cannot have sound, and a game they have to close is a game they do not finish.

And it should be quieter than you think. Every game we have built ended up quieter than its first version, and none of them ended up louder.

◆ Questions

Common questions

Why do browser games start with no sound?+

Browsers block audio until the person has interacted with the page. It is a deliberate rule that exists because of years of pages that made noise unprompted. A game therefore has to create its audio on the first click or keypress rather than on load.

What is headroom in audio?+

The gap between how loud the sound normally is and the maximum the system can represent. Without it, several sounds happening at once add up past the limit and the result is harsh distortion rather than simply loud.

Why does everything go through one volume control?+

So that relative levels stay fixed. Every sound is set relative to a single master, which means the overall volume can be changed, muted or ducked in one place without any sound drifting out of balance with the others.

Why does game audio sometimes stop after switching apps?+

The browser suspends the audio system when a tab goes to the background and does not always resume it automatically. A game that does not check for this comes back silent, which is a common and easily fixed bug.

Should a game have separate music and effects volumes?+

If it has enough of both to be worth separating, yes. Our adventure keeps music, effects and ambience on separate controls and saves them; the smaller builds have a single mute.

  • craft
  • audio
  • code
◆ Keep reading

Related from the journal.