Sound generator, a class playing simple notification sounds (1/2 frequency tones).

These tones are generated "in memory", so no sound files ("wav") are needed.

New lib blacksound for utils around audio
This commit is contained in:
Klaus Basan
2014-01-30 13:46:51 +01:00
committed by Mathew Sutcliffe
parent e877c5c368
commit f9225814f9
3 changed files with 178 additions and 76 deletions

View File

@@ -3,7 +3,6 @@
QT += network dbus gui multimedia QT += network dbus gui multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = blacksound TARGET = blacksound
TEMPLATE = lib TEMPLATE = lib
CONFIG += staticlib c++11 CONFIG += staticlib c++11

View File

@@ -3,54 +3,73 @@
#include <qmath.h> #include <qmath.h>
#include <qendian.h> #include <qendian.h>
#include <QAudioOutput> #include <QAudioOutput>
#include <QtConcurrent/QtConcurrent>
using namespace BlackMisc::Aviation; using namespace BlackMisc::Aviation;
using namespace BlackMisc::PhysicalQuantities; using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Voice;
namespace BlackSound namespace BlackSound
{ {
CSoundGenerator::CSoundGenerator(const QAudioFormat &format, const QList<Tone> &tones, bool singlePlay, QObject *parent) CSoundGenerator::CSoundGenerator(const QAudioDeviceInfo &device, const QAudioFormat &format, const QList<Tone> &tones, PlayMode mode, QObject *parent)
: QIODevice(parent), m_position(0), m_singlePlay(singlePlay), m_endReached(false), m_oneCycleDurationMs(calculateDurationMs(tones)) : QIODevice(parent),
m_tones(tones), m_position(0), m_playMode(mode), m_endReached(false), m_oneCycleDurationMs(calculateDurationMs(tones)),
m_device(device), m_audioFormat(format), m_audioOutput(new QAudioOutput(format))
{ {
Q_ASSERT(tones.size() > 0); Q_ASSERT(tones.size() > 0);
this->generateData(format, tones);
} }
CSoundGenerator::CSoundGenerator(const QList<Tone> &tones, bool singlePlay, QObject *parent) CSoundGenerator::CSoundGenerator(const QList<Tone> &tones, PlayMode mode, QObject *parent)
: QIODevice(parent), m_position(0), m_singlePlay(singlePlay), m_endReached(false), m_oneCycleDurationMs(calculateDurationMs(tones)) : QIODevice(parent),
m_tones(tones), m_position(0), m_playMode(mode), m_endReached(false), m_oneCycleDurationMs(calculateDurationMs(tones)),
m_device(QAudioDeviceInfo::defaultOutputDevice()), m_audioFormat(CSoundGenerator::defaultAudioFormat()),
m_audioOutput(new QAudioOutput(CSoundGenerator::defaultAudioFormat()))
{ {
Q_ASSERT(tones.size() > 0); Q_ASSERT(tones.size() > 0);
this->generateData(CSoundGenerator::defaultAudioFormat(), tones);
} }
CSoundGenerator::~CSoundGenerator() CSoundGenerator::~CSoundGenerator()
{ {
this->close(); this->stop(true);
} }
void CSoundGenerator::start() void CSoundGenerator::start(int volume)
{ {
if (volume < 1) return;
if (this->m_buffer.isEmpty()) this->generateData();
this->open(QIODevice::ReadOnly); this->open(QIODevice::ReadOnly);
this->m_audioOutput->setVolume(qreal(0.01 * volume));
this->m_audioOutput->start(this); // pull
} }
void CSoundGenerator::stop() void CSoundGenerator::stop(bool destructor)
{ {
this->close(); this->m_audioOutput->setVolume(0);
this->m_position = 0; this->m_audioOutput->stop();
if (this->isOpen())
{
// 1. isOpen avoids redundant signals
// 2. OK in destructor, see http://stackoverflow.com/a/14024955/356726
emit this->stopped(); emit this->stopped();
this->close(); // close IO Device
}
this->m_position = 0;
if (destructor) return;
// trigger own termination
if (this->m_playMode == SingleWithAutomaticDeletion) this->deleteLater();
} }
void CSoundGenerator::generateData(const QAudioFormat &format, const QList<Tone> &tones) void CSoundGenerator::generateData()
{ {
Q_ASSERT(tones.size() > 0); Q_ASSERT(this->m_tones.size() > 0);
const int bytesPerSample = this->m_audioFormat.sampleSize() / 8;
const int bytesPerSample = format.sampleSize() / 8; const int bytesForAllChannels = this->m_audioFormat.channelCount() * bytesPerSample;
const int bytesForAllChannels = format.channelCount() * bytesPerSample;
qint64 totalLength = 0; qint64 totalLength = 0;
foreach(Tone t, tones) foreach(Tone t, this->m_tones)
{ {
totalLength += format.sampleRate() * bytesForAllChannels * t.m_durationMs / 1000; totalLength += this->m_audioFormat.sampleRate() * bytesForAllChannels * t.m_durationMs / 1000;
} }
Q_ASSERT(totalLength % bytesForAllChannels == 0); Q_ASSERT(totalLength % bytesForAllChannels == 0);
@@ -59,16 +78,16 @@ namespace BlackSound
m_buffer.resize(totalLength); m_buffer.resize(totalLength);
unsigned char *bufferPointer = reinterpret_cast<unsigned char *>(m_buffer.data()); unsigned char *bufferPointer = reinterpret_cast<unsigned char *>(m_buffer.data());
foreach(Tone t, tones) foreach(Tone t, this->m_tones)
{ {
qint64 lengthPerTone = format.sampleRate() * bytesForAllChannels * t.m_durationMs / 1000; qint64 lengthPerTone = this->m_audioFormat.sampleRate() * bytesForAllChannels * t.m_durationMs / 1000;
int sampleIndexPerTone = 0; int sampleIndexPerTone = 0;
while (lengthPerTone) while (lengthPerTone)
{ {
// http://hyperphysics.phy-astr.gsu.edu/hbase/audio/sumdif.html // 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 // http://math.stackexchange.com/questions/164369/how-do-you-calculate-the-frequency-perceived-by-humans-of-two-sinusoidal-waves-a
const double pseudoTime = double(sampleIndexPerTone % format.sampleRate()) / format.sampleRate(); const double pseudoTime = double(sampleIndexPerTone % this->m_audioFormat.sampleRate()) / this->m_audioFormat.sampleRate();
double amplitude = 0; // silence double amplitude = 0; // silence
if (t.m_frequencyHz > 10) if (t.m_frequencyHz > 10)
{ {
@@ -81,30 +100,30 @@ namespace BlackSound
// the combination of two frequencies actually would have 2*amplitude, // the combination of two frequencies actually would have 2*amplitude,
// but I have to normalize with amplitude -1 -> +1 // but I have to normalize with amplitude -1 -> +1
for (int i = 0; i < format.channelCount(); ++i) for (int i = 0; i < this->m_audioFormat.channelCount(); ++i)
{ {
if (format.sampleSize() == 8 && format.sampleType() == QAudioFormat::UnSignedInt) if (this->m_audioFormat.sampleSize() == 8 && this->m_audioFormat.sampleType() == QAudioFormat::UnSignedInt)
{ {
const quint8 value = static_cast<quint8>((1.0 + amplitude) / 2 * 255); const quint8 value = static_cast<quint8>((1.0 + amplitude) / 2 * 255);
*reinterpret_cast<quint8 *>(bufferPointer) = value; *reinterpret_cast<quint8 *>(bufferPointer) = value;
} }
else if (format.sampleSize() == 8 && format.sampleType() == QAudioFormat::SignedInt) else if (this->m_audioFormat.sampleSize() == 8 && this->m_audioFormat.sampleType() == QAudioFormat::SignedInt)
{ {
const qint8 value = static_cast<qint8>(amplitude * 127); const qint8 value = static_cast<qint8>(amplitude * 127);
*reinterpret_cast<quint8 *>(bufferPointer) = value; *reinterpret_cast<quint8 *>(bufferPointer) = value;
} }
else if (format.sampleSize() == 16 && format.sampleType() == QAudioFormat::UnSignedInt) else if (this->m_audioFormat.sampleSize() == 16 && this->m_audioFormat.sampleType() == QAudioFormat::UnSignedInt)
{ {
quint16 value = static_cast<quint16>((1.0 + amplitude) / 2 * 65535); quint16 value = static_cast<quint16>((1.0 + amplitude) / 2 * 65535);
if (format.byteOrder() == QAudioFormat::LittleEndian) if (this->m_audioFormat.byteOrder() == QAudioFormat::LittleEndian)
qToLittleEndian<quint16>(value, bufferPointer); qToLittleEndian<quint16>(value, bufferPointer);
else else
qToBigEndian<quint16>(value, bufferPointer); qToBigEndian<quint16>(value, bufferPointer);
} }
else if (format.sampleSize() == 16 && format.sampleType() == QAudioFormat::SignedInt) else if (this->m_audioFormat.sampleSize() == 16 && this->m_audioFormat.sampleType() == QAudioFormat::SignedInt)
{ {
qint16 value = static_cast<qint16>(amplitude * 32767); qint16 value = static_cast<qint16>(amplitude * 32767);
if (format.byteOrder() == QAudioFormat::LittleEndian) if (this->m_audioFormat.byteOrder() == QAudioFormat::LittleEndian)
qToLittleEndian<qint16>(value, bufferPointer); qToLittleEndian<qint16>(value, bufferPointer);
else else
qToBigEndian<qint16>(value, bufferPointer); qToBigEndian<qint16>(value, bufferPointer);
@@ -131,19 +150,24 @@ namespace BlackSound
qint64 CSoundGenerator::readData(char *data, qint64 len) qint64 CSoundGenerator::readData(char *data, qint64 len)
{ {
if (this->m_endReached) return 0; if (len < 1) return 0;
if (this->m_endReached)
{
this->stop(); // all data read, we can stop output
return 0;
}
if (!this->isOpen()) return 0; if (!this->isOpen()) return 0;
qint64 total = 0; qint64 total = 0; // toal is used for the overflow when starting new wave again
while (len - total > 0) while (len - total > 0)
{ {
const qint64 chunk = qMin((m_buffer.size() - m_position), len - total); const qint64 chunkSize = qMin((m_buffer.size() - m_position), (len - total));
memcpy(data + total, m_buffer.constData() + m_position, chunk); memcpy(data + total, m_buffer.constData() + m_position, chunkSize);
this->m_position = (m_position + chunk) % m_buffer.size(); this->m_position = (m_position + chunkSize) % m_buffer.size();
total += chunk; total += chunkSize;
if (m_singlePlay && m_position == 0) if (m_position == 0 &&
(m_playMode == Single || m_playMode == SingleWithAutomaticDeletion))
{ {
this->m_endReached = true; this->m_endReached = true;
this->stop();
break; break;
} }
} }
@@ -175,23 +199,50 @@ namespace BlackSound
return format; return format;
} }
/*
* BlackMisc to Qt audio device
*/
QAudioDeviceInfo CSoundGenerator::findClosestOutputDevice(const BlackMisc::Voice::CAudioDevice &audioDevice)
{
Q_ASSERT(audioDevice.getType() == CAudioDevice::OutputDevice);
const QString lookFor = audioDevice.getName().toLower();
QAudioDeviceInfo qtDevice = QAudioDeviceInfo::defaultOutputDevice();
if (lookFor.startsWith("default")) return qtDevice;
int score = 0;
foreach(QAudioDeviceInfo qd, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
{
const QString cn = qd.deviceName().toLower();
if (lookFor == cn) return qd; // exact match
if (cn.length() < lookFor.length())
{
if (lookFor.contains(cn) && cn.length() > score)
{
qtDevice = qd;
score = cn.length();
}
}
else
{
if (cn.contains(lookFor) && lookFor.length() > score)
{
qtDevice = qd;
score = lookFor.length();
}
}
}
return qtDevice;
}
void CSoundGenerator::playSignal(qint32 volume, const QList<CSoundGenerator::Tone> &tones, QAudioDeviceInfo device) void CSoundGenerator::playSignal(qint32 volume, const QList<CSoundGenerator::Tone> &tones, QAudioDeviceInfo device)
{ {
if (tones.isEmpty()) return; // that was easy if (tones.isEmpty()) return; // that was easy
if (volume < 1) return; if (volume < 1) return;
qint64 timeOut = calculateDurationMs(tones); CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CSoundGenerator::SingleWithAutomaticDeletion);
if (timeOut < 10) return; // unable to hear if (generator->singleCyleDurationMs() < 10) return; // unable to hear
QAudioOutput *audioOutput = new QAudioOutput(device, CSoundGenerator::defaultAudioFormat());
CSoundGenerator *generator = new CSoundGenerator(tones, true, audioOutput);
// top and clean uo when done // top and clean uo when done
connect(generator, &CSoundGenerator::stopped, audioOutput, &QAudioOutput::stop); generator->start(volume);
connect(generator, &CSoundGenerator::stopped, audioOutput, &QAudioOutput::deleteLater);
double vol = volume / 100.0;
audioOutput->setVolume(vol);
generator->start();
audioOutput->start(generator);
} }
void CSoundGenerator::playSelcal(qint32 volume, const BlackMisc::Aviation::CSelcal &selcal, QAudioDeviceInfo device) void CSoundGenerator::playSelcal(qint32 volume, const BlackMisc::Aviation::CSelcal &selcal, QAudioDeviceInfo device)
@@ -208,4 +259,9 @@ namespace BlackSound
CSoundGenerator::playSignal(volume, tones, device); CSoundGenerator::playSignal(volume, tones, device);
} }
void CSoundGenerator::playSelcal(qint32 volume, const CSelcal &selcal, const CAudioDevice &audioDevice)
{
CSoundGenerator::playSelcal(volume, selcal, CSoundGenerator::findClosestOutputDevice(audioDevice));
}
} // namespace } // namespace

View File

@@ -7,26 +7,43 @@
#define BLACKSOUND_SOUNDGENERATOR_H #define BLACKSOUND_SOUNDGENERATOR_H
#include "blackmisc/avselcal.h" #include "blackmisc/avselcal.h"
#include "blackmisc/vaudiodevice.h"
#include <QIODevice> #include <QIODevice>
#include <QAudioFormat> #include <QAudioFormat>
#include <QAudioOutput>
#include <QAudioDeviceInfo> #include <QAudioDeviceInfo>
namespace BlackSound namespace BlackSound
{ {
/*!
* \brief Paying simple sounds
*/
class CSoundGenerator : public QIODevice class CSoundGenerator : public QIODevice
{ {
Q_OBJECT Q_OBJECT
public: public:
/*!
* \brief How to play
*/
enum PlayMode
{
Single,
SingleWithAutomaticDeletion,
EndlessLoop
};
/*! /*!
* \brief Tone to be played * \brief Tone to be played
*/ */
struct Tone struct Tone
{ {
int m_frequencyHz; int m_frequencyHz; /*!< first tone's frequency, use 0 for silence */
int m_secondaryFrequencyHz; int m_secondaryFrequencyHz; /*!< second tone's frequency, or 0 */
qint64 m_durationMs; qint64 m_durationMs; /*!< How long to play */
/*! /*!
* \brief Play frequency f for t milliseconds * \brief Play frequency f for t milliseconds
@@ -37,41 +54,42 @@ namespace BlackSound
* \brief Play 2 frequencies f for t milliseconds * \brief Play 2 frequencies f for t milliseconds
*/ */
Tone(int frequencyHz, int secondaryFrequencyHz, qint64 durationMs) : m_frequencyHz(frequencyHz), m_secondaryFrequencyHz(secondaryFrequencyHz), m_durationMs(durationMs) {} Tone(int frequencyHz, int secondaryFrequencyHz, qint64 durationMs) : m_frequencyHz(frequencyHz), m_secondaryFrequencyHz(secondaryFrequencyHz), m_durationMs(durationMs) {}
}; };
/*! /*!
* \brief Constructor * \brief Constructor
* \param format * \param device device
* \param format audio format
* \param tones list of Tones * \param tones list of Tones
* \param singlePlay play once? * \param mode play once?
* \param parent * \param parent
* \see PlayMode
*/ */
CSoundGenerator(const QAudioFormat &format, const QList<Tone> &tones, bool singlePlay, QObject *parent); CSoundGenerator(const QAudioDeviceInfo &device, const QAudioFormat &format, const QList<Tone> &tones, PlayMode mode, QObject *parent = nullptr);
/*! /*!
* \brief Constructor * \brief Constructor
* \param tones list of Tones * \param tones list of Tones
* \param singlePlay play once? * \param mode play once?
* \param parent * \param parent
* \see PlayMode
*/ */
CSoundGenerator(const QList<Tone> &tones, bool singlePlay, QObject *parent); CSoundGenerator(const QList<Tone> &tones, PlayMode mode, QObject *parent = nullptr);
/*! /*!
* Destructor * Destructor
*/ */
~CSoundGenerator(); ~CSoundGenerator();
/*!
* \brief Open device
*/
void start();
/*! /*!
* \brief Close device, buffer stays intact * \brief Close device, buffer stays intact
*/ */
void stop(); void stop(bool destructor = false);
/*!
* \brief sDuration of one cycle
*/
qint64 singleCyleDurationMs() const { return calculateDurationMs(this->m_tones); }
/*! /*!
* \copydoc QIODevice::readData() * \copydoc QIODevice::readData()
@@ -111,6 +129,13 @@ namespace BlackSound
*/ */
static QAudioFormat defaultAudioFormat(); static QAudioFormat defaultAudioFormat();
/*!
* \brief Find the closest Qt device to this audio device
* \param audioDevice output audio device
* \return
*/
static QAudioDeviceInfo findClosestOutputDevice(const BlackMisc::Voice::CAudioDevice &audioDevice);
/*! /*!
* \brief Play signal of tones once * \brief Play signal of tones once
* \param volume 0-100 * \param volume 0-100
@@ -128,18 +153,14 @@ namespace BlackSound
*/ */
static void playSelcal(qint32 volume, const BlackMisc::Aviation::CSelcal &selcal, QAudioDeviceInfo device = QAudioDeviceInfo::defaultOutputDevice()); static void playSelcal(qint32 volume, const BlackMisc::Aviation::CSelcal &selcal, QAudioDeviceInfo device = QAudioDeviceInfo::defaultOutputDevice());
signals:
/*! /*!
* \brief Device was closed * \brief Play SELCAL tone
* \remarks With singleShot the signal indicates that sound sequence has finished * \param volume 0-100
* \param selcal
* \param audioDevice device to be used
* \see BlackMisc::Aviation::CSelcal
*/ */
void stopped(); static void playSelcal(qint32 volume, const BlackMisc::Aviation::CSelcal &selcal, const BlackMisc::Voice::CAudioDevice &audioDevice);
private:
/*!
* \brief Generate tone data in internal buffer
*/
void generateData(const QAudioFormat &format, const QList<Tone> &tones);
/*! /*!
* \brief One cycle of tones takes t milliseconds * \brief One cycle of tones takes t milliseconds
@@ -149,12 +170,36 @@ namespace BlackSound
return this->m_oneCycleDurationMs; return this->m_oneCycleDurationMs;
} }
signals:
/*!
* \brief Device was closed
* \remarks With singleShot the signal indicates that sound sequence has finished
*/
void stopped();
public slots:
/*!
* \brief Play sound, open device
* \param volume 0..100
*/
void start(int volume);
private: private:
/*!
* \brief Generate tone data in internal buffer
*/
void generateData();
private:
QList<Tone> m_tones; /*! tones to be played */
qint64 m_position; /*!< position in buffer */ qint64 m_position; /*!< position in buffer */
bool m_singlePlay; /*!< end data provisioning after playing all tones */ bool m_playMode; /*!< end data provisioning after playing all tones, play endless loop */
bool m_endReached; /*!< indicates end in combination with single play */ bool m_endReached; /*!< indicates end in combination with single play */
qint64 m_oneCycleDurationMs; /*!< how long is one cycle of tones */ qint64 m_oneCycleDurationMs; /*!< how long is one cycle of tones */
QByteArray m_buffer; QByteArray m_buffer; /*!< generated buffer for data */
QAudioDeviceInfo m_device; /*!< audio device */
QAudioFormat m_audioFormat; /*!< used format */
QScopedPointer<QAudioOutput> m_audioOutput;
/*! /*!
* \brief Duration of these tones * \brief Duration of these tones
@@ -163,4 +208,6 @@ namespace BlackSound
}; };
} //namespace } //namespace
#endif // guard #endif // guard