2nd attempt at updated Waves audio backend, with added -fms-extensions as previously...
[ardour.git] / libs / backends / wavesaudio / wavesapi / devicemanager / WCMRPortAudioDeviceManager.h
1 //----------------------------------------------------------------------------------
2 //
3 // Copyright (c) 2008 Waves Audio Ltd. All rights reserved.
4 //
5 //! \file       WCMRPortAudioDeviceManager.h
6 //!
7 //! WCMRPortAudioDeviceManager and related class declarations
8 //!
9 //---------------------------------------------------------------------------------*/
10 #ifndef __WCMRPortAudioDeviceManager_h_
11         #define __WCMRPortAudioDeviceManager_h_
12
13 #include "WCMRAudioDeviceManager.h"
14 #include "WCMRNativeAudio.h"
15 #include "portaudio.h"
16
17 //forward decl.
18 class WCMRPortAudioDeviceManager;
19
20 //! Manages a port audio device, providing information
21 //! about the device, and managing audio callbacks.
22 class WCMRPortAudioDevice : public WCMRNativeAudioDevice
23 {
24 public:
25
26         WCMRPortAudioDevice (WCMRPortAudioDeviceManager *pManager, unsigned int  deviceID, bool useMultiThreading = true, bool bNoCopy = false);///<Constructor
27         virtual ~WCMRPortAudioDevice ();///<Destructor
28
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!
31
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!
34
35         virtual ConnectionStates ConnectionStatus();///< Connection Status - device available, gone, disconnected
36
37         virtual WTErr SetActive (bool newState);///<Prepare/Activate device.
38         
39         virtual WTErr SetStreaming (bool newState);///<Start/Stop Streaming - should reconnect connections when streaming starts!
40
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
43         
44         virtual WTErr ShowConfigPanel (void *pParam);///< Show Control Panel - in case of ASIO this will work only with Active device!
45
46         virtual int AudioCallback (const float *pInputBuffer, float *pOutputBuffer, unsigned long framesPerBuffe, bool dropsDetectedr);
47
48         virtual WTErr UpdateDeviceInfo ();
49
50         virtual WTErr ResetDevice();
51
52 #ifdef _WINDOWS
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);
55 #endif //_WINDOWS
56         
57 protected:
58         static DWORD WINAPI __DoIdle__(LPVOID lpThreadParameter);
59
60         // Methods which are executed by device processing thread
61         WTErr DoIdle();///<Do Idle Processing
62         void initDevice();
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!
70
71         PaError testStateValidness(int sampleRate, int bufferSize);
72         ///////////////////////////////////////////////////////////
73         
74         static int TheCallback (const void *pInputBuffer, void *pOutputBuffer, unsigned long framesPerBuffer, 
75                                                         const PaStreamCallbackTimeInfo* /*pTimeInfo*/, PaStreamCallbackFlags /*statusFlags*/, void *pUserData );
76
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;
83
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
87
88         int m_BufferSizeChangeRequested;
89         int m_BufferSizeChangeReported;
90         int m_ResetRequested;
91         int m_ResetReported;
92         int m_ResyncRequested;
93         int m_ResyncReported;
94
95         HANDLE m_hDeviceProcessingThread;
96         DWORD m_DeviceProcessingThreadID;
97
98         ///< Backend request events
99         HANDLE m_hResetRequestedEvent;
100         HANDLE m_hResetDone;
101
102         HANDLE m_hUpdateDeviceInfoRequestedEvent;
103         HANDLE m_hUpdateDeviceInfoDone;
104
105         HANDLE m_hActivateRequestedEvent;
106         HANDLE m_hActivationDone;
107
108         HANDLE m_hDeActivateRequestedEvent;
109         HANDLE m_hDeActivationDone;
110
111         HANDLE m_hStartStreamingRequestedEvent;
112         HANDLE m_hStartStreamingDone;
113
114         HANDLE m_hStopStreamingRequestedEvent;
115         HANDLE m_hStopStreamingDone;
116         /////////////////////////
117
118         ///< Device request events
119         HANDLE m_hResetFromDevRequestedEvent;
120         HANDLE m_hBufferSizeChangedEvent;
121         HANDLE m_hSampleRateChangedEvent;
122         /////////////////////////////
123
124         ///< Sync events
125         HANDLE m_hDeviceInitialized;
126         HANDLE m_hExitIdleThread;
127
128         //Should be set if the device connection status is "DeviceErrors"
129         WTErr m_lastErr;
130 };
131
132 //! WCMRPortAudioDeviceManager
133 /*! The PortAudio Device Manager class */
134 class WCMRPortAudioDeviceManager : public WCMRAudioDeviceManager
135 {
136 public:
137         WCMRPortAudioDeviceManager(WCMRAudioDeviceManagerClient *pTheClient, eAudioDeviceFilter eCurAudioDeviceFilter,
138                                                                 bool useMultithreading = true, bool bNocopy = false); ///< constructor
139         
140         virtual ~WCMRPortAudioDeviceManager(void); ///< destructor
141
142 protected:
143
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;
149
150         bool m_UseMultithreading; ///< Flag indicates whether to use multi-threading for audio processing.
151     bool m_bNoCopyAudioBuffer;
152
153 private:
154         // helper functions for this class only
155     WTErr getDeviceAvailableSampleRates(DeviceID deviceId, std::vector<int>& sampleRates);
156
157         WCMRAudioDevice*                        m_NoneDevice;
158 };
159
160 #endif //#ifndef __WCMRPortAudioDeviceManager_h_