Add a couple of missing header files (needed for cin /cout etc)
[ardour.git] / libs / backends / wavesaudio / wavesapi / devicemanager / WCMRAudioDeviceManager.cpp
1 //----------------------------------------------------------------------------------
2 //
3 // Copyright (c) 2008 Waves Audio Ltd. All rights reserved.
4 //
5 //! \file       WCMRAudioDeviceManager.cpp
6 //!
7 //! WCMRAudioDeviceManager and related class declarations
8 //!
9 //---------------------------------------------------------------------------------*/
10 #include <iostream>
11 #include "WCMRAudioDeviceManager.h"
12
13
14 //**********************************************************************************************
15 // WCMRAudioDevice::WCMRAudioDevice 
16 //
17 //! Constructor for the audio device. The derived classes will need to do more actual work, such
18 //!             as determining supported sampling rates, buffer sizes, and channel counts. Connection
19 //!             and streaming will also be provided by the derived implementations.
20 //!
21 //! \param *pManager : The audio device manager that's managing this device.
22 //! \return Nothing.
23 //! 
24 //**********************************************************************************************
25 WCMRAudioDevice::WCMRAudioDevice (WCMRAudioDeviceManager *pManager) :
26         m_pMyManager (pManager)
27         , m_ConnectionStatus (DeviceDisconnected)
28         , m_IsActive (false)
29         , m_IsStreaming (false)
30         , m_CurrentSamplingRate (-1)
31         , m_CurrentBufferSize (0)
32         , m_LeftMonitorChannel (-1)
33         , m_RightMonitorChannel (-1)
34         , m_MonitorGain (1.0f)
35 {
36         m_DeviceName = "Unknown";
37 }
38
39
40
41 //**********************************************************************************************
42 // WCMRAudioDevice::~WCMRAudioDevice 
43 //
44 //! Destructor for the audio device. It release all the connections that were created.
45 //!
46 //! \param none
47 //! 
48 //! \return Nothing.
49 //! 
50 //**********************************************************************************************
51 WCMRAudioDevice::~WCMRAudioDevice ()
52 {
53     AUTO_FUNC_DEBUG;
54         try 
55         {
56         }
57         catch (...)
58         {
59                 //destructors should absorb exceptions, no harm in logging though!!
60                 DEBUG_MSG ("Exception during destructor");
61         }
62 }
63
64
65
66
67 //**********************************************************************************************
68 // WCMRAudioDevice::DeviceName 
69 //
70 //! Retrieves Device's name.
71 //!
72 //! \param none
73 //! 
74 //! \return The device name.
75 //! 
76 //**********************************************************************************************
77 const std::string& WCMRAudioDevice::DeviceName () const
78 {
79         return (m_DeviceName);
80         
81 }
82
83
84
85 //**********************************************************************************************
86 // WCMRAudioDevice::InputChannels 
87 //
88 //! Retrieves Input Channel information. Note that the list may be changed at run-time.
89 //!
90 //! \param none
91 //! 
92 //! \return A vector with Input Channel Names.
93 //! 
94 //**********************************************************************************************
95 const std::vector<std::string>& WCMRAudioDevice::InputChannels ()
96 {
97         return (m_InputChannels);
98         
99 }
100
101
102
103 //**********************************************************************************************
104 // WCMRAudioDevice::OutputChannels 
105 //
106 //! Retrieves Output Channel Information. Note that the list may be changed at run-time.
107 //!
108 //! \param none
109 //! 
110 //! \return A vector with Output Channel Names.
111 //! 
112 //**********************************************************************************************
113 const std::vector<std::string>& WCMRAudioDevice::OutputChannels ()
114 {
115         return (m_OutputChannels);
116 }
117
118
119
120
121 //**********************************************************************************************
122 // WCMRAudioDevice::SamplingRates 
123 //
124 //! Retrieves supported sampling rate information.
125 //!
126 //! \param none
127 //! 
128 //! \return A vector with supported sampling rates.
129 //! 
130 //**********************************************************************************************
131 const std::vector<int>& WCMRAudioDevice::SamplingRates ()
132 {
133         return (m_SamplingRates);
134 }
135
136
137
138 //**********************************************************************************************
139 // WCMRAudioDevice::CurrentSamplingRate 
140 //
141 //! The device's current sampling rate. This may be overridden, if the device needs to 
142 //!             query the driver for the current rate.
143 //!
144 //! \param none
145 //! 
146 //! \return The device's current sampling rate. -1 on error.
147 //! 
148 //**********************************************************************************************
149 int WCMRAudioDevice::CurrentSamplingRate ()
150 {
151         return (m_CurrentSamplingRate);
152 }
153
154
155
156
157 //**********************************************************************************************
158 // WCMRAudioDevice::SetCurrentSamplingRate 
159 //
160 //! Change the sampling rate to be used by the device. This will most likely be overridden, 
161 //!             the base class simply updates the member variable.
162 //!
163 //! \param newRate : The rate to use (samples per sec).
164 //! 
165 //! \return eNoErr always. The derived classes may return error codes.
166 //! 
167 //**********************************************************************************************
168 WTErr WCMRAudioDevice::SetCurrentSamplingRate (int newRate)
169 {
170         //changes the status.
171         m_CurrentSamplingRate = newRate;
172         return (eNoErr);
173 }
174
175
176
177
178 //**********************************************************************************************
179 // WCMRAudioDevice::BufferSizes 
180 //
181 //! Retrieves supported buffer size information.
182 //!
183 //! \param none
184 //! 
185 //! \return A vector with supported buffer sizes.
186 //! 
187 //**********************************************************************************************
188 const std::vector<int>& WCMRAudioDevice::BufferSizes ()
189 {
190         return (m_BufferSizes);
191 }
192
193
194
195 //**********************************************************************************************
196 // WCMRAudioDevice::CurrentBufferSize
197 //
198 //! The device's current buffer size in use. This may be overridden, if the device needs to 
199 //!             query the driver for the current size.
200 //!
201 //! \param none
202 //! 
203 //! \return The device's current buffer size. 0 on error.
204 //! 
205 //**********************************************************************************************
206 int WCMRAudioDevice::CurrentBufferSize ()
207 {
208         return (m_CurrentBufferSize);
209 }
210
211 //**********************************************************************************************
212 // WCMRAudioDevice::CurrentBlockSize
213 //
214 //! Device's block size we use for holding the audio samples.
215 //! Usually this is equal to the buffer size, but in some cases the buffer size holds additional
216 //!   data other then the audio buffers, like frames info in SG, so it can be overriden
217 //!
218 //! \param none
219 //! 
220 //! \return The device's current block size. 0 on error.
221 //! 
222 //**********************************************************************************************
223 int WCMRAudioDevice::CurrentBlockSize()
224 {
225     // By default - return the buffer size
226     return CurrentBufferSize();
227 }
228
229
230 //**********************************************************************************************
231 // WCMRAudioDevice::SetCurrentBufferSize
232 //
233 //! Change the buffer size to be used by the device. This will most likely be overridden, 
234 //!             the base class simply updates the member variable.
235 //!
236 //! \param newSize : The buffer size to use (in sample-frames)
237 //! 
238 //! \return eNoErr always. The derived classes may return error codes.
239 //! 
240 //**********************************************************************************************
241 WTErr WCMRAudioDevice::SetCurrentBufferSize (int newSize)
242 {
243         //This will most likely be overridden, the base class simply
244         //changes the member.
245         m_CurrentBufferSize = newSize;
246         return (eNoErr);
247 }
248
249
250
251
252 //**********************************************************************************************
253 // WCMRAudioDevice::ConnectionStatus 
254 //
255 //! Retrieves the device's current connection status. This will most likely be overridden,
256 //!             in case some driver communication is required to query the status.
257 //!
258 //! \param none
259 //! 
260 //! \return A ConnectionStates value.
261 //! 
262 //**********************************************************************************************
263 WCMRAudioDevice::ConnectionStates WCMRAudioDevice::ConnectionStatus ()
264 {
265         return (m_ConnectionStatus);
266         
267 }
268
269
270
271
272 //**********************************************************************************************
273 // WCMRAudioDevice::Active 
274 //
275 //! Retrieves Device activation status.
276 //!
277 //! \param none
278 //! 
279 //! \return true if device is active, false otherwise.
280 //! 
281 //**********************************************************************************************
282 bool WCMRAudioDevice::Active ()
283 {
284         return (m_IsActive);
285         
286 }
287
288
289
290 //**********************************************************************************************
291 // WCMRAudioDevice::SetActive 
292 //
293 //! Sets the device's activation status.
294 //!
295 //! \param newState : Should be true to activate, false to deactivate. This roughly corresponds
296 //!             to opening and closing the device handle/stream/audio unit.
297 //! 
298 //! \return eNoErr always, the derived classes may return appropriate error code.
299 //! 
300 //**********************************************************************************************
301 WTErr WCMRAudioDevice::SetActive (bool newState)
302 {
303         //This will most likely be overridden, the base class simply
304         //changes the member.
305         m_IsActive = newState;
306         return (eNoErr);
307 }
308
309
310
311
312 //**********************************************************************************************
313 // WCMRAudioDevice::Streaming 
314 //
315 //! Retrieves Device streaming status.
316 //!
317 //! \param none
318 //! 
319 //! \return true if device is streaming, false otherwise.
320 //! 
321 //**********************************************************************************************
322 bool WCMRAudioDevice::Streaming ()
323 {
324         return (m_IsStreaming);
325 }
326
327
328
329 //**********************************************************************************************
330 // WCMRAudioDevice::SetStreaming
331 //
332 //! Sets the device's streaming status.
333 //!
334 //! \param newState : Should be true to start streaming, false to stop streaming. This roughly
335 //!             corresponds to calling Start/Stop on the lower level interface.
336 //! 
337 //! \return eNoErr always, the derived classes may return appropriate error code.
338 //! 
339 //**********************************************************************************************
340 WTErr WCMRAudioDevice::SetStreaming (bool newState)
341 {
342         //This will most likely be overridden, the base class simply
343         //changes the member.
344         m_IsStreaming = newState;
345         return (eNoErr);
346 }
347
348 ///////////////////////////////////////////////////////////////////////////////////////////////////////
349 // IsProcessActive - returns true if process code is running.
350 // A normal audio device should return the Streaming() value
351 ///////////////////////////////////////////////////////////////////////////////////////////////////////
352 bool WCMRAudioDevice::IsProcessActive()
353 {
354     return Streaming();
355 }
356
357
358
359
360
361 //**********************************************************************************************
362 // WCMRAudioDevice::DoIdle 
363 //
364 //! A place for doing idle time processing. The derived classes will probably do something
365 //!             meaningful.
366 //!
367 //! \param none
368 //! 
369 //! \return eNoErr always.
370 //! 
371 //**********************************************************************************************
372 WTErr WCMRAudioDevice::DoIdle ()
373 {
374         //We don't need to do anything here...
375         //the derived classes may want to use this however.
376         return (eNoErr);
377 }
378
379
380
381
382 //**********************************************************************************************
383 // WCMRAudioDevice::InputLevels 
384 //
385 //! Retrieve current input levels.
386 //!
387 //! \param none
388 //! 
389 //! \return A vector (the same size as input channels list) that contains current input levels.
390 //! 
391 //**********************************************************************************************
392 const std::vector<float>& WCMRAudioDevice::InputLevels ()
393 {
394         //The derived classes may override if they need to query
395         //the driver for the levels.
396         return (m_InputLevels);
397 }
398
399
400
401 //**********************************************************************************************
402 // WCMRAudioDevice::OutputLevels 
403 //
404 //! Retrieve current output levels.
405 //!
406 //! \param none
407 //! 
408 //! \return A vector (the same size as output channels list) that contains current output levels.
409 //! 
410 //**********************************************************************************************
411 const std::vector<float>& WCMRAudioDevice::OutputLevels ()
412 {
413         //The derived classes may override if they need to query
414         //the driver for the levels.
415         return (m_OutputLevels);
416 }
417
418
419
420 //**********************************************************************************************
421 // WCMRAudioDevice::GetMonitorInfo 
422 //
423 //! Retrieves current monitoring information.
424 //!
425 //! \param *pLeftChannel : Pointer to receive left monitor channel index.
426 //! \param *pRightChannel : Pointer to receive right monitor channel index.
427 //! \param *pGain : Pointer to receive the gain (linear) to be applied.
428 //! 
429 //! \return Nothing.
430 //! 
431 //**********************************************************************************************
432 void WCMRAudioDevice::GetMonitorInfo (int *pLeftChannel, int *pRightChannel, float *pGain)
433 {
434         if (pLeftChannel)
435                 *pLeftChannel = m_LeftMonitorChannel;
436         if (pRightChannel)      
437                 *pRightChannel = m_RightMonitorChannel;
438         if (pGain)      
439                 *pGain = m_MonitorGain;
440         return; 
441 }
442
443
444
445 //**********************************************************************************************
446 // WCMRAudioDevice::SetMonitorChannels 
447 //
448 //! Used to set the channels to be used for monitoring.
449 //!
450 //! \param leftChannel : Left monitor channel index.
451 //! \param rightChannel : Right monitor channel index.
452 //! 
453 //! \return eNoErr always, the derived classes may return appropriate errors.
454 //! 
455 //**********************************************************************************************
456 WTErr WCMRAudioDevice::SetMonitorChannels (int leftChannel, int rightChannel)
457 {
458         //This will most likely be overridden, the base class simply
459         //changes the member.
460         m_LeftMonitorChannel = leftChannel;
461         m_RightMonitorChannel = rightChannel;
462         return (eNoErr);
463 }
464
465
466
467 //**********************************************************************************************
468 // WCMRAudioDevice::SetMonitorGain 
469 //
470 //! Used to set monitor gain (or atten).
471 //!
472 //! \param newGain : The new gain or atten. value to use. Specified as a linear multiplier (not dB) 
473 //! 
474 //! \return eNoErr always, the derived classes may return appropriate errors.
475 //! 
476 //**********************************************************************************************
477 WTErr WCMRAudioDevice::SetMonitorGain (float newGain)
478 {
479         //This will most likely be overridden, the base class simply
480         //changes the member.
481         m_MonitorGain = newGain;
482         return (eNoErr);
483 }
484
485
486
487
488 //**********************************************************************************************
489 // WCMRAudioDevice::ShowConfigPanel 
490 //
491 //! Used to show device specific config/control panel. Some interfaces may not support it.
492 //!             Some interfaces may require the device to be active before it can display a panel.
493 //!
494 //! \param pParam : A device/interface specific parameter - optional.
495 //! 
496 //! \return eNoErr always, the derived classes may return errors.
497 //! 
498 //**********************************************************************************************
499 WTErr WCMRAudioDevice::ShowConfigPanel (void *WCUNUSEDPARAM(pParam))
500 {
501         //This will most likely be overridden...
502         return (eNoErr);
503 }
504
505
506 //**********************************************************************************************
507 // WCMRAudioDevice::SendCustomCommand 
508 //
509 //! Used to Send a custom command to the audiodevice. Some interfaces may require the device 
510 //!             to be active before it can do anything in this.
511 //!
512 //! \param customCommand : A device/interface specific command.
513 //! \param pCommandParam : A device/interface/command specific parameter - optional.
514 //! 
515 //! \return eNoErr always, the derived classes may return errors.
516 //! 
517 //**********************************************************************************************
518 WTErr WCMRAudioDevice::SendCustomCommand (int WCUNUSEDPARAM(customCommand), void *WCUNUSEDPARAM(pCommandParam))
519 {
520         //This will most likely be overridden...
521         return (eNoErr);
522 }
523
524 //**********************************************************************************************
525 // WCMRAudioDevice::GetLatency
526 //
527 //! Get Latency for device.
528 //!
529 //! Use 'kAudioDevicePropertyLatency' and 'kAudioDevicePropertySafetyOffset' + GetStreamLatencies
530 //!
531 //! \param isInput : Return latency for the input if isInput is true, otherwise the output latency
532 //!                  wiil be returned.
533 //! \return Latency in samples.
534 //!
535 //**********************************************************************************************
536 uint32_t WCMRAudioDevice::GetLatency (bool isInput)
537 {
538     //This will most likely be overridden...
539     return 0;
540 }
541
542
543 //**********************************************************************************************
544 // WCMRAudioDeviceManager::WCMRAudioDeviceManager
545 //
546 //! The constructuor, most of the work will be done in the derived class' constructor.
547 //!
548 //! \param *pTheClient : 
549 //! 
550 //! \return Nothing.
551 //! 
552 //**********************************************************************************************
553 WCMRAudioDeviceManager::WCMRAudioDeviceManager(WCMRAudioDeviceManagerClient *pTheClient, eAudioDeviceFilter eCurAudioDeviceFilter)
554     : m_eAudioDeviceFilter(eCurAudioDeviceFilter)
555     , m_CurrentDevice(0)
556     , m_pTheClient (pTheClient)
557 {
558 }
559
560
561 //**********************************************************************************************
562 // WCMRAudioDeviceManager::~WCMRAudioDeviceManager
563 //
564 //! It clears the device list, releasing each of the device.
565 //!
566 //! \param none
567 //! 
568 //! \return Nothing.
569 //! 
570 //**********************************************************************************************
571 WCMRAudioDeviceManager::~WCMRAudioDeviceManager()
572 {
573     AUTO_FUNC_DEBUG;
574
575         std::cout << "API::Destroying AudioDeviceManager " << std::endl;
576         try
577         {
578                 // clean up device info list
579         {
580             wvNS::wvThread::ThreadMutex::lock theLock(m_AudioDeviceInfoVecMutex);
581             while( m_DeviceInfoVec.size() )
582             {
583                 DeviceInfo* devInfo = m_DeviceInfoVec.back();
584                 m_DeviceInfoVec.pop_back();
585                 delete devInfo;
586             }
587         }
588                 delete m_CurrentDevice;
589
590         }
591         catch (...)
592         {
593                 //destructors should absorb exceptions, no harm in logging though!!
594                 DEBUG_MSG ("Exception during destructor");
595         }
596 }
597
598
599 WCMRAudioDevice* WCMRAudioDeviceManager::InitNewCurrentDevice(const std::string & deviceName)
600 {
601         return initNewCurrentDeviceImpl(deviceName);
602 }
603
604
605 void WCMRAudioDeviceManager::DestroyCurrentDevice()
606 {
607         return destroyCurrentDeviceImpl();
608 }
609
610
611 const DeviceInfoVec WCMRAudioDeviceManager::DeviceInfoList() const
612 {
613     wvNS::wvThread::ThreadMutex::lock theLock(m_AudioDeviceInfoVecMutex);
614         return m_DeviceInfoVec;
615 }
616
617
618 WTErr WCMRAudioDeviceManager::GetDeviceInfoByName(const std::string & nameToMatch, DeviceInfo & devInfo) const
619 {
620     wvNS::wvThread::ThreadMutex::lock theLock(m_AudioDeviceInfoVecMutex);
621         DeviceInfoVecConstIter iter = m_DeviceInfoVec.begin();
622         for (; iter != m_DeviceInfoVec.end(); ++iter)
623         {
624                 if (nameToMatch == (*iter)->m_DeviceName)
625         {
626                         devInfo = *(*iter);
627             return eNoErr;
628         }
629         }
630
631         return eRMResNotFound;
632 }
633
634
635 WTErr WCMRAudioDeviceManager::GetDeviceBufferSizes(const std::string & nameToMatch, std::vector<int>& bufferSizes) const
636 {
637         return getDeviceBufferSizesImpl(nameToMatch, bufferSizes);
638 }
639
640
641 //**********************************************************************************************
642 // WCMRAudioDeviceManager::NotifyClient 
643 //
644 //! A helper routine used to call the client for notification.
645 //!
646 //! \param forReason : The reason for notification.
647 //! \param *pParam : A parameter (if required) for notification.
648 //! 
649 //! \return Nothing.
650 //! 
651 //**********************************************************************************************
652 void WCMRAudioDeviceManager::NotifyClient (WCMRAudioDeviceManagerClient::NotificationReason forReason, void *pParam)
653 {
654         if (m_pTheClient)
655                 m_pTheClient->AudioDeviceManagerNotification (forReason, pParam);
656         return; 
657 }