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.

139 lines
4.7KB

  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackFrameTimer.h"
  20. #include "JackError.h"
  21. #include <math.h>
  22. namespace Jack
  23. {
  24. JackTimer::JackTimer()
  25. {
  26. fInitialized = false;
  27. fFrames = 0;
  28. fCurrentWakeup = 0;
  29. fCurrentCallback = 0;
  30. fNextWakeUp = 0;
  31. fFilterCoefficient = 0.01f;
  32. fSecondOrderIntegrator = 0.0f;
  33. }
  34. jack_nframes_t JackTimer::Time2Frames(jack_time_t time, jack_nframes_t buffer_size)
  35. {
  36. if (fInitialized) {
  37. return fFrames + (long)rint(((double) ((time - fCurrentWakeup)) / ((jack_time_t)(fNextWakeUp - fCurrentWakeup))) * buffer_size);
  38. } else {
  39. return 0;
  40. }
  41. }
  42. jack_time_t JackTimer::Frames2Time(jack_nframes_t frames, jack_nframes_t buffer_size)
  43. {
  44. if (fInitialized) {
  45. return fCurrentWakeup + (long)rint(((double) ((frames - fFrames)) * ((jack_time_t)(fNextWakeUp - fCurrentWakeup))) / buffer_size);
  46. } else {
  47. return 0;
  48. }
  49. }
  50. jack_nframes_t JackTimer::FramesSinceCycleStart(jack_time_t cur_time, jack_nframes_t frames_rate)
  51. {
  52. return (jack_nframes_t) floor((((float)frames_rate) / 1000000.0f) * (cur_time - fCurrentCallback));
  53. }
  54. void JackFrameTimer::InitFrameTime()
  55. {
  56. fFirstWakeUp = true;
  57. }
  58. void JackFrameTimer::IncFrameTime(jack_nframes_t buffer_size, jack_time_t callback_usecs, jack_time_t period_usecs)
  59. {
  60. if (fFirstWakeUp) {
  61. InitFrameTimeAux(callback_usecs, period_usecs);
  62. fFirstWakeUp = false;
  63. } else {
  64. IncFrameTimeAux(buffer_size, callback_usecs, period_usecs);
  65. }
  66. }
  67. void JackFrameTimer::ResetFrameTime(jack_nframes_t frames_rate, jack_time_t callback_usecs, jack_time_t period_usecs)
  68. {
  69. if (!fFirstWakeUp) { // ResetFrameTime may be called by a xrun/delayed wakeup on the first cycle
  70. JackTimer* timer = WriteNextStateStart();
  71. jack_nframes_t period_size_guess = (jack_nframes_t)(frames_rate * ((timer->fNextWakeUp - timer->fCurrentWakeup) / 1000000.0));
  72. timer->fFrames += ((callback_usecs - timer->fNextWakeUp) / period_size_guess) * period_size_guess;
  73. timer->fCurrentWakeup = callback_usecs;
  74. timer->fCurrentCallback = callback_usecs;
  75. timer->fNextWakeUp = callback_usecs + period_usecs;
  76. WriteNextStateStop();
  77. TrySwitchState(); // always succeed since there is only one writer
  78. }
  79. }
  80. /*
  81. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  82. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  83. read to loop forever.
  84. */
  85. void JackFrameTimer::ReadFrameTime(JackTimer* timer)
  86. {
  87. UInt16 next_index = GetCurrentIndex();
  88. UInt16 cur_index;
  89. do {
  90. cur_index = next_index;
  91. memcpy(timer, ReadCurrentState(), sizeof(JackTimer));
  92. next_index = GetCurrentIndex();
  93. } while (cur_index != next_index); // Until a coherent state has been read
  94. }
  95. // Internal
  96. void JackFrameTimer::InitFrameTimeAux(jack_time_t callback_usecs, jack_time_t period_usecs)
  97. {
  98. JackTimer* timer = WriteNextStateStart();
  99. timer->fSecondOrderIntegrator = 0.0f;
  100. timer->fCurrentCallback = callback_usecs;
  101. timer->fNextWakeUp = callback_usecs + period_usecs;
  102. WriteNextStateStop();
  103. TrySwitchState(); // always succeed since there is only one writer
  104. }
  105. void JackFrameTimer::IncFrameTimeAux(jack_nframes_t buffer_size, jack_time_t callback_usecs, jack_time_t period_usecs)
  106. {
  107. JackTimer* timer = WriteNextStateStart();
  108. float delta = (int64_t)callback_usecs - (int64_t)timer->fNextWakeUp;
  109. timer->fCurrentWakeup = timer->fNextWakeUp;
  110. timer->fCurrentCallback = callback_usecs;
  111. timer->fFrames += buffer_size;
  112. timer->fSecondOrderIntegrator += 0.5f * timer->fFilterCoefficient * delta;
  113. timer->fNextWakeUp = timer->fCurrentWakeup + period_usecs + (int64_t) floorf((timer->fFilterCoefficient * (delta + timer->fSecondOrderIntegrator)));
  114. timer->fInitialized = true;
  115. WriteNextStateStop();
  116. TrySwitchState(); // always succeed since there is only one writer
  117. }
  118. } // end of namespace