Skip to content

PyWebRTC Audio

PyWebRTC Audio provides Python bindings for the WebRTC audio processing module: echo cancellation, noise suppression, automatic gain control, voice activity detection, and high-pass filtering - the same algorithms that run in Chrome, Edge, and every WebRTC-based application, exposed as a Python library via pybind11.

from pywebrtc_audio import AudioProcessor
ap = AudioProcessor(
sample_rate=16000,
noise_suppression=True,
echo_cancellation=True,
auto_gain_control=True,
stream_delay_ms=40,
)
# near = what the mic picked up (speech + echo + noise)
# far = what you played through the speaker (reference signal)
# accepts int16 or float32 numpy arrays, returns the same dtype
clean = ap.process(near, far)
print(ap.speech_probability) # 0.0-1.0
print(ap.gain_db) # current AGC gain in dB
Terminal window
pip install pywebrtc-audio

Pre-built wheels ship for Linux (x86_64, aarch64), macOS (x86_64, arm64), and Windows (x86_64), for Python 3.10-3.14.

When an AI agent speaks through a speaker and listens through a mic on the same device, it hears its own output as echo. Echo cancellation removes the agent’s voice from the mic capture so it only hears the user. The library ships a working Strands BidiAgent integration (examples/strands_agents_bidi.py) with live echo cancellation.

Other use cases: speech-to-text preprocessing (cleaner mic audio lowers word error rates without model changes), telephony / VoIP, turn-taking via voice activity detection, and robotics (motor noise and reverberant rooms make the echo problem worse).

All processing runs in C++ with the GIL released. At 16kHz mono on an Apple M3 Pro, the full pipeline (AEC + NS + AGC) runs at ~154x real-time - processing one second of audio in ~7ms. Even at 48kHz stereo with all features enabled it holds 82x real-time.

Five classes, each with a process() method that accepts int16 or float32 numpy arrays of any length:

  • AudioProcessor - the combined pipeline (HP filter, AEC, NS, AGC in one pass).
  • EchoCanceller - AEC3 echo cancellation.
  • NoiseSuppressor - spectral noise suppression with speech probability.
  • GainController - AGC2 automatic gain control.
  • VoiceDetector - voice activity detection (speech probability only).

Instances are not thread-safe; use one per thread or synchronize externally.