Skip to content

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.

Four-Layer Real-Time Capture, Mixing & Delivery Pipeline


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):

  1. NativeKernelCapture: Single OS kernel stream captures the entire process tree directly (Windows WASAPI process-tree loopback).
  2. NativeGraphMix: CaptureManager maintains dynamic process membership on a single native audio-server graph node (Linux PipeWire support.null-audio-sink passive port links; macOS CoreAudio CATapDescription process-object list).
  3. SoftwareStreamMix: CaptureManager opens per-process/per-sink-input streams (e.g., Linux PulseAudio pa_stream_set_monitor_stream) and combines them in CspaMixer.

Lowering fallback_limit (or setting None in Python/Rust) opts out of software mixing when you require strictly native OS capture!

  • Auto (Default):
    • Single target with unity gain (gain == 1.0) and no OutputConverter →\rightarrow PassThrough (0 extra copies, 0 mixer latency).
    • Multiple PipeWire or CoreAudio tap targets combinable on one native graph →\rightarrow SharedNativeGraphMix.
    • Heterogeneous targets ([microphone(), find_app("Zoom")], cross-backend [ScreenCaptureKit + CoreAudio], [PipeWire + PulseAudio], non-unity gain, or OutputConverter) →\rightarrow CspaMixer.
  • NativeOnly: Requires PassThrough or SharedNativeGraphMix; fails explicitly if software mixing would be needed.
  • CspaMixer (CSPA_MIXER in Python/C11 wire enums): Forces software mixing even for targets that could share a native graph (useful when you want per-target static gain, InputStatusHandle telemetry, or SoftwareSummation duplicate-target summing).

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 (2x amplitude for an exact duplicate).
  • NativeSetUnion (SharedNativeGraphMix): Deduplicates native PipeWire port links or Core Audio AudioObjectIDs 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:

  1. Order-Independent Working Format: Promotes to max(input_rates) (e.g. 48000 Hz) + Stereo F32 regardless of target list order.
  2. Preallocated InputNormalizer + FrameReblocker:
    • When an input already matches the mixer’s working rate and stereo F32 layout, normalization enters BYPASS (is_identity_bypass == true).
    • Otherwise (InputConversionPolicy::AutoWhenMixing, or rejected if StrictNoResample), a preallocated 1025x64 4-term Blackman-Harris polyphase windowed-sinc resampler (122.9 dB stopband attenuation, exact integer modulo P/Q phase tracking) + ring FrameReblocker slices normalized audio into exact 20 ms (alignment_window_ms = 20) blocks (SYNC).
  3. Clock-Domain-Gated Slip/Repeat Drift Compensation:
    • Each backend stamps Stamp.clock_domain. When independent hardware clocks drift beyond drift_threshold_ms (5 ms default, 2 ms hysteresis, 500 ms hold), CspaMixer performs a 64-frame linear crossfade slip/repeat adjustment (drift_slips counter) without pitch-warping or audible clicks.
  4. Non-Finite Sample Sanitization & Idle Parking:
    • Sanitizes any NaN or ±Inf float sample from misbehaving drivers to 0.0 (nonfinite_samples counter), leaves F32 headroom unclamped by default (clamp_f32 = false), and either parks cleanly when all inputs are silent or synthesizes continuous silent blocks (synthesized_silence = true) when silence_on_idle = true.

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 (SharedNativeGraphMix in-place): Updates PipeWire passive links or macOS CATapDescription process lists in place (0 ms mixer buffering).
  • Tier 2 (CspaMixer differential reconciliation): Keeps unchanged input streams, normalizer phases, and drift state running untouched while opening only newly added targets and draining removed ones (0 heap 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)

Continue to Format Conversion, Layouts & TPDF Dither.