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-03 18:33:17 +02:00
|
|
|
ScStwSoundPlayer::PlayResult ScStwSoundPlayer::play(ScStwSoundPlayer::StartSound sound, double volume, double *timeOfStart) {
|
|
|
|
if(!this->soundFiles.contains(sound)) {
|
|
|
|
qDebug() << "[ERROR][SoundPlayer] Sound file was not found for sound" << sound;
|
|
|
|
return ScStwSoundPlayer::Error;
|
|
|
|
}
|
2020-04-13 23:48:29 +02:00
|
|
|
|
|
|
|
// 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-10-07 09:48:15 +02:00
|
|
|
#ifdef RASPI
|
2021-04-09 12:53:13 +02:00
|
|
|
// set volume on raspi using amixer
|
|
|
|
QProcess soundProcess;
|
|
|
|
|
|
|
|
// determine current audio output device
|
|
|
|
soundProcess.start("amixer", {"scontrols"});
|
|
|
|
soundProcess.waitForFinished();
|
|
|
|
QStringList outputDeviceNameList = QString(soundProcess.readAllStandardOutput()).split("'");
|
|
|
|
|
|
|
|
if(outputDeviceNameList.length() == 3) {
|
|
|
|
QString outputDeviceName = outputDeviceNameList[1];
|
|
|
|
soundProcess.execute("amixer", {"sset", outputDeviceName, QString::number(volume * 100, 'f', 0) + "%"});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << "[WARNING][SoundPlayer] Could not determine output device to set volume";
|
2021-05-02 15:29:08 +02:00
|
|
|
return Error;
|
2021-04-09 12:53:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->soundEffect->setVolume(1);
|
2020-10-07 09:48:15 +02:00
|
|
|
#else
|
2020-06-18 11:53:25 +02:00
|
|
|
this->soundEffect->setVolume(volume);
|
2020-10-07 09:48:15 +02:00
|
|
|
#endif
|
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) {
|
2020-10-03 18:33:17 +02:00
|
|
|
qDebug() << "[DEBUG][Sound] Sound is not ready yet, status is: " << this->soundEffect->status();
|
2020-06-18 11:53:25 +02:00
|
|
|
QObject::connect(this->soundEffect, &QSoundEffect::statusChanged, &loop, &QEventLoop::quit);
|
|
|
|
loop.exec();
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 15:11:07 +02:00
|
|
|
qDebug() << "[DEBUG][Sound] Playing sound now: " << sound;
|
|
|
|
|
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();
|
|
|
|
|
2020-10-03 18:33:17 +02:00
|
|
|
return ScStwSoundPlayer::Success;
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
bool ScStwSoundPlayer::cancel() {
|
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-10-03 18:33:17 +02:00
|
|
|
this->waitLoop->exit(-1);
|
2020-10-03 11:10:15 +02:00
|
|
|
|
|
|
|
return true;
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 18:33:17 +02:00
|
|
|
ScStwSoundPlayer::PlayResult ScStwSoundPlayer::waitForSoundFinish(double *timeOfStop) {
|
2020-06-18 11:53:25 +02:00
|
|
|
if(!this->soundEffect->isPlaying())
|
2020-10-03 18:33:17 +02:00
|
|
|
return ScStwSoundPlayer::Error;
|
2020-04-13 23:48:29 +02:00
|
|
|
|
|
|
|
// wait until the audio output reports the sound is over
|
2020-10-03 18:33:17 +02:00
|
|
|
if(waitLoop->exec() == -1)
|
|
|
|
return ScStwSoundPlayer::Cancelled;
|
2020-04-13 23:48:29 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-10-03 18:33:17 +02:00
|
|
|
return ScStwSoundPlayer::Success;
|
2020-04-13 23:48:29 +02:00
|
|
|
}
|
2020-10-03 13:49:56 +02:00
|
|
|
|
|
|
|
bool ScStwSoundPlayer::isPlaying() {
|
|
|
|
return this->soundEffect->isPlaying();
|
|
|
|
}
|