00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "JackClientInterface.h"
00022 #include "JackEngineControl.h"
00023 #include "JackGraphManager.h"
00024 #include "JackClientControl.h"
00025 #include <algorithm>
00026 #include <math.h>
00027
00028 namespace Jack
00029 {
00030
00031 static inline _jack_time_t JACK_MAX(_jack_time_t a, _jack_time_t b)
00032 {
00033 return (a < b) ? b : a;
00034 }
00035
00036 void JackEngineControl::CycleIncTime(jack_time_t callback_usecs)
00037 {
00038
00039 fFrameTimer.IncFrameTime(fBufferSize, callback_usecs, fPeriodUsecs);
00040 }
00041
00042 void JackEngineControl::CycleBegin(JackClientInterface** table,
00043 JackGraphManager* manager,
00044 jack_time_t cur_cycle_begin,
00045 jack_time_t prev_cycle_end)
00046 {
00047 fTransport.CycleBegin(fSampleRate, cur_cycle_begin);
00048 CalcCPULoad(table, manager, cur_cycle_begin, prev_cycle_end);
00049 }
00050
00051 void JackEngineControl::CycleEnd(JackClientInterface** table)
00052 {
00053 fTransport.CycleEnd(table, fSampleRate, fBufferSize);
00054 }
00055
00056 void JackEngineControl::InitFrameTime()
00057 {
00058 fFrameTimer.InitFrameTime();
00059 }
00060
00061 void JackEngineControl::ResetFrameTime(jack_time_t cur_cycle_begin)
00062 {
00063 fFrameTimer.ResetFrameTime(fSampleRate, cur_cycle_begin, fPeriodUsecs);
00064 }
00065
00066 void JackEngineControl::ReadFrameTime(JackTimer* timer)
00067 {
00068 fFrameTimer.ReadFrameTime(timer);
00069 }
00070
00071 void JackEngineControl::CalcCPULoad(JackClientInterface** table,
00072 JackGraphManager* manager,
00073 jack_time_t cur_cycle_begin,
00074 jack_time_t prev_cycle_end)
00075 {
00076 fPrevCycleTime = fCurCycleTime;
00077 fCurCycleTime = cur_cycle_begin;
00078 jack_time_t last_cycle_end = prev_cycle_end;
00079
00080
00081 if (!fSyncMode) {
00082 for (int i = REAL_REFNUM; i < CLIENT_NUM; i++) {
00083 JackClientInterface* client = table[i];
00084 JackClientTiming* timing = manager->GetClientTiming(i);
00085 if (client && client->GetClientControl()->fActive && timing->fStatus == Finished)
00086 last_cycle_end = JACK_MAX(last_cycle_end, timing->fFinishedAt);
00087 }
00088 }
00089
00090
00091 fRollingClientUsecs[fRollingClientUsecsIndex++] = last_cycle_end - fPrevCycleTime;
00092 if (fRollingClientUsecsIndex >= JACK_ENGINE_ROLLING_COUNT)
00093 fRollingClientUsecsIndex = 0;
00094
00095
00096
00097
00098 if (++fRollingClientUsecsCnt % fRollingInterval == 0) {
00099
00100 jack_time_t max_usecs = 0;
00101 for (int i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++)
00102 max_usecs = JACK_MAX(fRollingClientUsecs[i], max_usecs);
00103
00104 fMaxUsecs = JACK_MAX(fMaxUsecs, max_usecs);
00105 fSpareUsecs = jack_time_t((max_usecs < fPeriodUsecs) ? fPeriodUsecs - max_usecs : 0);
00106 fCPULoad = ((1.f - (float(fSpareUsecs) / float(fPeriodUsecs))) * 50.f + (fCPULoad * 0.5f));
00107 }
00108 }
00109
00110 void JackEngineControl::ResetRollingUsecs()
00111 {
00112 memset(fRollingClientUsecs, 0, sizeof(fRollingClientUsecs));
00113 fRollingClientUsecsIndex = 0;
00114 fRollingClientUsecsCnt = 0;
00115 fSpareUsecs = 0;
00116 fRollingInterval = int(floor((JACK_ENGINE_ROLLING_INTERVAL * 1000.f) / fPeriodUsecs));
00117 }
00118
00119 void JackEngineControl::NotifyXRun(float delayed_usecs)
00120 {
00121 fXrunDelayedUsecs = delayed_usecs;
00122 if (delayed_usecs > fMaxDelayedUsecs)
00123 fMaxDelayedUsecs = delayed_usecs;
00124 }
00125
00126 void JackEngineControl::ResetXRun()
00127 {
00128 fMaxDelayedUsecs = 0.f;
00129 }
00130
00131 }