Execution Planning, CspaMixer & Live Hot-Swap
Before opening a single native audio stream, CaptureRequest::inspect() evaluates your targets, OS capabilities, mixing mode, and memory budgets into a deterministic CapturePlanReport.
1. Two-Stage Strategy Resolution: ProcessTreeRange & CombineMode
Section titled “1. Two-Stage Strategy Resolution: ProcessTreeRange & CombineMode”Planning separates how an individual process tree is acquired from how multiple targets are combined:
Stage A — Per-Target ProcessTreeRange (Process & Application targets)
Section titled “Stage A — Per-Target ProcessTreeRange (Process & Application targets)”Ordered waterfall from lowest overhead to broadest software fallback (start_level..=fallback_limit):
NativeKernelCapture: Single OS kernel stream captures the entire process tree directly (WindowsWASAPIprocess-tree loopback).NativeGraphMix:CaptureManagermaintains dynamic process membership on a single native audio-server graph node (LinuxPipeWiresupport.null-audio-sinkpassive port links; macOSCoreAudioCATapDescriptionprocess-object list).SoftwareStreamMix:CaptureManageropens per-process/per-sink-input streams (e.g., LinuxPulseAudiopa_stream_set_monitor_stream) and combines them inCspaMixer.
Lowering
fallback_limit(or settingNonein Python/Rust) opts out of software mixing when you require strictly native OS capture!
Stage B — Session CombineMode
Section titled “Stage B — Session CombineMode”Auto(Default):- Single target with unity gain (
gain == 1.0) and noOutputConverterPassThrough(0extra copies,0mixer latency). - Multiple
PipeWireorCoreAudiotap targets combinable on one native graphSharedNativeGraphMix. - Heterogeneous targets (
[microphone(), find_app("Zoom")], cross-backend[ScreenCaptureKit + CoreAudio],[PipeWire + PulseAudio], non-unitygain, orOutputConverter)CspaMixer.
- Single target with unity gain (
NativeOnly: RequiresPassThroughorSharedNativeGraphMix; fails explicitly if software mixing would be needed.CspaMixer(CSPA_MIXERin Python/C11 wire enums): Forces software mixing even for targets that could share a native graph (useful when you want per-target staticgain,InputStatusHandletelemetry, orSoftwareSummationduplicate-target summing).
2. Caller-Owned Target Overlap
Section titled “2. Caller-Owned Target Overlap”If you pass overlapping targets (for example, [default_output(), find_app("Spotify")] or two copies of the same target), capture-system-or-process-audio never silently drops your targets:
SoftwareSummation(CspaMixer): Sums each stream’s contribution (2xamplitude for an exact duplicate).NativeSetUnion(SharedNativeGraphMix): Deduplicates native PipeWire port links or Core AudioAudioObjectIDs on the shared native node while tracking target reference counts so removing one target keeps shared links alive for remaining targets.
3. Inside CspaMixer: Normalizer, 20 ms Grid & Clock-Drift Sync 0 Steady-State Allocs
Section titled “3. Inside CspaMixer: Normalizer, 20 ms Grid & Clock-Drift Sync 0 Steady-State Allocs ”Mixing a USB microphone (16 kHz or 44.1 kHz mono I16 on its own crystal oscillator) with system speakers (48 kHz stereo F32 on motherboard audio) breaks naive sample-summing loops within seconds. CspaMixer handles every edge case deterministically off the real-time callback thread:
- Order-Independent Working Format: Promotes to
max(input_rates)(e.g.48000 Hz) +StereoF32regardless of target list order. - Preallocated
InputNormalizer + FrameReblocker:- When an input already matches the mixer’s working rate and stereo
F32layout, normalization entersBYPASS(is_identity_bypass == true). - Otherwise (
InputConversionPolicy::AutoWhenMixing, or rejected ifStrictNoResample), a preallocated1025x644-term Blackman-Harris polyphase windowed-sinc resampler (122.9 dBstopband attenuation, exact integer moduloP/Qphase tracking) + ringFrameReblockerslices normalized audio into exact20 ms(alignment_window_ms = 20) blocks (SYNC).
- When an input already matches the mixer’s working rate and stereo
- Clock-Domain-Gated Slip/Repeat Drift Compensation:
- Each backend stamps
Stamp.clock_domain. When independent hardware clocks drift beyonddrift_threshold_ms(5 msdefault,2 mshysteresis,500 mshold),CspaMixerperforms a64-frame linear crossfade slip/repeat adjustment (drift_slipscounter) without pitch-warping or audible clicks.
- Each backend stamps
- Non-Finite Sample Sanitization & Idle Parking:
- Sanitizes any
NaNor±Inffloat sample from misbehaving drivers to0.0(nonfinite_samplescounter), leavesF32headroom unclamped by default (clamp_f32 = false), and either parks cleanly when all inputs are silent or synthesizes continuous silent blocks (synthesized_silence = true) whensilence_on_idle = true.
- Sanitizes any
4. Inspecting Plans & Live Two-Tier Hot-Swap (update_targets)
Section titled “4. Inspecting Plans & Live Two-Tier Hot-Swap (update_targets)”update_targets(...) reconfigures an active CaptureManager session mid-recording without losing queued audio:
- Tier 1 (
SharedNativeGraphMixin-place): Updates PipeWire passive links or macOSCATapDescriptionprocess lists in place (0 msmixer buffering). - Tier 2 (
CspaMixerdifferential reconciliation): Keeps unchanged input streams, normalizer phases, and drift state running untouched while opening only newly added targets and draining removed ones (0heap allocations when slot capacity suffices).
import capture_system_or_process_audio as cspa
mic = cspa.microphone()spk = cspa.default_output()
# 1. Side-effect-free plan inspection:mixer = cspa.MixerConfig(alignment_window_ms=20, drift_threshold_ms=5, silence_on_idle=True)req = cspa.request([mic, spk], combine_mode=cspa.CombineMode.CSPA_MIXER, mixer_config=mixer)plan = req.inspect()print("Combine:", plan.combine_resolution.value, "PCM RAM:", plan.estimated_pcm_bytes)
# 2. Open & live hot-swap targets mid-session:if False: # illustrative runtime session with req.open() as stream: rev = stream.update_targets([mic]) for h in stream.input_handles(): print(h.target_id, h.participation_state.value, "bypass=", h.is_identity_bypass) print("Updated membership version:", rev)use capture_system_or_process_audio::{ CapabilityError, CaptureManager, CaptureTarget, CombineMode, DefaultDeviceMode, MixerConfig, Stop, request,};use std::time::Duration;
fn plan_and_swap() -> Result<(), CapabilityError> { let mic = CaptureTarget::microphone(DefaultDeviceMode::Follow); let spk = CaptureTarget::default_output(DefaultDeviceMode::Follow);
let req = request(vec![mic.clone(), spk]) .with_combine_mode(CombineMode::CspaMixer) .with_mixer_config(MixerConfig { silence_on_idle: true, ..MixerConfig::default() });
let plan = req.inspect()?; println!("Resolution: {:?}, bytes: {}", plan.combine_resolution, plan.estimated_pcm_bytes);
if false { let mut mgr = CaptureManager::open(req)?; let version = mgr.update_targets(vec![mic])?; println!("Hot-swapped to membership version {version}"); mgr.stop(Stop::Drain); mgr.wait_cleanup(Duration::from_secs(2)); } Ok(())}let _ = plan_and_swap();#include <cspa.hpp>#include <cstdio>
int main() { auto mic = cspa::Target::microphone().take_value(); auto spk = cspa::Target::default_output().take_value();
cspa::MixerConfig mixer; mixer.set_silence_on_idle(true);
cspa::CaptureRequest req({mic, spk}); req.set_combine_mode(cspa::CombineMode::CspaMixer) .set_mixer_config(mixer);
auto plan = req.inspect(); if (plan.is_ok()) { std::printf("Streams: %llu\n", static_cast<unsigned long long>(plan.value().estimated_active_streams)); } return 0;}#include <cspa_experimental.h>#include <stdio.h>
int main(void) { cspa_capture_target_record targets[2] = {{0}, {0}}; targets[0].struct_size = (uint32_t)sizeof(targets[0]); targets[0].schema_revision = CSPA_EXPERIMENTAL_SCHEMA_REVISION; targets[1].struct_size = (uint32_t)sizeof(targets[1]); targets[1].schema_revision = CSPA_EXPERIMENTAL_SCHEMA_REVISION;
if (cspa_target_microphone(CSPA_DEFAULT_DEVICE_FOLLOW, 1.0f, &targets[0]) != CSPA_STATUS_OK) return 1; if (cspa_target_default_output(CSPA_DEFAULT_DEVICE_FOLLOW, 1.0f, &targets[1]) != CSPA_STATUS_OK) return 1;
cspa_mixer_config_record mixer = {0}; cspa_mixer_config_init(&mixer); mixer.silence_on_idle = 1u;
cspa_capture_request_record req = {0}; cspa_capture_request_init(&req, targets, 2u); req.combine_mode = CSPA_COMBINE_MODE_CSPA_MIXER; req.mixer_config = &mixer;
cspa_plan_report_record report = {0}; report.struct_size = (uint32_t)sizeof(report); report.schema_revision = CSPA_EXPERIMENTAL_SCHEMA_REVISION; if (cspa_request_inspect(&req, &report) == CSPA_STATUS_OK) { printf("Estimated PCM bytes: %llu\n", (unsigned long long)report.estimated_pcm_bytes); } return 0;}Continue to Format Conversion, Layouts & TPDF Dither.