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.

179 lines
5.3KB

  1. /*
  2. Copyright (C) 2003 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 "JackEngineControl.h"
  20. #include "JackGraphManager.h"
  21. #include "JackClientControl.h"
  22. #include <algorithm>
  23. #include <math.h>
  24. namespace Jack
  25. {
  26. void JackEngineControl::CycleIncTime(jack_time_t callback_usecs)
  27. {
  28. // Timer
  29. fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
  30. }
  31. void JackEngineControl::CycleBegin(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  32. {
  33. // Transport
  34. fTransport.CycleBegin(fSampleRate, callback_usecs);
  35. // Timing
  36. GetTimeMeasure(table, manager, callback_usecs);
  37. CalcCPULoad(table, manager);
  38. }
  39. void JackEngineControl::CycleEnd(JackClientInterface** table)
  40. {
  41. fTransport.CycleEnd(table, fSampleRate, fBufferSize);
  42. }
  43. void JackEngineControl::InitFrameTime()
  44. {
  45. fFrameTimer.InitFrameTime();
  46. }
  47. void JackEngineControl::ResetFrameTime(jack_time_t callback_usecs)
  48. {
  49. fFrameTimer.ResetFrameTime(fSampleRate, callback_usecs, fPeriodUsecs);
  50. }
  51. void JackEngineControl::ReadFrameTime(JackTimer* timer)
  52. {
  53. fFrameTimer.ReadFrameTime(timer);
  54. }
  55. // Private
  56. #ifdef WIN32
  57. inline jack_time_t MAX(jack_time_t a, jack_time_t b)
  58. {
  59. return (a < b) ? b : a;
  60. }
  61. #else
  62. #define MAX(a,b) std::max((a),(b))
  63. #endif
  64. void JackEngineControl::CalcCPULoad(JackClientInterface** table, JackGraphManager* manager)
  65. {
  66. jack_time_t lastCycleEnd = fLastProcessTime;
  67. for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
  68. JackClientInterface* client = table[i];
  69. JackClientTiming* timing = manager->GetClientTiming(i);
  70. if (client && client->GetClientControl()->fActive && timing->fStatus == Finished) {
  71. lastCycleEnd = MAX(lastCycleEnd, timing->fFinishedAt);
  72. }
  73. }
  74. /* store the execution time for later averaging */
  75. fRollingClientUsecs[fRollingClientUsecsIndex++] = lastCycleEnd - fLastTime;
  76. if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT) {
  77. fRollingClientUsecsIndex = 0;
  78. }
  79. /* every so often, recompute the current maximum use over the
  80. last JACK_ENGINE_ROLLING_COUNT client iterations.
  81. */
  82. if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
  83. jack_time_t maxUsecs = 0;
  84. for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  85. maxUsecs = MAX(fRollingClientUsecs[i], maxUsecs);
  86. }
  87. fMaxUsecs = MAX(fMaxUsecs, maxUsecs);
  88. fSpareUsecs = jack_time_t((maxUsecs < fPeriodUsecs) ? fPeriodUsecs - maxUsecs : 0);
  89. fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
  90. }
  91. }
  92. void JackEngineControl::ResetRollingUsecs()
  93. {
  94. memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
  95. fRollingClientUsecsIndex = 0;
  96. fRollingClientUsecsCnt = 0;
  97. fSpareUsecs = 0;
  98. fRollingInterval = (int)floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs);
  99. }
  100. void JackEngineControl::GetTimeMeasure(JackClientInterface** table, JackGraphManager* manager, jack_time_t callback_usecs)
  101. {
  102. int pos = (++fAudioCycle) % TIME_POINTS;
  103. fLastTime = fCurTime;
  104. fCurTime = callback_usecs;
  105. fLastProcessTime = fProcessTime;
  106. fProcessTime = GetMicroSeconds();
  107. if (fLastTime > 0) {
  108. fMeasure[pos].fEngineTime = fLastTime;
  109. fMeasure[pos].fAudioCycle = fAudioCycle;
  110. for (int i = 0; i < CLIENT_NUM; i++) {
  111. JackClientInterface* client = table[i];
  112. JackClientTiming* timing = manager->GetClientTiming(i);
  113. if (client && client->GetClientControl()->fActive) {
  114. fMeasure[pos].fClientTable[i].fRefNum = i;
  115. fMeasure[pos].fClientTable[i].fSignaledAt = timing->fSignaledAt;
  116. fMeasure[pos].fClientTable[i].fAwakeAt = timing->fAwakeAt;
  117. fMeasure[pos].fClientTable[i].fFinishedAt = timing->fFinishedAt;
  118. fMeasure[pos].fClientTable[i].fStatus = timing->fStatus;
  119. }
  120. }
  121. }
  122. }
  123. void JackEngineControl::ClearTimeMeasures()
  124. {
  125. for (int i = 0; i < TIME_POINTS; i++) {
  126. for (int j = 0; j < CLIENT_NUM; j++) {
  127. fMeasure[i].fClientTable[j].fRefNum = 0;
  128. fMeasure[i].fClientTable[j].fSignaledAt = 0;
  129. fMeasure[i].fClientTable[j].fAwakeAt = 0;
  130. fMeasure[i].fClientTable[j].fFinishedAt = 0;
  131. }
  132. }
  133. fLastTime = fCurTime = 0;
  134. }
  135. void JackEngineControl::NotifyXRun(float delayed_usecs)
  136. {
  137. fXrunDelayedUsecs = delayed_usecs;
  138. if (delayed_usecs > fMaxDelayedUsecs)
  139. fMaxDelayedUsecs = delayed_usecs;
  140. }
  141. void JackEngineControl::ResetXRun()
  142. {
  143. fMaxDelayedUsecs = 0.f;
  144. }
  145. } // end of namespace