Skip to main content

A performant audio capture pipeline that encodes raw PCM to Opus, skipping silence.

Project description

pcmflux

PyPI version Docs

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.

Prerequisites

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-dev

Core Features

  • PulseAudio Capture: Captures system audio via PulseAudio using the asynchronous Context/Stream record API with a manually-pumped mainloop.
  • Opus Encoding: Integrates the high-quality, low-latency Opus codec.
  • Silence Detection: Intelligently skips encoding and sending silent audio chunks.
  • Native Audio Header: With omit_audio_header=False (the default), the encoder prepends a 2-byte [0x01, 0x00] header to each chunk natively, so WebSocket transports avoid an extra Python copy. Set it to True for raw Opus (WebRTC/RTP).
  • Optional RED redundancy (RFC 2198): red_distance (0–4, default 0) prepends redundant copies of recent Opus payloads for lossy/unreliable transports; 0 disables it (the default for reliable WebSocket/TCP).
  • Zero-copy Frames: Each callback receives a native AudioFrame that owns the encoded chunk and supports the buffer protocol — bytes(frame) / memoryview(frame) / len(frame) — on every supported Python version (3.9–3.14). memoryview(frame) aliases the buffer with no copy, and the frame keeps it alive until every view is released, so the hand-off is memory-safe.
  • Tunable Capture: Configurable latency_ms, validated frame_duration_ms (2.5/5/10/20/40/60 ms, default 20), VBR/CBR, and a toggleable silence gate.
  • Multichannel Opus: Mono, stereo, and 5.1 / 7.1 surround (via the Opus multistream API with Chromium-compatible channel layouts); channels accepts 1, 2, 6, or 8.
  • Mic-Uplink Playback: An AudioPlayback class decodes an inbound Opus stream (with optional RED recovery via write_red) and plays it into a PulseAudio sink — the reverse of capture, for client microphone audio. Playback is mono/stereo.
  • Live Bitrate Updates: Thread-safe update_audio_bitrate() adjusts the Opus bitrate during an active session.
  • PyO3 Extension Module: A native Rust pcmflux extension module (full CPython API, not Limited/abi3) provides PulseAudio capture + Opus encoding.
  • Python Build System: Uses setuptools-rust to build and package the pcmflux PyO3 extension.

Usage

AudioCapture.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()

API notes

  • update_audio_bitrate(bps) stores the new Opus bitrate atomically; the capture thread re-reads it on the next frame, so it only takes effect during an active capture session. Calling it while no capture is active is not an error — it is a silent no-op store, but the value does not persist into the next session: the next start_capture(settings, ...) snapshots the passed settings object and re-seeds the atomic bitrate mirror from settings.opus_bitrate. To change the bitrate for a new session, set settings.opus_bitrate before start_capture(); use update_audio_bitrate() only to adjust a session that is already running.

Example Usage

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:

  1. Install the module: pip3 install .
  2. Run the server: cd example && python3 audio_to_browser.py
  3. Open http://localhost:9001 in a modern web browser (Chrome, Edge, etc.).

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pcmflux-2.0.0-cp314-cp314-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

pcmflux-2.0.0-cp314-cp314-musllinux_1_2_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ ARM64

pcmflux-2.0.0-cp314-cp314-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64

pcmflux-2.0.0-cp314-cp314-manylinux_2_28_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ ARM64

pcmflux-2.0.0-cp313-cp313-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

pcmflux-2.0.0-cp313-cp313-musllinux_1_2_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ ARM64

pcmflux-2.0.0-cp313-cp313-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

pcmflux-2.0.0-cp313-cp313-manylinux_2_28_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ ARM64

pcmflux-2.0.0-cp312-cp312-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

pcmflux-2.0.0-cp312-cp312-musllinux_1_2_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ ARM64

pcmflux-2.0.0-cp312-cp312-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

pcmflux-2.0.0-cp312-cp312-manylinux_2_28_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

pcmflux-2.0.0-cp311-cp311-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

pcmflux-2.0.0-cp311-cp311-musllinux_1_2_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ ARM64

pcmflux-2.0.0-cp311-cp311-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

pcmflux-2.0.0-cp311-cp311-manylinux_2_28_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

pcmflux-2.0.0-cp310-cp310-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

pcmflux-2.0.0-cp310-cp310-musllinux_1_2_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ ARM64

pcmflux-2.0.0-cp310-cp310-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

pcmflux-2.0.0-cp310-cp310-manylinux_2_28_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

pcmflux-2.0.0-cp39-cp39-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

pcmflux-2.0.0-cp39-cp39-musllinux_1_2_aarch64.whl (1.9 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ ARM64

pcmflux-2.0.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

pcmflux-2.0.0-cp39-cp39-manylinux_2_28_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

File details

Details for the file pcmflux-2.0.0-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 64fa58c7004c9719b87342fab273b50a618cf39d8021455d3df0f124d2e61d0f
MD5 324fa92ba3b6a199bdbfb9a1b67133ab
BLAKE2b-256 12b058a9201f2110400ce6a3ed5974484acad536632e61870d387de518616bad

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp314-cp314-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp314-cp314-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 09ff0496f5e1f46b7f092cf07b3ccf7be27ef4c1d910ec29e6420f7094576e91
MD5 890a6a86222dcc297d5f825f4ccaecb3
BLAKE2b-256 7a95724be40b85fe8ee64381211cbf64c09dd7668e1a81747eda383c17f3f54c

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 050a8c7d7c099ee363279f085cab65e5f9891dc6ab9481a3edf64a901d276352
MD5 74eec07c969d57200569e26d2843d01c
BLAKE2b-256 fd6999bb9d02fe925a0f38d9359e0761a9d38d831c293eba71654f3d29f13acf

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp314-cp314-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp314-cp314-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d19c1f2c9773eb3e185f92e34ff331cfb5d2546d07494332f32e8aaa8f41f647
MD5 cfc424cfdd736578dae65c8c30911d97
BLAKE2b-256 7828a4209023b9d3420814e3d79dc179ef11eddf89b15bc1a1b15eaeb473967f

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dc37be36026f578d7068b2ea68d39a1484051619966c608c6db29b7edc512a1a
MD5 95841d070db3167e4be13453d5254eac
BLAKE2b-256 a925653ca49b0eefde185a417e80f333a61d319cc338e425bf93327c02f89b48

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp313-cp313-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp313-cp313-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8195667a366f5a76ec6cfcc096e564ae3ce77bee17c20f89f858a42c8f9673ac
MD5 7afd848bfc0e8ec226bd7a9c853fbd6f
BLAKE2b-256 138fcfc355979d3b573050f31fb3f24409964df7c7450c4718dfe681bb9a1131

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 28ec2b33327085b00d9c5773e8093b85256837bca03257b167869c9ab4584ab1
MD5 f5f13171e8ba94bbd440343835a67731
BLAKE2b-256 6de5ab80cd4d0b111bcda3cfe211a2f84b961dabaea8e1c9fb8e9118ca533e89

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp313-cp313-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp313-cp313-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c092419a228ba92058b2b1982ca6190006ea8fe207463dcdfcf6a5632c57bd4d
MD5 ede69e1b76b38926e34eaa2a8ca8eca2
BLAKE2b-256 0f9be4bf1d7da63951e3ca5c9dc57a6e0e41e0275b4451d55d7bb63bfe78b39b

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3b73e9bce2236a86bdf1135f3b1fbc23642e04094b87b51ffaf3de4de0460dba
MD5 06cdd6e4b3fa04027a689e73bf87ea7c
BLAKE2b-256 b5e26af30d83dd1d8532f547d60e8339f45770dd648892dd9d49a09dc87f8269

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp312-cp312-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 c49abe0f3ee9387dc40ca52ad96bcee7e24b7b47d978f8afb98dc9193e180252
MD5 1a0d7608014d6d2013ba8690fe923ca2
BLAKE2b-256 f226164efd38f0f1280c06ed5523422735510ae0bc44559c6a3dde49381d2726

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 97fa0a0a8bd21defb747cf6dfb1d0b68c5e1900347b23273456f121a2c184c65
MD5 715c7f4d9e65785f7bcfcb6f795dd991
BLAKE2b-256 61ca4b00cfa09f9c50adbc67882485fcea05cf3faf3d7ae2db37480d45d1af82

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp312-cp312-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4f8d6a245d51c9523a4420ccdc3549f2b77b8ec88e6ca16d058a1c01c5d4f77f
MD5 b42ffff1e04129d4ef8f37072f19d63e
BLAKE2b-256 5cac86cc0ec075eaae8eec057d1ad391c53bd3c600fd069cc9f0f5b08b868865

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d59705ad248eb3ba2d9b3a0eb996ddde6bacae8a75cd98b15e8db6f04307635c
MD5 5033df352ac7f118ce7eb08e105fbba9
BLAKE2b-256 2ae607f0b9acf87e2432461d7e332d01721245aae0341541cfd328aa7116e53f

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp311-cp311-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp311-cp311-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5acbc186b6f9de51fc494376016f27ece6a0791e93ec73226df0dcf72ba915ed
MD5 c758f3b2c91dc70a846840e1c2e19575
BLAKE2b-256 4fb1de32722ceceef3919d56f0fabf222f94208ea14900dc03292b7fb14b306e

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4f29542c345f9bec05987e8cf9be9d794d7660843a837f136ddd21078bb5d6ea
MD5 7d5c2cf717f9ef4ae71beba94ea698a8
BLAKE2b-256 88bd9ef2bfbfb6f2dc334df395686af5a305b8c42ea46b4c64351d89588ba5ad

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 226b322969dbae80e41e484631c2a0227cfd68ae6b899a45eb7a9758ebf8b879
MD5 5c1aeaa4f9b631423d4734b1a665ff10
BLAKE2b-256 d37b322c61ccd78e7c4f4bafa4dcc0aba07cb4645a7ec1b2089a0d956e61d428

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e14a364a05a404dc5a7bc59b8c48b6e706c104e77bd322a5b27fbfa8061fd703
MD5 1f474dc41ca7c53c34769bc1890c0bcd
BLAKE2b-256 17e110b9d4ce3807d89c7e6cf7fb3573ec89be1dfc1ed7cbfe3fcf9c0c8d2033

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp310-cp310-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp310-cp310-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 0cc99cdd155f242fbbdf5e16c34a89e2dc5cebb6af1eb3f5e5020cf622de640b
MD5 3681547ab86e2f895efdd297b2c19797
BLAKE2b-256 2916c4e92380f57e4cfe56c6dc294e8f16b06022f7901ec93ab051b308a831d3

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 26de737c0ac296a872bbc159486968079fc11f53c1df4ccc6b188b4305d69b05
MD5 3256b959e43f91abfa43c09c8f982592
BLAKE2b-256 4c572d16d5ae1177df1bf47bc78446dbd54ce2832730634c9d4b2319a1cad94e

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp310-cp310-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0a68c2c23f41eeaad24893f2d0af0200f665a6da3ba84de01e8b4a23a64f678f
MD5 e5dee80114361d6ff893dab73dc997dc
BLAKE2b-256 6bb4cffb11e9f35819d1724a163638a07c161210a8b50fa500fb5a7a84409e05

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 759b71f940c746e123530c8324a8895b8cdf016e363ae3fa1913c4175d11ea96
MD5 ba5650e8016863b112dd77e1b0690366
BLAKE2b-256 21c64aa6ecedd7b18d158264c891b55351f07e862b0feadc72b446f57ee7f4bc

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp39-cp39-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp39-cp39-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 3a03506f5bc1d212cd9384397ce0cf52bd7c5fb118511d7783f33b88fdc38e06
MD5 84ec08f57482ae9e929272ccf4793d71
BLAKE2b-256 d8b56b42ad9daf35b23d17514b320e6a01e942477914a40e0ee2821852f32dc9

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4c068c5a8749afe2f118c4b0757016c5120fed228b15a4b95ca527ba1a1a0749
MD5 b92513a0ba859ac1e0e5f2df24227ace
BLAKE2b-256 742b511b93376bc3e1befe2b22442fe66df372ad60e4f198fd5f9e72c4995ad1

See more details on using hashes here.

File details

Details for the file pcmflux-2.0.0-cp39-cp39-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pcmflux-2.0.0-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1d607beafa2cf3fefbe5de8034fdc4b94b7922fd3b27c8159736974ac42f1b96
MD5 348ab084a79cd930db60825b96656cac
BLAKE2b-256 ae648bedfcc94a1f045d72b6796940e79f13df715d54039cf2b9810e3dd8e5a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page