AUD_Time.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*****************************************************************************
  19. ** **
  20. ** Westwood Studios Pacific. **
  21. ** **
  22. ** Confidential Information **
  23. ** Copyright (C) 2000 - All Rights Reserved **
  24. ** **
  25. ******************************************************************************
  26. ** **
  27. ** Project: Dune Emperor **
  28. ** **
  29. ** Module: <module> (<prefix>_) **
  30. ** **
  31. ** Version: $ID$ **
  32. ** **
  33. ** File name: audtimer.cpp **
  34. ** **
  35. ** Created by: 04/??/99 TR **
  36. ** **
  37. ** Description: <description> **
  38. ** **
  39. *****************************************************************************/
  40. /*****************************************************************************
  41. ** Includes **
  42. *****************************************************************************/
  43. #define WIN32_LEAN_AND_MEAN
  44. #include <stdio.h>
  45. #include <windows.h>
  46. #include <mmsystem.h>
  47. #include <assert.h>
  48. #include <wpaudio/time.h>
  49. /*****************************************************************************
  50. ** Externals **
  51. *****************************************************************************/
  52. /*****************************************************************************
  53. ** Defines **
  54. *****************************************************************************/
  55. /*****************************************************************************
  56. ** Private Types **
  57. *****************************************************************************/
  58. /*****************************************************************************
  59. ** Private Data **
  60. *****************************************************************************/
  61. static TimeStamp lastTime = 0;
  62. static TimeStamp interval = 0;
  63. static TimeStamp timeOut = 0;
  64. static TimeStamp (*timerFunc)( void ) = NULL;// set to either highResGetTime or
  65. // failsafeGetTime at initialization
  66. static LONGLONG timerMilliSecScale; // res counter millisecond scaling factor
  67. /*****************************************************************************
  68. ** Public Data **
  69. *****************************************************************************/
  70. /*****************************************************************************
  71. ** Private Prototypes **
  72. *****************************************************************************/
  73. static TimeStamp failsafeGetTime( void );
  74. static TimeStamp highResGetTime( void );
  75. /*****************************************************************************
  76. ** Private Functions **
  77. *****************************************************************************/
  78. /******************************************************************/
  79. /* */
  80. /* */
  81. /******************************************************************/
  82. static TimeStamp highResGetTime( void )
  83. {
  84. LARGE_INTEGER count;
  85. union
  86. {
  87. TimeStamp timeStamp;
  88. LARGE_INTEGER largeInt;
  89. }
  90. myTime;
  91. // read the high res counter
  92. QueryPerformanceCounter(&count);
  93. // convert high res ticks to number of milliseconds
  94. myTime.largeInt.QuadPart = count.QuadPart / timerMilliSecScale;
  95. return myTime.timeStamp;
  96. }
  97. /******************************************************************/
  98. /* */
  99. /* */
  100. /******************************************************************/
  101. static TimeStamp failsafeGetTime( void )
  102. {
  103. static volatile TimeStamp time = (TimeStamp) 0x100000000; // last time value
  104. static unsigned int* const pl = (unsigned int* const) &time; // low word of time
  105. static unsigned int* const ph = (unsigned int* const) ((unsigned int)&time + 4); // high word of time
  106. unsigned int now, lw, hw, called;
  107. static volatile unsigned int calls = 0;
  108. calls++;
  109. retry:
  110. called = calls;
  111. hw = *ph; // read high word of time stamp
  112. lw = *pl; // read low word of time stamp
  113. if ( called != calls)
  114. {
  115. // AudioGetTime() has been re-entered. Cannot trust lw and lh values
  116. goto retry;
  117. }
  118. reread:
  119. now = timeGetTime ();
  120. if ( now < lw )
  121. {
  122. // wrap round
  123. hw++; // increment high word by one
  124. }
  125. *ph = hw;
  126. *pl = now;
  127. if ( called != calls )
  128. {
  129. // re-entered. cannot trust *ph and *pl to be correct
  130. called = calls;
  131. goto reread;
  132. }
  133. return time;
  134. }
  135. /*****************************************************************************
  136. ** Public Functions **
  137. *****************************************************************************/
  138. /******************************************************************/
  139. /* */
  140. /* */
  141. /******************************************************************/
  142. //
  143. // Initialize the high resolution timer by querying the system for it's
  144. // availability. If one does exist then we set the game timer function
  145. // to 'highResGetTime' otherwise we use the original code at 'failsafeGetTime'.
  146. // For the hi res counter we precalculate the millisecond scaling factor to
  147. // convert hi res ticks to millisecond usage.
  148. void InitAudioTimer( void )
  149. {
  150. LARGE_INTEGER freq;
  151. // does hardware exist for high res counter?
  152. if (QueryPerformanceFrequency(&freq))
  153. {
  154. // calculate timer ticks per second
  155. timerMilliSecScale = freq.QuadPart / (LONGLONG) SECONDS(1);
  156. timerFunc = highResGetTime;
  157. }
  158. else
  159. {
  160. // no high res timer, use old code instead
  161. timerFunc = failsafeGetTime;
  162. }
  163. }
  164. /******************************************************************/
  165. /* */
  166. /* */
  167. /******************************************************************/
  168. TimeStamp AudioGetTime( void )
  169. {
  170. return timerFunc ? timerFunc() : 0 ;
  171. }