1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by
4 Apple Inc. ("Apple") in consideration of your agreement to the
5 following terms, and your use, installation, modification or
6 redistribution of this Apple software constitutes acceptance of these
7 terms. If you do not agree with these terms, please do not use,
8 install, modify or redistribute this Apple software.
10 In consideration of your agreement to abide by the following terms, and
11 subject to these terms, Apple grants you a personal, non-exclusive
12 license, under Apple's copyrights in this original Apple software (the
13 "Apple Software"), to use, reproduce, modify and redistribute the Apple
14 Software, with or without modifications, in source and/or binary forms;
15 provided that if you redistribute the Apple Software in its entirety and
16 without modifications, you must retain this notice and the following
17 text and disclaimers in all such redistributions of the Apple Software.
18 Neither the name, trademarks, service marks or logos of Apple Inc.
19 may be used to endorse or promote products derived from the Apple
20 Software without specific prior written permission from Apple. Except
21 as expressly stated in this notice, no other rights or licenses, express
22 or implied, are granted by Apple herein, including but not limited to
23 any patent rights that may be infringed by your derivative works or by
24 other works in which the Apple Software may be incorporated.
26 The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
32 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
41 /*=============================================================================
44 =============================================================================*/
45 #if !defined(__CAHostTimeBase_h__)
46 #define __CAHostTimeBase_h__
48 //=============================================================================
50 //=============================================================================
52 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
53 #include <CoreAudio/CoreAudioTypes.h>
55 #include <CoreAudioTypes.h>
59 #include <mach/mach_time.h>
63 #error Unsupported operating system
66 #include "CADebugMacros.h"
68 //=============================================================================
71 // This class provides platform independent access to the host's time base.
72 //=============================================================================
75 // #define Log_Host_Time_Base_Parameters 1
76 // #define Track_Host_TimeBase 1
83 static UInt64
ConvertToNanos(UInt64 inHostTime
);
84 static UInt64
ConvertFromNanos(UInt64 inNanos
);
86 static UInt64
GetTheCurrentTime();
88 static UInt64
GetCurrentTime() { return GetTheCurrentTime(); }
90 static UInt64
GetCurrentTimeInNanos();
92 static Float64
GetFrequency() { if(!sIsInited
) { Initialize(); } return sFrequency
; }
93 static UInt32
GetMinimumDelta() { if(!sIsInited
) { Initialize(); } return sMinDelta
; }
95 static UInt64
AbsoluteHostDeltaToNanos(UInt64 inStartTime
, UInt64 inEndTime
);
96 static SInt64
HostDeltaToNanos(UInt64 inStartTime
, UInt64 inEndTime
);
99 static void Initialize();
101 static bool sIsInited
;
103 static Float64 sFrequency
;
104 static UInt32 sMinDelta
;
105 static UInt32 sToNanosNumerator
;
106 static UInt32 sToNanosDenominator
;
107 static UInt32 sFromNanosNumerator
;
108 static UInt32 sFromNanosDenominator
;
109 static bool sUseMicroseconds
;
110 #if Track_Host_TimeBase
111 static UInt64 sLastTime
;
115 inline UInt64
CAHostTimeBase::GetTheCurrentTime()
120 theTime
= mach_absolute_time();
121 #elif TARGET_OS_WIN32
122 LARGE_INTEGER theValue
;
123 QueryPerformanceCounter(&theValue
);
124 theTime
= *((UInt64
*)&theValue
);
127 #if Track_Host_TimeBase
130 if(theTime
<= sLastTime
)
132 DebugMessageN2("CAHostTimeBase::GetTheCurrentTime: the current time is earlier than the last time, now: %qd, then: %qd", theTime
, sLastTime
);
145 inline UInt64
CAHostTimeBase::ConvertToNanos(UInt64 inHostTime
)
152 Float64 theNumerator
= static_cast<Float64
>(sToNanosNumerator
);
153 Float64 theDenominator
= static_cast<Float64
>(sToNanosDenominator
);
154 Float64 theHostTime
= static_cast<Float64
>(inHostTime
);
156 Float64 thePartialAnswer
= theHostTime
/ theDenominator
;
157 Float64 theFloatAnswer
= thePartialAnswer
* theNumerator
;
158 UInt64 theAnswer
= static_cast<UInt64
>(theFloatAnswer
);
160 //Assert(!((theNumerator > theDenominator) && (theAnswer < inHostTime)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
161 //Assert(!((theDenominator > theNumerator) && (theAnswer > inHostTime)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
166 inline UInt64
CAHostTimeBase::ConvertFromNanos(UInt64 inNanos
)
173 Float64 theNumerator
= static_cast<Float64
>(sToNanosNumerator
);
174 Float64 theDenominator
= static_cast<Float64
>(sToNanosDenominator
);
175 Float64 theNanos
= static_cast<Float64
>(inNanos
);
177 Float64 thePartialAnswer
= theNanos
/ theNumerator
;
178 Float64 theFloatAnswer
= thePartialAnswer
* theDenominator
;
179 UInt64 theAnswer
= static_cast<UInt64
>(theFloatAnswer
);
181 //Assert(!((theDenominator > theNumerator) && (theAnswer < inNanos)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
182 //Assert(!((theNumerator > theDenominator) && (theAnswer > inNanos)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
188 inline UInt64
CAHostTimeBase::GetCurrentTimeInNanos()
190 return ConvertToNanos(GetTheCurrentTime());
193 inline UInt64
CAHostTimeBase::AbsoluteHostDeltaToNanos(UInt64 inStartTime
, UInt64 inEndTime
)
197 if(inStartTime
<= inEndTime
)
199 theAnswer
= inEndTime
- inStartTime
;
203 theAnswer
= inStartTime
- inEndTime
;
206 return ConvertToNanos(theAnswer
);
209 inline SInt64
CAHostTimeBase::HostDeltaToNanos(UInt64 inStartTime
, UInt64 inEndTime
)
214 if(inStartTime
<= inEndTime
)
216 theAnswer
= inEndTime
- inStartTime
;
220 theAnswer
= inStartTime
- inEndTime
;
224 return theSign
* ConvertToNanos(theAnswer
);