Chris@45: #ifndef PA_WIN_WASAPI_H Chris@45: #define PA_WIN_WASAPI_H Chris@45: /* Chris@45: * $Id: $ Chris@45: * PortAudio Portable Real-Time Audio Library Chris@45: * DirectSound specific extensions Chris@45: * Chris@45: * Copyright (c) 1999-2007 Ross Bencina and Phil Burk Chris@45: * Chris@45: * Permission is hereby granted, free of charge, to any person obtaining Chris@45: * a copy of this software and associated documentation files Chris@45: * (the "Software"), to deal in the Software without restriction, Chris@45: * including without limitation the rights to use, copy, modify, merge, Chris@45: * publish, distribute, sublicense, and/or sell copies of the Software, Chris@45: * and to permit persons to whom the Software is furnished to do so, Chris@45: * subject to the following conditions: Chris@45: * Chris@45: * The above copyright notice and this permission notice shall be Chris@45: * included in all copies or substantial portions of the Software. Chris@45: * Chris@45: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@45: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@45: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@45: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR Chris@45: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@45: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@45: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@45: */ Chris@45: Chris@45: /* Chris@45: * The text above constitutes the entire PortAudio license; however, Chris@45: * the PortAudio community also makes the following non-binding requests: Chris@45: * Chris@45: * Any person wishing to distribute modifications to the Software is Chris@45: * requested to send the modifications to the original developer so that Chris@45: * they can be incorporated into the canonical version. It is also Chris@45: * requested that these non-binding requests be included along with the Chris@45: * license above. Chris@45: */ Chris@45: Chris@45: /** @file Chris@45: @ingroup public_header Chris@45: @brief WASAPI-specific PortAudio API extension header file. Chris@45: */ Chris@45: Chris@45: #include "portaudio.h" Chris@45: #include "pa_win_waveformat.h" Chris@45: Chris@45: #ifdef __cplusplus Chris@45: extern "C" Chris@45: { Chris@45: #endif /* __cplusplus */ Chris@45: Chris@45: Chris@45: /* Setup flags */ Chris@45: typedef enum PaWasapiFlags Chris@45: { Chris@45: /* puts WASAPI into exclusive mode */ Chris@45: paWinWasapiExclusive = (1 << 0), Chris@45: Chris@45: /* allows to skip internal PA processing completely */ Chris@45: paWinWasapiRedirectHostProcessor = (1 << 1), Chris@45: Chris@45: /* assigns custom channel mask */ Chris@45: paWinWasapiUseChannelMask = (1 << 2), Chris@45: Chris@45: /* selects non-Event driven method of data read/write Chris@45: Note: WASAPI Event driven core is capable of 2ms latency!!!, but Polling Chris@45: method can only provide 15-20ms latency. */ Chris@45: paWinWasapiPolling = (1 << 3), Chris@45: Chris@45: /* forces custom thread priority setting. must be used if PaWasapiStreamInfo::threadPriority Chris@45: is set to custom value. */ Chris@45: paWinWasapiThreadPriority = (1 << 4) Chris@45: } Chris@45: PaWasapiFlags; Chris@45: #define paWinWasapiExclusive (paWinWasapiExclusive) Chris@45: #define paWinWasapiRedirectHostProcessor (paWinWasapiRedirectHostProcessor) Chris@45: #define paWinWasapiUseChannelMask (paWinWasapiUseChannelMask) Chris@45: #define paWinWasapiPolling (paWinWasapiPolling) Chris@45: #define paWinWasapiThreadPriority (paWinWasapiThreadPriority) Chris@45: Chris@45: Chris@45: /* Host processor. Allows to skip internal PA processing completely. Chris@45: You must set paWinWasapiRedirectHostProcessor flag to PaWasapiStreamInfo::flags member Chris@45: in order to have host processor redirected to your callback. Chris@45: Use with caution! inputFrames and outputFrames depend solely on final device setup. Chris@45: To query maximal values of inputFrames/outputFrames use PaWasapi_GetFramesPerHostBuffer. Chris@45: */ Chris@45: typedef void (*PaWasapiHostProcessorCallback) (void *inputBuffer, long inputFrames, Chris@45: void *outputBuffer, long outputFrames, Chris@45: void *userData); Chris@45: Chris@45: /* Device role */ Chris@45: typedef enum PaWasapiDeviceRole Chris@45: { Chris@45: eRoleRemoteNetworkDevice = 0, Chris@45: eRoleSpeakers, Chris@45: eRoleLineLevel, Chris@45: eRoleHeadphones, Chris@45: eRoleMicrophone, Chris@45: eRoleHeadset, Chris@45: eRoleHandset, Chris@45: eRoleUnknownDigitalPassthrough, Chris@45: eRoleSPDIF, Chris@45: eRoleHDMI, Chris@45: eRoleUnknownFormFactor Chris@45: } Chris@45: PaWasapiDeviceRole; Chris@45: Chris@45: Chris@45: /* Jack connection type */ Chris@45: typedef enum PaWasapiJackConnectionType Chris@45: { Chris@45: eJackConnTypeUnknown, Chris@45: eJackConnType3Point5mm, Chris@45: eJackConnTypeQuarter, Chris@45: eJackConnTypeAtapiInternal, Chris@45: eJackConnTypeRCA, Chris@45: eJackConnTypeOptical, Chris@45: eJackConnTypeOtherDigital, Chris@45: eJackConnTypeOtherAnalog, Chris@45: eJackConnTypeMultichannelAnalogDIN, Chris@45: eJackConnTypeXlrProfessional, Chris@45: eJackConnTypeRJ11Modem, Chris@45: eJackConnTypeCombination Chris@45: } Chris@45: PaWasapiJackConnectionType; Chris@45: Chris@45: Chris@45: /* Jack geometric location */ Chris@45: typedef enum PaWasapiJackGeoLocation Chris@45: { Chris@45: eJackGeoLocUnk = 0, Chris@45: eJackGeoLocRear = 0x1, /* matches EPcxGeoLocation::eGeoLocRear */ Chris@45: eJackGeoLocFront, Chris@45: eJackGeoLocLeft, Chris@45: eJackGeoLocRight, Chris@45: eJackGeoLocTop, Chris@45: eJackGeoLocBottom, Chris@45: eJackGeoLocRearPanel, Chris@45: eJackGeoLocRiser, Chris@45: eJackGeoLocInsideMobileLid, Chris@45: eJackGeoLocDrivebay, Chris@45: eJackGeoLocHDMI, Chris@45: eJackGeoLocOutsideMobileLid, Chris@45: eJackGeoLocATAPI, Chris@45: eJackGeoLocReserved5, Chris@45: eJackGeoLocReserved6, Chris@45: } Chris@45: PaWasapiJackGeoLocation; Chris@45: Chris@45: Chris@45: /* Jack general location */ Chris@45: typedef enum PaWasapiJackGenLocation Chris@45: { Chris@45: eJackGenLocPrimaryBox = 0, Chris@45: eJackGenLocInternal, Chris@45: eJackGenLocSeparate, Chris@45: eJackGenLocOther Chris@45: } Chris@45: PaWasapiJackGenLocation; Chris@45: Chris@45: Chris@45: /* Jack's type of port */ Chris@45: typedef enum PaWasapiJackPortConnection Chris@45: { Chris@45: eJackPortConnJack = 0, Chris@45: eJackPortConnIntegratedDevice, Chris@45: eJackPortConnBothIntegratedAndJack, Chris@45: eJackPortConnUnknown Chris@45: } Chris@45: PaWasapiJackPortConnection; Chris@45: Chris@45: Chris@45: /* Thread priority */ Chris@45: typedef enum PaWasapiThreadPriority Chris@45: { Chris@45: eThreadPriorityNone = 0, Chris@45: eThreadPriorityAudio, //!< Default for Shared mode. Chris@45: eThreadPriorityCapture, Chris@45: eThreadPriorityDistribution, Chris@45: eThreadPriorityGames, Chris@45: eThreadPriorityPlayback, Chris@45: eThreadPriorityProAudio, //!< Default for Exclusive mode. Chris@45: eThreadPriorityWindowManager Chris@45: } Chris@45: PaWasapiThreadPriority; Chris@45: Chris@45: Chris@45: /* Stream descriptor. */ Chris@45: typedef struct PaWasapiJackDescription Chris@45: { Chris@45: unsigned long channelMapping; Chris@45: unsigned long color; /* derived from macro: #define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))) */ Chris@45: PaWasapiJackConnectionType connectionType; Chris@45: PaWasapiJackGeoLocation geoLocation; Chris@45: PaWasapiJackGenLocation genLocation; Chris@45: PaWasapiJackPortConnection portConnection; Chris@45: unsigned int isConnected; Chris@45: } Chris@45: PaWasapiJackDescription; Chris@45: Chris@45: Chris@45: /* Stream descriptor. */ Chris@45: typedef struct PaWasapiStreamInfo Chris@45: { Chris@45: unsigned long size; /**< sizeof(PaWasapiStreamInfo) */ Chris@45: PaHostApiTypeId hostApiType; /**< paWASAPI */ Chris@45: unsigned long version; /**< 1 */ Chris@45: Chris@45: unsigned long flags; /**< collection of PaWasapiFlags */ Chris@45: Chris@45: /* Support for WAVEFORMATEXTENSIBLE channel masks. If flags contains Chris@45: paWinWasapiUseChannelMask this allows you to specify which speakers Chris@45: to address in a multichannel stream. Constants for channelMask Chris@45: are specified in pa_win_waveformat.h. Will be used only if Chris@45: paWinWasapiUseChannelMask flag is specified. Chris@45: */ Chris@45: PaWinWaveFormatChannelMask channelMask; Chris@45: Chris@45: /* Delivers raw data to callback obtained from GetBuffer() methods skipping Chris@45: internal PortAudio processing inventory completely. userData parameter will Chris@45: be the same that was passed to Pa_OpenStream method. Will be used only if Chris@45: paWinWasapiRedirectHostProcessor flag is specified. Chris@45: */ Chris@45: PaWasapiHostProcessorCallback hostProcessorOutput; Chris@45: PaWasapiHostProcessorCallback hostProcessorInput; Chris@45: Chris@45: /* Specifies thread priority explicitly. Will be used only if paWinWasapiThreadPriority flag Chris@45: is specified. Chris@45: Chris@45: Please note, if Input/Output streams are opened simultaniously (Full-Duplex mode) Chris@45: you shall specify same value for threadPriority or othervise one of the values will be used Chris@45: to setup thread priority. Chris@45: */ Chris@45: PaWasapiThreadPriority threadPriority; Chris@45: } Chris@45: PaWasapiStreamInfo; Chris@45: Chris@45: Chris@45: /** Returns default sound format for device. Format is represented by PaWinWaveFormat or Chris@45: WAVEFORMATEXTENSIBLE structure. Chris@45: Chris@45: @param pFormat Pointer to PaWinWaveFormat or WAVEFORMATEXTENSIBLE structure. Chris@45: @param nFormatSize Size of PaWinWaveFormat or WAVEFORMATEXTENSIBLE structure in bytes. Chris@45: @param nDevice Device index. Chris@45: Chris@45: @return Non-negative value indicating the number of bytes copied into format decriptor Chris@45: or, a PaErrorCode (which are always negative) if PortAudio is not initialized Chris@45: or an error is encountered. Chris@45: */ Chris@45: int PaWasapi_GetDeviceDefaultFormat( void *pFormat, unsigned int nFormatSize, PaDeviceIndex nDevice ); Chris@45: Chris@45: Chris@45: /** Returns device role (PaWasapiDeviceRole enum). Chris@45: Chris@45: @param nDevice device index. Chris@45: Chris@45: @return Non-negative value indicating device role or, a PaErrorCode (which are always negative) Chris@45: if PortAudio is not initialized or an error is encountered. Chris@45: */ Chris@45: int/*PaWasapiDeviceRole*/ PaWasapi_GetDeviceRole( PaDeviceIndex nDevice ); Chris@45: Chris@45: Chris@45: /** Boost thread priority of calling thread (MMCSS). Use it for Blocking Interface only for thread Chris@45: which makes calls to Pa_WriteStream/Pa_ReadStream. Chris@45: Chris@45: @param hTask Handle to pointer to priority task. Must be used with PaWasapi_RevertThreadPriority Chris@45: method to revert thread priority to initial state. Chris@45: Chris@45: @param nPriorityClass Id of thread priority of PaWasapiThreadPriority type. Specifying Chris@45: eThreadPriorityNone does nothing. Chris@45: Chris@45: @return Error code indicating success or failure. Chris@45: @see PaWasapi_RevertThreadPriority Chris@45: */ Chris@45: PaError PaWasapi_ThreadPriorityBoost( void **hTask, PaWasapiThreadPriority nPriorityClass ); Chris@45: Chris@45: Chris@45: /** Boost thread priority of calling thread (MMCSS). Use it for Blocking Interface only for thread Chris@45: which makes calls to Pa_WriteStream/Pa_ReadStream. Chris@45: Chris@45: @param hTask Task handle obtained by PaWasapi_BoostThreadPriority method. Chris@45: @return Error code indicating success or failure. Chris@45: @see PaWasapi_BoostThreadPriority Chris@45: */ Chris@45: PaError PaWasapi_ThreadPriorityRevert( void *hTask ); Chris@45: Chris@45: Chris@45: /** Get number of frames per host buffer. This is maximal value of frames of WASAPI buffer which Chris@45: can be locked for operations. Use this method as helper to findout maximal values of Chris@45: inputFrames/outputFrames of PaWasapiHostProcessorCallback. Chris@45: Chris@45: @param pStream Pointer to PaStream to query. Chris@45: @param nInput Pointer to variable to receive number of input frames. Can be NULL. Chris@45: @param nOutput Pointer to variable to receive number of output frames. Can be NULL. Chris@45: @return Error code indicating success or failure. Chris@45: @see PaWasapiHostProcessorCallback Chris@45: */ Chris@45: PaError PaWasapi_GetFramesPerHostBuffer( PaStream *pStream, unsigned int *nInput, unsigned int *nOutput ); Chris@45: Chris@45: Chris@45: /** Get number of jacks associated with a WASAPI device. Use this method to determine if Chris@45: there are any jacks associated with the provided WASAPI device. Not all audio devices Chris@45: will support this capability. This is valid for both input and output devices. Chris@45: @param nDevice device index. Chris@45: @param jcount Number of jacks is returned in this variable Chris@45: @return Error code indicating success or failure Chris@45: @see PaWasapi_GetJackDescription Chris@45: */ Chris@45: PaError PaWasapi_GetJackCount(PaDeviceIndex nDevice, int *jcount); Chris@45: Chris@45: Chris@45: /** Get the jack description associated with a WASAPI device and jack number Chris@45: Before this function is called, use PaWasapi_GetJackCount to determine the Chris@45: number of jacks associated with device. If jcount is greater than zero, then Chris@45: each jack from 0 to jcount can be queried with this function to get the jack Chris@45: description. Chris@45: @param nDevice device index. Chris@45: @param jindex Which jack to return information Chris@45: @param KSJACK_DESCRIPTION This structure filled in on success. Chris@45: @return Error code indicating success or failure Chris@45: @see PaWasapi_GetJackCount Chris@45: */ Chris@45: PaError PaWasapi_GetJackDescription(PaDeviceIndex nDevice, int jindex, PaWasapiJackDescription *pJackDescription); Chris@45: Chris@45: Chris@45: /* Chris@45: IMPORTANT: Chris@45: Chris@45: WASAPI is implemented for Callback and Blocking interfaces. It supports Shared and Exclusive Chris@45: share modes. Chris@45: Chris@45: Exclusive Mode: Chris@45: Chris@45: Exclusive mode allows to deliver audio data directly to hardware bypassing Chris@45: software mixing. Chris@45: Exclusive mode is specified by 'paWinWasapiExclusive' flag. Chris@45: Chris@45: Callback Interface: Chris@45: Chris@45: Provides best audio quality with low latency. Callback interface is implemented in Chris@45: two versions: Chris@45: Chris@45: 1) Event-Driven: Chris@45: This is the most powerful WASAPI implementation which provides glitch-free Chris@45: audio at around 3ms latency in Exclusive mode. Lowest possible latency for this mode is Chris@45: 3 ms for HD Audio class audio chips. For the Shared mode latency can not be Chris@45: lower than 20 ms. Chris@45: Chris@45: 2) Poll-Driven: Chris@45: Polling is another 2-nd method to operate with WASAPI. It is less efficient than Event-Driven Chris@45: and provides latency at around 10-13ms. Polling must be used to overcome a system bug Chris@45: under Windows Vista x64 when application is WOW64(32-bit) and Event-Driven method simply Chris@45: times out (event handle is never signalled on buffer completion). Please note, such WOW64 bug Chris@45: does not exist in Vista x86 or Windows 7. Chris@45: Polling can be setup by speciying 'paWinWasapiPolling' flag. Our WASAPI implementation detects Chris@45: WOW64 bug and sets 'paWinWasapiPolling' automatically. Chris@45: Chris@45: Thread priority: Chris@45: Chris@45: Normally thread priority is set automatically and does not require modification. Although Chris@45: if user wants some tweaking thread priority can be modified by setting 'paWinWasapiThreadPriority' Chris@45: flag and specifying 'PaWasapiStreamInfo::threadPriority' with value from PaWasapiThreadPriority Chris@45: enum. Chris@45: Chris@45: Blocking Interface: Chris@45: Chris@45: Blocking interface is implemented but due to above described Poll-Driven method can not Chris@45: deliver lowest possible latency. Specifying too low latency in Shared mode will result in Chris@45: distorted audio although Exclusive mode adds stability. Chris@45: Chris@45: Pa_IsFormatSupported: Chris@45: Chris@45: To check format with correct Share Mode (Exclusive/Shared) you must supply Chris@45: PaWasapiStreamInfo with flags paWinWasapiExclusive set through member of Chris@45: PaStreamParameters::hostApiSpecificStreamInfo structure. Chris@45: Chris@45: Pa_OpenStream: Chris@45: Chris@45: To set desired Share Mode (Exclusive/Shared) you must supply Chris@45: PaWasapiStreamInfo with flags paWinWasapiExclusive set through member of Chris@45: PaStreamParameters::hostApiSpecificStreamInfo structure. Chris@45: */ Chris@45: Chris@45: #ifdef __cplusplus Chris@45: } Chris@45: #endif /* __cplusplus */ Chris@45: Chris@45: #endif /* PA_WIN_WASAPI_H */