On this page
Voice chat: capture, codec, routing, playback
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.
Who hears whom#
| You are | Push-to-talk reaches | The leader key reaches |
|---|---|---|
| a squad member | your squad | |
| a squad leader | your squad | the commander |
| the commander | (from the commander screen) the squad leaders | |
| in no squad | nobody: 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:
- transmit only in a squad or as commander;
- receive: unmuted talkers are marked as talking, which the scoreboard, squad list and commander rows show;
- capture, once enough is queued;
- send when push-to-talk allows;
- talking for more than 10 seconds blocks you until you fall silent;
- keep the link alive;
- 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#
| Property | Value |
|---|---|
| format | 8 kHz, 16-bit, mono (set at the hardware start, BF2.exe!0x0065e1e0) |
| frame | 160 samples: 20 ms |
| capture buffer | a looping buffer of 1 s (0x0065ed90) |
| the gate | a frame's peak at or above the threshold opens the voice; it stays open 20 frames (0.4 s) after the last loud one |
| filter | a 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.voipQuality | Speex quality | Bytes a 20 ms frame |
|---|---|---|
| 1 | 1 | 9 |
| 2 | 2 | 14 |
| 3 (default) | 4 | 20 |
| 4 | 7 | 37 |
| 5 | 10 | 61 |
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#
| Property | Value |
|---|---|
| playback buffer | looping, 200 ms: 10 frames (0x0065ecd0) |
| sources | 8 talkers at once per device |
| jitter | a 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 packets | dropped |
| volume | set 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#
| Part | State |
|---|---|
| the voice library: capture, gate, sources, jitter, playback | ported in bf2-voip |
| the codec | the 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 rows | ported |
| the voice server | ported (bf2_voip::server) |
| the microphone mixer | ported over the system's interface; inactive on current Windows, as the original's |
| the default voice devices | differs: the port takes the system's default devices, not the ones marked for communication |
| Battlefield 2142's and Heroes' voice | not 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#
| What | Where |
|---|---|
| the voice library, devices, mixer, the voice server | crates/bf2-voip (gv, dsound, cpal_io, winmm, server) |
| the codec | crates/bf2-speex |
| the client's game side | crates/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.
docs/dev/audio/voip.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.