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/pthread_utils.h"
16 #include "MiscUtils/safe_delete.h"
19 #include <boost/assign/list_of.hpp>
21 #define NONE_DEVICE_NAME "None"
22 #define NONE_DEVICE_INPUT_NAMES "Input "
23 #define NONE_DEVICE_OUTPUT_NAMES "Output "
25 //**********************************************************************************************
26 // WCMRNativeAudioNoneDevice::WCMRNativeAudioNoneDevice
28 //! Constructor for the dummy "None" device. This constructor simply adds supported SRs,
29 //! buffer sizes, and channels, so that it may look like a real native device to
32 //! \param pManager : The managing device manager - simply passed on to the base class.
35 //**********************************************************************************************
36 WCMRNativeAudioNoneDevice::WCMRNativeAudioNoneDevice (WCMRAudioDeviceManager *pManager)
37 : WCMRNativeAudioDevice (pManager, false /*useMultiThreading*/)
38 #if defined (PLATFORM_WINDOWS)
39 , _waitableTimerForUsleep (CreateWaitableTimer(NULL, TRUE, NULL))
42 mark_pthread_inactive (m_SilenceThread);
44 m_DeviceName = NONE_DEVICE_NAME;
46 m_SamplingRates = boost::assign::list_of (m_CurrentSamplingRate=44100)(48000)(88200)(96000);
48 m_BufferSizes = boost::assign::list_of (32)(64)(128)(m_CurrentBufferSize=256)(512)(1024);
50 for (int channel = 0; channel < __m_NumInputChannels; channel++)
52 std::stringstream name;
53 name << NONE_DEVICE_INPUT_NAMES;
54 name << (channel + 1);
55 m_InputChannels.push_back(name.str());
58 for (int channel = 0; channel < __m_NumOutputChannels; channel++)
60 std::stringstream name;
61 name << NONE_DEVICE_INPUT_NAMES;
62 name << (channel + 1);
63 m_OutputChannels.push_back(name.str());
65 _m_inputBuffer = new float[__m_NumInputChannels * m_BufferSizes.back()];
66 _m_outputBuffer = new float[__m_NumOutputChannels * m_BufferSizes.back()];
70 WCMRNativeAudioNoneDevice::~WCMRNativeAudioNoneDevice ()
72 #if defined (PLATFORM_WINDOWS)
73 if(_waitableTimerForUsleep) {
74 CloseHandle(_waitableTimerForUsleep);
79 WTErr WCMRNativeAudioNoneDevice::SetActive (bool newState)
81 //This will most likely be overridden, the base class simply
83 if (Active() == newState)
88 if (Active() && Streaming())
92 return WCMRAudioDevice::SetActive(newState);
95 WTErr WCMRNativeAudioNoneDevice::SetCurrentBufferSize (int newSize)
99 int oldSize = CurrentBufferSize();
100 bool oldActive = Active();
102 //same size, nothing to do.
103 if (oldSize == newSize)
106 //see if this is one of our supported rates...
107 std::vector<int>::iterator intIter = find(m_BufferSizes.begin(), m_BufferSizes.end(), newSize);
108 if (intIter == m_BufferSizes.end())
110 //Can't change, perhaps use an "invalid param" type of error
111 return eCommandLineParameter;
116 //Can't change, perhaps use an "in use" type of error
121 return WCMRAudioDevice::SetCurrentBufferSize(newSize);
125 WTErr WCMRNativeAudioNoneDevice::UpdateDeviceInfo ()
131 WTErr WCMRNativeAudioNoneDevice::SetStreaming (bool newState)
133 if (Streaming() == newState)
138 WCMRAudioDevice::SetStreaming(newState);
142 if (is_pthread_active (m_SilenceThread))
143 std::cerr << "\t\t\t\t\t !!!!!!!!!!!!!!! Warning: the inactive NONE-DEVICE was streaming!" << std::endl;
145 pthread_attr_t attributes;
146 size_t stack_size = 100000;
148 stack_size = (((stack_size - 1) / PTHREAD_STACK_MIN) + 1) * PTHREAD_STACK_MIN;
150 if (pthread_attr_init (&attributes)) {
151 std::cerr << "WCMRNativeAudioNoneDevice::SetStreaming (): pthread_attr_init () failed!" << std::endl;
155 if (pthread_attr_setstacksize (&attributes, stack_size)) {
156 std::cerr << "WCMRNativeAudioNoneDevice::SetStreaming (): pthread_attr_setstacksize () failed!" << std::endl;
160 if (pthread_create (&m_SilenceThread, &attributes, __SilenceThread, this)) {
161 mark_pthread_inactive (m_SilenceThread);
162 std::cerr << "WCMRNativeAudioNoneDevice::SetStreaming (): pthread_create () failed!" << std::endl;
168 if (!is_pthread_active (m_SilenceThread))
170 std::cerr << "\t\t\t\t\t !!!!!!!!!!!!!!! Warning: the active NONE-DEVICE was NOT streaming!" << std::endl;
173 while (is_pthread_active (m_SilenceThread))
175 _usleep(1); //now wait for ended thread;
182 void WCMRNativeAudioNoneDevice::_SilenceThread()
184 #if defined(PLATFORM_WINDOWS)
185 float* theInpBuffers[__m_NumInputChannels];
186 for(int i = 0; i < __m_NumInputChannels; ++i)
188 theInpBuffers[i] = _m_inputBuffer + m_BufferSizes.back() * i;
191 float* theInpBuffers = _m_inputBuffer;
194 uint32_t currentSampleTime = 0;
195 const size_t buffer_size = CurrentBufferSize();
196 const uint64_t cyclePeriodNanos = (1000000000.0 * buffer_size) / CurrentSamplingRate();
198 struct WCMRAudioDeviceManagerClient::AudioCallbackData audioCallbackData =
200 (const float*)theInpBuffers,
207 audioCallbackData.acdCycleStartTimeNanos =__get_time_nanos();
209 // VERY ROUGH IMPLEMENTATION:
212 uint64_t cycleEndTimeNanos = audioCallbackData.acdCycleStartTimeNanos + cyclePeriodNanos;
214 m_pMyManager->NotifyClient (WCMRAudioDeviceManagerClient::AudioCallback, (void *)&audioCallbackData);
216 currentSampleTime += buffer_size;
218 int64_t timeToSleepUsecs = ((int64_t)cycleEndTimeNanos - (int64_t)__get_time_nanos())/1000;
220 if (timeToSleepUsecs > 0) {
221 _usleep (timeToSleepUsecs);
223 audioCallbackData.acdCycleStartTimeNanos = cycleEndTimeNanos+1;
225 mark_pthread_inactive (m_SilenceThread);
228 void* WCMRNativeAudioNoneDevice::__SilenceThread(void *This)
230 ((WCMRNativeAudioNoneDevice*)This)->_SilenceThread();
234 #if defined(PLATFORM_WINDOWS)
235 void WCMRNativeAudioNoneDevice::_usleep(uint64_t duration_usec)
239 ft.QuadPart = -(10*duration_usec); // Convert to 100 nanosecond interval, negative value indicates relative time
241 SetWaitableTimer(_waitableTimerForUsleep, &ft, 0, NULL, NULL, 0);
242 WaitForSingleObject(_waitableTimerForUsleep, INFINITE);
247 WCMRNativeAudioNoneDevice::__get_time_nanos ()
250 // here we exploit the time counting API which is used by the WCMRCoreAudioDeviceManager. However,
251 // the API should be a part of WCMRCoreAudioDeviceManager to give a chance of being tied to the
252 // audio device transport time�.
253 return AudioConvertHostTimeToNanos (AudioGetCurrentHostTime ());
255 #elif PLATFORM_WINDOWS
257 LARGE_INTEGER Frequency, Count ;
259 QueryPerformanceFrequency (&Frequency) ;
260 QueryPerformanceCounter (&Count);
261 return uint64_t ((Count.QuadPart * 1000000000.0 / Frequency.QuadPart));