Skip to main content

Crate kessel_audio

Crate kessel_audio 

Source
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§

AudioEngine
A running audio engine. Send + Sync: share it (for example in an Arc) between the transport’s tasks. Dropping it stops the threads and closes the devices.
AudioError
An error with its code and a human-readable detail.
CaptureStageStats
The capture stage’s state (crate::Stats::capture_stage, and crate::Event::CaptureStage whenever active, bypassed, strikes or permanent change).
CaptureStats
Capture-side counters.
DeviceInfo
One audio device.
DeviceList
The available devices.
DeviceSelection
A device change: None leaves that direction as it is.
DevicesInUse
The devices in use right now.
DspSchedConfig
What the processing thread asks the OS for (crate::EngineConfig::dsp_sched): the mode, the time-constraint figures and, for DspSchedMode::OwnWorkgroup, each interval’s deadline. Read once, as the thread starts.
DspSchedStats
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 is crate::Stats::dsp_scheduling.
DspTimeConstraint
The time-constraint policy’s figures (thread_time_constraint_policy), as durations; the thread converts them to Mach absolute-time units.
EncodedFrame
One encoded 20 ms frame from the capture path.
EncoderCfg
Encoder settings, set per call from the caller’s media policy. They may change mid-call (crate::AudioEngine::set_encoder).
EngineConfig
Everything crate::AudioEngine::start needs.
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).
JitterConfig
Jitter-buffer settings.
Level
A level reading for one stream.
Levels
Levels for this device and each remote participant, about every 50 ms.
NullDevice
The null device: no hardware. A thread of its own plays the device: every block_period it takes 10 ms of capture from input (silence if None) and hands 10 ms of playback to output (dropped if None).
ParticipantStats
Receive statistics for one participant, cumulative since it was added.
ProcessingConfig
Which processing runs on the capture path.
RecvFrame
One received frame for a remote participant.
Stats
A statistics snapshot (crate::AudioEngine::stats).

Enums§

Backend
Where audio comes from and goes to.
DeviceChoice
Which device to use for one direction.
DspSchedFallback
Why the processing thread fell back to DspSchedMode::Workgroup (DspSchedStats::fallback_reason).
DspSchedMode
Which scheduling the processing thread asks the OS for (DspSchedConfig::mode). Only macOS and iOS act on it; elsewhere the mode in effect is DspSchedMode::Unsupported.
DspScheduling
How the OS schedules the processing thread (kessel-audio-dsp): the best of the requests it made that the OS granted. In crate::Stats::dsp_scheduling and crate::CaptureStageStats::dsp_scheduling, so call logs show it.
ErrorCode
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.
NoiseSuppressionStrength
Noise-suppression strength.
StageBypassReason
Why the capture stage was bypassed.
StageClock
What times the capture stage’s calls for its time budget (ProcessingConfig::stage_clock; see CaptureStage).
TransientEvent
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 (see CaptureStage).

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 a TransientEvent, 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): with StageClock::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::fec is 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 ts48k step 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§

CaptureStage
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§

CaptureSource
The null device’s capture source: fills 10 ms of mono samples.
PlaybackSink
The null device’s playback sink: receives 10 ms of the mono mix.