-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundeffect.cpp
233 lines (192 loc) · 4.76 KB
/
soundeffect.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
#include "soundeffect.h"
#include <QTimer>
#include <QJsonObject>
#include "settings.h"
SoundEffect::SoundEffect(const QString &filename):
_filename (filename),
_startFrame(0),
_in(0.0),
_out(0.0),
_volume(100.0),
_playbackEnabled (false),
_isPlaying (false)
{
}
SoundEffect::SoundEffect(const QString &filename, int startFrame, double in, double out, double volume):
_filename (filename),
_startFrame(startFrame),
_in(in),
_out(out),
_volume(volume),
_playbackEnabled (false),
_isPlaying (false)
{
}
SoundEffect::SoundEffect(const SoundEffect &sfx) :
_filename(sfx._filename),
_startFrame(sfx._startFrame),
_in(sfx._in),
_out(sfx._out),
_volume(sfx._volume),
_playbackEnabled(false),
_isPlaying(false)
{
}
SoundEffect& SoundEffect::operator= (const SoundEffect& rhs)
{
if (this != &rhs) {
_filename = rhs._filename;
_startFrame = rhs._startFrame;
_in = rhs._in;
_out = rhs._out;
_volume = rhs._volume;
_isPlaying = false;
_playbackEnabled = false;
}
return *this;
}
bool SoundEffect::operator== (const SoundEffect& rhs) const
{
// Here we treat the sound effect as identical if it starts at the same frame and has the
// same filename. The other quantities are floating point numbers and should not be used
// in comparisons here due to rounding in the user interface.
return (_filename == rhs._filename &&
_startFrame == rhs._startFrame);
}
bool SoundEffect::operator< (const SoundEffect& rhs) const
{
if (_startFrame == rhs._startFrame) {
return _filename < rhs._filename;
} else {
return _startFrame < rhs._startFrame;
}
}
SoundEffect::operator bool() const
{
return (_filename.length()>0);
}
void SoundEffect::setStartTime (double t)
{
Settings settings;
int fps = settings.Get ("settings/framesPerSecond").toInt();
_startFrame = int(round(t * fps));
}
void SoundEffect::setStartFrame(int f)
{
_startFrame = f;
}
void SoundEffect::setInPoint (double t)
{
_in = t;
}
void SoundEffect::setOutPoint (double t)
{
_out = t;
}
void SoundEffect::setVolume (double v)
{
_volume = v;
if (_playbackEnabled) {
_playback->setVolume(static_cast<int>(_volume));
}
}
QString SoundEffect::getFilename () const
{
return _filename;
}
double SoundEffect::getStartTime () const
{
Settings settings;
int fps = settings.Get ("settings/framesPerSecond").toInt();
return static_cast<double>(_startFrame) / static_cast<double>(fps);
}
int SoundEffect::getStartFrame() const
{
return _startFrame;
}
double SoundEffect::getInPoint () const
{
return _in;
}
double SoundEffect::getOutPoint () const
{
return _out;
}
double SoundEffect::getVolume () const
{
return _volume;
}
void SoundEffect::mediaStatusChanged (QMediaPlayer::MediaStatus s)
{
qDebug() << "Playback status changed to " << s;
switch (s) {
case QMediaPlayer::LoadedMedia:
if (_out == 0.0) {
_out = _playback->duration() / 1000;
}
break;
default:
break;
}
}
void SoundEffect::enablePlayback()
{
if (!_playbackEnabled) {
_playback = new QMediaPlayer;
connect (_playback, &QMediaPlayer::mediaStatusChanged,
this, &SoundEffect::mediaStatusChanged);
if (_filename.length() > 0) {
_playback->setMedia (QUrl::fromLocalFile(_filename));
}
}
_playbackEnabled = true;
}
void SoundEffect::play () const
{
playFrom(0.0);
}
void SoundEffect::playFrom (double t) const
{
if (!_playbackEnabled) {
qDebug() << "Playback was disabled!";
return;
}
if (_out > _in || _out == 0.0) {
_isPlaying = true;
_playback->setPosition(static_cast<int>(1000 * (t+_in)));
_playback->setVolume(static_cast<int>(_volume));
_playback->play();
if (_out != 0.0) {
QTimer::singleShot (int(1000*(_out-_in)), this, SLOT(stop()));
}
}
}
void SoundEffect::stop () const
{
if (_playbackEnabled && _isPlaying) {
_isPlaying = false;
_playback->stop();
}
}
void SoundEffect::load (const QJsonObject &json)
{
_filename = json["filename"].toString();
_startFrame = json["startFrame"].toInt();
_in = json["in"].toDouble();
_out = json["out"].toDouble();
_volume = json["volume"].toDouble();
if (_volume == 0.0) {
_volume = 100.0;
}
if (_filename.length() > 0 && _playbackEnabled) {
_playback->setMedia (QUrl::fromLocalFile(_filename));
}
}
void SoundEffect::save (QJsonObject &json) const
{
json["filename"] = _filename;
json["startFrame"] = _startFrame;
json["in"] = _in;
json["out"] = _out;
json["volume"] = _volume;
}