Jamoma API  0.6.0.a19
CAHostTimeBase.h
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
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.
9 
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.
25 
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.
31 
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.
40 */
41 #if !defined(__CAHostTimeBase_h__)
42 #define __CAHostTimeBase_h__
43 
44 //=============================================================================
45 // Includes
46 //=============================================================================
47 
48 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
49  #include <CoreAudio/CoreAudioTypes.h>
50 #else
51  #include <CoreAudioTypes.h>
52 #endif
53 
54 #if TARGET_OS_MAC
55  #include <mach/mach_time.h>
56 #elif TARGET_OS_WIN32
57  #include <windows.h>
58 #else
59  #error Unsupported operating system
60 #endif
61 
62 #include "CADebugMacros.h"
63 
64 //=============================================================================
65 // CAHostTimeBase
66 //
67 // This class provides platform independent access to the host's time base.
68 //=============================================================================
69 
70 #if CoreAudio_Debug
71 // #define Log_Host_Time_Base_Parameters 1
72 // #define Track_Host_TimeBase 1
73 #endif
74 
75 class CAHostTimeBase
76 {
77 
78 public:
79  static UInt64 ConvertToNanos(UInt64 inHostTime);
80  static UInt64 ConvertFromNanos(UInt64 inNanos);
81 
82  static UInt64 GetTheCurrentTime();
83 #if TARGET_OS_MAC
84  static UInt64 GetCurrentTime() { return GetTheCurrentTime(); }
85 #endif
86  static UInt64 GetCurrentTimeInNanos();
87 
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; }
91 
92  static UInt64 AbsoluteHostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime);
93  static SInt64 HostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime);
94 
95 private:
96  static void Initialize();
97 
98  static bool sIsInited;
99 
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;
110 #endif
111 };
112 
113 inline UInt64 CAHostTimeBase::GetTheCurrentTime()
114 {
115  UInt64 theTime = 0;
116 
117  #if TARGET_OS_MAC
118  theTime = mach_absolute_time();
119  #elif TARGET_OS_WIN32
120  LARGE_INTEGER theValue;
121  QueryPerformanceCounter(&theValue);
122  theTime = *((UInt64*)&theValue);
123  #endif
124 
125  #if Track_Host_TimeBase
126  if(sLastTime != 0)
127  {
128  if(theTime <= sLastTime)
129  {
130  DebugMessageN2("CAHostTimeBase::GetTheCurrentTime: the current time is earlier than the last time, now: %qd, then: %qd", theTime, sLastTime);
131  }
132  sLastTime = theTime;
133  }
134  else
135  {
136  sLastTime = theTime;
137  }
138  #endif
139 
140  return theTime;
141 }
142 
143 inline UInt64 CAHostTimeBase::ConvertToNanos(UInt64 inHostTime)
144 {
145  if(!sIsInited)
146  {
147  Initialize();
148  }
149 
150  Float64 theNumerator = static_cast<Float64>(sToNanosNumerator);
151  Float64 theDenominator = static_cast<Float64>(sToNanosDenominator);
152  Float64 theHostTime = static_cast<Float64>(inHostTime);
153 
154  Float64 thePartialAnswer = theHostTime / theDenominator;
155  Float64 theFloatAnswer = thePartialAnswer * theNumerator;
156  UInt64 theAnswer = static_cast<UInt64>(theFloatAnswer);
157 
158  //Assert(!((theNumerator > theDenominator) && (theAnswer < inHostTime)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
159  //Assert(!((theDenominator > theNumerator) && (theAnswer > inHostTime)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
160 
161  return theAnswer;
162 }
163 
164 inline UInt64 CAHostTimeBase::ConvertFromNanos(UInt64 inNanos)
165 {
166  if(!sIsInited)
167  {
168  Initialize();
169  }
170 
171  Float64 theNumerator = static_cast<Float64>(sToNanosNumerator);
172  Float64 theDenominator = static_cast<Float64>(sToNanosDenominator);
173  Float64 theNanos = static_cast<Float64>(inNanos);
174 
175  Float64 thePartialAnswer = theNanos / theNumerator;
176  Float64 theFloatAnswer = thePartialAnswer * theDenominator;
177  UInt64 theAnswer = static_cast<UInt64>(theFloatAnswer);
178 
179  //Assert(!((theDenominator > theNumerator) && (theAnswer < inNanos)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
180  //Assert(!((theNumerator > theDenominator) && (theAnswer > inNanos)), "CAHostTimeBase::ConvertToNanos: The conversion wrapped");
181 
182  return theAnswer;
183 }
184 
185 
186 inline UInt64 CAHostTimeBase::GetCurrentTimeInNanos()
187 {
188  return ConvertToNanos(GetTheCurrentTime());
189 }
190 
191 inline UInt64 CAHostTimeBase::AbsoluteHostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime)
192 {
193  UInt64 theAnswer;
194 
195  if(inStartTime <= inEndTime)
196  {
197  theAnswer = inEndTime - inStartTime;
198  }
199  else
200  {
201  theAnswer = inStartTime - inEndTime;
202  }
203 
204  return ConvertToNanos(theAnswer);
205 }
206 
207 inline SInt64 CAHostTimeBase::HostDeltaToNanos(UInt64 inStartTime, UInt64 inEndTime)
208 {
209  SInt64 theAnswer;
210  SInt64 theSign = 1;
211 
212  if(inStartTime <= inEndTime)
213  {
214  theAnswer = inEndTime - inStartTime;
215  }
216  else
217  {
218  theAnswer = inStartTime - inEndTime;
219  theSign = -1;
220  }
221 
222  return theSign * ConvertToNanos(theAnswer);
223 }
224 
225 #endif