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.

116 lines
3.9KB

  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. void JackFrameTimer::InitFrameTime()
  35. {
  36. fFirstWakeUp = true;
  37. }
  38. void JackFrameTimer::IncFrameTime(jack_nframes_t nframes, jack_time_t callback_usecs, jack_time_t period_usecs)
  39. {
  40. if (fFirstWakeUp) {
  41. InitFrameTimeAux(callback_usecs, period_usecs);
  42. fFirstWakeUp = false;
  43. } else {
  44. IncFrameTimeAux(nframes, callback_usecs, period_usecs);
  45. }
  46. }
  47. void JackFrameTimer::ResetFrameTime(jack_nframes_t frames_rate, jack_time_t callback_usecs, jack_time_t period_usecs)
  48. {
  49. if (!fFirstWakeUp) { // ResetFrameTime may be called by a xrun/delayed wakeup on the first cycle
  50. JackTimer* timer = WriteNextStateStart();
  51. jack_nframes_t period_size_guess = (jack_nframes_t)(frames_rate * ((timer->fNextWakeUp - timer->fCurrentWakeup) / 1000000.0));
  52. timer->fFrames += ((callback_usecs - timer->fNextWakeUp) / period_size_guess) * period_size_guess;
  53. timer->fCurrentWakeup = callback_usecs;
  54. timer->fCurrentCallback = callback_usecs;
  55. timer->fNextWakeUp = callback_usecs + period_usecs;
  56. WriteNextStateStop();
  57. TrySwitchState(); // always succeed since there is only one writer
  58. }
  59. }
  60. /*
  61. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  62. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  63. read to loop forever.
  64. */
  65. void JackFrameTimer::ReadFrameTime(JackTimer* timer)
  66. {
  67. UInt16 next_index = GetCurrentIndex();
  68. UInt16 cur_index;
  69. do {
  70. cur_index = next_index;
  71. memcpy(timer, ReadCurrentState(), sizeof(JackTimer));
  72. next_index = GetCurrentIndex();
  73. } while (cur_index != next_index); // Until a coherent state has been read
  74. }
  75. // Internal
  76. void JackFrameTimer::InitFrameTimeAux(jack_time_t callback_usecs, jack_time_t period_usecs)
  77. {
  78. JackTimer* timer = WriteNextStateStart();
  79. timer->fSecondOrderIntegrator = 0.0f;
  80. timer->fCurrentCallback = callback_usecs;
  81. timer->fNextWakeUp = callback_usecs + period_usecs;
  82. WriteNextStateStop();
  83. TrySwitchState(); // always succeed since there is only one writer
  84. }
  85. void JackFrameTimer::IncFrameTimeAux(jack_nframes_t nframes, jack_time_t callback_usecs, jack_time_t period_usecs)
  86. {
  87. JackTimer* timer = WriteNextStateStart();
  88. float delta = (int64_t)callback_usecs - (int64_t)timer->fNextWakeUp;
  89. timer->fCurrentWakeup = timer->fNextWakeUp;
  90. timer->fCurrentCallback = callback_usecs;
  91. timer->fFrames += nframes;
  92. timer->fSecondOrderIntegrator += 0.5f * timer->fFilterCoefficient * delta;
  93. timer->fNextWakeUp = timer->fCurrentWakeup + period_usecs + (int64_t) floorf((timer->fFilterCoefficient * (delta + timer->fSecondOrderIntegrator)));
  94. timer->fInitialized = true;
  95. WriteNextStateStop();
  96. TrySwitchState(); // always succeed since there is only one writer
  97. }
  98. } // end of namespace