BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Voices and space: who gets heard, and from where

BF22142Heroes

A battle asks for far more sounds than a sound card of 2005 could play: 16 voices on the lowest setting, 32 on the usual one. So the sound library is, before anything else, a referee. This page is about its two decisions: which sounds get a voice, and where each one sits in the space around the listener.

The templates, the rolloff, engine sounds and music: the sound engine.

From a play request to a voice the play request culled when far and quiet: beyond its maximum distance or under a gain of 0.003 the delay heap a one-shot waits the time sound takes to travel: 1 s per 343 m the instance's two slots at most two samples of one sound at once; a third stops the first priority 2D: 2.0; 3D: volume over the distance term, minus 1 beyond the maximum distance a voice, or stealing one 16, 32 or 128 voices; a louder newcomer stops the weakest playing sample the mix, every update distance, cone, Doppler, obstruction, tinnitus, the group's volume BF2Audio.dll!0x10006180 (request), 0x10006880 (delay), 0x10016b80 (slots), 0x10005510 (priority), 0x10007140 (steal)
From a play request to a voice. A sound can be turned away at four places before it is ever heard.

How many voices#

The profile's audio settings choose a provider and a quality (AudioSettings::apply, BF2.exe!0x004b1420):

QualityVoicesCulling of far sounds
Low16on
Medium32on
High32on
UltraHigh128off; and only with the hardware provider, else it becomes High

A profile can force another count. A few sources are kept spare (min-free-sources).

Priority and stealing#

Every update each waiting and each playing sample gets a priority (BF2Audio.dll!0x10005510):

text
2D sound:   2.0
3D sound:   the instance's volume
            divided by ((d - minDistance) x rolloff / minDistance + 1)   beyond minDistance
            minus 1                                                       at or beyond maxDistance

Both lists are sorted by it, the newer first among equals. Then (0x10007140): while the best waiting sample outranks the weakest playing one, the playing one is stopped and the waiting one started. A stolen sample with more than 15 ticks left is put back in the queue for 5 ticks later, so it may return when the louder one ends.

A one-shot sound that waits more than 60 ticks (two seconds) without a voice is dropped. A sound that starts late does not start at its beginning: after 11 ticks of waiting a looping sound enters at its loop block, a single one at its end block.

Two slots an instance#

A sound instance (one gun's firing sound, say) holds at most two samples at once (0x10016b80) and accepts one a frame. A third sample of one instance stops the first, hard. Which slot is taken is the instance's policy (SoundSource::chooseSlot, 0x10016760); the game always uses the plain one, the slot after the last used. The library also has a policy that weighs front against back, side, age and distance; the notes found no caller of it in the game.

How it is tested#

  • The original's code, as traces: 1,000 generated scenarios through the original sound library's ambient triggers and crossfader (ambient_oracle.rs), and 200 scenario blocks through the game executable's sound glue: obstruction, the listener, the distance rules (exe_oracle.rs). The port must reproduce each trace.
  • Scheduling (crates/bf2-audio/src/engine.rs, 24 tests): the voice limit, priority sorting, stealing, the delay by distance, 2142's fade curve.

Three of eight addresses stand in a test's text. What a player hears (the mix on a real device) is not compared with the original: the tests hold the decisions, not the sound.

Where it sits#

For every playing 3D sample, each update (OpenALSample::update, 0x100134d0):

StepRule
positionthe template's position through the instance's transform, into the listener's space; a head-relative sound is at zero
distancethe device's inverse distance model, clamped: full inside minDistance, then min / (min + rolloff x (d - min)); no maximum
conethe template's inner and outer angle and outer gain when the inner angle is below 360 and the sound has a direction
Dopplerthe device's, from the listener's and the source's velocities; a velocity of 300 m/s or more counts as 0
gainclamp(volume x envelope x group volume x balance) x obstruction x global effect
pitchclamp(pitch x envelope, 0.3, 2.0)

Obstruction: is something in the way?#

The game casts rays from the listener to 3D sounds (Sound\BfSound.cpp). The ray ignores soldiers and the listener's own vehicle, does not test the terrain, and only looks at objects; for a sound within 40 m the second list of objects is searched too. The result is a number from 0 to the maximum the settings allow (the shipped settings: 1.0), and the library applies it gently: a sample's first update takes the value as it is, later ones move at most 0.1 an update toward the new value (0x10013130). A sound that passes behind a wall is therefore muffled over a few updates, not at once. Without the hardware effects the gain is clamp(1 - obstruction).

So a hill does not muffle a sound in this engine, and a building does.

Reverb: is there a roof?#

The listener's reverb level comes from three rays (0x007dee40): two straight up, a little to either side of the listener, and one straight down, each sound-raylength long (17 m in the shipped settings). If any of them hits nothing, the level is 0. Otherwise:

text
h     = clamp(height of the room: floor to the mean of the two ceilings, 0, L)
level = (max x h) / ((1 - s) x L + s x h)             max = sound-maxreverb, s = sound-reverb-steepness

These rays too see objects only. Standing on bare ground under a roof gives no floor hit and so no reverb; standing on a building's floor does. The level then offsets the room level of the level's reverb preset.

Tinnitus and the group fades#

  • Tinnitus: the game can set a global effect level that follows an envelope over a set duration; while it lasts every other sound is multiplied by 1 - 0.9 x level and a ringing plays.
  • Groups: sounds belong to volume groups (effects, music, the help voice). The game fades whole groups for its states: everything down behind the menu, the effects down while the commander's voice speaks.
  • Wind: one looping sound whose control value is the listener's speed over a reference speed (30 in the shipped setup), so it grows with speed.

What the port has#

PartState
the play request, culling, the delay heap, dispatch, slots, priority, start, stealing, stopported in bf2-audio; the slot choice in both policies
the mix: balance, obstruction smoothing, tinnitus, cone, envelopesported
the game's side: listener, obstruction rays, reverb rays, wind, group fadesverified against the original functions (oracle/ExeOracle.cs)
the software device, its reverb and effect emulationported: the port mixes in software, natively and in the browser
air absorptionthe original sends it only to the hardware effects; see the notes for the port's state

Scenarios add invariants for sound on every run: no pop, no sound that failed to resolve (test rigs).

In the code#

WhatWhere
the librarycrates/bf2-audio (slots, math, bfsound)
the browser's mixercrates/bf2-audio-worklet
sound templates and filessounds

Notes: notes/subsystems/audio.md (sections 2, 6, 7 and 13).

Source: docs/dev/audio/voices-and-space.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.