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

Voice chat: capture, codec, routing, playback

BF2

Battlefield 2's voice chat is squad chat. You can talk only in a squad or as the commander; a squad member is heard by his squad, a squad leader has a second key for the commander. The sound path is a small voice library of the time inside the client, a narrowband speech codec, and a separate little server that only forwards packets. The port has all three, and its test passes a tone from one client's microphone buffer to another's speaker buffer.

A voice frame, from a microphone to a speaker capture 8 kHz, 16-bit, mono; frames of 20 ms; a gate that the push-to-talk key opens the band-pass and the codec Speex narrowband: 9 to 61 bytes a frame by the server's quality the voice server forwards each frame to whom the game server's routing allows: squad, leaders, commander the receiver's sources 8 talkers at once; time stamps mapped to the local clock plus 10 frames playback a looping buffer of 200 ms; the game's own sound is ducked while a voice plays BF2.exe!0x0065e1e0 (hardware start), 0x0065ed90 (capture), 0x004510f0 (the filter), 0x0068b7b0 (frame size), 0x0065ecd0 (playback)
A voice frame's way. The game server only says who may hear whom; the voice server forwards.

Who hears whom#

You arePush-to-talk reachesThe leader key reaches
a squad memberyour squad
a squad leaderyour squadthe commander
the commander(from the commander screen) the squad leaders
in no squadnobody: nothing is transmitted

The routing is the game server's knowledge (team, squad, leader, commander), which it keeps the voice server told of. Each player gets a session id when he joins, by an event to his own client; a player's on/off flag and local mutes are events too.

One frame of the client#

Bf2VoipClient::update runs once a frame:

  1. transmit only in a squad or as commander;
  2. receive: unmuted talkers are marked as talking, which the scoreboard, squad list and commander rows show;
  3. capture, once enough is queued;
  4. send when push-to-talk allows;
  5. talking for more than 10 seconds blocks you until you fall silent;
  6. keep the link alive;
  7. duck the game's sound while a voice plays.

The two keys are game inputs (input): push-to-talk opens the capture; the leader key selects the leader channel, with an activation sound. A commander away from the commander screen cannot push to talk.

Capture#

PropertyValue
format8 kHz, 16-bit, mono (set at the hardware start, BF2.exe!0x0065e1e0)
frame160 samples: 20 ms
capture buffera looping buffer of 1 s (0x0065ed90)
the gatea frame's peak at or above the threshold opens the voice; it stays open 20 frames (0.4 s) after the last loud one
filtera band-pass around 2,500 Hz, 1.75 octaves wide, gain 2 (BF2.exe!0x004510f0)

With push-to-talk the threshold is 0 while the key is held and 1.0 (never) otherwise: the gate and the key are the same mechanism. Frames of a closed gate are consumed and dropped.

The client also drives the system's microphone mixer: selects the microphone, sets its level, mutes it on the speakers, switches the boost, and restores all of it at disconnect. On Windows since Vista that old mixer interface finds no recording line, and the original's mixer stays off there; so does the port's.

The codec#

Speex, narrowband. The server's sv.voipQuality picks the quality (0x00736d80, then 0x0065f080), and with it the frame's size (0x0068b7b0):

sv.voipQualitySpeex qualityBytes a 20 ms frame
119
2214
3 (default)420
4737
51061

The default is 8,000 bits a second. The frame size is computed by a whole-byte division that cuts the last few bits of a frame at four of the five qualities; the decoder reads them as zeros, on the original and so on the port. The port links a newer release of the same codec library: the narrowband bit stream has been frozen since its first version, so frames pass both ways, but the port's encoder does not produce the original's exact bytes.

Playback#

PropertyValue
playback bufferlooping, 200 ms: 10 frames (0x0065ecd0)
sources8 talkers at once per device
jittera packet's time stamp maps to the local clock plus 10 frames; the offset is smoothed by 1/32 a packet, and steps when the drift passes 10 frames one way or 5 the other
late and duplicate packetsdropped
volumeset in the device's decibel scale: the amplitude is the setting raised to ln 10

A source "talks" from its first mixed frame until its last has passed; that is what lights the speaker icon beside a name and what ducks the game's sound.

What you see#

The HUD reads the voice client: the scoreboard, squad and commander rows show who has voice on, who is muted and who is talking; the crosshair panel shows your own state, with a warning when your level is above 0.95. One quirk is kept: your own voice flag reads off unless you turned voice on in the audio options, because the event that sets it skips the local player.

What the port has#

PartState
the voice library: capture, gate, sources, jitter, playbackported in bf2-voip
the codecthe same library in a newer release; frame sizes per quality tested; frames interoperate, encoder bytes differ
the game side: session, on/off and mute events, the keys, the 10 s block, ducking, the HUD's rowsported
the voice serverported (bf2_voip::server)
the microphone mixerported over the system's interface; inactive on current Windows, as the original's
the default voice devicesdiffers: the port takes the system's default devices, not the ones marked for communication
Battlefield 2142's and Heroes' voicenot on this page: the note covers Battlefield 2

How it is tested#

  • The codec: frame sizes per quality, header bits, a tone encoded and decoded.
  • A loopback of the whole path (crates/bf2-voip/tests/voice_loopback.rs): two clients with in-process sound devices, the game server's link and the voice server, all on 127.0.0.1. A 1 kHz tone fed into a squad member's capture comes out of his leader's playback and not out of the commander's; both talking marks and the duck fire; under push-to-talk nothing passes until the key is held; the leader key reaches the commander only.
  • Two off-screen clients through the port's dedicated server: the member plays about 16 s of the leader's tone, shows him talking, and the squad list draws his speaker icon.
  • Hardware tests (a real microphone and speakers) exist and are not run by default.

In the code#

WhatWhere
the voice library, devices, mixer, the voice servercrates/bf2-voip (gv, dsound, cpal_io, winmm, server)
the codeccrates/bf2-speex
the client's game sidecrates/bf2-client/src/voip.rs

Notes: notes/subsystems/voip.md. See also voices and space for the game's own sound, and squads and commander.

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