41 #if !defined(__CAHostTimeBase_h__)
42 #define __CAHostTimeBase_h__
48 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
49 #include <CoreAudio/CoreAudioTypes.h>
51 #include <CoreAudioTypes.h>
55 #include <mach/mach_time.h>
59 #error Unsupported operating system
62 #include "CADebugMacros.h"
79 static UInt64 ConvertToNanos(UInt64 inHostTime);
80 static UInt64 ConvertFromNanos(UInt64 inNanos);
82 static UInt64 GetTheCurrentTime();
84 static UInt64 GetCurrentTime() {
return GetTheCurrentTime(); }
86 static UInt64 GetCurrentTimeInNanos();
88 static Float64 GetFrequency() {
if(!sIsInited) { Initialize(); }
return sFrequency; }
89 static Float64 GetInverseFrequency() {
if(!sIsInited) { Initialize(); }
return sInverseFrequency; }
90 static UInt32 GetMinimumDelta() {
if(!sIsInited) { Initialize(); }
return sMinDelta; }
92 static UInt64 AbsoluteHostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime);
93 static SInt64 HostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime);
96 static void Initialize();
98 static bool sIsInited;
100 static Float64 sFrequency;
101 static Float64 sInverseFrequency;
102 static UInt32 sMinDelta;
103 static UInt32 sToNanosNumerator;
104 static UInt32 sToNanosDenominator;
105 static UInt32 sFromNanosNumerator;
106 static UInt32 sFromNanosDenominator;
107 static bool sUseMicroseconds;
108 #if Track_Host_TimeBase
109 static UInt64 sLastTime;
113 inline UInt64 CAHostTimeBase::GetTheCurrentTime()
118 theTime = mach_absolute_time();
119 #elif TARGET_OS_WIN32
120 LARGE_INTEGER theValue;
121 QueryPerformanceCounter(&theValue);
122 theTime = *((UInt64*)&theValue);
125 #if Track_Host_TimeBase
128 if(theTime <= sLastTime)
130 DebugMessageN2(
"CAHostTimeBase::GetTheCurrentTime: the current time is earlier than the last time, now: %qd, then: %qd", theTime, sLastTime);
143 inline UInt64 CAHostTimeBase::ConvertToNanos(UInt64 inHostTime)
150 Float64 theNumerator =
static_cast<Float64
>(sToNanosNumerator);
151 Float64 theDenominator =
static_cast<Float64
>(sToNanosDenominator);
152 Float64 theHostTime =
static_cast<Float64
>(inHostTime);
154 Float64 thePartialAnswer = theHostTime / theDenominator;
155 Float64 theFloatAnswer = thePartialAnswer * theNumerator;
156 UInt64 theAnswer =
static_cast<UInt64
>(theFloatAnswer);
164 inline UInt64 CAHostTimeBase::ConvertFromNanos(UInt64 inNanos)
171 Float64 theNumerator =
static_cast<Float64
>(sToNanosNumerator);
172 Float64 theDenominator =
static_cast<Float64
>(sToNanosDenominator);
173 Float64 theNanos =
static_cast<Float64
>(inNanos);
175 Float64 thePartialAnswer = theNanos / theNumerator;
176 Float64 theFloatAnswer = thePartialAnswer * theDenominator;
177 UInt64 theAnswer =
static_cast<UInt64
>(theFloatAnswer);
186 inline UInt64 CAHostTimeBase::GetCurrentTimeInNanos()
188 return ConvertToNanos(GetTheCurrentTime());
191 inline UInt64 CAHostTimeBase::AbsoluteHostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime)
195 if(inStartTime <= inEndTime)
197 theAnswer = inEndTime - inStartTime;
201 theAnswer = inStartTime - inEndTime;
204 return ConvertToNanos(theAnswer);
207 inline SInt64 CAHostTimeBase::HostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime)
212 if(inStartTime <= inEndTime)
214 theAnswer = inEndTime - inStartTime;
218 theAnswer = inStartTime - inEndTime;
222 return theSign * ConvertToNanos(theAnswer);