What goes into a sound
A sound is a template in the object's CON tree that names a sample file and says how to play it: how loud, how high, how far it carries, and how pitch and volume follow a game value such as the engine's revs.
The template#
The jeep's tree holds 42 Sound templates as children of its parts (see the object tree). Their properties sit in the tweak:
ObjectTemplate.soundFilename "Sound\West\Vehicles\English\Jeep\West_SFX_Vehicle_GB_JEP_Bri_Jeep_Willys_StartIdleStop_01.wav"
ObjectTemplate.loopCount 0
ObjectTemplate.volume 0.7
ObjectTemplate.pitch 1.2
ObjectTemplate.pitchEnvelope 0/1/0/1.5/0/2/0.0/0.7/0/1/1.5/0/
ObjectTemplate.volumeEnvelope 0/1/0/1/0/3/0/1/0/0.5/1/0/1/1/0/
ObjectTemplate.minDistance 6
ObjectTemplate.halfVolumeDistance 20bf2-audio template.rs models the template (SoundTemplate: BF2.exe's ObjectTemplate.Sound plus the BF2Audio.dll Sound it forwards to); its setters clamp and derive the attenuation the way the DLL does (set_min_distance, set_half_volume_distance, recompute).
The samples#

bf2-audio soundfile.rs load reproduces SoundFile::load of BF2Audio.dll:
- WAV files are static: fully loaded, split into up to three buffers by the file's loop points (start block, loop block, end block). Above the quality level's rate cap (44100 / 22050 / 11025 Hz) they are decimated by an integer factor without filtering and keep only the first channel.
- Ogg Vorbis files stream. bf2-vorbis is a bit-exact port of the libvorbis 1.0 decoder linked into BF2Audio.dll, down to its x87 precision: sample streams decode on the main thread at 24-bit precision, music on the music thread at 53-bit (
SoundFile::pcm_at).
Heroes keeps BF2's choice: effects as WAV (Sound_client.zip West/...), music as Ogg (Music/*.ogg).
Envelopes#

An envelope string is xmin/xmax/ymin/ymax/random/count/ followed by count points x/y0/y1/ (bf2-audio envelope.rs EnvelopeSpec::parse, BF2.exe 0x007e4dd0). The control value comes from the game (an engine's rpm, a rotation speed); Envelope::eval interpolates linearly between the points and holds the end values outside them. With random set, 64 variants are precomputed from the C runtime's rand() and each loop of the sound picks one. The curves above are Envelope::eval samples, not a re-implementation.
3D settings#
| Line | Meaning |
|---|---|
minDistance | full volume up to this distance (m) |
halfVolumeDistance | the distance at which the volume has halved; the falloff curve follows from the two |
soundRadius, coneInnerAngle, coneOuterAngle, coneOuterVolume | size of the source and directional cones |
volumeGroup | the volume group the sound settings scale |
The derived attenuation and the cone are bf2-audio template.rs (recompute_in, has_cone).
How it is tested#
- Every shipped sound decodes (
crates/bf2-audio/tests/retail_sounds.rs): every wave, Ogg and EAX file of an install, with the original's rules. - The Ogg decoder against the original's (
crates/bf2-vorbis/tests/synthetic.rs): 300 generated streams covering the format's floors, residues and block sizes must decode as the original's decoder does. - Engine sounds (
engine_sound_oracle.rs): 400 generated scenarios through the original's engine and rotation sound code, compared as a trace; run again in a 2142 process.
What a sound template does in play is on the sound engine and voices and space.
In the code#
| What | Where |
|---|---|
| sound templates, the rolloff, the cone | crates/bf2-audio (template.rs, math) |
| Ogg Vorbis decoding | crates/bf2-vorbis |
| voices, priority and stealing | crates/bf2-audio (voices and space) |
| radio lines and soldiers' voices | crates/bf2-audio (voice; radio) |
| the mixer of the browser client | crates/bf2-audio-worklet |
docs/dev/assets/sound.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.