NOOP, remove trailing tabs/whitespace.
[ardour.git] / libs / backends / wavesaudio / wavesapi / devicemanager / WCMRAudioDeviceManager.h
1 /*
2     Copyright (C) 2014 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 //
23 //! \file       WCMRAudioDeviceManager.h
24 //!
25 //! WCMRAudioDeviceManager and related class declarations
26 //!
27 //---------------------------------------------------------------------------------*/
28 #ifndef __WCMRAudioDeviceManager_h_
29         #define __WCMRAudioDeviceManager_h_
30
31 /* Copy to include
32 #include "WCMRAudioDeviceManager.h"
33 */
34
35 #define AUTO_FUNC_DEBUG
36 #define DEBUG_MSG(a)
37 #define ASSERT_ERROR(a, b)
38 #define TRACE_MSG(a)
39
40 #include <string>
41 #include <vector>
42 #include <map>
43 #include "WCRefManager.h"
44 #include "BasicTypes/WUTypes.h"
45 #include "WUErrors.h"
46 #include "WCThreadSafe.h"
47
48 #define WCUNUSEDPARAM(a)
49
50 class WCMRAudioDevice;
51 class WCMRAudioDeviceManager;
52
53 typedef unsigned int DeviceID;
54
55 struct DeviceInfo
56 {
57         DeviceID m_DeviceId;
58         std::string m_DeviceName;
59         std::vector<int> m_AvailableSampleRates;
60         std::vector<int> m_AvailableBufferSizes;
61         unsigned int m_MaxInputChannels;
62         unsigned int m_MaxOutputChannels;
63
64     DeviceInfo():
65     m_DeviceId(-1), m_DeviceName("Unknown"), m_MaxInputChannels(0), m_MaxOutputChannels(0)
66         {};
67
68         DeviceInfo(unsigned int deviceID, const std::string & deviceName):
69                 m_DeviceId(deviceID), m_DeviceName(deviceName), m_MaxInputChannels(0), m_MaxOutputChannels(0)
70         {};
71 };
72
73 typedef std::vector<DeviceInfo*> DeviceInfoVec;
74 typedef DeviceInfoVec::iterator DeviceInfoVecIter;
75 typedef DeviceInfoVec::const_iterator DeviceInfoVecConstIter;
76
77 /// for notification... A client must derive it's class from us.
78 class WCMRAudioDeviceManagerClient
79 {
80         public:
81         enum NotificationReason
82         {
83                 DeviceListChanged,
84                 Dropout,
85                 RequestReset,
86                 RequestResync,
87                 SamplingRateChanged, //param has new SR, or -1 if not known
88         SamplingRateChangedSilent, //To indicate sampling rate changed but no need to notify user
89                 BufferSizeChanged,
90                 ClockSourceChanged,
91                 DeviceStoppedStreaming,
92                 DeviceStartsStreaming,
93                 DeviceDroppedSamples,
94                 DeviceConnectionLost,
95                 DeviceGenericError,
96                 DeviceStatusChanged,
97                 DeviceStatisticsUpdated,
98                 DeviceDebugInfo, //param has c string
99                 DeviceProgressInfo, //param has c string
100                 MIDIData,
101                 MIDINodeUp,
102                 MIDINodeDown,
103                 DeviceSampleRateMisMatch,
104                 SystemSamplingRateChangedInfoOnly,
105                 LostClockSource,
106                 IODeviceDisconnected,
107                 ChannelCountModified,
108                 MasterUp,
109                 MasterDown,
110                 AudioDropFound,
111                 ReflasherEvent,
112         AGDeviceSamplingRateChangedInfoOnly,
113                 IODeviceNameChanged,
114         SetDisplayNameFromIOModule,
115         IOMStateChanged,    ///< This is used when IOM state is changed.
116         AudioCallback // VKamyshniy: param  is AudioCallbackDataData*
117         };
118
119         WCMRAudioDeviceManagerClient () {}
120         virtual ~WCMRAudioDeviceManagerClient () {}
121
122     // VKamyshniy: This is a structure to call the client's AudioDeviceManagerNotification
123     // every AudioCallback time
124     struct AudioCallbackData
125     {
126         const float *acdInputBuffer;
127         float *acdOutputBuffer;
128         size_t acdFrames;
129         int64_t acdSampleTime;
130         uint64_t acdCycleStartTimeNanos;
131     };
132
133         virtual void AudioDeviceManagerNotification (NotificationReason WCUNUSEDPARAM(reason), void *WCUNUSEDPARAM(pParam)) {}
134 };
135
136
137 class WCMRAudioDevice : public WCRefManager
138 {
139 public:
140
141         enum ConnectionStates
142         {
143                 DeviceAvailable,
144                 DeviceDisconnected,
145                 DeviceErrors
146         };
147
148         WCMRAudioDevice (WCMRAudioDeviceManager *pManager);///<Constructor
149         virtual ~WCMRAudioDevice ();///<Destructor
150
151         virtual const std::string& DeviceName() const;///<Name?
152         virtual const std::vector<std::string>& InputChannels();///<Current Input Channel List? - note that this may change with change in sampling rate.
153         virtual const std::vector<std::string>& OutputChannels();///<Current Output Channel List? - note that this may change with change in sampling rate.
154
155         virtual const std::vector<int>& SamplingRates();///<Supported Sampling Rate List?
156         virtual int CurrentSamplingRate(); ///<Current Sampling rate.?
157         virtual WTErr SetCurrentSamplingRate(int newRate);///<Change Current Sampling Rate : This is a requset, might not be successful at run time!
158
159         virtual const std::vector<int>& BufferSizes();///<Supported Buffer Size List? - note that this may change with change in sampling rate.
160         virtual int CurrentBufferSize();///<Current Buffer Size.? - note that this may change with change in sampling rate.
161         virtual WTErr SetCurrentBufferSize (int newSize);///<Change Current Buffer Size : This is a requset, might not be successful at run time!
162
163     virtual int CurrentBlockSize();
164
165         virtual ConnectionStates ConnectionStatus();///< Connection Status - device available, gone, disconnected
166
167         virtual bool Active();///<Active status - mainly significant for ASIO, as certain ops can only be performed on active devices!
168         virtual WTErr SetActive (bool newState);///<Prepare/Activate device.
169
170         virtual bool Streaming();///<Streaming Status?
171         virtual WTErr SetStreaming (bool newState);///<Start/Stop Streaming - should reconnect connections when streaming starts!
172
173         virtual WTErr ResetDevice ();
174
175     virtual bool IsProcessActive();
176
177         virtual WTErr DoIdle();///<Do Idle Processing
178
179         virtual const std::vector<float>& InputLevels();///<Retrieve Input Levels (for VU display)?
180
181         virtual const std::vector<float>& OutputLevels();///<Retrieve Output Levels (for VU display)?
182
183         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
184         virtual WTErr SetMonitorChannels (int leftChannel, int rightChannel);///<Set monitor channels. - optional, will not be available with AG
185         virtual WTErr SetMonitorGain (float newGain);///<Set monitor gain. - optional, will not be available with AG
186
187         virtual WTErr ShowConfigPanel (void *pParam);///< Show Control Panel - in case of ASIO this will work only with Active device!
188         virtual WTErr SendCustomCommand (int customCommand, void *pCommandParam); ///< Send a custom command to the audiodevice...
189
190     virtual uint32_t GetLatency (bool isInput); ///Get latency.
191
192         virtual WTErr UpdateDeviceInfo () = 0;
193
194 protected:
195         WCMRAudioDeviceManager *m_pMyManager; ///< The manager who's managing this device, can be used for sending notifications!
196
197         std::string m_DeviceName; ///< Name of the device.
198         std::vector<std::string> m_InputChannels; ///< List of input channel names.
199         std::vector<std::string> m_OutputChannels; ///< List of output channel names.
200         std::vector<int> m_SamplingRates; ///< List of available sampling rates.
201         std::vector<int> m_BufferSizes; ///< List of available buffer sizes.
202
203         int m_CurrentSamplingRate; ///< Currently selected sampling rate.
204         int m_CurrentBufferSize; ///< Currently selected buffer size.
205
206         ConnectionStates m_ConnectionStatus; ///< Status of device connection
207         bool m_IsActive; ///< Flag for teh active status.
208         bool m_IsStreaming; ///< Flag for streaming status.
209         std::vector<float> m_InputLevels; ///< List of input levels.
210         std::vector<float> m_OutputLevels; ///< List of output levels.
211
212         int m_LeftMonitorChannel; ///< The device channel to use for monitoring left channel data.
213         int m_RightMonitorChannel; ///< The device channel to use for monitoring right channel data.
214         float m_MonitorGain; ///< Amount of gain to apply for monitoring signal.
215 };
216
217
218 // This enum is for choosing filter for audio devices scan
219 typedef enum eAudioDeviceFilter
220 {
221         eAllDevices = 0,        // Choose all audio devices
222         eInputOnlyDevices,      // Choose only input audio devices
223         eOutputOnlyDevices,     // Choose only output audio devices
224         eFullDuplexDevices,     // Choose audio devices that have both input and output channels on the same device
225         eMatchedDuplexDevices,  // Match(aggregate) audio devices that have both input and output channels but are considered different audio devices (For mac)
226         eAudioDeviceFilterNum   // Number of enums
227 }       eAudioDeviceFilter;
228
229
230 class WCMRAudioDeviceManager : public WCRefManager
231 {
232 public://< Public functions for the class.
233
234         WCMRAudioDeviceManager(WCMRAudioDeviceManagerClient *pTheClient, eAudioDeviceFilter eCurAudioDeviceFilter); ///< constructor
235         virtual ~WCMRAudioDeviceManager(void); ///< Destructor
236
237         //interfaces
238         WCMRAudioDevice*        InitNewCurrentDevice(const std::string & deviceName);
239         void                            DestroyCurrentDevice();
240         const DeviceInfoVec DeviceInfoList () const;
241     WTErr               GetDeviceInfoByName(const std::string & nameToMatch, DeviceInfo & devInfo) const;
242     WTErr               GetDeviceSampleRates(const std::string & nameToMatch, std::vector<int>& sampleRates) const;
243         WTErr                           GetDeviceBufferSizes(const std::string & nameToMatch, std::vector<int>& bufferSizes) const;
244
245     //virtual void              EnableVerboseLogging(bool /*bEnable*/, const std::string& /*logFilePath*/) { };
246
247         //notify backend
248         void                                    NotifyClient (WCMRAudioDeviceManagerClient::NotificationReason forReason, void *pParam = NULL);
249
250 protected:
251
252     mutable wvNS::wvThread::ThreadMutex         m_AudioDeviceInfoVecMutex; // mutex to lock device info list
253         DeviceInfoVec                               m_DeviceInfoVec;
254
255     eAudioDeviceFilter                          m_eAudioDeviceFilter;
256         WCMRAudioDevice*                            m_CurrentDevice;
257
258 private:
259         // override in derived classes
260         // made private to avoid pure virtual function call
261         virtual WCMRAudioDevice*        initNewCurrentDeviceImpl(const std::string & deviceName) = 0;
262         virtual void                            destroyCurrentDeviceImpl() = 0;
263     virtual WTErr                               getDeviceSampleRatesImpl(const std::string & deviceName, std::vector<int>& sampleRates) const = 0;
264         virtual WTErr                           getDeviceBufferSizesImpl(const std::string & deviceName, std::vector<int>& bufferSizes) const = 0;
265     virtual WTErr                               generateDeviceListImpl() = 0;
266     virtual WTErr                               updateDeviceListImpl() = 0;
267
268         WCMRAudioDeviceManagerClient    *m_pTheClient; ///< The device manager's client, used to send notifications.
269 };
270
271 #endif //#ifndef __WCMRAudioDeviceManager_h_