mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 04:25:35 +08:00
Refactor SECLAL player into new threaded player class
The reason for moving the implementation out from CSoundGenerator into its own class is, because CSoundGenerator was a very complex and obscure class. It mixed many tasks in one place. CSelcalPlayer is designed to play SELCALs only. The following design changes have been made, compared to CSoundGenerator: * Use pull mode instead of push mode. QBuffer is used as the QIODevice and is a wrapper around QByteArray. Therefore it is not necessary to implement our own QIODevice. * Internally it uses a CThreadedSelcalPlayer to relieve the load of the main thread. CThreadedSelcalPlayer inherits CContinuousWorker, no low level QThread implementation was necessary. * Push mode was not implemented. * It is important that the QAudioOutput is allocated in the worker thread. QAudioOutput allocates internal objects, which cannot be moved to the worker thread. * Data caching. The generated seclal audio data is cached. refs #736
This commit is contained in:
committed by
Mathew Sutcliffe
parent
1ff06a1174
commit
5486596335
46
src/blacksound/selcalplayer.cpp
Normal file
46
src/blacksound/selcalplayer.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "selcalplayer.h"
|
||||
#include <QTimer>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
using namespace BlackMisc::PhysicalQuantities;
|
||||
|
||||
namespace BlackSound
|
||||
{
|
||||
CSelcalPlayer::CSelcalPlayer(const QAudioDeviceInfo &device, QObject *parent)
|
||||
: QObject(parent),
|
||||
m_threadedPlayer(this, "CSelcalPlayer", device)
|
||||
{
|
||||
m_threadedPlayer.start();
|
||||
}
|
||||
|
||||
CSelcalPlayer::~CSelcalPlayer()
|
||||
{
|
||||
m_threadedPlayer.quitAndWait();
|
||||
}
|
||||
|
||||
void CSelcalPlayer::play(int volume, const BlackMisc::Aviation::CSelcal &selcal)
|
||||
{
|
||||
if (selcal.isValid())
|
||||
{
|
||||
QList<CFrequency> frequencies = selcal.getFrequencies();
|
||||
Q_ASSERT(frequencies.size() == 4);
|
||||
const BlackMisc::PhysicalQuantities::CTime oneSec(1000.0, BlackMisc::PhysicalQuantities::CTimeUnit::ms());
|
||||
CTonePair t1(frequencies.at(0), frequencies.at(1), oneSec);
|
||||
CTonePair t2({}, {}, oneSec / 5.0);
|
||||
CTonePair t3(frequencies.at(2), frequencies.at(3), oneSec);
|
||||
QList<CTonePair> tonePairs;
|
||||
tonePairs << t1 << t2 << t3;
|
||||
m_threadedPlayer.play(volume, tonePairs);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
src/blacksound/selcalplayer.h
Normal file
47
src/blacksound/selcalplayer.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKSOUND_SELCALPLAYER_H
|
||||
#define BLACKSOUND_SELCALPLAYER_H
|
||||
|
||||
#include "blacksoundexport.h"
|
||||
#include "blacksound/threadedtonepairplayer.h"
|
||||
#include "blacksound/tonepair.h"
|
||||
#include "blackmisc/aviation/selcal.h"
|
||||
#include "blackmisc/worker.h"
|
||||
|
||||
#include <QAudioDeviceInfo>
|
||||
|
||||
class QTimer;
|
||||
|
||||
namespace BlackSound
|
||||
{
|
||||
//! SELCAL player
|
||||
class BLACKSOUND_EXPORT CSelcalPlayer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CSelcalPlayer(const QAudioDeviceInfo &device = QAudioDeviceInfo::defaultOutputDevice(), QObject *parent = nullptr);
|
||||
|
||||
//! Destructor
|
||||
~CSelcalPlayer();
|
||||
|
||||
//! Play selcal
|
||||
void play(int volume, const BlackMisc::Aviation::CSelcal &selcal);
|
||||
|
||||
private:
|
||||
CThreadedTonePairPlayer m_threadedPlayer;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // guard
|
||||
170
src/blacksound/threadedtonepairplayer.cpp
Normal file
170
src/blacksound/threadedtonepairplayer.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "threadedtonepairplayer.h"
|
||||
#include <QTimer>
|
||||
|
||||
using namespace BlackMisc;
|
||||
|
||||
namespace BlackSound
|
||||
{
|
||||
|
||||
CThreadedTonePairPlayer::CThreadedTonePairPlayer(QObject *owner, const QString &name, const QAudioDeviceInfo &device)
|
||||
: CContinuousWorker(owner, name),
|
||||
m_deviceInfo(device)
|
||||
{ }
|
||||
|
||||
CThreadedTonePairPlayer::~CThreadedTonePairPlayer()
|
||||
{ }
|
||||
|
||||
void CThreadedTonePairPlayer::play(int volume, const QList<CTonePair> &tonePairs)
|
||||
{
|
||||
QMutexLocker ml(&m_mutex);
|
||||
if (m_audioOutput->state() != QAudio::StoppedState) { return; }
|
||||
|
||||
m_bufferData = getAudioByTonePairs(tonePairs);
|
||||
m_audioOutput->setVolume(static_cast<qreal>(0.01 * volume));
|
||||
QTimer::singleShot(0, this, &CThreadedTonePairPlayer::playBuffer);
|
||||
}
|
||||
|
||||
void CThreadedTonePairPlayer::initialize()
|
||||
{
|
||||
m_audioFormat.setSampleRate(44100);
|
||||
m_audioFormat.setChannelCount(1);
|
||||
m_audioFormat.setSampleSize(16); // 8 or 16 works
|
||||
m_audioFormat.setCodec("audio/pcm");
|
||||
m_audioFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
m_audioFormat.setSampleType(QAudioFormat::SignedInt);
|
||||
|
||||
m_audioOutput = new QAudioOutput(m_deviceInfo, m_audioFormat, this);
|
||||
connect(m_audioOutput, &QAudioOutput::stateChanged, this, &CThreadedTonePairPlayer::handleStateChanged);
|
||||
}
|
||||
|
||||
void CThreadedTonePairPlayer::handleStateChanged(QAudio::State newState)
|
||||
{
|
||||
QMutexLocker ml(&m_mutex);
|
||||
switch (newState)
|
||||
{
|
||||
case QAudio::IdleState: m_audioOutput->stop(); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void CThreadedTonePairPlayer::playBuffer()
|
||||
{
|
||||
QMutexLocker ml(&m_mutex);
|
||||
if (!m_audioOutput || m_audioOutput->state() == QAudio::ActiveState) { return; }
|
||||
m_buffer.close();
|
||||
m_buffer.setBuffer(&m_bufferData);
|
||||
m_buffer.open(QIODevice::ReadOnly);
|
||||
m_audioOutput->start(&m_buffer);
|
||||
}
|
||||
|
||||
QByteArray CThreadedTonePairPlayer::getAudioByTonePairs(const QList<CTonePair> &tonePairs)
|
||||
{
|
||||
Q_ASSERT(tonePairs.size() > 0);
|
||||
QByteArray finalBufferData;
|
||||
|
||||
for (const auto &tonePair : as_const(tonePairs))
|
||||
{
|
||||
if (m_tonePairCache.contains(tonePair))
|
||||
{
|
||||
QByteArray bufferData;
|
||||
bufferData = m_tonePairCache.value(tonePair);
|
||||
finalBufferData.append(bufferData);
|
||||
}
|
||||
else
|
||||
{
|
||||
QByteArray bufferData;
|
||||
bufferData = generateAudioFromTonePairs(tonePair);
|
||||
m_tonePairCache.insert(tonePair, bufferData);
|
||||
finalBufferData.append(bufferData);
|
||||
}
|
||||
}
|
||||
return finalBufferData;
|
||||
}
|
||||
|
||||
QByteArray CThreadedTonePairPlayer::generateAudioFromTonePairs(const CTonePair &tonePair)
|
||||
{
|
||||
const int bytesPerSample = m_audioFormat.sampleSize() / 8;
|
||||
const int bytesForAllChannels = m_audioFormat.channelCount() * bytesPerSample;
|
||||
|
||||
QByteArray bufferData;
|
||||
qint64 bytesPerTonePair = m_audioFormat.sampleRate() * bytesForAllChannels * tonePair.getDurationMs() / 1000;
|
||||
bufferData.resize(bytesPerTonePair);
|
||||
unsigned char *bufferPointer = reinterpret_cast<unsigned char *>(bufferData.data());
|
||||
|
||||
qint64 last0AmplitudeSample = bytesPerTonePair; // last sample when amplitude was 0
|
||||
int sampleIndexPerTonePair = 0;
|
||||
while (bytesPerTonePair)
|
||||
{
|
||||
// http://hyperphysics.phy-astr.gsu.edu/hbase/audio/sumdif.html
|
||||
// http://math.stackexchange.com/questions/164369/how-do-you-calculate-the-frequency-perceived-by-humans-of-two-sinusoidal-waves-a
|
||||
const double pseudoTime = static_cast<double>(sampleIndexPerTonePair % this->m_audioFormat.sampleRate()) / this->m_audioFormat.sampleRate();
|
||||
double amplitude = 0.0; // amplitude -1 -> +1 , 0 is silence
|
||||
if (tonePair.getFirstFrequencyHz() > 10)
|
||||
{
|
||||
// the combination of two frequencies actually would have 2*amplitude,
|
||||
// but I have to normalize with amplitude -1 -> +1
|
||||
amplitude = tonePair.getSecondFrequencyHz() == 0 ?
|
||||
qSin(2 * M_PI * tonePair.getFirstFrequencyHz() * pseudoTime) :
|
||||
qSin(M_PI * (tonePair.getFirstFrequencyHz() + tonePair.getSecondFrequencyHz()) * pseudoTime) *
|
||||
qCos(M_PI * (tonePair.getFirstFrequencyHz() - tonePair.getSecondFrequencyHz()) * pseudoTime);
|
||||
}
|
||||
|
||||
// avoid overflow
|
||||
Q_ASSERT(amplitude <= 1.0 && amplitude >= -1.0);
|
||||
if (amplitude < -1.0) { amplitude = -1.0; }
|
||||
else if (amplitude > 1.0) { amplitude = 1.0; }
|
||||
else if (qAbs(amplitude) < 1.0 / 65535)
|
||||
{
|
||||
amplitude = 0;
|
||||
last0AmplitudeSample = bytesPerTonePair;
|
||||
}
|
||||
|
||||
// generate this for all channels, usually 1 channel
|
||||
for (int i = 0; i < this->m_audioFormat.channelCount(); ++i)
|
||||
{
|
||||
this->writeAmplitudeToBuffer(amplitude, bufferPointer);
|
||||
bufferPointer += bytesPerSample;
|
||||
bytesPerTonePair -= bytesPerSample;
|
||||
}
|
||||
++sampleIndexPerTonePair;
|
||||
}
|
||||
|
||||
// fixes the range from the last 0 pass through
|
||||
if (last0AmplitudeSample > 0)
|
||||
{
|
||||
bufferPointer -= last0AmplitudeSample;
|
||||
while (last0AmplitudeSample)
|
||||
{
|
||||
double amplitude = 0.0; // amplitude -1 -> +1 , 0 is silence
|
||||
|
||||
// generate this for all channels, usually 1 channel
|
||||
for (int i = 0; i < this->m_audioFormat.channelCount(); ++i)
|
||||
{
|
||||
this->writeAmplitudeToBuffer(amplitude, bufferPointer);
|
||||
bufferPointer += bytesPerSample;
|
||||
last0AmplitudeSample -= bytesPerSample;
|
||||
}
|
||||
}
|
||||
}
|
||||
return bufferData;
|
||||
}
|
||||
|
||||
void CThreadedTonePairPlayer::writeAmplitudeToBuffer(double amplitude, unsigned char *bufferPointer)
|
||||
{
|
||||
Q_ASSERT(this->m_audioFormat.sampleSize() == 16);
|
||||
Q_ASSERT(this->m_audioFormat.sampleType() == QAudioFormat::SignedInt);
|
||||
Q_ASSERT(this->m_audioFormat.byteOrder() == QAudioFormat::LittleEndian);
|
||||
|
||||
qint16 value = static_cast<qint16>(amplitude * 32767);
|
||||
qToLittleEndian<qint16>(value, bufferPointer);
|
||||
}
|
||||
}
|
||||
78
src/blacksound/threadedtonepairplayer.h
Normal file
78
src/blacksound/threadedtonepairplayer.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKSOUND_THREADEDTONEPAIRPLAYER_H
|
||||
#define BLACKSOUND_THREADEDTONEPAIRPLAYER_H
|
||||
|
||||
#include "blacksoundexport.h"
|
||||
#include "blacksound/tonepair.h"
|
||||
#include "blackmisc/worker.h"
|
||||
|
||||
#include <QAudioDeviceInfo>
|
||||
#include <QAudioOutput>
|
||||
#include <QBuffer>
|
||||
#include <QMap>
|
||||
#include <QReadWriteLock>
|
||||
#include <QtEndian>
|
||||
#include <QtGlobal>
|
||||
|
||||
class QTimer;
|
||||
|
||||
namespace BlackSound
|
||||
{
|
||||
//! Threaded tone player. Don't use it directly but use \sa CSelcalPlayer instead.
|
||||
class BLACKSOUND_EXPORT CThreadedTonePairPlayer : public BlackMisc::CContinuousWorker
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
CThreadedTonePairPlayer(QObject *owner, const QString &name, const QAudioDeviceInfo &device = QAudioDeviceInfo::defaultOutputDevice());
|
||||
|
||||
//! Destructor
|
||||
~CThreadedTonePairPlayer();
|
||||
|
||||
public slots:
|
||||
//! Play the list of tones.
|
||||
//! If the player is currently active, this call will be ignored.
|
||||
void play(int volume, const QList<CTonePair> &tonePairs);
|
||||
|
||||
protected slots:
|
||||
//! \copydoc BlackMisc::CContinuousWorker::initialize
|
||||
virtual void initialize() override;
|
||||
|
||||
//! \copydoc BlackMisc::CContinuousWorker::cleanup
|
||||
virtual void cleanup() override {}
|
||||
|
||||
private:
|
||||
void handleStateChanged(QAudio::State newState);
|
||||
void playBuffer();
|
||||
QByteArray getAudioByTonePairs(const QList<CTonePair> &tonePairs);
|
||||
QByteArray generateAudioFromTonePairs(const CTonePair &tonePair);
|
||||
|
||||
//! Write audio amplitude to data buffer
|
||||
//! This method assumes that
|
||||
//! \li sampleSize == 16
|
||||
//! \li byte order == little endian
|
||||
//! \li sample type == signed int
|
||||
void writeAmplitudeToBuffer(double amplitude, unsigned char *bufferPointer);
|
||||
|
||||
QAudioDeviceInfo m_deviceInfo;
|
||||
QAudioOutput *m_audioOutput = nullptr;
|
||||
QByteArray m_bufferData;
|
||||
QBuffer m_buffer;
|
||||
QMutex m_mutex { QMutex::Recursive };
|
||||
QAudioFormat m_audioFormat;
|
||||
QMap<CTonePair, QByteArray> m_tonePairCache;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // guard
|
||||
22
src/blacksound/tonepair.cpp
Normal file
22
src/blacksound/tonepair.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "tonepair.h"
|
||||
|
||||
namespace BlackSound
|
||||
{
|
||||
CTonePair::CTonePair(const BlackMisc::PhysicalQuantities::CFrequency &frequency,
|
||||
const BlackMisc::PhysicalQuantities::CFrequency &secondaryFrequency,
|
||||
const BlackMisc::PhysicalQuantities::CTime &duration) :
|
||||
m_firstFrequencyHz(static_cast<int>(frequency.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::Hz()))),
|
||||
m_secondFrequencyHz(static_cast<int>(secondaryFrequency.valueRounded(BlackMisc::PhysicalQuantities::CFrequencyUnit::Hz()))),
|
||||
m_durationMs(static_cast<qint64>(duration.valueRounded(BlackMisc::PhysicalQuantities::CTimeUnit::ms())))
|
||||
{ }
|
||||
|
||||
}
|
||||
55
src/blacksound/tonepair.h
Normal file
55
src/blacksound/tonepair.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKSOUND_TONEPAIR_H
|
||||
#define BLACKSOUND_TONEPAIR_H
|
||||
|
||||
#include "blacksoundexport.h"
|
||||
#include "blackmisc/pq/frequency.h"
|
||||
#include "blackmisc/pq/time.h"
|
||||
|
||||
#include <tuple>
|
||||
|
||||
namespace BlackSound
|
||||
{
|
||||
//! Tone pair to be played
|
||||
class BLACKSOUND_EXPORT CTonePair
|
||||
{
|
||||
public:
|
||||
//! Play two tones with frequencies f for t milliseconds
|
||||
CTonePair(const BlackMisc::PhysicalQuantities::CFrequency &frequency,
|
||||
const BlackMisc::PhysicalQuantities::CFrequency &secondaryFrequency,
|
||||
const BlackMisc::PhysicalQuantities::CTime &duration);
|
||||
|
||||
//! Get frequency of the first tone
|
||||
int getFirstFrequencyHz() const { return m_firstFrequencyHz; }
|
||||
|
||||
//! Get frequency of the second tone
|
||||
int getSecondFrequencyHz() const { return m_secondFrequencyHz; }
|
||||
|
||||
//! Get play duration
|
||||
qint64 getDurationMs() const { return m_durationMs; }
|
||||
|
||||
//! Comparison operator
|
||||
friend bool operator <(const CTonePair& lhs, const CTonePair& rhs)
|
||||
{
|
||||
return std::tie(lhs.m_firstFrequencyHz, lhs.m_secondFrequencyHz, lhs.m_durationMs)
|
||||
< std::tie(rhs.m_firstFrequencyHz, rhs.m_secondFrequencyHz, rhs.m_durationMs);
|
||||
}
|
||||
|
||||
private:
|
||||
int m_firstFrequencyHz; //!< first tone's frequency, use 0 for silence
|
||||
int m_secondFrequencyHz; //!< second tone's frequency, or 0
|
||||
qint64 m_durationMs; //!< How long to play (duration)
|
||||
};
|
||||
}
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user