Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.9 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. /* CPhaseLockClock
  3. */
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <stdio.h>
  7. #include "dmusicc.h"
  8. #include "dmusics.h"
  9. #include "plclock.h"
  10. #include "misc.h"
  11. #define MILS_TO_REF 10000
  12. CPhaseLockClock::CPhaseLockClock()
  13. {
  14. m_rfOffset = 0;
  15. }
  16. void CPhaseLockClock::Start(REFERENCE_TIME rfMasterTime, REFERENCE_TIME rfSlaveTime)
  17. /* When the clock starts, it needs to mark down the
  18. difference between the time it is given and its concept of time.
  19. */
  20. {
  21. m_rfOffset = rfMasterTime - rfSlaveTime;
  22. }
  23. void CPhaseLockClock::GetSlaveTime(REFERENCE_TIME rfSlaveTime, REFERENCE_TIME *prfTime)
  24. /* Convert the passed time to use the same base as the master clock.
  25. */
  26. {
  27. rfSlaveTime += m_rfOffset;
  28. *prfTime = rfSlaveTime;
  29. }
  30. void CPhaseLockClock::SetSlaveTime(REFERENCE_TIME rfSlaveTime, REFERENCE_TIME *prfTime)
  31. {
  32. rfSlaveTime -= m_rfOffset;
  33. *prfTime = rfSlaveTime;
  34. }
  35. void CPhaseLockClock::SyncToMaster(REFERENCE_TIME rfSlaveTime, REFERENCE_TIME rfMasterTime)
  36. /* SyncToTime provides the needed magic to keep the clock
  37. in sync. Since the clock uses its own clock (rfSlaveTime)
  38. to increment, it can drift. This call provides a reference
  39. time which the clock compares with its internal
  40. concept of time. The difference between the two is
  41. considered the drift. Since the sync time may increment in
  42. a lurching way, the correction has to be subtle.
  43. So, the difference between the two is divided by
  44. 100 and added to the offset.
  45. */
  46. {
  47. rfSlaveTime += m_rfOffset;
  48. rfSlaveTime -= rfMasterTime; // Find difference between calculated and expected time.
  49. rfSlaveTime /= 100; // Reduce in magnitude.
  50. m_rfOffset -= rfSlaveTime; // Subtract that from the original offset.
  51. }
  52. CSampleClock::CSampleClock()
  53. {
  54. m_dwStart = 0;
  55. m_dwSampleRate = 22050;
  56. }
  57. void CSampleClock::Start(IReferenceClock *pIClock, DWORD dwSampleRate, DWORD dwSamples)
  58. {
  59. REFERENCE_TIME rfStart;
  60. m_dwStart = dwSamples;
  61. m_dwSampleRate = dwSampleRate;
  62. if (pIClock)
  63. {
  64. pIClock->GetTime(&rfStart);
  65. m_PLClock.Start(rfStart,0);
  66. }
  67. }
  68. void CSampleClock::SampleToRefTime(LONGLONG llSampleTime,REFERENCE_TIME *prfTime)
  69. {
  70. llSampleTime -= m_dwStart;
  71. llSampleTime *= MILS_TO_REF;
  72. llSampleTime /= m_dwSampleRate;
  73. llSampleTime *= 1000;
  74. m_PLClock.GetSlaveTime(llSampleTime, prfTime);
  75. }
  76. LONGLONG CSampleClock::RefTimeToSample(REFERENCE_TIME rfTime)
  77. {
  78. m_PLClock.SetSlaveTime(rfTime, &rfTime);
  79. rfTime /= 1000;
  80. rfTime *= m_dwSampleRate;
  81. rfTime /= MILS_TO_REF;
  82. return rfTime + m_dwStart;
  83. }
  84. void CSampleClock::SyncToMaster(LONGLONG llSampleTime, IReferenceClock *pIClock)
  85. {
  86. llSampleTime -= m_dwStart;
  87. llSampleTime *= MILS_TO_REF;
  88. llSampleTime /= m_dwSampleRate;
  89. llSampleTime *= 1000;
  90. if (pIClock)
  91. {
  92. REFERENCE_TIME rfMasterTime;
  93. pIClock->GetTime(&rfMasterTime);
  94. m_PLClock.SyncToMaster(llSampleTime, rfMasterTime);
  95. }
  96. }