mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-17 19:05:31 +08:00
Use CWorker to play sound in background
This commit is contained in:
committed by
Mat Sutcliffe
parent
6dfd0aff12
commit
77a8c46790
@@ -9,6 +9,7 @@
|
|||||||
#include "blacksound/soundgenerator.h"
|
#include "blacksound/soundgenerator.h"
|
||||||
#include "blackmisc/directoryutils.h"
|
#include "blackmisc/directoryutils.h"
|
||||||
#include "blackmisc/filedeleter.h"
|
#include "blackmisc/filedeleter.h"
|
||||||
|
#include "blackmisc/worker.h"
|
||||||
#include <QtCore/qendian.h>
|
#include <QtCore/qendian.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <qmath.h>
|
#include <qmath.h>
|
||||||
@@ -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)
|
void CSoundGenerator::stop(bool destructor)
|
||||||
{
|
{
|
||||||
// m_audioOutput->setVolume(0); // Bug or feature, killing the applicaions volume?
|
// m_audioOutput->setVolume(0); // Bug or feature, killing the applicaions volume?
|
||||||
@@ -431,34 +413,22 @@ namespace BlackSound
|
|||||||
return generator;
|
return generator;
|
||||||
}
|
}
|
||||||
|
|
||||||
CSoundGenerator *CSoundGenerator::playSignalInBackground(int volume, const QList<CSoundGenerator::Tone> &tones, const QAudioDeviceInfo &device)
|
void CSoundGenerator::playSignalInBackground(int volume, const QList<CSoundGenerator::Tone> &tones, const QAudioDeviceInfo &device)
|
||||||
{
|
{
|
||||||
CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CNotificationSounds::SingleWithAutomaticDeletion);
|
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<CSoundGenerator::Tone> &tones, const QAudioDeviceInfo &device)
|
|
||||||
{
|
|
||||||
if (tones.isEmpty()) { return; } // that was easy
|
if (tones.isEmpty()) { return; } // that was easy
|
||||||
if (volume < 1) { return; }
|
if (volume < 1) { return; }
|
||||||
|
if (generator->singleCyleDurationMs() < 10) { return; } // unable to hear
|
||||||
|
|
||||||
CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CNotificationSounds::SingleWithAutomaticDeletion);
|
CWorker *worker = CWorker::fromTask(QCoreApplication::instance(), "CSoundGenerator::playSignalInBackground", [generator, volume]()
|
||||||
if (generator->singleCyleDurationMs() > 10)
|
{
|
||||||
|
generator->start(volume, false);
|
||||||
|
|
||||||
|
});
|
||||||
|
worker->then([generator]()
|
||||||
{
|
{
|
||||||
// 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->deleteLater();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSoundGenerator::playSelcal(int volume, const CSelcal &selcal, const QAudioDeviceInfo &device)
|
void CSoundGenerator::playSelcal(int volume, const CSelcal &selcal, const QAudioDeviceInfo &device)
|
||||||
|
|||||||
@@ -130,14 +130,7 @@ namespace BlackSound
|
|||||||
//! \param volume 0-100
|
//! \param volume 0-100
|
||||||
//! \param tones list of tones
|
//! \param tones list of tones
|
||||||
//! \param device device to be used
|
//! \param device device to be used
|
||||||
//! \return generator used, important with SingleWithAutomaticDeletion automatically deleted
|
static void playSignalInBackground(int volume, const QList<CSoundGenerator::Tone> &tones, const QAudioDeviceInfo &device);
|
||||||
static CSoundGenerator *playSignalInBackground(int volume, const QList<CSoundGenerator::Tone> &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<CSoundGenerator::Tone> &tones, const QAudioDeviceInfo &device);
|
|
||||||
|
|
||||||
//! Play SELCAL tone
|
//! Play SELCAL tone
|
||||||
//! \param volume 0-100
|
//! \param volume 0-100
|
||||||
@@ -187,11 +180,6 @@ namespace BlackSound
|
|||||||
//! \param pull pull/push, if false push mode
|
//! \param pull pull/push, if false push mode
|
||||||
void start(int volume, bool pull = true);
|
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:
|
private slots:
|
||||||
//! Push mode, timer expired
|
//! Push mode, timer expired
|
||||||
void pushTimerExpired();
|
void pushTimerExpired();
|
||||||
|
|||||||
Reference in New Issue
Block a user