Source code of Windows XP (NT5)
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.

180 lines
3.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) 1998-1999 Microsoft Corporation
  6. //
  7. // File: timesig.h
  8. //
  9. //--------------------------------------------------------------------------
  10. // TimeSig.h : time signature stuff
  11. #ifndef __TIME_CONVERT__
  12. #define __TIME_CONVERT__
  13. #include "dmusici.h"
  14. #include "dmusicf.h"
  15. #include "score.h"
  16. #include "debug.h"
  17. struct DirectMusicTimeSig
  18. {
  19. // Time signatures define how many beats per measure, which note receives
  20. // the beat, and the grid resolution.
  21. DirectMusicTimeSig() : m_bBeatsPerMeasure(0), m_bBeat(0), m_wGridsPerBeat(0) { }
  22. DirectMusicTimeSig(BYTE bBPM, BYTE bBeat, WORD wGPB) :
  23. m_bBeatsPerMeasure(bBPM),
  24. m_bBeat(bBeat),
  25. m_wGridsPerBeat(wGPB)
  26. { }
  27. DirectMusicTimeSig(DMUS_TIMESIGNATURE& TSE) :
  28. m_bBeatsPerMeasure(TSE.bBeatsPerMeasure),
  29. m_bBeat(TSE.bBeat),
  30. m_wGridsPerBeat(TSE.wGridsPerBeat)
  31. { }
  32. operator DMUS_TIMESIGNATURE()
  33. {
  34. DMUS_TIMESIGNATURE TSE;
  35. TSE.bBeatsPerMeasure = m_bBeatsPerMeasure;
  36. TSE.bBeat = m_bBeat;
  37. TSE.wGridsPerBeat = m_wGridsPerBeat;
  38. TSE.mtTime = 0;
  39. return TSE;
  40. }
  41. MUSIC_TIME ClocksPerBeat()
  42. {
  43. if (m_bBeat)
  44. {
  45. return DMUS_PPQ * 4 / m_bBeat;
  46. }
  47. else
  48. {
  49. return 0;
  50. }
  51. }
  52. MUSIC_TIME FloorBeat(MUSIC_TIME mtTime)
  53. { MUSIC_TIME mtOneBeat = ClocksPerBeat();
  54. return (!mtOneBeat || mtTime < mtOneBeat) ? 0 : (mtTime - (mtTime % mtOneBeat));
  55. }
  56. MUSIC_TIME CeilingBeat(MUSIC_TIME mtTime)
  57. { return OnBeat(mtTime) ? mtTime : (FloorBeat(mtTime) + ClocksPerBeat());
  58. }
  59. BOOL OnBeat(MUSIC_TIME mtTime)
  60. { MUSIC_TIME mtOneBeat = ClocksPerBeat();
  61. return (!mtOneBeat) ? FALSE : !(mtTime % mtOneBeat);
  62. }
  63. MUSIC_TIME GridsToMeasure(WORD wGrid)
  64. {
  65. if (m_wGridsPerBeat && m_bBeatsPerMeasure)
  66. {
  67. return (wGrid / m_wGridsPerBeat) / m_bBeatsPerMeasure;
  68. }
  69. else
  70. {
  71. return 0;
  72. }
  73. }
  74. MUSIC_TIME GridsToBeat(WORD wGrid)
  75. {
  76. if (m_wGridsPerBeat && m_bBeatsPerMeasure)
  77. {
  78. return (wGrid / m_wGridsPerBeat) % m_bBeatsPerMeasure;
  79. }
  80. else
  81. {
  82. return 0;
  83. }
  84. }
  85. MUSIC_TIME GridOffset(WORD wGrid)
  86. {
  87. if (m_wGridsPerBeat)
  88. {
  89. return wGrid - ((wGrid / m_wGridsPerBeat) * m_wGridsPerBeat);
  90. }
  91. else
  92. {
  93. return 0;
  94. }
  95. }
  96. MUSIC_TIME ClocksPerGrid()
  97. {
  98. if (m_wGridsPerBeat)
  99. {
  100. return ClocksPerBeat() / m_wGridsPerBeat;
  101. }
  102. else
  103. {
  104. return 0;
  105. }
  106. }
  107. MUSIC_TIME ClocksPerMeasure()
  108. {
  109. return ClocksPerBeat() * m_bBeatsPerMeasure;
  110. }
  111. MUSIC_TIME ClocksToMeasure(DWORD dwTotalClocks)
  112. {
  113. MUSIC_TIME mtCPM = ClocksPerMeasure();
  114. if (mtCPM)
  115. {
  116. return (dwTotalClocks / mtCPM);
  117. }
  118. else
  119. {
  120. return 0;
  121. }
  122. }
  123. MUSIC_TIME ClocksToBeat(DWORD dwTotalClocks)
  124. {
  125. MUSIC_TIME mtCPB = ClocksPerBeat();
  126. if (mtCPB)
  127. {
  128. return dwTotalClocks / mtCPB;
  129. }
  130. else
  131. {
  132. return 0;
  133. }
  134. }
  135. MUSIC_TIME MeasureAndBeatToClocks(WORD wMeasure, BYTE bBeat)
  136. {
  137. return ClocksPerMeasure() * wMeasure + (ClocksPerBeat() * bBeat);
  138. }
  139. MUSIC_TIME GridToClocks(WORD wGrid)
  140. {
  141. if (m_wGridsPerBeat)
  142. {
  143. return (ClocksPerBeat() * (wGrid / m_wGridsPerBeat)) + (ClocksPerGrid() * (wGrid % m_wGridsPerBeat));
  144. }
  145. else
  146. {
  147. return ClocksPerGrid() * wGrid;
  148. }
  149. }
  150. BYTE m_bBeatsPerMeasure; // beats per measure (top of time sig)
  151. BYTE m_bBeat; // what note receives the beat (bottom of time sig.)
  152. // we can assume that 0 means 256th note
  153. WORD m_wGridsPerBeat; // grids per beat
  154. };
  155. // Convert old clocks to new clocks
  156. template <class T>
  157. inline T ConvertTime(T oldTime)
  158. { return (T)((DMUS_PPQ / PPQN) * oldTime); }
  159. #endif