1 //----------------------------------------------------------------------------------
3 // Copyright (c) 2008 Waves Audio Ltd. All rights reserved.
5 //! \file WCMRNativeAudio.cpp
7 //! WCMRNativeAudioConnection and related class defienitions
9 //---------------------------------------------------------------------------------*/
10 #if defined(__APPLE__)
11 #include <CoreAudio/CoreAudio.h>
14 #include "WCMRNativeAudio.h"
15 #include "MiscUtils/safe_delete.h"
17 #include <boost/assign/list_of.hpp>
19 #define NONE_DEVICE_NAME "None"
20 #define NONE_DEVICE_INPUT_NAMES "Input "
21 #define NONE_DEVICE_OUTPUT_NAMES "Output "
23 //**********************************************************************************************
24 // WCMRNativeAudioNoneDevice::WCMRNativeAudioNoneDevice
26 //! Constructor for the dummy "None" device. This constructor simply adds supported SRs,
27 //! buffer sizes, and channels, so that it may look like a real native device to
30 //! \param pManager : The managing device manager - simply passed on to the base class.
33 //**********************************************************************************************
34 WCMRNativeAudioNoneDevice::WCMRNativeAudioNoneDevice (WCMRAudioDeviceManager *pManager)
35 : WCMRNativeAudioDevice (pManager, false /*useMultiThreading*/)
37 #if defined (PLATFORM_WINDOWS)
38 , _waitableTimerForUsleep (CreateWaitableTimer(NULL, TRUE, NULL))
41 m_DeviceName = NONE_DEVICE_NAME;
43 m_SamplingRates = boost::assign::list_of (m_CurrentSamplingRate=44100)(48000)(88200)(96000);
45 m_BufferSizes = boost::assign::list_of (32)(64)(128)(m_CurrentBufferSize=256)(512)(1024);
47 for (int channel = 0; channel < __m_NumInputChannels; channel++)
49 std::stringstream name;
50 name << NONE_DEVICE_INPUT_NAMES;
51 name << (channel + 1);
52 m_InputChannels.push_back(name.str());
55 for (int channel = 0; channel < __m_NumOutputChannels; channel++)
57 std::stringstream name;
58 name << NONE_DEVICE_INPUT_NAMES;
59 name << (channel + 1);
60 m_OutputChannels.push_back(name.str());
62 _m_inputBuffer = new float[__m_NumInputChannels * m_BufferSizes.back()];
63 _m_outputBuffer = new float[__m_NumOutputChannels * m_BufferSizes.back()];
67 WCMRNativeAudioNoneDevice::~WCMRNativeAudioNoneDevice ()
69 #if defined (PLATFORM_WINDOWS)
70 if(_waitableTimerForUsleep) {
71 CloseHandle(_waitableTimerForUsleep);
76 WTErr WCMRNativeAudioNoneDevice::SetActive (bool newState)
78 //This will most likely be overridden, the base class simply
80 if (Active() == newState)
85 if (Active() && Streaming())
89 return WCMRAudioDevice::SetActive(newState);
92 WTErr WCMRNativeAudioNoneDevice::SetCurrentBufferSize (int newSize)
96 int oldSize = CurrentBufferSize();
97 bool oldActive = Active();
99 //same size, nothing to do.
100 if (oldSize == newSize)
103 //see if this is one of our supported rates...
104 std::vector<int>::iterator intIter = find(m_BufferSizes.begin(), m_BufferSizes.end(), newSize);
105 if (intIter == m_BufferSizes.end())
107 //Can't change, perhaps use an "invalid param" type of error
108 return eCommandLineParameter;
113 //Can't change, perhaps use an "in use" type of error
118 return WCMRAudioDevice::SetCurrentBufferSize(newSize);
122 WTErr WCMRNativeAudioNoneDevice::UpdateDeviceInfo ()
128 WTErr WCMRNativeAudioNoneDevice::SetStreaming (bool newState)
130 if (Streaming() == newState)
135 WCMRAudioDevice::SetStreaming(newState);
140 std::cerr << "\t\t\t\t\t !!!!!!!!!!!!!!! Warning: the inactive NONE-DEVICE was streaming!" << std::endl;
142 pthread_attr_t attributes;
143 size_t stack_size = 100000;
145 stack_size = (((stack_size - 1) / PTHREAD_STACK_MIN) + 1) * PTHREAD_STACK_MIN;
147 if (pthread_attr_init (&attributes)) {
148 std::cerr << "WCMRNativeAudioNoneDevice::SetStreaming (): pthread_attr_init () failed!" << std::endl;
152 if (pthread_attr_setstacksize (&attributes, stack_size)) {
153 std::cerr << "WCMRNativeAudioNoneDevice::SetStreaming (): pthread_attr_setstacksize () failed!" << std::endl;
157 if (pthread_create (&m_SilenceThread, &attributes, __SilenceThread, this)) {
159 std::cerr << "WCMRNativeAudioNoneDevice::SetStreaming (): pthread_create () failed!" << std::endl;
165 if (!m_SilenceThread)
167 std::cerr << "\t\t\t\t\t !!!!!!!!!!!!!!! Warning: the active NONE-DEVICE was NOT streaming!" << std::endl;
170 while (m_SilenceThread)
172 _usleep(1); //now wait for ended thread;
179 void WCMRNativeAudioNoneDevice::_SilenceThread()
181 #if defined(PLATFORM_WINDOWS)
182 float* theInpBuffers[__m_NumInputChannels];
183 for(int i = 0; i < __m_NumInputChannels; ++i)
185 theInpBuffers[i] = _m_inputBuffer + m_BufferSizes.back() * i;
188 float* theInpBuffers = _m_inputBuffer;
191 uint32_t currentSampleTime = 0;
192 const size_t buffer_size = CurrentBufferSize();
193 const uint64_t cyclePeriodNanos = (1000000000.0 * buffer_size) / CurrentSamplingRate();
195 struct WCMRAudioDeviceManagerClient::AudioCallbackData audioCallbackData =
197 (const float*)theInpBuffers,
204 audioCallbackData.acdCycleStartTimeNanos =__get_time_nanos();
206 // VERY ROUGH IMPLEMENTATION:
209 uint64_t cycleEndTimeNanos = audioCallbackData.acdCycleStartTimeNanos + cyclePeriodNanos;
211 m_pMyManager->NotifyClient (WCMRAudioDeviceManagerClient::AudioCallback, (void *)&audioCallbackData);
213 currentSampleTime += buffer_size;
215 int64_t timeToSleepUsecs = ((int64_t)cycleEndTimeNanos - (int64_t)__get_time_nanos())/1000;
217 if (timeToSleepUsecs > 0) {
218 _usleep (timeToSleepUsecs);
220 audioCallbackData.acdCycleStartTimeNanos = cycleEndTimeNanos+1;
225 void* WCMRNativeAudioNoneDevice::__SilenceThread(void *This)
227 ((WCMRNativeAudioNoneDevice*)This)->_SilenceThread();
231 #if defined(PLATFORM_WINDOWS)
232 void WCMRNativeAudioNoneDevice::_usleep(uint64_t duration_usec)
236 ft.QuadPart = -(10*duration_usec); // Convert to 100 nanosecond interval, negative value indicates relative time
238 SetWaitableTimer(_waitableTimerForUsleep, &ft, 0, NULL, NULL, 0);
239 WaitForSingleObject(_waitableTimerForUsleep, INFINITE);
244 WCMRNativeAudioNoneDevice::__get_time_nanos ()
247 // here we exploit the time counting API which is used by the WCMRCoreAudioDeviceManager. However,
248 // the API should be a part of WCMRCoreAudioDeviceManager to give a chance of being tied to the
249 // audio device transport time�.
250 return AudioConvertHostTimeToNanos (AudioGetCurrentHostTime ());
252 #elif PLATFORM_WINDOWS
254 LARGE_INTEGER Frequency, Count ;
256 QueryPerformanceFrequency (&Frequency) ;
257 QueryPerformanceCounter (&Count);
258 return uint64_t ((Count.QuadPart * 1000000000.0 / Frequency.QuadPart));