Files
VoelinClient/docs/audio.md
T
faumarayandClaude Sonnet 5.5 6c1898bf5a Put the voice source behind a ServerLink
The session no longer spawns the TeamSpeak connection itself. link.rs adds
ServerLink (spawn(options, ctx, cmds, events)) with LinkCmd and LinkEvent,
VoiceCmd and VoiceEvent moved and renamed, plus ConnectOptions (VoiceOptions
stays as its name for the app), LinkCtx and Remote. TsLink (ts_link.rs) is
the TeamSpeak implementation and the engine's default; Engine::start_with_link
takes another.

The stream task moves inside the connection: voice.rs spawns it when the
server has streams, feeds it the server's notifications and the clients'
channels, and sends its requests. The session reaches it only through
LinkCmd::Stream(StreamCmd) and hears of our own stream (OwnStream) and the
audio of watched streams (StreamAudio) as link events, so the link holds no
handle on the audio thread. stream.rs keeps its tests, adapted to the split
of StreamInput into StreamCmd and the link-private input.

tests/link_fake.rs drives the session with an in-memory link: connect,
presence, commands, a message sent and echoed, a message that comes twice,
history, and a replaced connection, without a TeamSpeak crate. The
architecture doc describes ServerLink and the planned move of TsLink to
VoelinGateWay and the QUIC link of VoelinProtocol.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:54:08 +03:00

7.3 KiB
Raw Blame History

Audio pipeline

microphone ─ cpal ─ resample 48 kHz mono ─ Processor (AEC3, NS, AGC2) ─ 20 ms frames
          ─ Vad / push-to-talk / continuous ─ Opus ─ EncodedVoice ─ voice connection

voice connection ─ VoicePacket ─ Mixer (jitter buffer, Opus decode, per-talker
          volume/mute, priority speaker dimming)
          ─ output volume ─┬─ resample to device ─ cpal ─ speakers
                           └─ Processor::render (echo canceller reference)

The audio thread lives in voelin-core/src/audio.rs; the building blocks are in voelin-audio. All user settings are one serde struct, voelin_audio::AudioSettings.

Voice packets

voelin-audio knows no voice transport (it depends on no TeamSpeak crate). What crosses its boundary are the plain types of voelin_audio::packet:

Type Meaning
TalkerId(u16) who is talking; the key of the mixer's queues, volumes and mutes (TeamSpeak's client id is used as is, watched streams get made-up ids)
VoiceCodec Voice (mono Opus, speech) or Music (stereo Opus)
VoicePacket { talker, seq, codec, whisper, payload } one received Opus packet; seq counts up per talker and wraps, an empty payload (or a single byte) ends the talk
EncodedVoice { codec, payload } one frame from VoiceEncoder, an empty payload ends the transmission

The transport adapter converts between these and its wire format; for the TeamSpeak connection that is out_audio and voice_packet in voelin-core/src/voice.rs, the only place that builds TeamSpeak audio packets from EncodedVoice or reads them into VoicePackets (codecs other than Opus are dropped there, as the jitter buffer never played them). The audio thread, the session and the mixer see only the types above, and so does the link's seam: LinkCmd::Audio carries an EncodedVoice out and LinkEvent::Audio a VoicePacket in (voelin-core/src/link.rs).

Echo cancellation, noise suppression, gain control

sonora 0.2 (BSD-3-Clause), a pure Rust port of the WebRTC audio processing module: AEC3, the WebRTC noise suppressor, AGC2 (adaptive digital gain + limiter) and a high-pass filter. Chosen over:

  • webrtc-audio-processing: binds the C++ library, either a system package found through pkg-config or a bundled C++ build (autotools, bindgen/libclang); its licence is not an SPDX expression, and Windows/Android need extra toolchain work;
  • nnnoiseless (RNNoise port): noise suppression only, no echo cancellation.

sonora needs no C/C++ toolchain, so it builds the same everywhere. It runs on 10 ms frames; Processor frames arbitrary lengths internally. The dev profile compiles the sonora crates with opt-level = 3: unoptimised, AEC alone takes a third of a core; optimised, the full chain takes about 0.3 ms per 10 ms frame.

Measured with the unit tests (synthetic signals):

Result
Echo (far end delayed 20–120 ms, -6 dB) about 25–33 dB attenuation within the first second, 50 dB after two; the delay estimate matches
Double talk (near-end tone during echo) near-end tone loses about 3 dB
White noise, suppression High 10 dB or more lower; a tone in 200 ms bursts keeps its level within 3 dB

A steady tone is treated as stationary noise and suppressed, as expected from this kind of noise suppressor, so the test uses bursts like syllables.

Voice activity

Vad: a level gate in dBFS with hangover (default -40 dBFS, 300 ms). The optional Speech mode also requires the RNN speech probability of WebRTC's AGC2 VAD (sonora-agc2), which rejects noise loud enough to pass the gate. The RNN also fires on pure tones, and on synthetic vowels only at low pitch, so the plain gate stays the default until it is checked with real voices. With voice activation the two 20 ms frames before the gate opened are sent too, so onsets are not cut.

Jitter buffer and clock drift

Mixer wraps voelin_audio::jitter::AudioHandler (one queue per talker; it started as tsclientlib::audio::AudioHandler and now reads VoicePackets) and adds per-talker volume and mute, and priority speaker dimming. The handler absorbs sender clock drift: an underrun plays Opus loss concealment for the missing frame (one inserted frame, and the talker is dropped after three in a row), and when the smallest queue length over the last 255 packets exceeds its spread, it drops every 100th sample (1 % faster) until the surplus is gone; queues above 0.5 s are truncated.

crates/voelin-audio/tests/drift.rs simulates one hour of continuous speech with 20–60 ms network jitter, reordering and 0.5 % loss, with the sender clock at +200 and -200 ppm (720 ms of disagreement per hour). Marker frames every 30 s measure the end-to-end latency:

Sender clock Latency min / max First 10 min avg Last 10 min avg
+200 ppm 73 / 131 ms 112 ms 125 ms
-200 ppm 67 / 95 ms 82 ms 84 ms

Settings, levels and volumes

The engine keeps one AudioSettings for all sessions (Command::SetAudioSettings; each new audio thread starts with it) and applies changes live. The desktop app stores them under the setting audio and shows them on its settings page: devices, transmit mode (push-to-talk, voice activation, continuous), the voice activation threshold, echo cancellation, noise suppression and its level, automatic gain, microphone gain, output volume and the playback buffer.

The audio thread reports the loudest 10 ms level since the last report about ten times a second (Event::InputLevel, with whether voice is sent), which the settings page shows as a meter with the threshold. Without a voice connection, Command::TestMicrophone opens the microphone for the meter alone.

Per-client volume and mute are local (SetClientVolume, SetClientMuted); the app keeps them by unique id and sends them again when the client shows up. Client ids are reused, so the session forgets the settings of clients that left. Watched streams play through the same mixer under made-up talker ids (counting down from 65535) with their own volume (SetStreamVolume).

While a priority speaker has a queue (talking, or buffering before it plays) and we have not muted it, every other queue, streams included, is dimmed by the server's virtualserver_priority_speaker_dimm_modificator (ServerDetails::priority_speaker_dimm_db, usually -18 dB, clamped to -60..=0). The dimming fades in and out sample by sample over 100 ms (5 frames), so the gain does not jump; priority speakers are never dimmed. The session sends the priority speakers and the server's value to the audio thread when either changes (Mixer::set_priority).

Push-to-talk works with the button in the window and a global hotkey (voelin_platform::HotkeyManager: the portal on Wayland, XInput2 on X11, a hook on Windows), configurable as e.g. Ctrl+Shift+T.

Devices

Devices are selected by cpal's stable id (host:device, e.g. alsa:hw:1,0); device::input_devices() / output_devices() list them with the system defaults marked. cpal's error callback flags a stream as lost on fatal errors (not on xruns or rerouting); device::Managed then reopens the selected device, or the default one while the selected one is missing (retrying every 2 s, moving back within 5 s of it reappearing). The audio thread reports these changes as session errors.