audcore-aud-io 0.2.0__py3-none-win_amd64.whl
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.
- audcore_aud_io-0.2.0.data/purelib/aud_io/__init__.py +279 -0
- audcore_aud_io-0.2.0.data/purelib/aud_io/native/x64/aud-io-v0.2.0.dll +0 -0
- audcore_aud_io-0.2.0.dist-info/METADATA +41 -0
- audcore_aud_io-0.2.0.dist-info/RECORD +7 -0
- audcore_aud_io-0.2.0.dist-info/WHEEL +5 -0
- audcore_aud_io-0.2.0.dist-info/licenses/LICENSE +21 -0
- audcore_aud_io-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"""Non-realtime Python wrapper for aud-io. Names are case sensitive."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import ctypes as C
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from enum import IntEnum
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import platform
|
|
9
|
+
import re
|
|
10
|
+
import threading
|
|
11
|
+
|
|
12
|
+
__version__ = "0.2.0"
|
|
13
|
+
NAME_PATTERN = r"^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$"
|
|
14
|
+
WAITING, UNDERRUN, DISCONTINUITY = 1, 2, 4
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Sample(IntEnum):
|
|
18
|
+
S16 = 1
|
|
19
|
+
S24 = 2
|
|
20
|
+
S32 = 3
|
|
21
|
+
F32 = 4
|
|
22
|
+
F64 = 5
|
|
23
|
+
S8 = 6
|
|
24
|
+
U8 = 7
|
|
25
|
+
S64 = 8
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class Format:
|
|
30
|
+
sample_rate: int = 48000
|
|
31
|
+
channels: int = 2
|
|
32
|
+
sample_type: Sample = Sample.F32
|
|
33
|
+
valid_bits: int = 0
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def frame_bytes(self) -> int:
|
|
37
|
+
return self.channels * {1: 2, 2: 3, 3: 4, 4: 4, 5: 8, 6: 1, 7: 1, 8: 8}[self.sample_type]
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def bit_rate(self) -> int:
|
|
41
|
+
return self.sample_rate * self.frame_bytes * 8
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class Source:
|
|
46
|
+
name: str
|
|
47
|
+
format: Format
|
|
48
|
+
generation: int
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True)
|
|
52
|
+
class AudioBlock:
|
|
53
|
+
data: bytes
|
|
54
|
+
frames: int
|
|
55
|
+
flags: int
|
|
56
|
+
format: Format
|
|
57
|
+
generation: int
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class _Format(C.Structure):
|
|
61
|
+
_fields_ = [(key, C.c_uint32) for key in ("sample_rate", "channels", "sample_type", "valid_bits")]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class _Source(C.Structure):
|
|
65
|
+
_fields_ = [("name", C.c_char * 65),
|
|
66
|
+
("format", _Format), ("generation", C.c_uint64)]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class _Result(C.Structure):
|
|
70
|
+
_fields_ = [("frames", C.c_uint32), ("flags", C.c_uint32),
|
|
71
|
+
("format", _Format), ("generation", C.c_uint64)]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
_library = None
|
|
75
|
+
_library_lock = threading.Lock()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _native():
|
|
79
|
+
global _library
|
|
80
|
+
with _library_lock:
|
|
81
|
+
if _library is not None:
|
|
82
|
+
return _library
|
|
83
|
+
machine = platform.machine().lower()
|
|
84
|
+
if platform.system() != "Windows" or machine not in ("amd64", "x86_64", "x86", "i386", "i686"):
|
|
85
|
+
raise OSError("aud-io requires Windows x86 or x64")
|
|
86
|
+
arch = "x64" if C.sizeof(C.c_void_p) == 8 else "x86"
|
|
87
|
+
lib = C.CDLL(str(Path(__file__).parent / "native" / arch / "aud-io-v0.2.0.dll"))
|
|
88
|
+
def bind(name, result, *args):
|
|
89
|
+
function = getattr(lib, "aud_io_" + name)
|
|
90
|
+
function.restype, function.argtypes = result, args
|
|
91
|
+
P, F = C.c_void_p, C.POINTER(_Format)
|
|
92
|
+
bind("last_error", C.c_char_p)
|
|
93
|
+
bind("list_sources", C.c_int, C.POINTER(C.POINTER(_Source)), C.POINTER(C.c_uint32))
|
|
94
|
+
bind("free_sources", None, C.POINTER(_Source))
|
|
95
|
+
bind("get_source", C.c_int, C.c_char_p, C.POINTER(_Source))
|
|
96
|
+
bind("sender_open", C.c_int, C.c_char_p, F, C.POINTER(P))
|
|
97
|
+
bind("sender_info", C.c_int, P, C.POINTER(_Source))
|
|
98
|
+
bind("sender_set_format", C.c_int, P, F)
|
|
99
|
+
bind("sender_write", C.c_uint32, P, P, C.c_uint64, C.c_uint32)
|
|
100
|
+
bind("sender_close", None, P)
|
|
101
|
+
bind("receiver_open", C.c_int, C.c_char_p, F, C.POINTER(P))
|
|
102
|
+
bind("receiver_info", C.c_int, P, C.POINTER(_Source))
|
|
103
|
+
bind("receiver_set_source", C.c_int, P, C.c_char_p)
|
|
104
|
+
bind("receiver_read", _Result, P, P, C.c_uint64, C.c_uint32)
|
|
105
|
+
bind("receiver_close", None, P)
|
|
106
|
+
_library = lib
|
|
107
|
+
return lib
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _check(status):
|
|
111
|
+
if status:
|
|
112
|
+
message = _native().aud_io_last_error().decode("utf-8", "replace")
|
|
113
|
+
raise RuntimeError(message or f"Invalid aud-io operation ({status})")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _name(value):
|
|
117
|
+
if not isinstance(value, str) or re.fullmatch(NAME_PATTERN, value) is None:
|
|
118
|
+
raise ValueError(f"Name must match {NAME_PATTERN}")
|
|
119
|
+
return value.encode("ascii")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _format(value):
|
|
123
|
+
if not isinstance(value, Format):
|
|
124
|
+
raise TypeError("Expected a Format")
|
|
125
|
+
# Do not let ctypes silently wrap negative/oversized Python integers.
|
|
126
|
+
if any(not isinstance(x, int) or not 0 <= x <= 0xFFFFFFFF for x in
|
|
127
|
+
(value.sample_rate, value.channels, value.sample_type, value.valid_bits)):
|
|
128
|
+
raise ValueError("Format fields must be unsigned 32-bit integers")
|
|
129
|
+
return _Format(value.sample_rate, value.channels, value.sample_type, value.valid_bits)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _public_format(f):
|
|
133
|
+
return Format(f.sample_rate, f.channels, Sample(f.sample_type) if f.sample_type else Sample.F32, f.valid_bits)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _source(s):
|
|
137
|
+
return Source(s.name.decode("ascii"), _public_format(s.format), s.generation)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def list_sources() -> list[Source]:
|
|
141
|
+
"""Return an owned snapshot of currently published sources."""
|
|
142
|
+
lib = _native()
|
|
143
|
+
values, count = C.POINTER(_Source)(), C.c_uint32()
|
|
144
|
+
_check(lib.aud_io_list_sources(C.byref(values), C.byref(count)))
|
|
145
|
+
try:
|
|
146
|
+
return [_source(values[i]) for i in range(count.value)]
|
|
147
|
+
finally:
|
|
148
|
+
lib.aud_io_free_sources(values)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def get_source(name: str) -> Source | None:
|
|
152
|
+
"""Query one source without opening a receiver."""
|
|
153
|
+
result = _Source()
|
|
154
|
+
status = _native().aud_io_get_source(_name(name), C.byref(result))
|
|
155
|
+
if status == -3:
|
|
156
|
+
return None
|
|
157
|
+
_check(status)
|
|
158
|
+
return _source(result)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def get_format(name: str) -> Format | None:
|
|
162
|
+
source = get_source(name)
|
|
163
|
+
return source.format if source else None
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class _Handle:
|
|
167
|
+
def __enter__(self):
|
|
168
|
+
return self
|
|
169
|
+
|
|
170
|
+
def __exit__(self, *_):
|
|
171
|
+
self.close()
|
|
172
|
+
|
|
173
|
+
def _get(self):
|
|
174
|
+
if not self._handle:
|
|
175
|
+
raise RuntimeError("The aud-io handle is closed")
|
|
176
|
+
return self._handle
|
|
177
|
+
|
|
178
|
+
def close(self):
|
|
179
|
+
with self._lock:
|
|
180
|
+
if self._handle:
|
|
181
|
+
getattr(_native(), self._close_function)(self._handle)
|
|
182
|
+
self._handle = None
|
|
183
|
+
|
|
184
|
+
def __del__(self):
|
|
185
|
+
if getattr(self, "_handle", None):
|
|
186
|
+
self.close()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class Sender(_Handle):
|
|
190
|
+
_close_function = "aud_io_sender_close"
|
|
191
|
+
|
|
192
|
+
def __init__(self, name: str, format: Format = Format()):
|
|
193
|
+
self._handle, self._lock = None, threading.RLock()
|
|
194
|
+
output, native_format = C.c_void_p(), _format(format)
|
|
195
|
+
_check(_native().aud_io_sender_open(_name(name), C.byref(native_format), C.byref(output)))
|
|
196
|
+
self._handle, self._format = output, format
|
|
197
|
+
|
|
198
|
+
@property
|
|
199
|
+
def format(self) -> Format:
|
|
200
|
+
return self._format
|
|
201
|
+
|
|
202
|
+
@property
|
|
203
|
+
def info(self) -> Source:
|
|
204
|
+
with self._lock:
|
|
205
|
+
source = _Source()
|
|
206
|
+
_check(_native().aud_io_sender_info(self._get(), C.byref(source)))
|
|
207
|
+
return _source(source)
|
|
208
|
+
|
|
209
|
+
def write(self, pcm) -> int:
|
|
210
|
+
"""Accept a contiguous bytes-like buffer, including array('f')."""
|
|
211
|
+
data = memoryview(pcm).cast("B")
|
|
212
|
+
with self._lock:
|
|
213
|
+
if len(data) % self.format.frame_bytes:
|
|
214
|
+
raise ValueError("PCM must contain complete frames")
|
|
215
|
+
frames = len(data) // self.format.frame_bytes
|
|
216
|
+
if not 0 <= frames <= 8192:
|
|
217
|
+
raise ValueError("At most 8192 frames per write")
|
|
218
|
+
if len(data) > 32 * 1024 * 1024:
|
|
219
|
+
raise ValueError("Write buffer exceeds 32 MiB; submit fewer frames")
|
|
220
|
+
buffer = (C.c_ubyte * len(data)).from_buffer_copy(data)
|
|
221
|
+
return _native().aud_io_sender_write(self._get(), buffer, len(data), frames)
|
|
222
|
+
|
|
223
|
+
def set_format(self, format: Format):
|
|
224
|
+
with self._lock:
|
|
225
|
+
value = _format(format)
|
|
226
|
+
_check(_native().aud_io_sender_set_format(self._get(), C.byref(value)))
|
|
227
|
+
self._format = format
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class Receiver(_Handle):
|
|
231
|
+
_close_function = "aud_io_receiver_close"
|
|
232
|
+
|
|
233
|
+
def __init__(self, name: str, format: Format | None = Format()):
|
|
234
|
+
self._handle, self._lock = None, threading.RLock()
|
|
235
|
+
output = C.c_void_p()
|
|
236
|
+
value = _format(format) if format is not None else None
|
|
237
|
+
_check(_native().aud_io_receiver_open(_name(name), C.byref(value) if value else None, C.byref(output)))
|
|
238
|
+
self._handle, self._format = output, format
|
|
239
|
+
|
|
240
|
+
@property
|
|
241
|
+
def format(self) -> Format | None:
|
|
242
|
+
return self._format
|
|
243
|
+
|
|
244
|
+
@classmethod
|
|
245
|
+
def native(cls, name: str):
|
|
246
|
+
return cls(name, None)
|
|
247
|
+
|
|
248
|
+
@property
|
|
249
|
+
def info(self) -> Source | None:
|
|
250
|
+
with self._lock:
|
|
251
|
+
source = _Source()
|
|
252
|
+
status = _native().aud_io_receiver_info(self._get(), C.byref(source))
|
|
253
|
+
if status == -3:
|
|
254
|
+
return None
|
|
255
|
+
_check(status)
|
|
256
|
+
return _source(source)
|
|
257
|
+
|
|
258
|
+
@property
|
|
259
|
+
def source_format(self) -> Format | None:
|
|
260
|
+
source = self.info
|
|
261
|
+
return source.format if source else None
|
|
262
|
+
|
|
263
|
+
def set_source(self, name: str):
|
|
264
|
+
with self._lock:
|
|
265
|
+
_check(_native().aud_io_receiver_set_source(self._get(), _name(name)))
|
|
266
|
+
|
|
267
|
+
def read(self, frames: int = 480) -> AudioBlock:
|
|
268
|
+
if not isinstance(frames, int) or not 1 <= frames <= 8192:
|
|
269
|
+
raise ValueError("Read 1–8192 frames per call")
|
|
270
|
+
with self._lock:
|
|
271
|
+
source = self.info if self.format is None else None
|
|
272
|
+
size = frames * (self.format.frame_bytes if self.format else max(16, source.format.frame_bytes if source else 16))
|
|
273
|
+
if size > 32 * 1024 * 1024:
|
|
274
|
+
raise ValueError("Read buffer exceeds 32 MiB; request fewer frames")
|
|
275
|
+
buffer = (C.c_ubyte * size)()
|
|
276
|
+
result = _native().aud_io_receiver_read(self._get(), buffer, size, frames)
|
|
277
|
+
format = _public_format(result.format)
|
|
278
|
+
length = size if self.format else result.frames * format.frame_bytes
|
|
279
|
+
return AudioBlock(bytes(buffer[:length]), result.frames, result.flags, format, result.generation)
|
|
Binary file
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: audcore-aud-io
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Named shared-memory audio broadcast for Windows x86/x64
|
|
5
|
+
Author: Audcore contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Source, https://github.com/audcore/aud-io
|
|
8
|
+
Project-URL: Issues, https://github.com/audcore/aud-io/issues
|
|
9
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
10
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# aud-io (Python)
|
|
17
|
+
|
|
18
|
+
Windows x86/x64, Python 3.10+. pip selects the wheel for the Python process architecture. Each wheel bundles its native DLL; no compiler or Python dependency is required. Non-realtime API.
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
pip install audcore-aud-io
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from array import array
|
|
26
|
+
from aud_io import Sender, Receiver, list_sources
|
|
27
|
+
|
|
28
|
+
with Sender("music") as sender, Receiver.native(sender.info.name) as receiver:
|
|
29
|
+
print(list_sources())
|
|
30
|
+
sender.write(array("f", [0.1, 0.1] * 480))
|
|
31
|
+
audio = receiver.read(480)
|
|
32
|
+
print(audio.format, audio.frames)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Use `Format(rate, channels, Sample.S16)` for another format, `sender.set_format(...)` to change it, and `receiver.set_source("other")` to select another source. A normal `Receiver("music")` returns Float32/48 kHz/stereo, including silence while waiting. Native reads preserve source bytes and return the matching format.
|
|
36
|
+
|
|
37
|
+
`get_format("music")` queries a source without opening a receiver. Channel counts and sample rates have no whitelist; transfer buffers are bounded. `Format.bit_rate` reports the PCM bitrate.
|
|
38
|
+
|
|
39
|
+
Names are case sensitive and match `^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$`; duplicates become `music_1`, `music_2`. Keep the name from `sender.info.name` for reconnecting. Use context managers or `close()`.
|
|
40
|
+
|
|
41
|
+
See [demo.py](examples/demo.py) for list/send/receive/switch examples. Run it from separate terminals for one-to-many reception. [C++ SDK and languages](https://github.com/audcore/aud-io).
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
audcore_aud_io-0.2.0.data/purelib/aud_io/__init__.py,sha256=gj1xmRsYOGf9xNWNiMxWBFRka7GjRcKQxHm2dtXuLYY,9639
|
|
2
|
+
audcore_aud_io-0.2.0.data/purelib/aud_io/native/x64/aud-io-v0.2.0.dll,sha256=HI_2Zf1GjFAaZzcs4e7_H1tBCa8Cnptz4SeRp23eNNc,219136
|
|
3
|
+
audcore_aud_io-0.2.0.dist-info/licenses/LICENSE,sha256=8Mwg4lM2AwXtFqd4TVgcmjrW4R_YJF1DOIxb_5F8YQw,1077
|
|
4
|
+
audcore_aud_io-0.2.0.dist-info/METADATA,sha256=wxw7GXT6vMjqV3xZ_hy5LDi_xf22PAtvS-kg0tTNcvU,2007
|
|
5
|
+
audcore_aud_io-0.2.0.dist-info/WHEEL,sha256=79BCzrXBJOa2YV6ugKrK59dqWifMGjAkgVd_6AQrqbU,98
|
|
6
|
+
audcore_aud_io-0.2.0.dist-info/top_level.txt,sha256=XMDOLcxjB0MAls7vy5gjFj7lSi5BoEZp6D7e-Lw2W_I,7
|
|
7
|
+
audcore_aud_io-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Audcore contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
aud_io
|