-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
236 lines (194 loc) · 5.28 KB
/
player.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (C) 2023 Michel de Boer
// License: GPLv3
#include "player.h"
#include <QTime>
#include <chrono>
using namespace std::chrono_literals;
namespace SpiralFun {
Player::Player(const CircleList &circles, std::unique_ptr<MusicGenerator> musicGenerator) :
mCircles(circles),
mMusicGenerator(std::move(musicGenerator))
{
mPlayTimer.setInterval(mMusicGenerator ? mMusicGenerator->getTonePlayInterval() : 0);
QObject::connect(&mPlayTimer, &QTimer::timeout, this, &Player::advance);
mSceneRefreshTimer.setInterval(40ms);
QObject::connect(&mSceneRefreshTimer, &QTimer::timeout, this, [this]{ emit refreshScene(); });
}
Player::~Player()
{
stopTimers();
}
bool Player::play(std::unique_ptr<Recorder> recorder)
{
for (auto& circle : mCircles)
circle->preparePlay();
mStepsPerInterval = 1;
mStartTime = QTime::currentTime().msecsSinceStartOfDay();
mCycles = 0;
mRecorder = std::move(recorder);
startTimers();
if (mRecorder)
{
if (!setupRecording())
return false;
}
return true;
}
void Player::playAll()
{
for (auto& circle : mCircles)
circle->preparePlay();
mStepsPerInterval = 100;
mStartTime = QTime::currentTime().msecsSinceStartOfDay();
mCycles = 0;
mPlayTimer.start();
}
void Player::startTimers()
{
mPlayTimer.start();
mSceneRefreshTimer.start();
}
void Player::stopTimers()
{
mPlayTimer.stop();
mSceneRefreshTimer.stop();
}
void Player::advance()
{
++mCycles;
for (unsigned step = 0.0; step < mStepsPerInterval; ++step)
{
advanceCircles(mStepAngle);
mAngle += mStepAngle;
emit angleChanged();
if (mAngle >= M_PI * 2)
{
finishPlaying();
break;
}
if (mRecording)
{
if (!record())
recordingFailed();
}
}
if (mMusicGenerator)
mMusicGenerator->playNotes();
}
void Player::recordingFailed()
{
stopTimers();
Stats stats;
stats.mRecordingFailed = true;
emit done(stats);
mRecorder = nullptr;
}
void Player::finishPlaying()
{
stopTimers();
const qreal t = (QTime::currentTime().msecsSinceStartOfDay() - mStartTime) / 1000.0;
const qreal avgCycleT = t / mCycles;
qInfo() << "Play duration:" << t << "secs" << "cycles:" << mCycles <<
"avg-cycle-time:" << avgCycleT * 1000 << "ms";
Stats stats;
stats.mCycles = mCycles;
stats.mPlayTime = std::lround(t * 1000) * 1ms;
// Draw the last line to close the curve. It may not have been drawn yet
// due to the minimum draw length.
forceDraw();
emit refreshScene();
if (mRecording)
{
// Record last frame
updateRecordingRect();
const bool frameAdded = mRecorder->addFrame(mRecordingRect, [this, stats](bool frameAdded){
if (!frameAdded)
qWarning() << "Adding last frame failed";
mRecorder->stopRecording(true);
emit done(stats);
});
if (!frameAdded)
{
qWarning() << "Failed to add last frame";
mRecorder->stopRecording(true);
emit done(stats);
}
}
else
{
emit done(stats);
}
}
void Player::advanceCircles(qreal angle)
{
for(unsigned i = 1; i < mCircles.size(); ++i)
advanceCircle(i, angle);
}
void Player::advanceCircle(unsigned index, qreal angle)
{
if (index < 1 || index >= mCircles.size())
throw std::out_of_range(std::string("index=") + std::to_string(index));
const QPointF& rotationCenter = mCircles[index - 1]->getCenter();
const int speed = mCircles[index]->getSpeed();
const bool clockwise = speed > 0;
for (unsigned i = index; i < mCircles.size(); ++i)
{
auto& circle = *mCircles[i];
for (int n = 0; n < std::abs(speed); ++n)
circle.rotate(rotationCenter, angle, clockwise);
}
}
void Player::forceDraw()
{
for (auto& circle : mCircles)
{
if (circle->getDraw())
circle->forceDrawToCenter();
}
}
bool Player::setupRecording()
{
Q_ASSERT(mRecorder);
if (!mRecorder->startRecording(Recorder::FPS_25))
return false;
mFullFrameRect = mRecorder->getFullFrameRect();
// Capture one full frame, next frames will be subframes where changes happened.
mRecordingRect = mFullFrameRect;
mRecordAngle = mRecordAngleThreshold;
mRecording = true;
return record();
}
void Player::resetRecordingRect()
{
mRecordingRect = mRecorder->calcBoundingRectangle(mCircles) & mFullFrameRect;
}
void Player::updateRecordingRect()
{
mRecordingRect |= mRecorder->calcBoundingRectangle(mCircles) & mFullFrameRect;
}
bool Player::record()
{
updateRecordingRect();
mRecordAngle += mStepAngle;
if (mRecordAngle < mRecordAngleThreshold)
return true;
mRecordAngle = 0.0;
const bool frameAdded = mRecorder->addFrame(mRecordingRect, [this](bool frameAdded){
if (!frameAdded)
{
qWarning() << "Adding last frame failed";
recordingFailed();
return;
}
mPlayTimer.start();
});
if (!frameAdded)
{
qDebug() << "Could not add frame";
return false;
}
resetRecordingRect();
mPlayTimer.stop();
return true;
}
}