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:
Roland Winklmeier
2016-08-15 13:16:34 +02:00
committed by Mathew Sutcliffe
parent 1ff06a1174
commit 5486596335
8 changed files with 423 additions and 2 deletions

View 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