Skip to content

Commit 78093ca

Browse files
committed
Add WasapiSettings
Closes #35.
1 parent b36dcc4 commit 78093ca

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

doc/index.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ API Documentation
2222
:exclude-members: RawInputStream, RawOutputStream, RawStream,
2323
InputStream, OutputStream, Stream,
2424
CallbackFlags, CallbackStop, CallbackAbort,
25-
PortAudioError, DeviceList, AsioSettings
25+
PortAudioError, DeviceList, AsioSettings, WasapiSettings
2626

2727
.. autoclass:: Stream
2828
:members:
@@ -53,6 +53,8 @@ API Documentation
5353

5454
.. autoclass:: AsioSettings
5555

56+
.. autoclass:: WasapiSettings
57+
5658
.. only:: html
5759

5860
Index

sounddevice.py

+108-1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@
225225
PaError Pa_GetSampleSize( PaSampleFormat format );
226226
void Pa_Sleep( long msec );
227227
228+
/* pa_win_waveformat.h */
229+
230+
typedef unsigned long PaWinWaveFormatChannelMask;
231+
228232
/* pa_asio.h */
229233
230234
#define paAsioUseChannelSelectors 0x01
@@ -237,6 +241,68 @@
237241
unsigned long flags;
238242
int *channelSelectors;
239243
} PaAsioStreamInfo;
244+
245+
/* pa_win_wasapi.h */
246+
247+
typedef enum PaWasapiFlags
248+
{
249+
paWinWasapiExclusive = 1,
250+
paWinWasapiRedirectHostProcessor = 2,
251+
paWinWasapiUseChannelMask = 4,
252+
paWinWasapiPolling = 8,
253+
paWinWasapiThreadPriority = 16
254+
} PaWasapiFlags;
255+
256+
typedef void (*PaWasapiHostProcessorCallback) (
257+
void *inputBuffer, long inputFrames,
258+
void *outputBuffer, long outputFrames, void *userData);
259+
260+
typedef enum PaWasapiThreadPriority
261+
{
262+
eThreadPriorityNone = 0,
263+
eThreadPriorityAudio,
264+
eThreadPriorityCapture,
265+
eThreadPriorityDistribution,
266+
eThreadPriorityGames,
267+
eThreadPriorityPlayback,
268+
eThreadPriorityProAudio,
269+
eThreadPriorityWindowManager
270+
} PaWasapiThreadPriority;
271+
272+
typedef enum PaWasapiStreamCategory
273+
{
274+
eAudioCategoryOther = 0,
275+
eAudioCategoryCommunications = 3,
276+
eAudioCategoryAlerts = 4,
277+
eAudioCategorySoundEffects = 5,
278+
eAudioCategoryGameEffects = 6,
279+
eAudioCategoryGameMedia = 7,
280+
eAudioCategoryGameChat = 8,
281+
eAudioCategorySpeech = 9,
282+
eAudioCategoryMovie = 10,
283+
eAudioCategoryMedia = 11
284+
} PaWasapiStreamCategory;
285+
286+
typedef enum PaWasapiStreamOption
287+
{
288+
eStreamOptionNone = 0,
289+
eStreamOptionRaw = 1,
290+
eStreamOptionMatchFormat = 2
291+
} PaWasapiStreamOption;
292+
293+
typedef struct PaWasapiStreamInfo
294+
{
295+
unsigned long size;
296+
PaHostApiTypeId hostApiType;
297+
unsigned long version;
298+
unsigned long flags;
299+
PaWinWaveFormatChannelMask channelMask;
300+
PaWasapiHostProcessorCallback hostProcessorOutput;
301+
PaWasapiHostProcessorCallback hostProcessorInput;
302+
PaWasapiThreadPriority threadPriority;
303+
PaWasapiStreamCategory streamCategory;
304+
PaWasapiStreamOption streamOption;
305+
} PaWasapiStreamInfo;
240306
""")
241307

242308
try:
@@ -2052,7 +2118,7 @@ class default(object):
20522118
20532119
See Also
20542120
--------
2055-
AsioSettings
2121+
AsioSettings, WasapiSettings
20562122
20572123
"""
20582124

@@ -2222,6 +2288,47 @@ def __init__(self, channel_selectors):
22222288
channelSelectors=self._selectors))
22232289

22242290

2291+
class WasapiSettings(object):
2292+
2293+
def __init__(self, exclusive=False):
2294+
"""WASAPI-specific input/output settings.
2295+
2296+
Objects of this class can be used as *extra_settings* argument
2297+
to `Stream()` (and variants) or as `default.extra_settings`.
2298+
They can also be used in `check_input_settings()` and
2299+
`check_output_settings()`.
2300+
2301+
Parameters
2302+
----------
2303+
exclusive : bool
2304+
Exclusive mode allows to deliver audio data directly to
2305+
hardware bypassing software mixing.
2306+
2307+
Examples
2308+
--------
2309+
Setting exclusive mode when calling `play()`:
2310+
2311+
>>> import sounddevice as sd
2312+
>>> wasapi_exclusive = sd.WasapiSettings(exclusive=True)
2313+
>>> sd.play(..., extra_settings=wasapi_exclusive)
2314+
2315+
Setting exclusive mode as default:
2316+
2317+
>>> sd.default.extra_settings = wasapi_exclusive
2318+
>>> sd.play(...)
2319+
2320+
"""
2321+
flags = 0x0
2322+
if exclusive:
2323+
flags |= paWinWasapiExclusive
2324+
self._streaminfo = _ffi.new('PaWasapiStreamInfo*', dict(
2325+
size=_ffi.sizeof('PaWasapiStreamInfo'),
2326+
hostApiType=_lib.paWASAPI,
2327+
version=1,
2328+
flags=flags,
2329+
))
2330+
2331+
22252332
class _CallbackContext(object):
22262333
"""Helper class for re-use in play()/rec()/playrec() callbacks."""
22272334

0 commit comments

Comments
 (0)