-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundselectiondialog.cpp
335 lines (294 loc) · 10.6 KB
/
soundselectiondialog.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "soundselectiondialog.h"
#include "ui_soundselectiondialog.h"
#include <QFileDialog>
#include <QAudioDecoder>
#include <QTimer>
#include <QStyle>
#include "settings.h"
#include "settingsdialog.h"
#include <iostream>
#include "utils.h"
#include "multiregionwaveform.h"
#include "variableselectionwaveform.h"
SoundSelectionDialog::SoundSelectionDialog(Mode mode, QWidget *parent) :
QDialog(parent),
ui(new Ui::SoundSelectionDialog),
_fileDialog (nullptr),
_mode (mode),
_decoder(nullptr),
_musicSet (false),
_loading (false)
{
ui->setupUi(this);
if (_mode == Mode::BACKGROUND_MUSIC) {
_waveform = new MultiRegionWaveform (this);
} else {
_waveform = new VariableSelectionWaveform (this);
}
ui->upperHLayout->insertWidget(0, _waveform);
ui->dummyWaveform->hide(); // This is a placeholder for UI development
ui->playPauseButton->setText("");
ui->rewindButton->setText("");
ui->playPauseButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
ui->rewindButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaSkipBackward));
connect (this, &SoundSelectionDialog::finished, this, &SoundSelectionDialog::dialogClosed);
_player = new QMediaPlayer (this);
_player->setNotifyInterval(75);
connect(_player, &QMediaPlayer::positionChanged, this, &SoundSelectionDialog::playerPositionChanged);
connect(_player, &QMediaPlayer::stateChanged, this, &SoundSelectionDialog::playerStateChanged);
connect(_waveform, &Waveform::playheadManuallyChanged, this, &SoundSelectionDialog::setPlayhead);
_decoder = new QAudioDecoder(this);
connect (_decoder, &QAudioDecoder::bufferReady, this, &SoundSelectionDialog::readBuffer);
connect (_decoder, &QAudioDecoder::finished, this, &SoundSelectionDialog::readFinished);
QString startingDirectory = "";
switch (_mode) {
case Mode::BACKGROUND_MUSIC:
startingDirectory = "Music";
break;
case Mode::SOUND_EFFECT:
startingDirectory = "Sound Effects";
break;
}
_fileDialog = new QFileDialog (this, "Choose a sound file", startingDirectory, "Sound files (*.mp3 *.wav);;All files (*.*)");
_fileDialog->setFileMode(QFileDialog::ExistingFile);
_fileDialog->setNameFilter(tr("Sound files (*.mp3 *.wav);;All files (*.*)"));
_fileDialog->setOption (QFileDialog::DontUseNativeDialog, true);
_fileDialog->hide();
connect (_fileDialog, &QFileDialog::accepted, this, &SoundSelectionDialog::fileDialogAccepted);
connect (_fileDialog, &QFileDialog::rejected, this, &SoundSelectionDialog::fileDialogRejected);
}
SoundSelectionDialog::~SoundSelectionDialog()
{
delete _decoder;
delete _player;
delete _waveform;
delete ui;
}
void SoundSelectionDialog::on_chooseMusicFileButton_clicked()
{
// Stop any playing that is happening now...
ui->playPauseButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
_player->pause();
_fileDialog->show();
}
void SoundSelectionDialog::fileDialogAccepted ()
{
QStringList selectedFiles = _fileDialog->selectedFiles();
for (auto &&filename: selectedFiles) {
QFile file (filename);
qDebug() << "Loading a file: " << filename;
if (file.exists()) {
loadFile (filename);
return;
} else {
qDebug() << "Something is wrong with " << filename;
}
}
}
void SoundSelectionDialog::fileDialogRejected ()
{
if (!_musicSet) {
close();
}
}
void SoundSelectionDialog::loadFile (const QString &filename)
{
ui->playPauseButton->setDisabled(true);
ui->removeMusicButton->setDisabled(true);
ui->rewindButton->setDisabled(true);
ui->buttonBox->setDisabled(true);
ui->chooseMusicFileButton->setDisabled(true);
ui->resetSelectionButton->setDisabled(true);
_filename = filename;
QAudioFormat desiredFormat;
desiredFormat.setChannelCount(2);
desiredFormat.setCodec("audio/x-raw");
desiredFormat.setSampleType(QAudioFormat::UnSignedInt);
desiredFormat.setSampleRate(48000);
desiredFormat.setSampleSize(16);
_decoder->setAudioFormat(desiredFormat);
_decoder->setSourceFilename(filename);
_waveform->reset();
_loading = true;
_decoder->start();
}
void SoundSelectionDialog::readBuffer ()
{
if (!_loading) {
qDebug() << "What am I doing here?";
}
QAudioBuffer buffer = _decoder->read();
_waveform->setDuration(_decoder->duration());
_waveform->addBuffer(buffer);
}
void SoundSelectionDialog::readFinished ()
{
_loading = false;
Settings settings;
_waveform->setDuration(_decoder->duration());
if (_sfx) {
_waveform->setSelectionStart (int(_sfx.getInPoint()*1000));
_waveform->setSelectionLength (int((_sfx.getOutPoint())-_sfx.getInPoint()*1000));
double logVolume = QAudio::convertVolume(_sfx.getVolume()/qreal(100.0),
QAudio::LinearVolumeScale,
QAudio::LogarithmicVolumeScale);
ui->volumeSlider->setValue (qRound(logVolume * 100));
} else {
_waveform->setSelectionStart(0);
_waveform->setSelectionLength(0);
ui->volumeSlider->setValue (50);
}
if (_mode==Mode::BACKGROUND_MUSIC) {
double preTitleDuration = settings.Get("settings/preTitleScreenDuration").toDouble();
double titleDuration = settings.Get("settings/titleScreenDuration").toDouble();
double creditsDuration = settings.Get("settings/creditsDuration").toDouble();
double movieDuration = _movieDuration;
MultiRegionWaveform *w = dynamic_cast<MultiRegionWaveform *> (_waveform);
w->ClearRegions();
qint64 offset = 0;
w->AddRegion("Intro", offset, int(offset+preTitleDuration*1000), Qt::black);
offset += preTitleDuration*1000;
w->AddRegion("Title", offset, int(offset+titleDuration*1000), Qt::black);
offset += titleDuration*1000;
w->AddRegion("Movie", offset, int(offset+movieDuration*1000), Qt::blue);
offset += movieDuration*1000;
w->AddRegion("Credits", offset, int(offset+creditsDuration*1000), Qt::black);
w->DoneAddingRegions();
}
_musicSet = true;
_waveform->bufferComplete();
_player->setMedia(QUrl::fromLocalFile(_filename));
ui->playPauseButton->setDisabled(false);
ui->removeMusicButton->setDisabled(false);
ui->rewindButton->setDisabled(false);
ui->buttonBox->setDisabled(false);
ui->chooseMusicFileButton->setDisabled(false);
ui->resetSelectionButton->setDisabled(false);
}
void SoundSelectionDialog::setMovieDuration (double duration)
{
_movieDuration = duration;
if (_musicSet && _mode==Mode::BACKGROUND_MUSIC) {
// Trigger a re-creation of the regions
readFinished ();
}
}
void SoundSelectionDialog::on_playPauseButton_clicked()
{
if (_player->state() == QMediaPlayer::StoppedState ||
_player->state() == QMediaPlayer::PausedState) {
_player->setPosition(_waveform->getPlayheadPosition());
on_volumeSlider_valueChanged (ui->volumeSlider->value());
_player->play();
} else {
_player->pause();
}
}
void SoundSelectionDialog::playerPositionChanged (qint64 newPosition)
{
if (newPosition != 0 &&
newPosition == _player->duration()) {
qDebug() << "Looping...";
_player->setPosition(0);
_player->play();
}
_waveform->setPlayheadPosition(newPosition);
}
void SoundSelectionDialog::playerStateChanged (QMediaPlayer::State state)
{
switch (state) {
case QMediaPlayer::StoppedState:
ui->playPauseButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
break;
case QMediaPlayer::PausedState:
ui->playPauseButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
break;
case QMediaPlayer::PlayingState:
ui->playPauseButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPause));
break;
}
}
void SoundSelectionDialog::setPlayhead (qint64 newPosition)
{
if (_player->state() == QMediaPlayer::PlayingState) {
_player->setPosition(newPosition);
}
}
void SoundSelectionDialog::showEvent(QShowEvent *)
{
if (!_musicSet) {
on_chooseMusicFileButton_clicked();
}
//stressTest();
}
void SoundSelectionDialog::dialogClosed(int)
{
_player->pause();
}
void SoundSelectionDialog::on_removeMusicButton_clicked()
{
ui->playPauseButton->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
_player->pause();
ui->removeMusicButton->setDisabled(true);
ui->playPauseButton->setDisabled(true);
ui->rewindButton->setDisabled(true);
_waveform->reset();
_musicSet = false;
_sfx = SoundEffect();
}
void SoundSelectionDialog::on_rewindButton_clicked()
{
_player->setPosition(0);
_waveform->setPlayheadPosition(0);
}
SoundEffect SoundSelectionDialog::getSelectedSound () const
{
if (_musicSet) {
double linearVolume = QAudio::convertVolume(ui->volumeSlider->value() / qreal(100.0),
QAudio::LogarithmicVolumeScale,
QAudio::LinearVolumeScale);
double in = double(_waveform->getSelectionStart()) / 1000.0;
double out = in + double(_waveform->getSelectionLength()) / 1000.0;
return SoundEffect (_filename, 0, in, out, qRound(linearVolume * 100));
} else {
return SoundEffect ();
}
}
void SoundSelectionDialog::setSound (const SoundEffect &sfx)
{
_sfx = sfx;
if (sfx) {
_sfx = sfx;
loadFile (sfx.getFilename());
_musicSet = true; // Make sure to set this here to prevent the File dialog from opening
} else {
_musicSet = false;
}
}
void SoundSelectionDialog::on_volumeSlider_valueChanged(int value)
{
qreal linearVolume = QAudio::convertVolume(value / qreal(100.0),
QAudio::LogarithmicVolumeScale,
QAudio::LinearVolumeScale);
_player->setVolume (qRound(linearVolume * 100));
if (_sfx) {
_sfx.setVolume(qRound(linearVolume * 100));
}
}
void SoundSelectionDialog::on_resetSelectionButton_clicked()
{
// Trigger a recreation of the boxes
readFinished();
}
void SoundSelectionDialog::stressTest ()
{
stressTestLoad();
}
void SoundSelectionDialog::stressTestLoad ()
{
if (!_loading) {
QString sfxToLoad ("C:\\Users\\chennes\\OneDrive - Pioneer Library System\\Software Development\\Sound Effects\\Laser_Gun.mp3");
loadFile(sfxToLoad);
}
QTimer::singleShot(1000, this, &SoundSelectionDialog::stressTestLoad);
}