-
-
Notifications
You must be signed in to change notification settings - Fork 383
/
Copy pathTimeKeeper.cpp
133 lines (108 loc) · 2.66 KB
/
TimeKeeper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "TimeKeeper.hpp"
#include <algorithm>
#include <random>
namespace libprojectM {
TimeKeeper::TimeKeeper(double presetDuration, double smoothDuration, double hardcutDuration, double easterEgg)
: m_easterEgg(easterEgg)
, m_presetDuration(presetDuration)
, m_softCutDuration(smoothDuration)
, m_hardCutDuration(hardcutDuration)
{
UpdateTimers();
}
void TimeKeeper::SetFrameTime(double secondsSinceStart)
{
m_userSpecifiedTime = secondsSinceStart;
}
double TimeKeeper::GetFrameTime() const
{
return m_currentTime;
}
void TimeKeeper::UpdateTimers()
{
double currentFrameTime{m_userSpecifiedTime};
if (m_userSpecifiedTime < 0.0)
{
auto currentTime = std::chrono::high_resolution_clock::now();
currentFrameTime = std::chrono::duration<double>(currentTime - m_startTime).count();
}
m_secondsSinceLastFrame = currentFrameTime - m_currentTime;
m_currentTime = currentFrameTime;
m_presetFrameA++;
m_presetFrameB++;
}
void TimeKeeper::StartPreset()
{
m_isSmoothing = false;
m_presetTimeA = m_currentTime;
m_presetFrameA = 1;
m_presetDurationA = sampledPresetDuration();
}
void TimeKeeper::StartSmoothing()
{
m_isSmoothing = true;
m_presetTimeB = m_currentTime;
m_presetFrameB = 1;
m_presetDurationB = sampledPresetDuration();
}
void TimeKeeper::EndSmoothing()
{
m_isSmoothing = false;
m_presetTimeA = m_presetTimeB;
m_presetFrameA = m_presetFrameB;
m_presetDurationA = m_presetDurationB;
}
bool TimeKeeper::CanHardCut()
{
return (m_currentTime - m_presetTimeA) > m_hardCutDuration;
}
double TimeKeeper::SmoothRatio()
{
return (m_currentTime - m_presetTimeB) / m_softCutDuration;
}
bool TimeKeeper::IsSmoothing()
{
return m_isSmoothing;
}
double TimeKeeper::GetRunningTime()
{
return m_currentTime;
}
double TimeKeeper::PresetProgressA()
{
if (m_isSmoothing)
{
return 1.0;
}
return std::min((m_currentTime - m_presetTimeA) / m_presetDurationA, 1.0);
}
double TimeKeeper::PresetProgressB()
{
return std::min((m_currentTime - m_presetTimeB) / m_presetDurationB, 1.0);
}
int TimeKeeper::PresetFrameB()
{
return m_presetFrameB;
}
int TimeKeeper::PresetFrameA()
{
return m_presetFrameA;
}
double TimeKeeper::PresetTimeB()
{
return m_presetTimeB;
}
double TimeKeeper::PresetTimeA()
{
return m_presetTimeA;
}
double TimeKeeper::sampledPresetDuration()
{
if (m_easterEgg < 0.001)
{
return m_presetDuration;
}
std::normal_distribution<double> gaussianDistribution(m_presetDuration, m_easterEgg);
return std::max<double>(1.0, gaussianDistribution(m_randomGenerator));
}
} // namespace libprojectM