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  * DirectSound specific extensions
7  *
8  * Copyright (c) 1999-2007 Ross Bencina and Phil Burk
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files
12  * (the "Software"), to deal in the Software without restriction,
13  * including without limitation the rights to use, copy, modify, merge,
14  * publish, distribute, sublicense, and/or sell copies of the Software,
15  * and to permit persons to whom the Software is furnished to do so,
16  * subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
25  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 /*
31  * The text above constitutes the entire PortAudio license; however,
32  * the PortAudio community also makes the following non-binding requests:
33  *
34  * Any person wishing to distribute modifications to the Software is
35  * requested to send the modifications to the original developer so that
36  * they can be incorporated into the canonical version. It is also
37  * requested that these non-binding requests be included along with the
38  * license above.
39  */
40 
46 #include "portaudio.h"
47 #include "pa_win_waveformat.h"
48 
49 #ifdef __cplusplus
50 extern "C"
51 {
52 #endif /* __cplusplus */
53 
54 
55 /* Setup flags */
56 typedef enum PaWasapiFlags
57 {
58  /* put WASAPI into exclusive mode */
59  paWinWasapiExclusive = (1 << 0),
60 
61  /* allow to skip internal PA processing completely */
62  paWinWasapiRedirectHostProcessor = (1 << 1),
63 
64  /* assign custom channel mask */
65  paWinWasapiUseChannelMask = (1 << 2),
66 
67  /* select non-Event driven method of data read/write
68  Note: WASAPI Event driven core is capable of 2ms latency!!!, but Polling
69  method can only provide 15-20ms latency. */
70  paWinWasapiPolling = (1 << 3),
71 
72  /* force custom thread priority setting, must be used if PaWasapiStreamInfo::threadPriority
73  is set to a custom value */
74  paWinWasapiThreadPriority = (1 << 4),
75 
76  /* force explicit sample format and do not allow PA to select suitable working format, API will
77  fail if provided sample format is not supported by audio hardware in Exclusive mode
78  or system mixer in Shared mode */
79  paWinWasapiExplicitSampleFormat = (1 << 5)
80 }
81 PaWasapiFlags;
82 #define paWinWasapiExclusive (paWinWasapiExclusive)
83 #define paWinWasapiRedirectHostProcessor (paWinWasapiRedirectHostProcessor)
84 #define paWinWasapiUseChannelMask (paWinWasapiUseChannelMask)
85 #define paWinWasapiPolling (paWinWasapiPolling)
86 #define paWinWasapiThreadPriority (paWinWasapiThreadPriority)
87 #define paWinWasapiExplicitSampleFormat (paWinWasapiExplicitSampleFormat)
88 
89 
90 /* Host processor. Allows to skip internal PA processing completely.
91  You must set paWinWasapiRedirectHostProcessor flag to PaWasapiStreamInfo::flags member
92  in order to have host processor redirected to your callback.
93  Use with caution! inputFrames and outputFrames depend solely on final device setup.
94  To query maximal values of inputFrames/outputFrames use PaWasapi_GetFramesPerHostBuffer.
95 */
96 typedef void (*PaWasapiHostProcessorCallback) (void *inputBuffer, long inputFrames,
97  void *outputBuffer, long outputFrames,
98  void *userData);
99 
100 /* Device role. */
101 typedef enum PaWasapiDeviceRole
102 {
103  eRoleRemoteNetworkDevice = 0,
104  eRoleSpeakers,
105  eRoleLineLevel,
106  eRoleHeadphones,
107  eRoleMicrophone,
108  eRoleHeadset,
109  eRoleHandset,
110  eRoleUnknownDigitalPassthrough,
111  eRoleSPDIF,
112  eRoleHDMI,
113  eRoleUnknownFormFactor
114 }
115 PaWasapiDeviceRole;
116 
117 
118 /* Jack connection type. */
119 typedef enum PaWasapiJackConnectionType
120 {
121  eJackConnTypeUnknown,
122  eJackConnType3Point5mm,
123  eJackConnTypeQuarter,
124  eJackConnTypeAtapiInternal,
125  eJackConnTypeRCA,
126  eJackConnTypeOptical,
127  eJackConnTypeOtherDigital,
128  eJackConnTypeOtherAnalog,
129  eJackConnTypeMultichannelAnalogDIN,
130  eJackConnTypeXlrProfessional,
131  eJackConnTypeRJ11Modem,
132  eJackConnTypeCombination
133 }
134 PaWasapiJackConnectionType;
135 
136 
137 /* Jack geometric location. */
138 typedef enum PaWasapiJackGeoLocation
139 {
140  eJackGeoLocUnk = 0,
141  eJackGeoLocRear = 0x1, /* matches EPcxGeoLocation::eGeoLocRear */
142  eJackGeoLocFront,
143  eJackGeoLocLeft,
144  eJackGeoLocRight,
145  eJackGeoLocTop,
146  eJackGeoLocBottom,
147  eJackGeoLocRearPanel,
148  eJackGeoLocRiser,
149  eJackGeoLocInsideMobileLid,
150  eJackGeoLocDrivebay,
151  eJackGeoLocHDMI,
152  eJackGeoLocOutsideMobileLid,
153  eJackGeoLocATAPI,
154  eJackGeoLocReserved5,
155  eJackGeoLocReserved6,
156 }
157 PaWasapiJackGeoLocation;
158 
159 
160 /* Jack general location. */
161 typedef enum PaWasapiJackGenLocation
162 {
163  eJackGenLocPrimaryBox = 0,
164  eJackGenLocInternal,
165  eJackGenLocSeparate,
166  eJackGenLocOther
167 }
168 PaWasapiJackGenLocation;
169 
170 
171 /* Jack's type of port. */
172 typedef enum PaWasapiJackPortConnection
173 {
174  eJackPortConnJack = 0,
175  eJackPortConnIntegratedDevice,
176  eJackPortConnBothIntegratedAndJack,
177  eJackPortConnUnknown
178 }
179 PaWasapiJackPortConnection;
180 
181 
182 /* Thread priority. */
184 {
185  eThreadPriorityNone = 0,
187  eThreadPriorityCapture,
188  eThreadPriorityDistribution,
189  eThreadPriorityGames,
190  eThreadPriorityPlayback,
192  eThreadPriorityWindowManager
193 }
195 
196 
197 /* Stream descriptor. */
198 typedef struct PaWasapiJackDescription
199 {
200  unsigned long channelMapping;
201  unsigned long color; /* derived from macro: #define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))) */
202  PaWasapiJackConnectionType connectionType;
203  PaWasapiJackGeoLocation geoLocation;
204  PaWasapiJackGenLocation genLocation;
205  PaWasapiJackPortConnection portConnection;
206  unsigned int isConnected;
207 }
209 
210 
220 {
221  eAudioCategoryOther = 0,
222  eAudioCategoryCommunications = 3,
223  eAudioCategoryAlerts = 4,
224  eAudioCategorySoundEffects = 5,
225  eAudioCategoryGameEffects = 6,
226  eAudioCategoryGameMedia = 7,
227  eAudioCategoryGameChat = 8,
228  eAudioCategorySpeech = 9,
229  eAudioCategoryMovie = 10,
230  eAudioCategoryMedia = 11
231 }
233 
234 
243 {
247 }
249 
250 
251 /* Stream descriptor. */
252 typedef struct PaWasapiStreamInfo
253 {
254  unsigned long size;
256  unsigned long version;
258  unsigned long flags;
266  PaWinWaveFormatChannelMask channelMask;
267 
273  PaWasapiHostProcessorCallback hostProcessorOutput;
274  PaWasapiHostProcessorCallback hostProcessorInput;
275 
284 
290 
296 }
298 
299 
308 PaError PaWasapi_GetAudioClient( PaStream *pStream, void **pAudioClient, int bOutput );
309 
310 
321 
322 
336 int PaWasapi_GetDeviceCurrentFormat( PaStream *pStream, void *pFormat, unsigned int nFormatSize, int bOutput );
337 
338 
350 int PaWasapi_GetDeviceDefaultFormat( void *pFormat, unsigned int nFormatSize, PaDeviceIndex nDevice );
351 
352 
360 int/*PaWasapiDeviceRole*/ PaWasapi_GetDeviceRole( PaDeviceIndex nDevice );
361 
362 
375 PaError PaWasapi_ThreadPriorityBoost( void **hTask, PaWasapiThreadPriority nPriorityClass );
376 
377 
386 
387 
398 PaError PaWasapi_GetFramesPerHostBuffer( PaStream *pStream, unsigned int *nInput, unsigned int *nOutput );
399 
400 
412 PaError PaWasapi_GetJackCount( PaDeviceIndex nDevice, int *jcount );
413 
414 
429 PaError PaWasapi_GetJackDescription( PaDeviceIndex nDevice, int jindex, PaWasapiJackDescription *pJackDescription );
430 
431 
449 PaError PaWasapi_SetDefaultInterfaceId( unsigned short *pId, int bOutput );
450 
451 
452 /*
453  IMPORTANT:
454 
455  WASAPI is implemented for Callback and Blocking interfaces. It supports Shared and Exclusive
456  share modes.
457 
458  Exclusive Mode:
459 
460  Exclusive mode allows to deliver audio data directly to hardware bypassing
461  software mixing.
462  Exclusive mode is specified by 'paWinWasapiExclusive' flag.
463 
464  Callback Interface:
465 
466  Provides best audio quality with low latency. Callback interface is implemented in
467  two versions:
468 
469  1) Event-Driven:
470  This is the most powerful WASAPI implementation which provides glitch-free
471  audio at around 3ms latency in Exclusive mode. Lowest possible latency for this mode is
472  3 ms for HD Audio class audio chips. For the Shared mode latency can not be
473  lower than 20 ms.
474 
475  2) Poll-Driven:
476  Polling is another 2-nd method to operate with WASAPI. It is less efficient than Event-Driven
477  and provides latency at around 10-13ms. Polling must be used to overcome a system bug
478  under Windows Vista x64 when application is WOW64(32-bit) and Event-Driven method simply
479  times out (event handle is never signalled on buffer completion). Please note, such WOW64 bug
480  does not exist in Vista x86 or Windows 7.
481  Polling can be setup by speciying 'paWinWasapiPolling' flag. Our WASAPI implementation detects
482  WOW64 bug and sets 'paWinWasapiPolling' automatically.
483 
484  Thread priority:
485 
486  Normally thread priority is set automatically and does not require modification. Although
487  if user wants some tweaking thread priority can be modified by setting 'paWinWasapiThreadPriority'
488  flag and specifying 'PaWasapiStreamInfo::threadPriority' with value from PaWasapiThreadPriority
489  enum.
490 
491  Blocking Interface:
492 
493  Blocking interface is implemented but due to above described Poll-Driven method can not
494  deliver lowest possible latency. Specifying too low latency in Shared mode will result in
495  distorted audio although Exclusive mode adds stability.
496 
497  8.24 format:
498 
499  If paCustomFormat is specified as sample format then the implementation will understand it
500  as valid 24-bits inside 32-bit container (e.g. wBitsPerSample = 32, Samples.wValidBitsPerSample = 24).
501 
502  By using paCustomFormat there will be small optimization when samples are be copied
503  with Copy_24_To_24 by PA processor instead of conversion from packed 3-byte (24-bit) data
504  with Int24_To_Int32.
505 
506  Pa_IsFormatSupported:
507 
508  To check format with correct Share Mode (Exclusive/Shared) you must supply PaWasapiStreamInfo
509  with flags paWinWasapiExclusive set through member of PaStreamParameters::hostApiSpecificStreamInfo
510  structure.
511 
512  If paWinWasapiExplicitSampleFormat flag is provided then implementation will not try to select
513  suitable close format and will return an error instead of paFormatIsSupported. By specifying
514  paWinWasapiExplicitSampleFormat flag it is possible to find out what sample formats are
515  supported by Exclusive or Shared modes.
516 
517  Pa_OpenStream:
518 
519  To set desired Share Mode (Exclusive/Shared) you must supply
520  PaWasapiStreamInfo with flags paWinWasapiExclusive set through member of
521  PaStreamParameters::hostApiSpecificStreamInfo structure.
522 */
523 
524 #ifdef __cplusplus
525 }
526 #endif /* __cplusplus */
527 
528 #endif /* PA_WIN_WASAPI_H */