✚ Game development

Positional audio and adaptive music: sound that reacts to play

Panning, distance attenuation, occlusion and layered or segmented adaptive music: how to make sound react to the player's position and in-game actions.

Playing a sound when something happens is the first level of game audio. The second is making the sound tell where it is happening and how the game is going. A footstep behind you, a distant explosion beyond a wall, music that grows more intense as enemies arrive: this is information players use, often without realising it.

Positional audio in 2D

Even in a two-dimensional game, the position of sound sources matters. Two parameters are enough for a good result.

Panning

Panning distributes the sound between the left and right channels according to the source's horizontal position relative to the listener, usually the camera or the character.

pan = clamp((source.x - listener.x) / pan_radius, -1, 1)

A value of −1 means fully left, 1 fully right. To prevent the overall volume from dipping in the centre, a constant-power panning law is used, which distributes the gains following sine and cosine rather than a simple linear proportion.

Distance attenuation

A distant sound should be quieter. The most common curves:

Curve Behaviour Use
Linear Volume drops evenly to zero at maximum distance 2D games, simple control
Inverse Volume drops quickly near the source, then slowly Close to physical behaviour, 3D games
Custom Hand-drawn curve Important sounds that must stay audible

In every case it is worth defining a minimum distance, within which volume is full, and a maximum distance, beyond which the sound is not even played. This second limit frees up precious voices.

Positional audio in 3D

In three-dimensional games the audio engine computes the position and orientation of sources and listener. Beyond panning and attenuation, other effects come into play.

  • Doppler effect: an approaching source sounds slightly higher, a receding one slightly lower. It should be used carefully: realistic Doppler on a racing car is effective, on a walking character it is pointless or annoying.
  • Binaural spatialisation: with headphones, filters based on the shape of the head and ears make it possible to perceive height and whether a sound comes from in front or behind.
  • Environmental reverb: the same footstep sounds different in a cathedral, a corridor or outdoors. Zones are defined with their own reverb settings and the game blends gradually between them.

Occlusion and obstruction

A sound behind a wall should not arrive as if the wall were not there. Practical solutions:

  • cast a ray from the source to the listener;
  • if it hits solid geometry, apply a low-pass filter and a volume reduction, because walls mostly let low frequencies through;
  • to avoid abrupt changes, interpolate the filter parameters over time.

The term occlusion is used when the source is completely separated, for example in another room, and obstruction when an obstacle is in the way but sound can travel around it, like a pillar. For many independent games a simple ray with a filter is already a big step forward.

Adaptive music

Linear music always sounds the same, whatever the player does. Adaptive music changes in response to the game. There are two main approaches, often combined.

Vertical layers

The piece is made of several synchronised tracks of the same length that always play together: for example percussion, bass, strings, melody. The game adjusts each one's volume according to the situation:

  • exploration: strings and a soft pad only;
  • enemies nearby: the percussion comes in;
  • combat: all tracks at full volume.

Because the tracks are always in sync, changes happen with smooth fades and no rhythmic jumps.

Horizontal segments

The piece is divided into sections, such as intro, exploration, tension, combat, victory. The game decides which section to play after the current one. To avoid cutting the music mid-bar, the switch happens at the next valid transition point, for example at the end of the bar or phrase, possibly through a short bridging segment.

Linking music to game state

The tricky part is deciding when to change. An «intensity» indicator, computed from the number of enemies, distance, player health or boss phase, is more robust than many separate events. Two tricks:

  • hysteresis: the music rises in intensity quickly but falls with a delay, avoiding constant flipping when an enemy moves in and out of range;
  • minimum duration: a music state stays active for a few seconds before it can change again.

Professional audio middleware tools offer visual editors for these behaviours, but with an engine that can synchronise several tracks you can achieve excellent results with your own code too.

In short

  • Panning and distance attenuation are enough to position sounds in 2D.
  • 3D games add Doppler, binaural spatialisation and environmental reverb.
  • Occlusion and obstruction are simulated with a ray and a low-pass filter.
  • Adaptive music uses synchronised vertical layers, horizontal segments or both.
  • An intensity indicator with hysteresis drives musical changes without annoying flip-flopping.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also