jack2 codebase
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.

239 lines
8.5KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackFrameTimer.h"
  17. #include "JackError.h"
  18. #include <math.h>
  19. #include <stdio.h>
  20. namespace Jack
  21. {
  22. #if defined(WIN32) && !defined(__MINGW32__)
  23. /* missing on Windows : see http://bugs.mysql.com/bug.php?id=15936 */
  24. inline double rint(double nr)
  25. {
  26. double f = floor(nr);
  27. double c = ceil(nr);
  28. return (((c -nr) >= (nr - f)) ? f : c);
  29. }
  30. #endif
  31. JackTimer::JackTimer()
  32. {
  33. fInitialized = false;
  34. fFrames = 0;
  35. fCurrentWakeup = 0;
  36. fCurrentCallback = 0;
  37. fNextWakeUp = 0;
  38. fPeriodUsecs = 0.0f;
  39. fFilterOmega = 0.0f; /* Initialised later */
  40. }
  41. jack_nframes_t JackTimer::Time2Frames(jack_time_t usecs, jack_nframes_t buffer_size)
  42. {
  43. if (fInitialized) {
  44. /*
  45. Make sure we have signed differences. It would make a lot of sense
  46. to use the standard signed intNN_t types everywhere instead of e.g.
  47. jack_nframes_t and jack_time_t. This would at least ensure that the
  48. types used below are the correct ones. There is no way to get a type
  49. that would be 'a signed version of jack_time_t' for example - the
  50. types below are inherently fragile and there is no automatic way to
  51. check they are the correct ones. The only way is to check manually
  52. against jack/types.h. FA - 16/02/2012
  53. */
  54. int64_t du = usecs - fCurrentWakeup;
  55. int64_t dp = fNextWakeUp - fCurrentWakeup;
  56. return fFrames + (int32_t)rint((double)du / (double)dp * buffer_size);
  57. } else {
  58. return 0;
  59. }
  60. }
  61. jack_time_t JackTimer::Frames2Time(jack_nframes_t frames, jack_nframes_t buffer_size)
  62. {
  63. if (fInitialized) {
  64. /*
  65. Make sure we have signed differences. It would make a lot of sense
  66. to use the standard signed intNN_t types everywhere instead of e.g.
  67. jack_nframes_t and jack_time_t. This would at least ensure that the
  68. types used below are the correct ones. There is no way to get a type
  69. that would be 'a signed version of jack_time_t' for example - the
  70. types below are inherently fragile and there is no automatic way to
  71. check they are the correct ones. The only way is to check manually
  72. against jack/types.h. FA - 16/02/2012
  73. */
  74. int32_t df = frames - fFrames;
  75. int64_t dp = fNextWakeUp - fCurrentWakeup;
  76. return fCurrentWakeup + (int64_t)rint((double) df * (double) dp / buffer_size);
  77. } else {
  78. return 0;
  79. }
  80. }
  81. int JackTimer::GetCycleTimes(jack_nframes_t* current_frames, jack_time_t* current_usecs, jack_time_t* next_usecs, float* period_usecs)
  82. {
  83. if (fInitialized) {
  84. *current_frames = fFrames;
  85. *current_usecs = fCurrentWakeup;
  86. *next_usecs = fNextWakeUp;
  87. *period_usecs = fPeriodUsecs;
  88. return 0;
  89. } else {
  90. return -1;
  91. }
  92. }
  93. jack_nframes_t JackTimer::FramesSinceCycleStart(jack_time_t cur_time, jack_nframes_t frames_rate)
  94. {
  95. return (jack_nframes_t) floor((((float)frames_rate) / 1000000.0f) * (cur_time - fCurrentCallback));
  96. }
  97. void JackFrameTimer::InitFrameTime()
  98. {
  99. fFirstWakeUp = true;
  100. }
  101. void JackFrameTimer::IncFrameTime(jack_nframes_t buffer_size, jack_time_t callback_usecs, jack_time_t period_usecs)
  102. {
  103. if (fFirstWakeUp) {
  104. InitFrameTimeAux(callback_usecs, period_usecs);
  105. fFirstWakeUp = false;
  106. }
  107. IncFrameTimeAux(buffer_size, callback_usecs, period_usecs);
  108. }
  109. void JackFrameTimer::ResetFrameTime(jack_time_t callback_usecs)
  110. {
  111. if (!fFirstWakeUp) { // ResetFrameTime may be called by a xrun/delayed wakeup on the first cycle
  112. JackTimer* timer = WriteNextStateStart();
  113. timer->fCurrentWakeup = callback_usecs;
  114. timer->fCurrentCallback = callback_usecs;
  115. WriteNextStateStop();
  116. TrySwitchState(); // always succeed since there is only one writer
  117. }
  118. }
  119. /*
  120. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  121. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  122. read to loop forever.
  123. */
  124. void JackFrameTimer::ReadFrameTime(JackTimer* timer)
  125. {
  126. UInt16 next_index = GetCurrentIndex();
  127. UInt16 cur_index;
  128. do {
  129. cur_index = next_index;
  130. memcpy(timer, ReadCurrentState(), sizeof(JackTimer));
  131. next_index = GetCurrentIndex();
  132. } while (cur_index != next_index); // Until a coherent state has been read
  133. }
  134. // Internal
  135. void JackFrameTimer::InitFrameTimeAux(jack_time_t callback_usecs, jack_time_t period_usecs)
  136. {
  137. /* the first wakeup or post-freewheeling or post-xrun */
  138. /* There seems to be no significant difference between
  139. the two conditions OR-ed above. Incrementing the
  140. frame_time after an xrun shouldn't harm, as there
  141. will be a discontinuity anyway. So the two are
  142. combined in this version.
  143. FA 16/03/2012
  144. */
  145. /* Since the DLL *will* be run, next_wakeup should be the
  146. current wakeup time *without* adding the period time, as
  147. if it were computed in the previous period.
  148. FA 16/03/2012
  149. */
  150. /* Added initialisation of timer->period_usecs, required
  151. due to the modified implementation of the DLL itself.
  152. OTOH, this should maybe not be repeated after e.g.
  153. freewheeling or an xrun, as the current value would be
  154. more accurate than the nominal one. But it doesn't really
  155. harm either. Implementing this would require a new flag
  156. in the engine structure, to be used after freewheeling
  157. or an xrun instead of first_wakeup. I don't know if this
  158. can be done without breaking compatibility, so I did not
  159. add this
  160. FA 13/02/2012
  161. */
  162. /* Added initialisation of timer->filter_omega. This makes
  163. the DLL bandwidth independent of the actual period time.
  164. The bandwidth is now 1/8 Hz in all cases. The value of
  165. timer->filter_omega is 2 * pi * BW * Tperiod.
  166. FA 13/02/2012
  167. */
  168. JackTimer* timer = WriteNextStateStart();
  169. timer->fPeriodUsecs = (float)period_usecs;
  170. timer->fCurrentCallback = callback_usecs;
  171. timer->fNextWakeUp = callback_usecs;
  172. timer->fFilterOmega = period_usecs * 7.854e-7f;
  173. WriteNextStateStop();
  174. TrySwitchState(); // always succeed since there is only one writer
  175. }
  176. void JackFrameTimer::IncFrameTimeAux(jack_nframes_t buffer_size, jack_time_t callback_usecs, jack_time_t period_usecs)
  177. {
  178. JackTimer* timer = WriteNextStateStart();
  179. /* Modified implementation (the actual result is the same).
  180. 'fSecondOrderIntegrator' is renamed to 'fPeriodUsecs'
  181. and now represents the DLL's best estimate of the
  182. period time in microseconds (before it was a scaled
  183. version of the difference w.r.t. the nominal value).
  184. This allows this value to be made available to clients
  185. that are interested in it (see jack_get_cycle_times).
  186. This change also means that 'fPeriodUsecs' must be
  187. initialised to the nominal period time instead of zero.
  188. This is done in the first cycle in jack_run_cycle().
  189. 'fFilterCoefficient' is renamed to 'fFilterOmega'. It
  190. is now equal to the 'omega' value as defined in the
  191. 'Using a DLL to filter time' paper (before it was a
  192. scaled version of this value). It is computed once in
  193. jack_run_cycle() rather than set to a fixed value. This
  194. makes the DLL bandwidth independent of the period time.
  195. FA 13/02/2012
  196. */
  197. float delta = (float)((int64_t)callback_usecs - (int64_t)timer->fNextWakeUp);
  198. delta *= timer->fFilterOmega;
  199. timer->fCurrentWakeup = timer->fNextWakeUp;
  200. timer->fCurrentCallback = callback_usecs;
  201. timer->fFrames += buffer_size;
  202. timer->fPeriodUsecs += timer->fFilterOmega * delta;
  203. timer->fNextWakeUp += (int64_t)floorf(timer->fPeriodUsecs + 1.41f * delta + 0.5f);
  204. timer->fInitialized = true;
  205. WriteNextStateStop();
  206. TrySwitchState(); // always succeed since there is only one writer
  207. }
  208. } // end of namespace