forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeKeeper.cpp
109 lines (88 loc) · 2.1 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
#include "TimeKeeper.hpp"
#include "RandomNumberGenerators.hpp"
#include <algorithm>
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::UpdateTimers()
{
auto currentTime = std::chrono::high_resolution_clock::now();
m_currentTime = std::chrono::duration<double>(currentTime - m_startTime).count();
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 (m_currentTime - m_presetTimeA) / m_presetDurationA;
}
double TimeKeeper::PresetProgressB()
{
return (m_currentTime - m_presetTimeB) / m_presetDurationB;
}
int TimeKeeper::PresetFrameB()
{
return m_presetFrameB;
}
int TimeKeeper::PresetFrameA()
{
return m_presetFrameA;
}
int TimeKeeper::PresetTimeB()
{
return m_presetTimeB;
}
int TimeKeeper::PresetTimeA()
{
return m_presetTimeA;
}
double TimeKeeper::sampledPresetDuration()
{
return std::max<double>(1, std::min<double>(60, RandomNumberGenerators::gaussian
(m_presetDuration, m_easterEgg)));
}