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