User reported persistent crackling/popping from peer audio on Linux even
after fixing the 48k->device-rate resampler boundary discontinuities,
clamping pre-Opus-encode peaks, and pre-allocating the playback scratch
buffer. Logs confirmed cpal opened raw ALSA at 44.1k native, no callback
budget violations, no underrun warnings -- yet the audio was still poor.
Root cause: cpal on Linux opens raw ALSA's 'default' PCM. On modern
PipeWire / pipewire-alsa boxes that virtual device routes through ALSA's
dmix + plug layers, whose default resampler is nearest-neighbour. cpal
also picks a small default period size (~256 frames / 5.8 ms) leaving no
headroom for kernel scheduler jitter. Both effects compound into the
crackling the user heard.
Upstream tsclientlib's own audio example
(tsclientlib/examples/audio_utils/ts_to_audio.rs) and the official Qint
client both use SDL2 with AudioSpecDesired { freq: 48000, channels: 2,
samples: 960 }. SDL2 on the same systems routes through PipeWire's PA
bridge (or PulseAudio directly), both carrying high-quality resamplers.
Fix:
* Add sdl2 = '0.37' as a target_os=linux dependency. Links libSDL2-2.0
.so (Arch sdl2-compat over SDL3, Debian libsdl2-2.0-0, Fedora SDL2).
* New module crates/chanora_audio/src/sdl_output.rs implementing
SdlOutput: opens a 48 kHz stereo 960-frame callback that zeroes the
buffer and calls AudioHandler::fill_buffer directly (no user-side
resampler). Master gain + hard-mute atomics wired in identically to
the cpal callback so set_output_gain / set_output_muted keep working.
* engine.rs cfg-gated: target_os='linux' builds SdlOutput; everywhere
else continues with the cpal output path (including the device-native-
rate negotiation and resampler-continuity fixes shipped earlier --
those remain correct on Windows/macOS where cpal targets WASAPI /
CoreAudio cleanly).
* The cpal output helpers (build_output_stream, PlaybackResampleState,
FromF32) are now cfg(not(target_os='linux'))-gated so the Linux
build doesn't emit dead-code warnings.
Capture path still cpal on every platform -- outbound audio was not
reported as bad. Resampler-continuity fix on the capture side stays:
microphone -> Opus encoder still goes through the linear interpolator
with the last-sample anchor.
Tests: 32 / 0 / 0 (chanora_audio), workspace 78 / 0 / 1 unchanged.
114 lines
3.6 KiB
Rust
114 lines
3.6 KiB
Rust
//! # `chanora_audio`
|
|
//!
|
|
//! Audio subsystem promoted from `poc/audio-capture-playback-spike`
|
|
//! and wired against `chanora_protocol`'s voice channels.
|
|
//!
|
|
//! ## What's wired in this Beta
|
|
//!
|
|
//! * Default-input capture via `cpal` (DEC-011.1)
|
|
//! * Frame-aligned 20 ms / 48 kHz mono Opus encoding via `audiopus`
|
|
//! * Forward encoded frames to the protocol crate as `OutPacket`s
|
|
//! * Inbound voice packets fed to `tsclientlib::audio::AudioHandler`
|
|
//! which owns Opus decode + per-client jitter buffer + mix
|
|
//! * Mixed f32 PCM pulled by the cpal output callback at 48 kHz stereo
|
|
//! * Push-to-talk: capture stream is permanently open; encoding is
|
|
//! gated by an atomic `ptt_active` flag
|
|
//!
|
|
//! ## What's NOT wired in this Beta
|
|
//!
|
|
//! * AEC / AGC / NS / HPF DSP chain (DEC-007/008/009/010 — Beta+
|
|
//! work; the toggles in `AudioEffects` are honoured by *naming*
|
|
//! but the filters are no-ops)
|
|
//! * Mobile audio paths (DEC-011.1 desktop + Android proven; this
|
|
//! integration is desktop-only for v0.2.0-beta.1)
|
|
//! * Hot-plug device-change handling
|
|
//! * Sample-rate adaptation if the device cannot do 48 kHz / mono in
|
|
//! the format we request (returns `AudioError::StreamConfig`)
|
|
//! * Multi-channel speaker layouts beyond stereo
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
mod engine;
|
|
pub mod ptt;
|
|
pub mod ptt_backends;
|
|
pub mod release_tail;
|
|
pub mod transmit_mode;
|
|
pub mod transmit_selector;
|
|
|
|
#[cfg(target_os = "linux")]
|
|
mod sdl_output;
|
|
|
|
pub use engine::{AudioEngine, AudioEngineConfig};
|
|
pub use ptt::{
|
|
AudioTransmitGate, MissedKeyUpWatchdog, PttBackendDescriptor, PttCapabilityLevel,
|
|
};
|
|
pub use ptt_backends::{
|
|
select as select_ptt_backend, DesktopPttBackend, FocusedPttBackend, PttBackendError,
|
|
PttBinding, PttInputClass,
|
|
};
|
|
pub use release_tail::{ReleaseTailTimer, DEFAULT_TAIL_MS, MAX_TAIL_MS};
|
|
pub use transmit_mode::TransmitMode;
|
|
pub use transmit_selector::TransmitModeSelector;
|
|
|
|
use thiserror::Error;
|
|
|
|
/// Errors raised by the audio subsystem.
|
|
#[derive(Debug, Error)]
|
|
pub enum AudioError {
|
|
/// Platform did not expose a usable default input device.
|
|
#[error("no default input device")]
|
|
NoInputDevice,
|
|
/// Platform did not expose a usable default output device.
|
|
#[error("no default output device")]
|
|
NoOutputDevice,
|
|
/// The audio backend rejected a stream configuration.
|
|
#[error("stream config rejected: {0}")]
|
|
StreamConfig(String),
|
|
/// Opus codec init/encode/decode failure.
|
|
#[error("opus: {0}")]
|
|
Opus(String),
|
|
/// A backend-specific failure surfaced without a typed mapping.
|
|
#[error("audio backend: {0}")]
|
|
Backend(String),
|
|
}
|
|
|
|
/// Audio-effect toggles. Defaults match DEC-007 (AEC),
|
|
/// DEC-008 (AGC), DEC-009 (NS), DEC-010 (HPF) — all enabled.
|
|
///
|
|
/// Note: in Beta v0.2.0-beta.1 the actual DSP filters are not yet
|
|
/// implemented; the struct is kept here as the public API surface so
|
|
/// later work can flip an internal flag without breaking callers.
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub struct AudioEffects {
|
|
/// Acoustic echo cancellation (DEC-007).
|
|
pub aec: bool,
|
|
/// Automatic gain control (DEC-008).
|
|
pub agc: bool,
|
|
/// Noise suppression (DEC-009).
|
|
pub noise_suppression: bool,
|
|
/// High-pass filter (DEC-010).
|
|
pub high_pass: bool,
|
|
}
|
|
|
|
impl Default for AudioEffects {
|
|
fn default() -> Self {
|
|
Self {
|
|
aec: true,
|
|
agc: true,
|
|
noise_suppression: true,
|
|
high_pass: true,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn defaults_match_decisions() {
|
|
let e = AudioEffects::default();
|
|
assert!(e.aec && e.agc && e.noise_suppression && e.high_pass);
|
|
}
|
|
}
|