Expand description
§kessel-audio
A real-time voice audio pipeline in pure Rust, for apps that carry live calls over their own transport. It owns everything between the microphone and speaker and the encoded frames on the wire:
- devices: capture and playback through cpal (CoreAudio, WASAPI,
ALSA), device lists, hot-plug, switching, and a null device for
headless use; on iOS and Android, the OS’s own voice processing
(
Backend::OsVoiceProcessing), or raw capture with sonora’s echo cancellation and no noise suppression (PhoneMode::Raw), switchable mid-call; - resampling between the device rate and 48 kHz (rubato);
- echo cancellation, noise suppression and gain control (sonora, a port of WebRTC’s audio processing), with the playback mix as the echo reference;
- a capture stage of your own (a noise-removal model, say) between
echo cancellation and gain control, held to a time budget (on the
processing thread’s CPU time where the platform has it,
StageClock) with a bypass that never shifts the timing, and a grace window for the host’s transient events (CaptureStage,AudioEngine::transient_event); - Opus encode and decode (ropus), 48 kHz mono, 20 ms frames, with the caller’s bitrate, FEC, DTX and complexity;
- a jitter buffer per participant with loss concealment and FEC recovery;
- mixing, levels (about every 50 ms) and statistics.
The transport (RTP, encryption, signalling) stays outside: kessel-audio
produces plaintext EncodedFrames and consumes RecvFrames that the
transport has already decrypted and verified.
§Example
use kessel_audio::{AudioEngine, EngineConfig, RecvFrame};
let engine = AudioEngine::start(EngineConfig::default())?;
let frames = engine.capture_frames();
engine.add_participant(7);
loop {
// Send path: encrypt and send each encoded frame.
while let Some(f) = frames.try_recv() {
let _ = (f.payload, f.ts48k);
}
// Receive path: hand over each decrypted, verified packet.
if let Some(p) = packet {
engine.push(p);
}
}§Threads
AudioEngine is a Send + Sync handle; no async runtime is needed.
Device callbacks only move samples through lock-free rings; one
processing thread does the signal processing and codec work; a control
thread handles devices and events. On macOS and iOS the processing thread
runs real-time (by default the QoS class, Mach’s time-constraint policy,
and on iOS the audio unit’s OS workgroup; EngineConfig::dsp_sched
picks another DspSchedMode) and the capture callback wakes it;
Stats::dsp_scheduling says which it got (DspScheduling) and
Stats::dsp_sched the mode in effect. Every queue between the caller and
the engine is bounded and lock-free, and the audio side never blocks on
one: a full queue drops (and counts) instead.
Re-exports§
pub use bytes;
Structs§
- Audio
Engine - A running audio engine.
Send + Sync: share it (for example in anArc) between the transport’s tasks. Dropping it stops the threads and closes the devices. - Audio
Error - An error with its code and a human-readable detail.
- Capture
Stage Stats - The capture stage’s state (
crate::Stats::capture_stage, andcrate::Event::CaptureStagewheneveractive,bypassed,strikesorpermanentchange). - Capture
Stats - Capture-side counters.
- Device
Info - One audio device.
- Device
List - The available devices.
- Device
Selection - A device change:
Noneleaves that direction as it is. - Devices
InUse - The devices in use right now.
- DspSched
Config - What the processing thread asks the OS for
(
crate::EngineConfig::dsp_sched): the mode, the time-constraint figures and, forDspSchedMode::OwnWorkgroup, each interval’s deadline. Read once, as the thread starts. - DspSched
Stats - The processing thread’s scheduling set-up, for call logs
(
crate::Stats::dsp_sched): the mode asked for and the mode in effect, why they differ, and the figures asked for. What the OS granted iscrate::Stats::dsp_scheduling. - DspTime
Constraint - The time-constraint policy’s figures (
thread_time_constraint_policy), as durations; the thread converts them to Mach absolute-time units. - Encoded
Frame - One encoded 20 ms frame from the capture path.
- Encoder
Cfg - Encoder settings, set per call from the caller’s media policy. They may
change mid-call (
crate::AudioEngine::set_encoder). - Engine
Config - Everything
crate::AudioEngine::startneeds. - EventRx
- The receiving end of the event queue (bounded, lock-free). When it is full, the oldest event is dropped, so a slow reader sees the latest state.
- FrameRx
- The receiving end of the encoded-frame queue (bounded, lock-free).
- Jitter
Config - Jitter-buffer settings.
- Level
- A level reading for one stream.
- Levels
- Levels for this device and each remote participant, about every 50 ms.
- Null
Device - The null device: no hardware. A thread of its own plays the device:
every
block_periodit takes 10 ms of capture frominput(silence ifNone) and hands 10 ms of playback tooutput(dropped ifNone). - Participant
Stats - Receive statistics for one participant, cumulative since it was added.
- Processing
Config - Which processing runs on the capture path.
- Recv
Frame - One received frame for a remote participant.
- Stats
- A statistics snapshot (
crate::AudioEngine::stats).
Enums§
- Backend
- Where audio comes from and goes to.
- Device
Choice - Which device to use for one direction.
- DspSched
Fallback - Why the processing thread fell back to
DspSchedMode::Workgroup(DspSchedStats::fallback_reason). - DspSched
Mode - Which scheduling the processing thread asks the OS for
(
DspSchedConfig::mode). Only macOS and iOS act on it; elsewhere the mode in effect isDspSchedMode::Unsupported. - DspScheduling
- How the OS schedules the processing thread (
kessel-audio-dsp): the best of the requests it made that the OS granted. Incrate::Stats::dsp_schedulingandcrate::CaptureStageStats::dsp_scheduling, so call logs show it. - Error
Code - What went wrong, as a stable code. The string forms
(
ErrorCode::as_str) are part of the public API. - Event
- Something the caller should know about.
- Noise
Suppression Strength - Noise-suppression strength.
- Stage
Bypass Reason - Why the capture stage was bypassed.
- Stage
Clock - What times the capture stage’s calls for its time budget
(
ProcessingConfig::stage_clock; seeCaptureStage). - Transient
Event - Something the host knows is happening that can stall the processing
thread for a moment, for
AudioEngine::transient_event: it opens (or extends) the capture stage’s grace window, so slow calls right after it don’t count toward a strike (seeCaptureStage).
Constants§
- AGC2_
HEADROOM_ DB - AGC2 adaptive-digital headroom, in dB.
- DEFAULT_
STAGE_ GRACE - The default grace window
(
ProcessingConfig::stage_grace): 1.5 s of capture after aTransientEvent, the stage’s start or a capture stream (re)start, during which slow calls don’t count toward a strike. - DEFAULT_
STAGE_ STARVATION_ CUTOFF - The default starvation cutoff
(
ProcessingConfig::stage_starvation_cutoff): withStageClock::ThreadCpu, a call that takes 20 ms or more on the wall clock counts as slow toward the 2-in-a-row rule, whatever its CPU time. - FEC_
LOSS_ HINT_ PCT - The packet-loss percentage the encoder is told to expect when
EncoderCfg::fecis on. Opus adds in-band FEC only when this is above 0. - FRAME_
MS - Opus frame duration: 20 ms.
- FRAME_
SAMPLES - Samples per 20 ms frame at 48 kHz (also the
ts48kstep per frame). - MAX_
STAGE_ GRACE - The longest grace window the engine accepts: 10 s, the retry period.
- MAX_
STAGE_ LATENCY_ SAMPLES - The longest stage latency the engine accepts: one second.
- SAMPLE_
RATE - Sample rate of every PCM signal inside the pipeline and of the Opus timestamps: 48 kHz.
Traits§
- Capture
Stage - A caller’s own processing on the capture path, run once per 10 ms block
after the high-pass filter and echo cancellation and before gain control
(see
crate::ProcessingConfig::capture_stage).
Type Aliases§
- Capture
Source - The null device’s capture source: fills 10 ms of mono samples.
- Playback
Sink - The null device’s playback sink: receives 10 ms of the mono mix.