2020-06-07 14:43:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
** ScStw Libraries
|
|
|
|
** Copyright (C) 2020 Itsblue development
|
|
|
|
**
|
|
|
|
** This program is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-13 23:48:29 +02:00
|
|
|
#include "../headers/scstwsoundplayer.h"
|
|
|
|
|
|
|
|
ScStwSoundPlayer::ScStwSoundPlayer(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
this->waitLoop = new QEventLoop(this);
|
|
|
|
this->waitTimer = new QTimer(this);
|
|
|
|
|
2020-10-02 15:20:11 +02:00
|
|
|
this->soundFiles.insert(AtYourMarks, {{"path","qrc:/sound/AtYourMarksSound.wav"}, {"duration", 1000}});
|
|
|
|
this->soundFiles.insert(Ready, {{"path","qrc:/sound/ReadySound.wav"}, {"duration", 570}});
|
|
|
|
this->soundFiles.insert(Start, {{"path","qrc:/sound/StartsignalSoundExtended.wav"}, {"duration", 3200}});
|
|
|
|
this->soundFiles.insert(FalseStart, {{"path","qrc:/sound/FalseStartSound.wav"}, {"duration", 2000}});
|
2020-04-13 23:48:29 +02:00
|
|
|
|
2020-06-18 11:53:25 +02:00
|
|
|
this->soundEffect = new QSoundEffect(this);
|
|
|
|
this->soundEffect->setLoopCount(1);
|
2020-04-13 23:48:29 +02:00
|
|
|
|
|
|
|
connect(this, &ScStwSoundPlayer::playbackStarted, this->waitLoop, &QEventLoop::quit);
|
2020-06-18 11:53:25 +02:00
|
|
|
connect(this->soundEffect, &QSoundEffect::playingChanged, this->waitLoop, &QEventLoop::quit);
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:20:11 +02:00
|
|
|
bool ScStwSoundPlayer::play(ScStwSoundPlayer::StartSound sound, double volume, double *timeOfStart) {
|
|
|
|
if(!this->soundFiles.contains(sound))
|
2020-04-13 23:48:29 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// stop playback
|
2020-06-18 11:53:25 +02:00
|
|
|
if(this->soundEffect->isPlaying())
|
|
|
|
this->soundEffect->stop();
|
2020-04-13 23:48:29 +02:00
|
|
|
|
|
|
|
// update currently playing action
|
2020-10-02 15:20:11 +02:00
|
|
|
this->currentlyPlayingSound = sound;
|
2020-04-13 23:48:29 +02:00
|
|
|
|
2020-06-18 11:53:25 +02:00
|
|
|
// update volume
|
|
|
|
this->soundEffect->setVolume(volume);
|
2020-04-13 23:48:29 +02:00
|
|
|
|
2020-06-18 11:53:25 +02:00
|
|
|
// load
|
2020-10-02 15:20:11 +02:00
|
|
|
this->soundEffect->setSource(this->soundFiles[sound]["path"].toString());
|
2020-04-13 23:48:29 +02:00
|
|
|
|
2020-06-18 11:53:25 +02:00
|
|
|
// wait for the effect to load
|
|
|
|
QEventLoop loop;
|
|
|
|
while(this->soundEffect->status() != QSoundEffect::Ready) {
|
|
|
|
QObject::connect(this->soundEffect, &QSoundEffect::statusChanged, &loop, &QEventLoop::quit);
|
|
|
|
loop.exec();
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// start
|
2020-06-18 11:53:25 +02:00
|
|
|
this->soundEffect->play();
|
|
|
|
|
|
|
|
// emit a playback start
|
|
|
|
emit this->playbackStarted();
|
|
|
|
|
|
|
|
// save started at
|
|
|
|
this->playingStartedAt = QDateTime::currentMSecsSinceEpoch();
|
2020-04-13 23:48:29 +02:00
|
|
|
|
|
|
|
// pass the time of start if requested
|
|
|
|
if(timeOfStart != nullptr) {
|
2020-06-18 11:53:25 +02:00
|
|
|
*timeOfStart = this->playingStartedAt;
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 17:05:52 +02:00
|
|
|
if(sound < Start)
|
2020-04-13 23:48:29 +02:00
|
|
|
return this->waitForSoundFinish();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwSoundPlayer::cancel(double volume) {
|
2020-06-18 11:53:25 +02:00
|
|
|
if(!this->soundEffect->isPlaying() )
|
2020-04-13 23:48:29 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// stop playback
|
2020-06-18 11:53:25 +02:00
|
|
|
this->soundEffect->stop();
|
2020-04-15 19:21:32 +02:00
|
|
|
this->waitLoop->quit();
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwSoundPlayer::waitForSoundFinish(double *timeOfStop) {
|
2020-06-18 11:53:25 +02:00
|
|
|
if(!this->soundEffect->isPlaying())
|
2020-04-13 23:48:29 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// wait until the audio output reports the sound is over
|
|
|
|
waitLoop->exec();
|
|
|
|
|
|
|
|
// wait until the sound is actually over
|
|
|
|
// the timeOffset is the buffer time before the audio started!
|
2020-10-02 15:20:11 +02:00
|
|
|
int timeOffset = this->soundFiles[this->currentlyPlayingSound]["duration"].toDouble() - (QDateTime::currentMSecsSinceEpoch() - playingStartedAt);
|
2020-04-13 23:48:29 +02:00
|
|
|
|
|
|
|
if(timeOffset > 0) {
|
|
|
|
QTimer timer;
|
|
|
|
timer.singleShot(timeOffset, this->waitLoop, &QEventLoop::quit);
|
|
|
|
this->waitLoop->exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the point in time where the sound playback actually ended
|
|
|
|
if(timeOfStop != nullptr) {
|
2020-10-02 15:20:11 +02:00
|
|
|
int latency = this->playingStartedAt + this->soundFiles[this->currentlyPlayingSound]["duration"].toDouble();
|
2020-04-13 23:48:29 +02:00
|
|
|
*timeOfStop = QDateTime::currentMSecsSinceEpoch() - latency;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|