capture-system-or-process-audio 1.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. capture_system_or_process_audio-1.3.1/PKG-INFO +240 -0
  2. capture_system_or_process_audio-1.3.1/README.md +207 -0
  3. capture_system_or_process_audio-1.3.1/THIRD_PARTY_NOTICES.md +12 -0
  4. capture_system_or_process_audio-1.3.1/capture/Cargo.lock +218 -0
  5. capture_system_or_process_audio-1.3.1/capture/Cargo.toml +54 -0
  6. capture_system_or_process_audio-1.3.1/capture/README.md +218 -0
  7. capture_system_or_process_audio-1.3.1/capture/THIRD_PARTY_NOTICES.md +12 -0
  8. capture_system_or_process_audio-1.3.1/capture/src/lib.rs +257 -0
  9. capture_system_or_process_audio-1.3.1/capture/tests/cross_platform_api.rs +266 -0
  10. capture_system_or_process_audio-1.3.1/capture-core/Cargo.lock +23 -0
  11. capture_system_or_process_audio-1.3.1/capture-core/Cargo.toml +36 -0
  12. capture_system_or_process_audio-1.3.1/capture-core/README.md +20 -0
  13. capture_system_or_process_audio-1.3.1/capture-core/THIRD_PARTY_NOTICES.md +13 -0
  14. capture_system_or_process_audio-1.3.1/capture-core/src/converter.rs +1212 -0
  15. capture_system_or_process_audio-1.3.1/capture-core/src/discovery.rs +3079 -0
  16. capture_system_or_process_audio-1.3.1/capture-core/src/lib.rs +74 -0
  17. capture_system_or_process_audio-1.3.1/capture-core/src/lifecycle.rs +215 -0
  18. capture_system_or_process_audio-1.3.1/capture-core/src/manager.rs +2051 -0
  19. capture_system_or_process_audio-1.3.1/capture-core/src/measurements.rs +191 -0
  20. capture_system_or_process_audio-1.3.1/capture-core/src/mixer.rs +1084 -0
  21. capture_system_or_process_audio-1.3.1/capture-core/src/ordering.rs +1293 -0
  22. capture_system_or_process_audio-1.3.1/capture-core/src/planner.rs +1132 -0
  23. capture_system_or_process_audio-1.3.1/capture-core/src/reads.rs +723 -0
  24. capture_system_or_process_audio-1.3.1/capture-core/src/routing.rs +150 -0
  25. capture_system_or_process_audio-1.3.1/capture-core/src/schema.rs +1350 -0
  26. capture_system_or_process_audio-1.3.1/capture-core/src/storage.rs +269 -0
  27. capture_system_or_process_audio-1.3.1/capture-core/src/stream_runtime.rs +269 -0
  28. capture_system_or_process_audio-1.3.1/capture-core/src/traits.rs +1916 -0
  29. capture_system_or_process_audio-1.3.1/capture-core/tests/conformance.rs +3371 -0
  30. capture_system_or_process_audio-1.3.1/capture-core/tests/support/mod.rs +95 -0
  31. capture_system_or_process_audio-1.3.1/capture-core/tests/support/oracle.rs +189 -0
  32. capture_system_or_process_audio-1.3.1/capture-core/tests/support/representation.rs +400 -0
  33. capture_system_or_process_audio-1.3.1/capture-core/tests/support/synthetic.rs +113 -0
  34. capture_system_or_process_audio-1.3.1/capture-linux/Cargo.lock +149 -0
  35. capture_system_or_process_audio-1.3.1/capture-linux/Cargo.toml +40 -0
  36. capture_system_or_process_audio-1.3.1/capture-linux/README.md +19 -0
  37. capture_system_or_process_audio-1.3.1/capture-linux/THIRD_PARTY_NOTICES.md +14 -0
  38. capture_system_or_process_audio-1.3.1/capture-linux/src/capabilities.rs +220 -0
  39. capture_system_or_process_audio-1.3.1/capture-linux/src/discovery.rs +2534 -0
  40. capture_system_or_process_audio-1.3.1/capture-linux/src/lib.rs +42 -0
  41. capture_system_or_process_audio-1.3.1/capture-linux/src/loader.rs +1604 -0
  42. capture_system_or_process_audio-1.3.1/capture-linux/src/manager.rs +3157 -0
  43. capture_system_or_process_audio-1.3.1/capture-linux/src/permissions.rs +214 -0
  44. capture_system_or_process_audio-1.3.1/capture-linux/src/pipewire_stream.rs +1687 -0
  45. capture_system_or_process_audio-1.3.1/capture-linux/src/procfs.rs +544 -0
  46. capture_system_or_process_audio-1.3.1/capture-linux/src/pulse_stream.rs +1067 -0
  47. capture_system_or_process_audio-1.3.1/capture-macos/Cargo.lock +126 -0
  48. capture_system_or_process_audio-1.3.1/capture-macos/Cargo.toml +38 -0
  49. capture_system_or_process_audio-1.3.1/capture-macos/README.md +20 -0
  50. capture_system_or_process_audio-1.3.1/capture-macos/THIRD_PARTY_NOTICES.md +16 -0
  51. capture_system_or_process_audio-1.3.1/capture-macos/build.rs +65 -0
  52. capture_system_or_process_audio-1.3.1/capture-macos/src/capabilities.rs +315 -0
  53. capture_system_or_process_audio-1.3.1/capture-macos/src/coreaudio_stream.rs +1544 -0
  54. capture_system_or_process_audio-1.3.1/capture-macos/src/discovery.rs +1023 -0
  55. capture_system_or_process_audio-1.3.1/capture-macos/src/lib.rs +50 -0
  56. capture_system_or_process_audio-1.3.1/capture-macos/src/macos_bridge.m +1436 -0
  57. capture_system_or_process_audio-1.3.1/capture-macos/src/manager.rs +2468 -0
  58. capture_system_or_process_audio-1.3.1/capture-macos/src/native_bridge.rs +824 -0
  59. capture_system_or_process_audio-1.3.1/capture-macos/src/permissions.rs +362 -0
  60. capture_system_or_process_audio-1.3.1/capture-macos/src/proc_info.rs +505 -0
  61. capture_system_or_process_audio-1.3.1/capture-macos/src/sck_stream.rs +750 -0
  62. capture_system_or_process_audio-1.3.1/capture-python/Cargo.lock +447 -0
  63. capture_system_or_process_audio-1.3.1/capture-python/Cargo.toml +43 -0
  64. capture_system_or_process_audio-1.3.1/capture-python/README.md +207 -0
  65. capture_system_or_process_audio-1.3.1/capture-python/THIRD_PARTY_NOTICES.md +12 -0
  66. capture_system_or_process_audio-1.3.1/capture-python/src/enums.rs +599 -0
  67. capture_system_or_process_audio-1.3.1/capture-python/src/lib.rs +3257 -0
  68. capture_system_or_process_audio-1.3.1/capture-python/src/stubgen.rs +244 -0
  69. capture_system_or_process_audio-1.3.1/capture-python/src/synthetic.rs +783 -0
  70. capture_system_or_process_audio-1.3.1/capture-python/src/types.rs +2024 -0
  71. capture_system_or_process_audio-1.3.1/capture-python/tests/test_native_slice.py +1233 -0
  72. capture_system_or_process_audio-1.3.1/capture-windows/Cargo.lock +249 -0
  73. capture_system_or_process_audio-1.3.1/capture-windows/Cargo.toml +41 -0
  74. capture_system_or_process_audio-1.3.1/capture-windows/README.md +25 -0
  75. capture_system_or_process_audio-1.3.1/capture-windows/THIRD_PARTY_NOTICES.md +18 -0
  76. capture_system_or_process_audio-1.3.1/capture-windows/src/capabilities.rs +365 -0
  77. capture_system_or_process_audio-1.3.1/capture-windows/src/discovery.rs +1043 -0
  78. capture_system_or_process_audio-1.3.1/capture-windows/src/endpoint.rs +1343 -0
  79. capture_system_or_process_audio-1.3.1/capture-windows/src/lib.rs +59 -0
  80. capture_system_or_process_audio-1.3.1/capture-windows/src/manager.rs +2638 -0
  81. capture_system_or_process_audio-1.3.1/capture-windows/src/packets.rs +349 -0
  82. capture_system_or_process_audio-1.3.1/capture-windows/src/permissions.rs +379 -0
  83. capture_system_or_process_audio-1.3.1/capture-windows/src/process.rs +326 -0
  84. capture_system_or_process_audio-1.3.1/pyproject.toml +75 -0
  85. capture_system_or_process_audio-1.3.1/python/capture_system_or_process_audio/__init__.py +7 -0
  86. capture_system_or_process_audio-1.3.1/python/capture_system_or_process_audio/__init__.pyi +819 -0
  87. capture_system_or_process_audio-1.3.1/python/capture_system_or_process_audio/__main__.py +313 -0
  88. capture_system_or_process_audio-1.3.1/python/capture_system_or_process_audio/py.typed +0 -0
@@ -0,0 +1,240 @@
1
+ Metadata-Version: 2.4
2
+ Name: capture-system-or-process-audio
3
+ Version: 1.3.1
4
+ Classifier: Operating System :: Microsoft :: Windows :: Windows 10
5
+ Classifier: Operating System :: Microsoft :: Windows :: Windows 11
6
+ Classifier: Operating System :: POSIX :: Linux
7
+ Classifier: Operating System :: MacOS :: MacOS X
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Classifier: Programming Language :: Python :: 3.15
14
+ Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
15
+ Classifier: Programming Language :: Rust
16
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Capture/Recording
17
+ Classifier: Typing :: Typed
18
+ Requires-Dist: numpy>=2.4.6 ; python_full_version == '3.11.*' and extra == 'numpy'
19
+ Requires-Dist: numpy>=2.5.5 ; python_full_version >= '3.12' and extra == 'numpy'
20
+ Provides-Extra: numpy
21
+ License-File: THIRD_PARTY_NOTICES.md
22
+ Summary: Cross-platform system loopback, per-process/app, and microphone PCM audio capture & live mixer (Windows WASAPI, Linux PipeWire/PulseAudio, macOS Core Audio/ScreenCaptureKit)
23
+ Keywords: audio,audio-capture,loopback,system-audio,process-audio,speaker-recording,microphone,pcm,pyaudio,pyaudiowpatch,sounddevice,wasapi,pipewire,pulseaudio,coreaudio,screencapturekit
24
+ License-Expression: MIT OR Apache-2.0
25
+ Requires-Python: >=3.11
26
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
27
+ Project-URL: Documentation, https://s0d3s.github.io/capture-system-or-process-audio/
28
+ Project-URL: Homepage, https://s0d3s.github.io/capture-system-or-process-audio/
29
+ Project-URL: Issues, https://github.com/s0d3s/capture-system-or-process-audio/issues
30
+ Project-URL: Releases, https://github.com/s0d3s/capture-system-or-process-audio/releases
31
+ Project-URL: Repository, https://github.com/s0d3s/capture-system-or-process-audio
32
+
33
+ <div align="center">
34
+
35
+ <img src="https://raw.githubusercontent.com/s0d3s/capture-system-or-process-audio/main/assets/repo_banner.svg" alt="capture-system-or-process-audio — Cross-platform system speaker loopback, single application/process, and microphone PCM audio capture library for Python" width="100%">
36
+
37
+ <h1 align="center">capture-system-or-process-audio</h1>
38
+
39
+ <p>
40
+ <b>Capture system speakers, isolate any single app/process, or mix microphones into one live PCM stream across Windows, Linux &amp; macOS.</b><br>
41
+ <sub>Native WASAPI · PipeWire · PulseAudio · Core Audio Process Taps · ScreenCaptureKit — zero virtual audio cables, zero silent fallbacks.</sub>
42
+ </p>
43
+
44
+ <p>
45
+ <i>Created by <a href="https://github.com/s0d3s">@s0d3s</a> — author of <a href="https://github.com/s0d3s/PyAudioWPatch"><b>PyAudioWPatch</b></a> (140k+ monthly PyPI downloads), rebuilt from scratch in safe Rust + PyO3.</i>
46
+ </p>
47
+
48
+ <p>
49
+ <a href="https://s0d3s.github.io/capture-system-or-process-audio/sdks/python/"><img src="https://img.shields.io/badge/docs-Python%20Handbook%20%26%20API-22d3ee?logo=readthedocs&logoColor=white" alt="Python SDK Handbook and API Reference"></a>
50
+ <a href="https://pypi.org/project/capture-system-or-process-audio/"><img src="https://img.shields.io/pypi/v/capture-system-or-process-audio?label=PyPI&color=3776AB&logo=pypi&logoColor=white" alt="PyPI Version"></a>
51
+ <a href="https://pypi.org/project/capture-system-or-process-audio/"><img src="https://img.shields.io/badge/Python-3.11–3.15%20%7C%203.14t–3.15t-3776AB?logo=python&logoColor=ffdd54" alt="Supported Python Versions: 3.11 through 3.15 and Free-Threaded 3.14t/3.15t"></a>
52
+ <a href="https://github.com/s0d3s/capture-system-or-process-audio#platform--backend-matrix"><img src="https://img.shields.io/badge/OS-Windows%20%7C%20Linux%20%7C%20macOS-00bcd4" alt="Supported Platforms: Windows 10/11, Linux, macOS"></a>
53
+ <a href="https://github.com/s0d3s/capture-system-or-process-audio/blob/main/LICENSE-MIT"><img src="https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-2ecc71" alt="License: MIT OR Apache-2.0"></a>
54
+ </p>
55
+
56
+ </div>
57
+
58
+ ---
59
+
60
+ ## Installation
61
+
62
+ Prebuilt binary wheels (`cp311-abi3`, `cp314-cp314t`, and PEP 803 `cp315-abi3.abi3t`) ship for **Windows (`x64`, `ARM64`)**, **Linux (`x86_64`, `aarch64` `manylinux_2_28`)**, and **Apple Silicon macOS (`arm64`)** — **no Rust or C compiler required**.
63
+
64
+ ```bash
65
+ pip install capture-system-or-process-audio
66
+ # Optional: zero-copy NumPy ndarray views via block.to_numpy()
67
+ pip install "capture-system-or-process-audio[numpy]"
68
+ # Inspect active OS backends, permissions, and live CaptureTarget tables:
69
+ python -m capture_system_or_process_audio
70
+ ```
71
+
72
+ - **GIL & Free-Threaded Python Ready:** Certified across CPython `3.11`–`3.15` plus Free-Threaded `3.14t` & `3.15t` (`-Xgil=0`, `Py_MOD_GIL_NOT_USED` — keeps `sys._is_gil_enabled() == False` without fallback warnings).
73
+ - **100% Typed (`py.typed` + `.pyi`) & Runnable (`python -m`):** Full IDE autocomplete and `mypy`/`pyright` type checking (`33` stdlib `enum.Enum` classes accepting `<Enum> | str`) plus a built-in zero-dependency diagnostic CLI (`python -m capture_system_or_process_audio`).
74
+
75
+ ---
76
+
77
+ ## Quick Start Recipes
78
+
79
+ ### 1. Capture System Speaker Output (Loopback)
80
+
81
+ ```python
82
+ import capture_system_or_process_audio as cspa
83
+
84
+ # Follows OS default speaker/headphone switches automatically (or pass default_device_mode="pin"):
85
+ with cspa.capture(cspa.default_output()) as stream:
86
+ for outcome in stream:
87
+ for block in outcome.blocks:
88
+ pcm_bytes = memoryview(block) # Zero-copy read-only PEP 3118 buffer
89
+ print(f"{block.frames} frames @ {block.rate} Hz, {block.channels} ch ({block.sample_format.value}), {len(pcm_bytes)} B")
90
+ break
91
+ ```
92
+
93
+ ### 2. List, Filter & Capture `CaptureTarget`s Deterministically
94
+
95
+ `cspa.find_app("Name")` is a quick single-match shortcut, but it raises an error when multiple instances run (for example, multiple browser profiles or Python/Java workers) or when display names vary across platforms. Enumerating `cspa.discover()` lets you inspect every available `CaptureTarget` (`applications`, `processes`, `output_devices`, `input_devices`), filter deterministically on `active_audio`, `pid`, `executable_path`, `app_id`, or `device_id`, refine with `.with_gain(...)` / `.with_backend(...)`, and pass the exact target(s) to `cspa.capture(...)`:
96
+
97
+ ```python
98
+ import os
99
+ import capture_system_or_process_audio as cspa
100
+
101
+ catalog = cspa.discover(active_only=False)
102
+
103
+ for t in catalog.targets:
104
+ print(f"{t.kind.value:12} name={t.name!r} pid={t.pid} active={t.active_audio} id={t.logical_id}")
105
+
106
+ # Select active audio-producing applications (excluding our own PID), or fall back to default speakers:
107
+ my_pid = os.getpid()
108
+ playing_apps = [
109
+ app.with_gain(0.9)
110
+ for app in catalog.applications
111
+ if app.active_audio is True and app.pid != my_pid
112
+ ]
113
+ selected = playing_apps[:2] or [cspa.default_output()]
114
+
115
+ with cspa.capture(selected, silence_on_idle=True) as stream:
116
+ outcome = stream.read(frames=960, timeout_ms=250)
117
+ print(f"Captured {outcome.frames} frames across {len(stream.targets)} target(s)")
118
+ ```
119
+
120
+ ### 3. Single-Match App Shortcut (`find_app`) or Explicit PID (`process`)
121
+
122
+ When you already know a unique application name or exact OS PID:
123
+
124
+ ```python
125
+ import capture_system_or_process_audio as cspa
126
+
127
+ # Roll up child audio helpers automatically (e.g. browser/Electron/game audio workers):
128
+ spotify = cspa.find_app("Spotify") # Or cspa.process(1234, scope="tree")
129
+
130
+ with cspa.capture(spotify) as stream:
131
+ outcome = stream.read(frames=960, timeout_ms=250)
132
+ for block in outcome.blocks:
133
+ raw_bytes = block.to_bytes()
134
+ print("Captured isolated app bytes:", len(raw_bytes))
135
+ ```
136
+
137
+ ### 4. Mix Microphone + Meeting App at `16 kHz` Mono for Whisper / STT
138
+
139
+ ```python
140
+ import capture_system_or_process_audio as cspa
141
+
142
+ converter = cspa.OutputConverter(rate=16000, channels=1, sample_format="f32")
143
+
144
+ with cspa.capture(
145
+ [cspa.microphone(), cspa.find_app("Zoom")],
146
+ output_converter=converter,
147
+ silence_on_idle=True, # Keep emitting silent frames when speakers are quiet
148
+ ) as stream:
149
+ outcome = stream.read(frames=1600, timeout_ms=200) # 100 ms @ 16 kHz
150
+ for block in outcome.blocks:
151
+ samples = block.to_numpy() # 2D C-contiguous ndarray: shape (frames, 1), dtype float32
152
+ ```
153
+
154
+ ### 5. `asyncio`, Zero-Allocation `read_into`, & Live Target Hot-Swap
155
+
156
+ ```python
157
+ import asyncio
158
+ import capture_system_or_process_audio as cspa
159
+
160
+ async def main() -> None:
161
+ # Non-blocking async iteration / read_async:
162
+ async with cspa.capture(cspa.default_output(), silence_on_idle=True) as stream:
163
+ outcome = await stream.read_async(frames=480, timeout_ms=200)
164
+ print("Async frames:", outcome.frames)
165
+
166
+ # Hot-swap capture targets mid-session without losing the stream handle:
167
+ stream.update_targets([cspa.default_output(), cspa.microphone()])
168
+
169
+ # Zero-allocation steady-state read_into (safe on -Xgil=0 via Python critical sections):
170
+ conv = cspa.OutputConverter(rate=48000, channels=2, sample_format="i16")
171
+ with cspa.capture(cspa.default_output(), output_converter=conv, silence_on_idle=True) as sync_stream:
172
+ buf = bytearray(480 * 2 * 2)
173
+ res = sync_stream.read_into(buf, frames=480, timeout_ms=200)
174
+ print("Copied bytes:", res.copy_bytes)
175
+
176
+ asyncio.run(main())
177
+ ```
178
+
179
+ ---
180
+
181
+ ## Migrating From `PyAudioWPatch` & Ecosystem Drop-In `Adapters`
182
+
183
+ [`PyAudioWPatch`](https://github.com/s0d3s/PyAudioWPatch) (created by `@s0d3s`, **140,000+ monthly PyPI downloads** across **340+ open-source repositories**) brought Windows WASAPI loopback to `PyAudio`, but PortAudio's C architecture made the most-requested user features — single-app isolation, macOS/Linux capture, `[Mic + Speaker]` drift sync, silent-speaker hang protection, and `-Xgil=0` free-threading — impossible to implement there. **`capture-system-or-process-audio` is the result of nearly a year of ground-up safe-Rust development and polish** built specifically to solve them:
184
+
185
+ | Common Task | `PyAudioWPatch` (Windows Only) | `capture_system_or_process_audio` (Win · Linux · macOS) |
186
+ | --- | --- | --- |
187
+ | Default speaker loopback | `p.get_default_wasapi_loopback()` | `cspa.default_output()` *(auto-follows device changes!)* |
188
+ | Record one specific app | Not supported | `cspa.find_app("Zoom")` or `cspa.process(pid)` |
189
+ | Prevent hang during speaker silence | Blocks forever on `stream.read()` | Pass `silence_on_idle=True` to `cspa.capture(...)` |
190
+ | Resample to `16 kHz` mono `i16`/`f32` | Manual external resampler | `output_converter=cspa.OutputConverter(rate=16000, channels=1, sample_format="i16")` |
191
+ | Zero-copy NumPy array | `np.frombuffer(data, dtype=...)` | `block.to_numpy(copy=False)` -> `(frames, channels)` |
192
+
193
+ > **Planned for Next Release — Zero-Rewrite Drop-In `Adapters`:** Upcoming compatibility wrappers for **`PyAudio` / `PyAudioWPatch`** (`Stream.read` & `stream_callback`), **`python-sounddevice`** (`InputStream`, `RawInputStream`, `rec`), **`SoundCard`** (`_Recorder`), **`SpeechRecognition`** (`AudioSource`), and **`faster-whisper`** sliding-window streams will let existing Python audio code switch to per-process or cross-platform loopback with a 1-line import swap. Follow progress on [GitHub](https://github.com/s0d3s/capture-system-or-process-audio#adapters-and-nodejs-roadmap).
194
+
195
+ ---
196
+
197
+ ## OS Recording Permissions & Discovery
198
+
199
+ Inspect or request OS recording consent (`Microphone`, `SystemAudio`, `ScreenRecording`) cleanly across Windows, macOS, and Linux:
200
+
201
+ ```python
202
+ import capture_system_or_process_audio as cspa
203
+
204
+ # Side-effect-free preflight (never triggers modal OS popups on CI or background workers):
205
+ report = cspa.check_permissions(cspa.default_output())
206
+ print("System audio state:", report.system_audio.state.value, "granted:", report.system_audio.is_granted)
207
+
208
+ # Enumerate active audio-producing applications, output endpoints, and microphones:
209
+ catalog = cspa.discover(active_only=False)
210
+ for app in catalog.applications:
211
+ print(f"App: {app.name} (pid={app.pid}, backends={[b.value for b in app.available_backends]})")
212
+
213
+ # Preview the exact backend & mixer plan without opening hardware streams:
214
+ plan = cspa.request([cspa.microphone(), cspa.default_output()]).inspect()
215
+ print("Resolution:", plan.combine_resolution.value, "software_mix:", plan.uses_software_mixer)
216
+ ```
217
+
218
+ - Streams use `PermissionPolicy.AUTO_REQUEST` by default on interactive runs; pass `request_permissions=False` (`PermissionPolicy.CHECK_ONLY`) to opt out of OS prompts and raise standard Python `PermissionError` (`OSError`) when denied.
219
+
220
+ ---
221
+
222
+ ## Platform Support Matrix
223
+
224
+ Capabilities differ across operating systems. Unsupported scopes or OS versions raise explicit errors rather than silently falling back to full-system capture:
225
+
226
+ | Platform | Backends (`Backends`) | Supported Scopes & Capabilities | Explicit Rejections / Notes |
227
+ | --- | --- | --- | --- |
228
+ | **Windows 10 / 11** (`x86_64`, `ARM64`) | `[BackendId.WASAPI]` | `default_output` & `microphone` (`follow`/`pin`), `device(id)` & `input_device(id)` (`>= 19041`); `process`/`application` `scope="tree"` (`>= 20348`) and `cspa_mixer`. | `scope="exact"` and uncalibrated `timing="precise"` reject explicitly. |
229
+ | **Linux** (`x86_64`, `aarch64`) | `[BackendId.PIPEWIRE, BackendId.PULSEAUDIO]` (runtime `dlopen` of `libpipewire-0.3.so.0 >= 0.3.65` & `libpulse.so.0 >= 15.0`, `0` compile-time `NEEDED` links) | PipeWire & PulseAudio output endpoints, microphones (`.monitor` sinks excluded), PipeWire `native_graph_mix` fan-in, PulseAudio per-stream `cspa_mixer`, and cross-backend `[pipewire + pulseaudio]` `cspa_mixer`. | `native_kernel_capture` for `process`/`application` and uncalibrated `timing="precise"` reject explicitly. |
230
+ | **macOS** (`Apple Silicon arm64`) | `[BackendId.COREAUDIO, BackendId.SCREENCAPTUREKIT]` | Core Audio Process Taps (`macOS 14.2+`), Core Audio HAL microphone input (`macOS 13.0+`), ScreenCaptureKit `application` (`scope="exact"`, `macOS 13.0+`), and cross-backend `[screencapturekit + coreaudio]` `cspa_mixer`. | Intel macOS (`x86_64`), SCK `scope="tree"`, and uncalibrated `timing="precise"` reject explicitly. |
231
+
232
+ ---
233
+
234
+ ## Links & License
235
+
236
+ - **Official Documentation, Python Handbook & `.pyi` API Reference:** <https://s0d3s.github.io/capture-system-or-process-audio/>
237
+ - **Source, Rust / C / C++ SDKs & Live TUI Demo:** <https://github.com/s0d3s/capture-system-or-process-audio>
238
+ - **Prebuilt Binaries & Releases:** <https://github.com/s0d3s/capture-system-or-process-audio/releases>
239
+ - **License:** Dual-licensed under [MIT](https://github.com/s0d3s/capture-system-or-process-audio/blob/main/LICENSE-MIT) or [Apache-2.0](https://github.com/s0d3s/capture-system-or-process-audio/blob/main/LICENSE-APACHE), at your option.
240
+
@@ -0,0 +1,207 @@
1
+ <div align="center">
2
+
3
+ <img src="https://raw.githubusercontent.com/s0d3s/capture-system-or-process-audio/main/assets/repo_banner.svg" alt="capture-system-or-process-audio — Cross-platform system speaker loopback, single application/process, and microphone PCM audio capture library for Python" width="100%">
4
+
5
+ <h1 align="center">capture-system-or-process-audio</h1>
6
+
7
+ <p>
8
+ <b>Capture system speakers, isolate any single app/process, or mix microphones into one live PCM stream across Windows, Linux &amp; macOS.</b><br>
9
+ <sub>Native WASAPI · PipeWire · PulseAudio · Core Audio Process Taps · ScreenCaptureKit — zero virtual audio cables, zero silent fallbacks.</sub>
10
+ </p>
11
+
12
+ <p>
13
+ <i>Created by <a href="https://github.com/s0d3s">@s0d3s</a> — author of <a href="https://github.com/s0d3s/PyAudioWPatch"><b>PyAudioWPatch</b></a> (140k+ monthly PyPI downloads), rebuilt from scratch in safe Rust + PyO3.</i>
14
+ </p>
15
+
16
+ <p>
17
+ <a href="https://s0d3s.github.io/capture-system-or-process-audio/sdks/python/"><img src="https://img.shields.io/badge/docs-Python%20Handbook%20%26%20API-22d3ee?logo=readthedocs&logoColor=white" alt="Python SDK Handbook and API Reference"></a>
18
+ <a href="https://pypi.org/project/capture-system-or-process-audio/"><img src="https://img.shields.io/pypi/v/capture-system-or-process-audio?label=PyPI&color=3776AB&logo=pypi&logoColor=white" alt="PyPI Version"></a>
19
+ <a href="https://pypi.org/project/capture-system-or-process-audio/"><img src="https://img.shields.io/badge/Python-3.11–3.15%20%7C%203.14t–3.15t-3776AB?logo=python&logoColor=ffdd54" alt="Supported Python Versions: 3.11 through 3.15 and Free-Threaded 3.14t/3.15t"></a>
20
+ <a href="https://github.com/s0d3s/capture-system-or-process-audio#platform--backend-matrix"><img src="https://img.shields.io/badge/OS-Windows%20%7C%20Linux%20%7C%20macOS-00bcd4" alt="Supported Platforms: Windows 10/11, Linux, macOS"></a>
21
+ <a href="https://github.com/s0d3s/capture-system-or-process-audio/blob/main/LICENSE-MIT"><img src="https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-2ecc71" alt="License: MIT OR Apache-2.0"></a>
22
+ </p>
23
+
24
+ </div>
25
+
26
+ ---
27
+
28
+ ## Installation
29
+
30
+ Prebuilt binary wheels (`cp311-abi3`, `cp314-cp314t`, and PEP 803 `cp315-abi3.abi3t`) ship for **Windows (`x64`, `ARM64`)**, **Linux (`x86_64`, `aarch64` `manylinux_2_28`)**, and **Apple Silicon macOS (`arm64`)** — **no Rust or C compiler required**.
31
+
32
+ ```bash
33
+ pip install capture-system-or-process-audio
34
+ # Optional: zero-copy NumPy ndarray views via block.to_numpy()
35
+ pip install "capture-system-or-process-audio[numpy]"
36
+ # Inspect active OS backends, permissions, and live CaptureTarget tables:
37
+ python -m capture_system_or_process_audio
38
+ ```
39
+
40
+ - **GIL & Free-Threaded Python Ready:** Certified across CPython `3.11`–`3.15` plus Free-Threaded `3.14t` & `3.15t` (`-Xgil=0`, `Py_MOD_GIL_NOT_USED` — keeps `sys._is_gil_enabled() == False` without fallback warnings).
41
+ - **100% Typed (`py.typed` + `.pyi`) & Runnable (`python -m`):** Full IDE autocomplete and `mypy`/`pyright` type checking (`33` stdlib `enum.Enum` classes accepting `<Enum> | str`) plus a built-in zero-dependency diagnostic CLI (`python -m capture_system_or_process_audio`).
42
+
43
+ ---
44
+
45
+ ## Quick Start Recipes
46
+
47
+ ### 1. Capture System Speaker Output (Loopback)
48
+
49
+ ```python
50
+ import capture_system_or_process_audio as cspa
51
+
52
+ # Follows OS default speaker/headphone switches automatically (or pass default_device_mode="pin"):
53
+ with cspa.capture(cspa.default_output()) as stream:
54
+ for outcome in stream:
55
+ for block in outcome.blocks:
56
+ pcm_bytes = memoryview(block) # Zero-copy read-only PEP 3118 buffer
57
+ print(f"{block.frames} frames @ {block.rate} Hz, {block.channels} ch ({block.sample_format.value}), {len(pcm_bytes)} B")
58
+ break
59
+ ```
60
+
61
+ ### 2. List, Filter & Capture `CaptureTarget`s Deterministically
62
+
63
+ `cspa.find_app("Name")` is a quick single-match shortcut, but it raises an error when multiple instances run (for example, multiple browser profiles or Python/Java workers) or when display names vary across platforms. Enumerating `cspa.discover()` lets you inspect every available `CaptureTarget` (`applications`, `processes`, `output_devices`, `input_devices`), filter deterministically on `active_audio`, `pid`, `executable_path`, `app_id`, or `device_id`, refine with `.with_gain(...)` / `.with_backend(...)`, and pass the exact target(s) to `cspa.capture(...)`:
64
+
65
+ ```python
66
+ import os
67
+ import capture_system_or_process_audio as cspa
68
+
69
+ catalog = cspa.discover(active_only=False)
70
+
71
+ for t in catalog.targets:
72
+ print(f"{t.kind.value:12} name={t.name!r} pid={t.pid} active={t.active_audio} id={t.logical_id}")
73
+
74
+ # Select active audio-producing applications (excluding our own PID), or fall back to default speakers:
75
+ my_pid = os.getpid()
76
+ playing_apps = [
77
+ app.with_gain(0.9)
78
+ for app in catalog.applications
79
+ if app.active_audio is True and app.pid != my_pid
80
+ ]
81
+ selected = playing_apps[:2] or [cspa.default_output()]
82
+
83
+ with cspa.capture(selected, silence_on_idle=True) as stream:
84
+ outcome = stream.read(frames=960, timeout_ms=250)
85
+ print(f"Captured {outcome.frames} frames across {len(stream.targets)} target(s)")
86
+ ```
87
+
88
+ ### 3. Single-Match App Shortcut (`find_app`) or Explicit PID (`process`)
89
+
90
+ When you already know a unique application name or exact OS PID:
91
+
92
+ ```python
93
+ import capture_system_or_process_audio as cspa
94
+
95
+ # Roll up child audio helpers automatically (e.g. browser/Electron/game audio workers):
96
+ spotify = cspa.find_app("Spotify") # Or cspa.process(1234, scope="tree")
97
+
98
+ with cspa.capture(spotify) as stream:
99
+ outcome = stream.read(frames=960, timeout_ms=250)
100
+ for block in outcome.blocks:
101
+ raw_bytes = block.to_bytes()
102
+ print("Captured isolated app bytes:", len(raw_bytes))
103
+ ```
104
+
105
+ ### 4. Mix Microphone + Meeting App at `16 kHz` Mono for Whisper / STT
106
+
107
+ ```python
108
+ import capture_system_or_process_audio as cspa
109
+
110
+ converter = cspa.OutputConverter(rate=16000, channels=1, sample_format="f32")
111
+
112
+ with cspa.capture(
113
+ [cspa.microphone(), cspa.find_app("Zoom")],
114
+ output_converter=converter,
115
+ silence_on_idle=True, # Keep emitting silent frames when speakers are quiet
116
+ ) as stream:
117
+ outcome = stream.read(frames=1600, timeout_ms=200) # 100 ms @ 16 kHz
118
+ for block in outcome.blocks:
119
+ samples = block.to_numpy() # 2D C-contiguous ndarray: shape (frames, 1), dtype float32
120
+ ```
121
+
122
+ ### 5. `asyncio`, Zero-Allocation `read_into`, & Live Target Hot-Swap
123
+
124
+ ```python
125
+ import asyncio
126
+ import capture_system_or_process_audio as cspa
127
+
128
+ async def main() -> None:
129
+ # Non-blocking async iteration / read_async:
130
+ async with cspa.capture(cspa.default_output(), silence_on_idle=True) as stream:
131
+ outcome = await stream.read_async(frames=480, timeout_ms=200)
132
+ print("Async frames:", outcome.frames)
133
+
134
+ # Hot-swap capture targets mid-session without losing the stream handle:
135
+ stream.update_targets([cspa.default_output(), cspa.microphone()])
136
+
137
+ # Zero-allocation steady-state read_into (safe on -Xgil=0 via Python critical sections):
138
+ conv = cspa.OutputConverter(rate=48000, channels=2, sample_format="i16")
139
+ with cspa.capture(cspa.default_output(), output_converter=conv, silence_on_idle=True) as sync_stream:
140
+ buf = bytearray(480 * 2 * 2)
141
+ res = sync_stream.read_into(buf, frames=480, timeout_ms=200)
142
+ print("Copied bytes:", res.copy_bytes)
143
+
144
+ asyncio.run(main())
145
+ ```
146
+
147
+ ---
148
+
149
+ ## Migrating From `PyAudioWPatch` & Ecosystem Drop-In `Adapters`
150
+
151
+ [`PyAudioWPatch`](https://github.com/s0d3s/PyAudioWPatch) (created by `@s0d3s`, **140,000+ monthly PyPI downloads** across **340+ open-source repositories**) brought Windows WASAPI loopback to `PyAudio`, but PortAudio's C architecture made the most-requested user features — single-app isolation, macOS/Linux capture, `[Mic + Speaker]` drift sync, silent-speaker hang protection, and `-Xgil=0` free-threading — impossible to implement there. **`capture-system-or-process-audio` is the result of nearly a year of ground-up safe-Rust development and polish** built specifically to solve them:
152
+
153
+ | Common Task | `PyAudioWPatch` (Windows Only) | `capture_system_or_process_audio` (Win · Linux · macOS) |
154
+ | --- | --- | --- |
155
+ | Default speaker loopback | `p.get_default_wasapi_loopback()` | `cspa.default_output()` *(auto-follows device changes!)* |
156
+ | Record one specific app | Not supported | `cspa.find_app("Zoom")` or `cspa.process(pid)` |
157
+ | Prevent hang during speaker silence | Blocks forever on `stream.read()` | Pass `silence_on_idle=True` to `cspa.capture(...)` |
158
+ | Resample to `16 kHz` mono `i16`/`f32` | Manual external resampler | `output_converter=cspa.OutputConverter(rate=16000, channels=1, sample_format="i16")` |
159
+ | Zero-copy NumPy array | `np.frombuffer(data, dtype=...)` | `block.to_numpy(copy=False)` -> `(frames, channels)` |
160
+
161
+ > **Planned for Next Release — Zero-Rewrite Drop-In `Adapters`:** Upcoming compatibility wrappers for **`PyAudio` / `PyAudioWPatch`** (`Stream.read` & `stream_callback`), **`python-sounddevice`** (`InputStream`, `RawInputStream`, `rec`), **`SoundCard`** (`_Recorder`), **`SpeechRecognition`** (`AudioSource`), and **`faster-whisper`** sliding-window streams will let existing Python audio code switch to per-process or cross-platform loopback with a 1-line import swap. Follow progress on [GitHub](https://github.com/s0d3s/capture-system-or-process-audio#adapters-and-nodejs-roadmap).
162
+
163
+ ---
164
+
165
+ ## OS Recording Permissions & Discovery
166
+
167
+ Inspect or request OS recording consent (`Microphone`, `SystemAudio`, `ScreenRecording`) cleanly across Windows, macOS, and Linux:
168
+
169
+ ```python
170
+ import capture_system_or_process_audio as cspa
171
+
172
+ # Side-effect-free preflight (never triggers modal OS popups on CI or background workers):
173
+ report = cspa.check_permissions(cspa.default_output())
174
+ print("System audio state:", report.system_audio.state.value, "granted:", report.system_audio.is_granted)
175
+
176
+ # Enumerate active audio-producing applications, output endpoints, and microphones:
177
+ catalog = cspa.discover(active_only=False)
178
+ for app in catalog.applications:
179
+ print(f"App: {app.name} (pid={app.pid}, backends={[b.value for b in app.available_backends]})")
180
+
181
+ # Preview the exact backend & mixer plan without opening hardware streams:
182
+ plan = cspa.request([cspa.microphone(), cspa.default_output()]).inspect()
183
+ print("Resolution:", plan.combine_resolution.value, "software_mix:", plan.uses_software_mixer)
184
+ ```
185
+
186
+ - Streams use `PermissionPolicy.AUTO_REQUEST` by default on interactive runs; pass `request_permissions=False` (`PermissionPolicy.CHECK_ONLY`) to opt out of OS prompts and raise standard Python `PermissionError` (`OSError`) when denied.
187
+
188
+ ---
189
+
190
+ ## Platform Support Matrix
191
+
192
+ Capabilities differ across operating systems. Unsupported scopes or OS versions raise explicit errors rather than silently falling back to full-system capture:
193
+
194
+ | Platform | Backends (`Backends`) | Supported Scopes & Capabilities | Explicit Rejections / Notes |
195
+ | --- | --- | --- | --- |
196
+ | **Windows 10 / 11** (`x86_64`, `ARM64`) | `[BackendId.WASAPI]` | `default_output` & `microphone` (`follow`/`pin`), `device(id)` & `input_device(id)` (`>= 19041`); `process`/`application` `scope="tree"` (`>= 20348`) and `cspa_mixer`. | `scope="exact"` and uncalibrated `timing="precise"` reject explicitly. |
197
+ | **Linux** (`x86_64`, `aarch64`) | `[BackendId.PIPEWIRE, BackendId.PULSEAUDIO]` (runtime `dlopen` of `libpipewire-0.3.so.0 >= 0.3.65` & `libpulse.so.0 >= 15.0`, `0` compile-time `NEEDED` links) | PipeWire & PulseAudio output endpoints, microphones (`.monitor` sinks excluded), PipeWire `native_graph_mix` fan-in, PulseAudio per-stream `cspa_mixer`, and cross-backend `[pipewire + pulseaudio]` `cspa_mixer`. | `native_kernel_capture` for `process`/`application` and uncalibrated `timing="precise"` reject explicitly. |
198
+ | **macOS** (`Apple Silicon arm64`) | `[BackendId.COREAUDIO, BackendId.SCREENCAPTUREKIT]` | Core Audio Process Taps (`macOS 14.2+`), Core Audio HAL microphone input (`macOS 13.0+`), ScreenCaptureKit `application` (`scope="exact"`, `macOS 13.0+`), and cross-backend `[screencapturekit + coreaudio]` `cspa_mixer`. | Intel macOS (`x86_64`), SCK `scope="tree"`, and uncalibrated `timing="precise"` reject explicitly. |
199
+
200
+ ---
201
+
202
+ ## Links & License
203
+
204
+ - **Official Documentation, Python Handbook & `.pyi` API Reference:** <https://s0d3s.github.io/capture-system-or-process-audio/>
205
+ - **Source, Rust / C / C++ SDKs & Live TUI Demo:** <https://github.com/s0d3s/capture-system-or-process-audio>
206
+ - **Prebuilt Binaries & Releases:** <https://github.com/s0d3s/capture-system-or-process-audio/releases>
207
+ - **License:** Dual-licensed under [MIT](https://github.com/s0d3s/capture-system-or-process-audio/blob/main/LICENSE-MIT) or [Apache-2.0](https://github.com/s0d3s/capture-system-or-process-audio/blob/main/LICENSE-APACHE), at your option.
@@ -0,0 +1,12 @@
1
+ # Dependency Attribution
2
+
3
+ No third-party implementation source is copied into this crate. Registry checksums
4
+ are pinned by Cargo.lock. Retain applicable upstream licenses on redistribution.
5
+
6
+ | Dependency | Version | Selected License | Upstream |
7
+ | --- | --- | --- | --- |
8
+ | pyo3 / pyo3-ffi / pyo3-macros | 0.30.1 | MIT OR Apache-2.0 | https://github.com/PyO3/pyo3 |
9
+ | crossbeam-utils (via core) | 0.8.23 | MIT OR Apache-2.0 | https://github.com/crossbeam-rs/crossbeam |
10
+ | windows / windows-core (on Windows) | 0.62.2 | MIT | https://github.com/microsoft/windows-rs |
11
+ | libloading (on Linux) | 0.8.9 | ISC | https://github.com/nagisa/rust_libloading |
12
+ | allocation-counter (dev-only) | 0.8.1 | MIT OR Apache-2.0 | https://github.com/fornwall/allocation-counter |