From 77a8c46790a0d903a8110ac8b255a72cb7d5b0de Mon Sep 17 00:00:00 2001 From: Roland Rossgotterer Date: Fri, 1 Mar 2019 17:09:00 +0100 Subject: [PATCH] Use CWorker to play sound in background --- src/blacksound/soundgenerator.cpp | 52 +++++++------------------------ src/blacksound/soundgenerator.h | 14 +-------- 2 files changed, 12 insertions(+), 54 deletions(-) diff --git a/src/blacksound/soundgenerator.cpp b/src/blacksound/soundgenerator.cpp index bb889c0b1..a3282f09b 100644 --- a/src/blacksound/soundgenerator.cpp +++ b/src/blacksound/soundgenerator.cpp @@ -9,6 +9,7 @@ #include "blacksound/soundgenerator.h" #include "blackmisc/directoryutils.h" #include "blackmisc/filedeleter.h" +#include "blackmisc/worker.h" #include #include #include @@ -82,25 +83,6 @@ namespace BlackSound } } - void CSoundGenerator::startInOwnThread(int volume) - { - m_ownThread = new QThread(); // deleted by signals, hence no parent - this->moveToThread(m_ownThread); - // connect(this, &CSoundGenerator::startThread, this, &CSoundGenerator::start); - - connect(m_ownThread, &QThread::started, this, [ = ]() { this->start(volume, false); }); - connect(this, &CSoundGenerator::stopping, m_ownThread, &QThread::quit); - - // in auto delete mode force deleteLater when thread is finished - if (m_playMode == CNotificationSounds::SingleWithAutomaticDeletion) - { - connect(m_ownThread, &QThread::finished, this, &CSoundGenerator::deleteLater); - } - - // start thread and begin processing by calling start via signal startThread - m_ownThread->start(); - } - void CSoundGenerator::stop(bool destructor) { // m_audioOutput->setVolume(0); // Bug or feature, killing the applicaions volume? @@ -431,34 +413,22 @@ namespace BlackSound return generator; } - CSoundGenerator *CSoundGenerator::playSignalInBackground(int volume, const QList &tones, const QAudioDeviceInfo &device) + void CSoundGenerator::playSignalInBackground(int volume, const QList &tones, const QAudioDeviceInfo &device) { CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CNotificationSounds::SingleWithAutomaticDeletion); - if (tones.isEmpty()) { return generator; } // that was easy - if (volume < 1) { return generator; } - if (generator->singleCyleDurationMs() < 10) { return generator; } // unable to hear - - // play, and maybe clean up when done - generator->startInOwnThread(volume); - return generator; - } - - void CSoundGenerator::playSignalRecorded(int volume, const QList &tones, const QAudioDeviceInfo &device) - { if (tones.isEmpty()) { return; } // that was easy if (volume < 1) { return; } + if (generator->singleCyleDurationMs() < 10) { return; } // unable to hear - CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CNotificationSounds::SingleWithAutomaticDeletion); - if (generator->singleCyleDurationMs() > 10) + CWorker *worker = CWorker::fromTask(QCoreApplication::instance(), "CSoundGenerator::playSignalInBackground", [generator, volume]() { - // play, and maybe clean up when done - QString fileName = QString("blacksound").append(QString::number(QDateTime::currentMSecsSinceEpoch())).append(".wav"); - fileName = QDir::temp().filePath(fileName); - generator->generateData(); - generator->saveToWavFile(fileName); - CSoundGenerator::playFile(volume, fileName, true); - } - generator->deleteLater(); + generator->start(volume, false); + + }); + worker->then([generator]() + { + generator->deleteLater(); + }); } void CSoundGenerator::playSelcal(int volume, const CSelcal &selcal, const QAudioDeviceInfo &device) diff --git a/src/blacksound/soundgenerator.h b/src/blacksound/soundgenerator.h index 86281fedf..d6b0f5043 100644 --- a/src/blacksound/soundgenerator.h +++ b/src/blacksound/soundgenerator.h @@ -130,14 +130,7 @@ namespace BlackSound //! \param volume 0-100 //! \param tones list of tones //! \param device device to be used - //! \return generator used, important with SingleWithAutomaticDeletion automatically deleted - static CSoundGenerator *playSignalInBackground(int volume, const QList &tones, const QAudioDeviceInfo &device); - - //! Record the tones to a wav file, then play the wav file - //! \param volume 0-100 - //! \param tones list of tones - //! \param device device to be used - static void playSignalRecorded(int volume, const QList &tones, const QAudioDeviceInfo &device); + static void playSignalInBackground(int volume, const QList &tones, const QAudioDeviceInfo &device); //! Play SELCAL tone //! \param volume 0-100 @@ -187,11 +180,6 @@ namespace BlackSound //! \param pull pull/push, if false push mode void start(int volume, bool pull = true); - //! Play sound in own thread, open device - //! \remarks always push mode - //! \param volume 0..100 - void startInOwnThread(int volume); - private slots: //! Push mode, timer expired void pushTimerExpired();