PortAudio  2.0
pa_win_wasapi.h
Go to the documentation of this file.
1 #ifndef PA_WIN_WASAPI_H
2 #define PA_WIN_WASAPI_H
3 /*
4  * $Id: $
5  * PortAudio Portable Real-Time Audio Library
6  * WASAPI specific extensions
7  *
8  * Copyright (c) 1999-2018 Ross Bencina and Phil Burk
9  * Copyright (c) 2006-2010 David Viens
10  * Copyright (c) 2010-2018 Dmitry Kostjuchenko
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining
13  * a copy of this software and associated documentation files
14  * (the "Software"), to deal in the Software without restriction,
15  * including without limitation the rights to use, copy, modify, merge,
16  * publish, distribute, sublicense, and/or sell copies of the Software,
17  * and to permit persons to whom the Software is furnished to do so,
18  * subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be
21  * included in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
27  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30  */
31 
32 /*
33  * The text above constitutes the entire PortAudio license; however,
34  * the PortAudio community also makes the following non-binding requests:
35  *
36  * Any person wishing to distribute modifications to the Software is
37  * requested to send the modifications to the original developer so that
38  * they can be incorporated into the canonical version. It is also
39  * requested that these non-binding requests be included along with the
40  * license above.
41  */
42 
48 #include "portaudio.h"
49 #include "pa_win_waveformat.h"
50 
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif /* __cplusplus */
55 
56 
57 /* Stream setup flags. */
58 typedef enum PaWasapiFlags
59 {
60  /* put WASAPI into exclusive mode */
62 
63  /* allow to skip internal PA processing completely */
65 
66  /* assign custom channel mask */
68 
69  /* select non-Event driven method of data read/write
70  Note: WASAPI Event driven core is capable of 2ms latency!!!, but Polling
71  method can only provide 15-20ms latency. */
72  paWinWasapiPolling = (1 << 3),
73 
74  /* force custom thread priority setting, must be used if PaWasapiStreamInfo::threadPriority
75  is set to a custom value */
77 
78  /* force explicit sample format and do not allow PA to select suitable working format, API will
79  fail if provided sample format is not supported by audio hardware in Exclusive mode
80  or system mixer in Shared mode */
82 
83  /* allow API to insert system-level channel matrix mixer and sample rate converter to allow
84  playback formats that do not match the current configured system settings.
85  this is in particular required for streams not matching the system mixer sample rate.
86  only applies in Shared mode. */
88 }
90 #define paWinWasapiExclusive (paWinWasapiExclusive)
91 #define paWinWasapiRedirectHostProcessor (paWinWasapiRedirectHostProcessor)
92 #define paWinWasapiUseChannelMask (paWinWasapiUseChannelMask)
93 #define paWinWasapiPolling (paWinWasapiPolling)
94 #define paWinWasapiThreadPriority (paWinWasapiThreadPriority)
95 #define paWinWasapiExplicitSampleFormat (paWinWasapiExplicitSampleFormat)
96 #define paWinWasapiAutoConvert (paWinWasapiAutoConvert)
97 
98 
99 /* Stream state.
100 
101  @note Multiple states can be united into a bitmask.
102  @see PaWasapiStreamStateCallback, PaWasapi_SetStreamStateHandler
103 */
105 {
106  /* state change was caused by the error:
107 
108  Example:
109  1) If thread execution stopped due to AUDCLNT_E_RESOURCES_INVALIDATED then state
110  value will contain paWasapiStreamStateError|paWasapiStreamStateThreadStop.
111  */
113 
114  /* processing thread is preparing to start execution */
116 
117  /* processing thread started execution (enters its loop) */
119 
120  /* processing thread stopped execution */
122 }
124 #define paWasapiStreamStateError (paWasapiStreamStateError)
125 #define paWasapiStreamStateThreadPrepare (paWasapiStreamStateThreadPrepare)
126 #define paWasapiStreamStateThreadStart (paWasapiStreamStateThreadStart)
127 #define paWasapiStreamStateThreadStop (paWasapiStreamStateThreadStop)
128 
129 
130 /* Host processor.
131 
132  Allows to skip internal PA processing completely. paWinWasapiRedirectHostProcessor flag
133  must be set to the PaWasapiStreamInfo::flags member in order to have host processor
134  redirected to this callback.
135 
136  Use with caution! inputFrames and outputFrames depend solely on final device setup.
137  To query max values of inputFrames/outputFrames use PaWasapi_GetFramesPerHostBuffer.
138 */
139 typedef void (*PaWasapiHostProcessorCallback) (void *inputBuffer, long inputFrames,
140  void *outputBuffer, long outputFrames, void *userData);
141 
142 
143 /* Stream state handler.
144 
145  @param pStream Pointer to PaStream object.
146  @param stateFlags State flags, a collection of values from PaWasapiStreamState enum.
147  @param errorId Error id provided by system API (HRESULT).
148  @param userData Pointer to user data.
149 
150  @see PaWasapiStreamState
151 */
152 typedef void (*PaWasapiStreamStateCallback) (PaStream *pStream, unsigned int stateFlags,
153  unsigned int errorId, void *pUserData);
154 
155 
156 /* Device role. */
157 typedef enum PaWasapiDeviceRole
158 {
170 }
172 
173 
174 /* Jack connection type. */
176 {
189 }
191 
192 
193 /* Jack geometric location. */
195 {
197  eJackGeoLocRear = 0x1, /* matches EPcxGeoLocation::eGeoLocRear */
212 }
214 
215 
216 /* Jack general location. */
218 {
223 }
225 
226 
227 /* Jack's type of port. */
229 {
234 }
236 
237 
238 /* Thread priority. */
240 {
249 }
251 
252 
253 /* Stream descriptor. */
255 {
256  unsigned long channelMapping;
257  unsigned long color; /* derived from macro: #define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))) */
262  unsigned int isConnected;
263 }
265 
266 
276 {
287 }
289 
290 
299 {
303 }
305 
306 
307 /* Stream descriptor. */
308 typedef struct PaWasapiStreamInfo
309 {
310  unsigned long size;
312  unsigned long version;
314  unsigned long flags;
323 
331 
340 
346 
352 }
354 
355 
364 PaError PaWasapi_GetAudioClient( PaStream *pStream, void **pAudioClient, int bOutput );
365 
366 
378 
379 
395 int PaWasapi_GetDeviceCurrentFormat( PaStream *pStream, void *pFormat, unsigned int formatSize, int bOutput );
396 
397 
411 int PaWasapi_GetDeviceDefaultFormat( void *pFormat, unsigned int formatSize, PaDeviceIndex device );
412 
413 
427 int PaWasapi_GetDeviceMixFormat( void *pFormat, unsigned int formatSize, PaDeviceIndex device );
428 
429 
437 int/*PaWasapiDeviceRole*/ PaWasapi_GetDeviceRole( PaDeviceIndex device );
438 
439 
453 PaError PaWasapi_ThreadPriorityBoost( void **pTask, PaWasapiThreadPriority priorityClass );
454 
455 
466 
467 
481 PaError PaWasapi_GetFramesPerHostBuffer( PaStream *pStream, unsigned int *pInput, unsigned int *pOutput );
482 
483 
497 PaError PaWasapi_GetJackCount( PaDeviceIndex device, int *pJackCount );
498 
499 
516 PaError PaWasapi_GetJackDescription( PaDeviceIndex device, int jackIndex, PaWasapiJackDescription *pJackDescription );
517 
518 
527 PaError PaWasapi_SetStreamStateHandler( PaStream *pStream, PaWasapiStreamStateCallback fnStateHandler, void *pUserData );
528 
529 
557 PaError PaWasapiWinrt_SetDefaultDeviceId( const unsigned short *pId, int bOutput );
558 
559 
594 PaError PaWasapiWinrt_PopulateDeviceList( const unsigned short **pId, const unsigned short **pName,
595  const PaWasapiDeviceRole *pRole, unsigned int count, int bOutput );
596 
597 
598 /*
599  IMPORTANT:
600 
601  WASAPI is implemented for Callback and Blocking interfaces. It supports Shared and Exclusive
602  share modes.
603 
604  Exclusive Mode:
605 
606  Exclusive mode allows to deliver audio data directly to hardware bypassing
607  software mixing.
608  Exclusive mode is specified by 'paWinWasapiExclusive' flag.
609 
610  Callback Interface:
611 
612  Provides best audio quality with low latency. Callback interface is implemented in
613  two versions:
614 
615  1) Event-Driven:
616  This is the most powerful WASAPI implementation which provides glitch-free
617  audio at around 3ms latency in Exclusive mode. Lowest possible latency for this mode is
618  3 ms for HD Audio class audio chips. For the Shared mode latency can not be
619  lower than 20 ms.
620 
621  2) Poll-Driven:
622  Polling is another 2-nd method to operate with WASAPI. It is less efficient than Event-Driven
623  and provides latency at around 10-13ms. Polling must be used to overcome a system bug
624  under Windows Vista x64 when application is WOW64(32-bit) and Event-Driven method simply
625  times out (event handle is never signalled on buffer completion). Please note, such WOW64 bug
626  does not exist in Vista x86 or Windows 7.
627  Polling can be setup by speciying 'paWinWasapiPolling' flag. Our WASAPI implementation detects
628  WOW64 bug and sets 'paWinWasapiPolling' automatically.
629 
630  Thread priority:
631 
632  Normally thread priority is set automatically and does not require modification. Although
633  if user wants some tweaking thread priority can be modified by setting 'paWinWasapiThreadPriority'
634  flag and specifying 'PaWasapiStreamInfo::threadPriority' with value from PaWasapiThreadPriority
635  enum.
636 
637  Blocking Interface:
638 
639  Blocking interface is implemented but due to above described Poll-Driven method can not
640  deliver lowest possible latency. Specifying too low latency in Shared mode will result in
641  distorted audio although Exclusive mode adds stability.
642 
643  8.24 format:
644 
645  If paCustomFormat is specified as sample format then the implementation will understand it
646  as valid 24-bits inside 32-bit container (e.g. wBitsPerSample = 32, Samples.wValidBitsPerSample = 24).
647 
648  By using paCustomFormat there will be small optimization when samples are be copied
649  with Copy_24_To_24 by PA processor instead of conversion from packed 3-byte (24-bit) data
650  with Int24_To_Int32.
651 
652  Pa_IsFormatSupported:
653 
654  To check format with correct Share Mode (Exclusive/Shared) you must supply PaWasapiStreamInfo
655  with flags paWinWasapiExclusive set through member of PaStreamParameters::hostApiSpecificStreamInfo
656  structure.
657 
658  If paWinWasapiExplicitSampleFormat flag is provided then implementation will not try to select
659  suitable close format and will return an error instead of paFormatIsSupported. By specifying
660  paWinWasapiExplicitSampleFormat flag it is possible to find out what sample formats are
661  supported by Exclusive or Shared modes.
662 
663  Pa_OpenStream:
664 
665  To set desired Share Mode (Exclusive/Shared) you must supply
666  PaWasapiStreamInfo with flags paWinWasapiExclusive set through member of
667  PaStreamParameters::hostApiSpecificStreamInfo structure.
668 
669  Coding style for parameters and structure members of the public API:
670 
671  1) bXXX - boolean, [1 (TRUE), 0 (FALSE)]
672  2) pXXX - pointer
673  3) fnXXX - pointer to function
674  4) structure members are never prefixed with a type distinguisher
675 
676 
677  UWP/WinRT:
678 
679  This platform has number of limitations which do not allow to enumerate audio devices without
680  an additional external help. Enumeration is possible though from C++/CX, check the related API
681  Windows::Devices::Enumeration::DeviceInformation::FindAllAsync().
682 
683  The main limitation is an absence of the device enumeration from inside the PA's implementation.
684  This problem can be solved by using the following functions:
685 
686  PaWasapiWinrt_SetDefaultDeviceId() - to set default input/output device,
687  PaWasapiWinrt_PopulateDeviceList() - to populate device list with devices.
688 
689  Here is an example of populating the device list which can also be updated dynamically depending on
690  whether device was removed from or added to the system:
691 
692  ----------------
693 
694  std::vector<const UINT16 *> ids, names;
695  std::vector<PaWasapiDeviceRole> role;
696 
697  ids.resize(count);
698  names.resize(count);
699  role.resize(count);
700 
701  for (UINT32 i = 0; i < count; ++i)
702  {
703  ids[i] = (const UINT16 *)device_ids[i].c_str();
704  names[i] = (const UINT16 *)device_names[i].c_str();
705  role[i] = eRoleUnknownFormFactor;
706  }
707 
708  PaWasapiWinrt_SetDefaultDeviceId((const UINT16 *)default_device_id.c_str(), !capture);
709  PaWasapiWinrt_PopulateDeviceList(ids.data(), names.data(), role.data(), count, !capture);
710  PaWasapi_UpdateDeviceList();
711 
712  ----------------
713 */
714 
715 #ifdef __cplusplus
716 }
717 #endif /* __cplusplus */
718 
719 #endif /* PA_WIN_WASAPI_H */
PaWasapiJackGenLocation
Definition: pa_win_wasapi.h:217
PaWasapiHostProcessorCallback hostProcessorInput
Definition: pa_win_wasapi.h:330
Default for Shared mode.
Definition: pa_win_wasapi.h:242
Definition: pa_win_wasapi.h:219
Definition: pa_win_wasapi.h:180
Definition: pa_win_wasapi.h:285
Definition: pa_win_wasapi.h:164
Definition: pa_win_wasapi.h:205
Definition: pa_win_wasapi.h:203
PaWasapiThreadPriority
Definition: pa_win_wasapi.h:239
PaError PaWasapi_GetFramesPerHostBuffer(PaStream *pStream, unsigned int *pInput, unsigned int *pOutput)
Definition: pa_win_wasapi.c:2613
PaError PaWasapi_SetStreamStateHandler(PaStream *pStream, PaWasapiStreamStateCallback fnStateHandler, void *pUserData)
Definition: pa_win_wasapi.c:5553
PaWasapiHostProcessorCallback hostProcessorOutput
Definition: pa_win_wasapi.h:329
PaWasapiDeviceRole
Definition: pa_win_wasapi.h:157
Definition: pa_win_wasapi.h:284
Definition: pa_win_wasapi.h:209
Definition: pa_win_wasapi.h:206
#define paWinWasapiUseChannelMask
Definition: pa_win_wasapi.h:92
Definition: pa_win_wasapi.h:281
Definition: pa_win_wasapi.h:221
PaError PaWasapi_ThreadPriorityBoost(void **pTask, PaWasapiThreadPriority priorityClass)
Definition: pa_win_wasapi.c:5112
Definition: pa_win_wasapi.h:160
Definition: pa_win_wasapi.h:182
Definition: pa_win_wasapi.h:184
#define paWasapiStreamStateThreadPrepare
Definition: pa_win_wasapi.h:125
bypass WASAPI Audio Engine DSP effects, supported since Windows 8.1
Definition: pa_win_wasapi.h:301
Definition: pa_win_wasapi.h:159
void PaStream
Definition: portaudio.h:635
Definition: pa_win_wasapi.h:200
PaWasapiFlags
Definition: pa_win_wasapi.h:58
PaWasapiJackGenLocation genLocation
Definition: pa_win_wasapi.h:260
int PaWasapi_GetDeviceDefaultFormat(void *pFormat, unsigned int formatSize, PaDeviceIndex device)
Definition: pa_win_wasapi.c:2524
Definition: pa_win_wasapi.h:245
Definition: pa_win_wasapi.h:199
unsigned long flags
Definition: pa_win_wasapi.h:314
Definition: pa_win_wasapi.h:211
PaWasapiJackPortConnection portConnection
Definition: pa_win_wasapi.h:261
PaError PaWasapi_GetJackCount(PaDeviceIndex device, int *pJackCount)
Definition: pa_win_wasapi.c:5173
Definition: pa_win_wasapi.h:163
Definition: pa_win_wasapi.h:179
Definition: pa_win_wasapi.h:241
#define paWinWasapiRedirectHostProcessor
Definition: pa_win_wasapi.h:91
PaWasapiStreamCategory streamCategory
Definition: pa_win_wasapi.h:345
#define paWinWasapiThreadPriority
Definition: pa_win_wasapi.h:94
PaWasapiThreadPriority threadPriority
Definition: pa_win_wasapi.h:339
Definition: pa_win_wasapi.h:210
Definition: pa_win_wasapi.h:230
Definition: pa_win_wasapi.h:278
Definition: pa_win_wasapi.h:201
Definition: pa_win_wasapi.h:246
PaError PaWasapi_GetAudioClient(PaStream *pStream, void **pAudioClient, int bOutput)
Definition: pa_win_wasapi.c:5436
Definition: pa_win_wasapi.h:198
Definition: pa_win_wasapi.h:233
void(* PaWasapiStreamStateCallback)(PaStream *pStream, unsigned int stateFlags, unsigned int errorId, void *pUserData)
Definition: pa_win_wasapi.h:152
unsigned long color
Definition: pa_win_wasapi.h:257
Definition: pa_win_wasapi.h:188
unsigned long PaWinWaveFormatChannelMask
Definition: pa_win_waveformat.h:57
unsigned int isConnected
Definition: pa_win_wasapi.h:262
int PaDeviceIndex
Definition: portaudio.h:212
Definition: pa_win_wasapi.h:204
Default for Exclusive mode.
Definition: pa_win_wasapi.h:247
force WASAPI Audio Engine into a stream format, supported since Windows 10
Definition: pa_win_wasapi.h:302
Definition: pa_win_wasapi.h:232
PaWasapiJackConnectionType connectionType
Definition: pa_win_wasapi.h:258
Definition: pa_win_wasapi.h:207
Definition: pa_win_wasapi.h:185
Definition: pa_win_wasapi.h:197
The portable PortAudio API.
unsigned long size
Definition: pa_win_wasapi.h:310
#define paWinWasapiExplicitSampleFormat
Definition: pa_win_wasapi.h:95
void(* PaWasapiHostProcessorCallback)(void *inputBuffer, long inputFrames, void *outputBuffer, long outputFrames, void *userData)
Definition: pa_win_wasapi.h:139
Definition: pa_win_wasapi.h:181
PaError PaWasapi_UpdateDeviceList()
Definition: pa_win_wasapi.h:168
PaError PaWasapi_GetJackDescription(PaDeviceIndex device, int jackIndex, PaWasapiJackDescription *pJackDescription)
Definition: pa_win_wasapi.c:5348
PaHostApiTypeId hostApiType
Definition: pa_win_wasapi.h:311
#define paWinWasapiExclusive
Definition: pa_win_wasapi.h:90
PaWinWaveFormatChannelMask channelMask
Definition: pa_win_wasapi.h:322
#define paWasapiStreamStateThreadStart
Definition: pa_win_wasapi.h:126
Definition: pa_win_wasapi.h:169
Definition: pa_win_wasapi.h:254
int PaError
Definition: portaudio.h:121
Definition: pa_win_wasapi.h:162
PaHostApiTypeId
Definition: portaudio.h:275
Definition: pa_win_wasapi.h:283
Definition: pa_win_wasapi.h:183
int PaWasapi_GetDeviceMixFormat(void *pFormat, unsigned int formatSize, PaDeviceIndex device)
Definition: pa_win_wasapi.c:2557
PaWasapiJackPortConnection
Definition: pa_win_wasapi.h:228
Definition: pa_win_wasapi.h:277
PaError PaWasapiWinrt_SetDefaultDeviceId(const unsigned short *pId, int bOutput)
Definition: pa_win_wasapi.c:5468
Definition: pa_win_wasapi.h:186
Definition: pa_win_wasapi.h:243
unsigned long version
Definition: pa_win_wasapi.h:312
Definition: pa_win_wasapi.h:220
Definition: pa_win_wasapi.h:196
PaWasapiStreamCategory
Definition: pa_win_wasapi.h:275
PaWasapiJackConnectionType
Definition: pa_win_wasapi.h:175
struct PaWasapiJackDescription PaWasapiJackDescription
Definition: pa_win_wasapi.h:166
Definition: pa_win_wasapi.h:222
Definition: pa_win_wasapi.h:282
Definition: pa_win_wasapi.h:161
PaWasapiStreamOption
Definition: pa_win_wasapi.h:298
Windows specific PortAudio API extension and utilities header file.
#define paWasapiStreamStateThreadStop
Definition: pa_win_wasapi.h:127
Definition: pa_win_wasapi.h:167
Definition: pa_win_wasapi.h:208
PaWasapiJackGeoLocation
Definition: pa_win_wasapi.h:194
PaError PaWasapi_ThreadPriorityRevert(void *pTask)
Definition: pa_win_wasapi.c:5153
Definition: pa_win_wasapi.h:178
PaWasapiStreamState
Definition: pa_win_wasapi.h:104
PaWasapiJackGeoLocation geoLocation
Definition: pa_win_wasapi.h:259
Definition: pa_win_wasapi.h:248
Definition: pa_win_wasapi.h:231
int PaWasapi_GetDeviceCurrentFormat(PaStream *pStream, void *pFormat, unsigned int formatSize, int bOutput)
Definition: pa_win_wasapi.c:2506
#define paWinWasapiPolling
Definition: pa_win_wasapi.h:93
Definition: pa_win_wasapi.h:202
#define paWasapiStreamStateError
Definition: pa_win_wasapi.h:124
Definition: pa_win_wasapi.h:280
Definition: pa_win_wasapi.h:308
#define paWinWasapiAutoConvert
Definition: pa_win_wasapi.h:96
Definition: pa_win_wasapi.h:187
PaError PaWasapiWinrt_PopulateDeviceList(const unsigned short **pId, const unsigned short **pName, const PaWasapiDeviceRole *pRole, unsigned int count, int bOutput)
Definition: pa_win_wasapi.c:5496
Definition: pa_win_wasapi.h:177
PaWasapiStreamOption streamOption
Definition: pa_win_wasapi.h:351
int PaWasapi_GetDeviceRole(PaDeviceIndex device)
Definition: pa_win_wasapi.c:2590
Definition: pa_win_wasapi.h:165
Definition: pa_win_wasapi.h:279
default
Definition: pa_win_wasapi.h:300
Definition: pa_win_wasapi.h:286
struct PaWasapiStreamInfo PaWasapiStreamInfo
Definition: pa_win_wasapi.h:244
unsigned long channelMapping
Definition: pa_win_wasapi.h:256