new audio engine backend for native CoreAudio audio I/O, and PortMIDI for MIDI.
[ardour.git] / libs / backends / wavesaudio / wavesapi / devicemanager / WCMRAudioDeviceManager.h
1 /*
2     Copyright (C) 2013 Waves Audio Ltd.
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19 //----------------------------------------------------------------------------------
20 //
21 //
22 //! \file       WCMRAudioDeviceManager.h
23 //!
24 //! WCMRAudioDeviceManager and related class declarations
25 //!
26 //---------------------------------------------------------------------------------*/
27 #ifndef __WCMRAudioDeviceManager_h_
28         #define __WCMRAudioDeviceManager_h_
29
30 /* Copy to include
31 #include "WCMRAudioDeviceManager.h"
32 */
33
34 #define AUTO_FUNC_DEBUG
35 #define DEBUG_MSG(a)
36 #define ASSERT_ERROR(a, b)
37 #define TRACE_MSG(a)
38
39 #include <string>
40 #include <vector>
41 #include <map>
42 #include "WCRefManager.h"
43 #include "BasicTypes/WUTypes.h"
44 #include "WUErrors.h"
45
46 #define WCUNUSEDPARAM(a)
47
48 //forward decl.
49 class WCMRAudioConnection;
50 class WCMRAudioDevice;
51 class WCMRAudioDeviceManager;
52
53 typedef std::vector<WCMRAudioDevice *> WCMRAudioDeviceList; ///< Vector for audio devices
54 typedef std::vector<WCMRAudioDevice *>::iterator WCMRAudioDeviceListIter; ///< Vector iterator for audio devices
55 typedef std::vector<WCMRAudioDevice *>::const_iterator WCMRAudioDeviceListConstIter; ///< Vector iterator for audio devices
56 typedef std::vector<WCMRAudioConnection *> WCMRAudioConnectionsList; ///< Vector for audio devices
57
58
59 /// for notification... A client must derive it's class from us.
60 class WCMRAudioDeviceManagerClient
61 {
62         public:
63         enum NotificationReason
64         {
65                 DeviceListChanged,
66                 Dropout,
67                 RequestReset,
68                 RequestResync,
69                 SamplingRateChanged, //param has new SR, or -1 if not known
70         SamplingRateChangedSilent, //To indicate sampling rate changed but no need to notify user
71                 BufferSizeChanged,
72                 ClockSourceChanged,
73                 DeviceStoppedStreaming,
74                 DeviceDroppedSamples,
75                 DeviceConnectionLost,
76                 DeviceGenericError,
77                 DeviceStatusChanged,
78                 DeviceStatisticsUpdated,
79                 DeviceDebugInfo, //param has c string
80                 DeviceProgressInfo, //param has c string
81                 MIDIData,
82                 MIDINodeUp,
83                 MIDINodeDown,
84                 DeviceSampleRateMisMatch,
85                 SystemSamplingRateChangedInfoOnly,
86                 LostClockSource,
87                 IODeviceDisconnected,
88                 ChannelCountModified,
89                 MasterUp,
90                 MasterDown,
91                 AudioDropFound,
92                 ReflasherEvent,
93         AGDeviceSamplingRateChangedInfoOnly,
94                 IODeviceNameChanged,
95         SetDisplayNameFromIOModule,
96         IOMStateChanged,    ///< This is used when IOM state is changed.
97         AudioCallback // VKamyshniy: param  is AudioCallbackDataData*
98         };
99
100         WCMRAudioDeviceManagerClient () {}
101         virtual ~WCMRAudioDeviceManagerClient () {}
102
103     // VKamyshniy: This is a structure to call the client's AudioDeviceManagerNotification
104     // every AudioCallback time
105     struct AudioCallbackData
106     {
107         const float *acdInputBuffer;
108         float *acdOutputBuffer;
109         size_t acdFrames;
110         uint32_t acdSampleTime;
111         uint64_t acdCycleStartTimeNanos;
112     };
113
114         virtual void AudioDeviceManagerNotification (NotificationReason WCUNUSEDPARAM(reason), void *WCUNUSEDPARAM(pParam)) {}
115 };
116
117
118 class WCMRAudioDevice : public WCRefManager
119 {
120 public:
121
122         enum ConnectionStates
123         {
124                 DeviceAvailable,
125                 DeviceDisconnected,
126                 DeviceError
127         };
128
129         WCMRAudioDevice (WCMRAudioDeviceManager *pManager);///<Constructor
130         virtual ~WCMRAudioDevice ();///<Destructor
131
132         virtual const std::string& DeviceName() const;///<Name?
133         virtual const std::vector<std::string>& InputChannels();///<Current Input Channel List? - note that this may change with change in sampling rate.
134         virtual const std::vector<std::string>& OutputChannels();///<Current Output Channel List? - note that this may change with change in sampling rate.
135
136         virtual const std::vector<int>& SamplingRates();///<Supported Sampling Rate List?
137         virtual int CurrentSamplingRate(); ///<Current Sampling rate.?
138         virtual WTErr SetCurrentSamplingRate(int newRate);///<Change Current Sampling Rate : This is a requset, might not be successful at run time!
139
140         virtual const std::vector<int>& BufferSizes();///<Supported Buffer Size List? - note that this may change with change in sampling rate.
141         virtual int CurrentBufferSize();///<Current Buffer Size.? - note that this may change with change in sampling rate.
142         virtual WTErr SetCurrentBufferSize (int newSize);///<Change Current Buffer Size : This is a requset, might not be successful at run time!
143
144     virtual int CurrentBlockSize();
145
146         virtual ConnectionStates ConnectionStatus();///< Connection Status - device available, gone, disconnected
147
148         virtual bool Active();///<Active status - mainly significant for ASIO, as certain ops can only be performed on active devices!
149         virtual WTErr SetActive (bool newState);///<Prepare/Activate device.
150         
151         virtual bool Streaming();///<Streaming Status?
152         virtual WTErr SetStreaming (bool newState);///<Start/Stop Streaming - should reconnect connections when streaming starts!
153
154     virtual bool IsProcessActive();
155         
156         virtual WTErr DoIdle();///<Do Idle Processing
157         
158         virtual const std::vector<float>& InputLevels();///<Retrieve Input Levels (for VU display)?
159         
160         virtual const std::vector<float>& OutputLevels();///<Retrieve Output Levels (for VU display)?
161
162         void GetMonitorInfo (int *pLeftChannel = NULL, int *pRightChannel = NULL, float *pGain = NULL);///<Retrieve current monitor channel pair and gain - optional, will not be available with AG
163         virtual WTErr SetMonitorChannels (int leftChannel, int rightChannel);///<Set monitor channels. - optional, will not be available with AG
164         virtual WTErr SetMonitorGain (float newGain);///<Set monitor gain. - optional, will not be available with AG
165         
166         virtual WTErr ShowConfigPanel (void *pParam);///< Show Control Panel - in case of ASIO this will work only with Active device!
167         virtual WTErr SendCustomCommand (int customCommand, void *pCommandParam); ///< Send a custom command to the audiodevice...
168     
169     virtual uint32_t GetLatency (bool isInput); ///Get latency.
170     
171 protected:
172         WCMRAudioDeviceManager *m_pMyManager; ///< The manager who's managing this device, can be used for sending notifications!
173         
174         std::string m_DeviceName; ///< Name of the device.
175         std::vector<std::string> m_InputChannels; ///< List of input channel names.
176         std::vector<std::string> m_OutputChannels; ///< List of output channel names.
177         std::vector<int> m_SamplingRates; ///< List of available sampling rates.
178         std::vector<int> m_BufferSizes; ///< List of available buffer sizes.
179         
180         int m_CurrentSamplingRate; ///< Currently selected sampling rate.
181         int m_CurrentBufferSize; ///< Currently selected buffer size.
182
183         ConnectionStates m_ConnectionStatus; ///< Status of device connection
184         bool m_IsActive; ///< Flag for teh active status.
185         bool m_IsStreaming; ///< Flag for streaming status.
186         std::vector<float> m_InputLevels; ///< List of input levels.
187         std::vector<float> m_OutputLevels; ///< List of output levels.
188         
189         int m_LeftMonitorChannel; ///< The device channel to use for monitoring left channel data.
190         int m_RightMonitorChannel; ///< The device channel to use for monitoring right channel data.
191         float m_MonitorGain; ///< Amount of gain to apply for monitoring signal.
192 };
193
194 // This enum is for choosing filter for audio devices scan
195 typedef enum eAudioDeviceFilter
196 {
197         eAllDevices = 0,        // Choose all audio devices
198         eInputOnlyDevices,      // Choose only input audio devices
199         eOutputOnlyDevices,     // Choose only output audio devices
200         eFullDuplexDevices,     // Choose audio devices that have both input and output channels on the same device
201         eMatchedDuplexDevices,  // Match(aggregate) audio devices that have both input and output channels but are considered different audio devices (For mac)
202         eAudioDeviceFilterNum   // Number of enums
203 }       eAudioDeviceFilter;
204
205 //! WCMRAudioDeviceManager
206 /*! The Audio Device Manager class */
207 class WCMRAudioDeviceManager : public WCRefManager
208 {
209 private://< Private version of class functions which will be called by class's public function after mutex lock acquistion.
210     WCMRAudioDevice* GetDefaultDevice_Private();
211     WTErr DoIdle_Private();
212     const WCMRAudioDeviceList& Devices_Private() const; 
213     WCMRAudioDevice* GetDeviceByName_Private(const std::string & nameToMatch) const;
214
215 public://< Public functions for the class.
216     WCMRAudioDevice* GetDefaultDevice()
217     {
218                 //wvNS::wvThread::ThreadMutex::lock theLock(m_AudioDeviceManagerMutex);
219         return GetDefaultDevice_Private();
220     }
221
222     virtual WTErr DoIdle()
223     {
224         //wvNS::wvThread::ThreadMutex::lock theLock(m_AudioDeviceManagerMutex);
225         return DoIdle_Private();
226     }
227
228     const WCMRAudioDeviceList& Devices() const
229     {
230         //wvNS::wvThread::ThreadMutex::lock theLock(m_AudioDeviceManagerMutex);
231         return Devices_Private();
232     }
233
234     WCMRAudioDevice* GetDeviceByName(const std::string & nameToMatch) const
235     {
236         //wvNS::wvThread::ThreadMutex::lock theLock(m_AudioDeviceManagerMutex);
237         return GetDeviceByName_Private(nameToMatch);
238     }
239
240 public:
241         
242         WCMRAudioDeviceManager(WCMRAudioDeviceManagerClient *pTheClient, eAudioDeviceFilter eCurAudioDeviceFilter
243                 ); ///< constructor
244         virtual ~WCMRAudioDeviceManager(void); ///< Destructor  
245     
246         virtual WTErr UpdateDeviceList () = 0; //has to be overridden!
247         
248         
249         //This is primarily for use by WCMRAudioDevice and it's descendants... We could have made it
250         //protected and made WCMRAudioDevice a friend, and then in some way found a way to extend 
251         //the friendship to WCMRAudioDevice's descendants, but that would require a lot of extra
252         //effort!
253         void NotifyClient (WCMRAudioDeviceManagerClient::NotificationReason forReason, void *pParam = NULL);
254     virtual void EnableVerboseLogging(bool /*bEnable*/, const std::string& /*logFilePath*/) { };
255
256 protected:
257     
258     //< NOTE : Mutex protection is commented, but wrapper classes are still there, in case they are required in future.
259     //wvNS::wvThread::ThreadMutex   m_AudioDeviceManagerMutex;   ///< Mutex for Audio device manager class function access.
260         WCMRAudioDeviceManagerClient *m_pTheClient; ///< The device manager's client, used to send notifications.
261         
262         WCMRAudioDeviceList m_Devices; ///< List of all relevant devices devices
263         eAudioDeviceFilter m_eAudioDeviceFilter; // filter of 'm_Devices'
264 };
265
266 #endif //#ifndef __WCMRAudioDeviceManager_h_