| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
pcmflux is a high-performance audio capture and encoding module for Python.
It is designed to capture system audio using PulseAudio, encode it into the Opus format, and stream it with low latency. A key optimization is its ability to detect and discard silent audio chunks, significantly reducing network traffic and CPU usage during periods of no sound.
This package builds a native Rust extension (via setuptools-rust/PyO3). It requires a Rust toolchain (cargo/rustc) plus the development headers for PulseAudio and Opus on your system.
On Debian/Ubuntu, you can install them with:
sudo apt-get install libpulse-dev libopus-devAudioCapture.start_capture(settings, callback) spawns a capture thread and invokes callback(frame) once per encoded chunk. When the silence gate is on (use_silence_gate=True, the default), silent chunks are dropped before encoding and the callback is simply not called for them — it never receives an empty frame, so there's no silence to filter out. The frame is a zero-copy AudioFrame (buffer protocol + a .pts presentation timestamp in samples). Copy it out with bytes(frame) if it must outlive the callback, or pass memoryview(frame) for a zero-copy hand-off (keep the frame referenced for the duration of the send so its buffer stays alive).
from pcmflux import AudioCapture, AudioCaptureSettings
def on_chunk(frame):
# Silence-gated chunks are never delivered (the callback is skipped for
# them), so there's no empty/"silence" frame to filter out here.
data = bytes(frame) # copy out (header+Opus, or raw Opus per settings)
pts = frame.pts # presentation timestamp, in samples
# send `data` to your client...
settings = AudioCaptureSettings()
settings.device_name = None # None / "" => system default source
settings.frame_duration_ms = 20 # one of 2.5/5/10/20/40/60
capture = AudioCapture()
capture.start_capture(settings, on_chunk)
# capture.update_audio_bitrate(96000) # adjust the Opus bitrate while running
# ...
capture.stop_capture()The example directory contains a standalone demo that captures system audio, broadcasts it over a WebSocket, and plays it back in a web browser using the WebCodecs API.
To run the example:
The example client (index.html) strips the 2-byte [0x01, 0x00] header before decoding, and its FRAME_DURATION_US constant must match the server's frame_duration_ms (the value is not announced over the wire).
| Back | FazBrowse Home | New Git URL |