1 //----------------------------------------------------------------------------------
3 // Copyright (c) 2008 Waves Audio Ltd. All rights reserved.
5 //! \file WCMRPortAudioDeviceManager.h
7 //! WCMRPortAudioDeviceManager and related class declarations
9 //---------------------------------------------------------------------------------*/
10 #ifndef __WCMRPortAudioDeviceManager_h_
11 #define __WCMRPortAudioDeviceManager_h_
13 #include "WCMRAudioDeviceManager.h"
14 #include "WCMRNativeAudio.h"
15 #include "portaudio.h"
18 class WCMRPortAudioDeviceManager;
20 //! Manages a port audio device, providing information
21 //! about the device, and managing audio callbacks.
22 class WCMRPortAudioDevice : public WCMRNativeAudioDevice
26 WCMRPortAudioDevice (WCMRPortAudioDeviceManager *pManager, unsigned int deviceID, bool useMultiThreading = true, bool bNoCopy = false);///<Constructor
27 virtual ~WCMRPortAudioDevice ();///<Destructor
29 virtual int CurrentSamplingRate(); ///<Current Sampling rate.?
30 virtual WTErr SetCurrentSamplingRate(int newRate);///<Change Current Sampling Rate : This is a requset, might not be successful at run time!
32 virtual int CurrentBufferSize();///<Current Buffer Size.? - note that this may change with change in sampling rate.
33 virtual WTErr SetCurrentBufferSize (int newSize);///<Change Current Buffer Size : This is a requset, might not be successful at run time!
35 virtual ConnectionStates ConnectionStatus();///< Connection Status - device available, gone, disconnected
37 virtual WTErr SetActive (bool newState);///<Prepare/Activate device.
39 virtual WTErr SetStreaming (bool newState);///<Start/Stop Streaming - should reconnect connections when streaming starts!
41 virtual WTErr SetMonitorChannels (int leftChannel, int rightChannel);///<Set monitor channels. - optional, will not be available with AG
42 virtual WTErr SetMonitorGain (float newGain);///<Set monitor gain. - optional, will not be available with AG
44 virtual WTErr ShowConfigPanel (void *pParam);///< Show Control Panel - in case of ASIO this will work only with Active device!
46 virtual int AudioCallback (const float *pInputBuffer, float *pOutputBuffer, unsigned long framesPerBuffe, bool dropsDetectedr);
48 virtual WTErr UpdateDeviceInfo ();
50 virtual WTErr ResetDevice();
53 static long StaticASIOMessageHook (void *pRefCon, long selector, long value, void* message, double* opt);
54 long ASIOMessageHook (long selector, long value, void* message, double* opt);
58 static DWORD WINAPI __DoIdle__(LPVOID lpThreadParameter);
60 // Methods which are executed by device processing thread
61 WTErr DoIdle();///<Do Idle Processing
63 void terminateDevice();
64 void updateDeviceInfo(bool callerIsWaiting = false);
65 void activateDevice(bool callerIsWaiting = false);
66 void deactivateDevice(bool callerIsWaiting = false);
67 void startStreaming(bool callerIsWaiting = false);
68 void stopStreaming(bool callerIsWaiting = false);
69 void resetDevice (bool callerIsWaiting = false);///<Reset device - close and reopen stream, update device information!
71 PaError testStateValidness(int sampleRate, int bufferSize);
72 ///////////////////////////////////////////////////////////
74 static int TheCallback (const void *pInputBuffer, void *pOutputBuffer, unsigned long framesPerBuffer,
75 const PaStreamCallbackTimeInfo* /*pTimeInfo*/, PaStreamCallbackFlags /*statusFlags*/, void *pUserData );
77 unsigned int m_DeviceID; ///< The PA device id
78 PaStream* m_PortAudioStream; ///< Port audio stream, when the device is active!
79 bool m_StopRequested; ///< should be set to true when want to stop, set to false otherwise.
80 const float *m_pInputData; ///< This is what came in with the most recent callback.
81 int m_SampleCounter; ///< The current running sample counter, updated by the audio callback.
82 int m_SampleCountAtLastIdle;
84 int m_DropsDetected; ///< Number of times audio drops have been detected so far.
85 int m_DropsReported; ///< Number of times audio drops have been reported so far to the client.
86 bool m_IgnoreThisDrop; ///< Allows disregarding the first drop
88 int m_BufferSizeChangeRequested;
89 int m_BufferSizeChangeReported;
92 int m_ResyncRequested;
95 HANDLE m_hDeviceProcessingThread;
96 DWORD m_DeviceProcessingThreadID;
98 ///< Backend request events
99 HANDLE m_hResetRequestedEvent;
102 HANDLE m_hUpdateDeviceInfoRequestedEvent;
103 HANDLE m_hUpdateDeviceInfoDone;
105 HANDLE m_hActivateRequestedEvent;
106 HANDLE m_hActivationDone;
108 HANDLE m_hDeActivateRequestedEvent;
109 HANDLE m_hDeActivationDone;
111 HANDLE m_hStartStreamingRequestedEvent;
112 HANDLE m_hStartStreamingDone;
114 HANDLE m_hStopStreamingRequestedEvent;
115 HANDLE m_hStopStreamingDone;
116 /////////////////////////
118 ///< Device request events
119 HANDLE m_hResetFromDevRequestedEvent;
120 HANDLE m_hBufferSizeChangedEvent;
121 HANDLE m_hSampleRateChangedEvent;
122 /////////////////////////////
125 HANDLE m_hDeviceInitialized;
126 HANDLE m_hExitIdleThread;
128 //Should be set if the device connection status is "DeviceErrors"
132 //! WCMRPortAudioDeviceManager
133 /*! The PortAudio Device Manager class */
134 class WCMRPortAudioDeviceManager : public WCMRAudioDeviceManager
137 WCMRPortAudioDeviceManager(WCMRAudioDeviceManagerClient *pTheClient, eAudioDeviceFilter eCurAudioDeviceFilter,
138 bool useMultithreading = true, bool bNocopy = false); ///< constructor
140 virtual ~WCMRPortAudioDeviceManager(void); ///< destructor
144 virtual WCMRAudioDevice* initNewCurrentDeviceImpl(const std::string & deviceName);
145 virtual void destroyCurrentDeviceImpl();
146 virtual WTErr generateDeviceListImpl(); // use this in derived class to fill device list
147 virtual WTErr updateDeviceListImpl() {return eNoErr; } // not supported
148 virtual WTErr getDeviceBufferSizesImpl(const std::string & deviceName, std::vector<int>& buffers) const;
150 bool m_UseMultithreading; ///< Flag indicates whether to use multi-threading for audio processing.
151 bool m_bNoCopyAudioBuffer;
154 // helper functions for this class only
155 WTErr getDeviceAvailableSampleRates(DeviceID deviceId, std::vector<int>& sampleRates);
157 WCMRAudioDevice* m_NoneDevice;
160 #endif //#ifndef __WCMRPortAudioDeviceManager_h_