From 4c952ab782da22049f208a8d8f1cbaaaf147b840 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 19 May 2014 18:18:56 +0200 Subject: [PATCH] refs #240, moved notification sounds in project blackmisc Notification sounds are used in BlackGui components. They require the whole Qt multimedia library. However, GUI only triggers playing the sound. As a workaround the notification sounds enumeration has been moved in an own class, and moved to the subproject BlackMisc. Hence it is possible to compile BlackGui without dependency to BlackSound. --- samples/blackgui/mainwindow.cpp | 6 ++--- samples/blackgui/mainwindow_voice.cpp | 4 ++-- src/blackcore/context_audio_impl.cpp | 3 ++- src/blackmisc/notificationsounds.h | 32 +++++++++++++++++++++++++++ src/blacksound/soundgenerator.cpp | 20 ++++++++--------- src/blacksound/soundgenerator.h | 30 +++++-------------------- 6 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 src/blackmisc/notificationsounds.h diff --git a/samples/blackgui/mainwindow.cpp b/samples/blackgui/mainwindow.cpp index aa13d55f2..4b3663719 100644 --- a/samples/blackgui/mainwindow.cpp +++ b/samples/blackgui/mainwindow.cpp @@ -310,13 +310,13 @@ void MainWindow::connectionStatusChanged(uint /** from **/, uint to, const QStri switch (newStatus) { case INetwork::Connected: - this->playNotifcationSound(BlackSound::CSoundGenerator::NotificationLogin); + this->playNotifcationSound(BlackSound::CNotificationSounds::NotificationLogin); break; case INetwork::Disconnected: - this->playNotifcationSound(BlackSound::CSoundGenerator::NotificationLogoff); + this->playNotifcationSound(BlackSound::CNotificationSounds::NotificationLogoff); break; case INetwork::DisconnectedError: - this->playNotifcationSound(BlackSound::CSoundGenerator::NotificationError); + this->playNotifcationSound(BlackSound::CNotificationSounds::NotificationError); break; default: break; diff --git a/samples/blackgui/mainwindow_voice.cpp b/samples/blackgui/mainwindow_voice.cpp index 5fadf68f5..44bdd89fd 100644 --- a/samples/blackgui/mainwindow_voice.cpp +++ b/samples/blackgui/mainwindow_voice.cpp @@ -234,10 +234,10 @@ void MainWindow::audioTestUpdate() /* * Notification */ -void MainWindow::playNotifcationSound(CSoundGenerator::Notification notification) const +void MainWindow::playNotifcationSound(CNotificationSounds::Notification notification) const { if (!this->m_contextAudioAvailable) return; if (!this->ui->cb_SettingsAudioPlayNotificationSounds->isChecked()) return; - if (notification == CSoundGenerator::NotificationTextMessage && !this->ui->cb_SettingsAudioNotificationTextMessage->isChecked()) return; + if (notification == CNotificationSounds::NotificationTextMessage && !this->ui->cb_SettingsAudioNotificationTextMessage->isChecked()) return; this->getIContextAudio()->playNotification(static_cast(notification)); } diff --git a/src/blackcore/context_audio_impl.cpp b/src/blackcore/context_audio_impl.cpp index d1011ee4b..6edb1c1c7 100644 --- a/src/blackcore/context_audio_impl.cpp +++ b/src/blackcore/context_audio_impl.cpp @@ -7,6 +7,7 @@ #include "context_network.h" #include "blacksound/soundgenerator.h" +#include "blackmisc/notificationsounds.h" #include @@ -263,7 +264,7 @@ namespace BlackCore { Q_ASSERT(this->m_voice); if (this->getRuntime()->isSlotLogForAudioEnabled()) this->getRuntime()->logSlot(Q_FUNC_INFO, QString::number(notification)); - BlackSound::CSoundGenerator::playNotificationSound(90, static_cast(notification)); + BlackSound::CSoundGenerator::playNotificationSound(90, static_cast(notification)); } /* diff --git a/src/blackmisc/notificationsounds.h b/src/blackmisc/notificationsounds.h new file mode 100644 index 000000000..342e4b870 --- /dev/null +++ b/src/blackmisc/notificationsounds.h @@ -0,0 +1,32 @@ +#ifndef BLACKMISC_NOTIFICATIONSOUNDS_H +#define BLACKMISC_NOTIFICATIONSOUNDS_H + +namespace BlackSound +{ + + /*! + * Simplified enums to play sound. Outside BlackSound as this allows + * to trigger sounds without using Multimedia libraries. + */ + struct CNotificationSounds + { + //! How to play? + enum PlayMode + { + Single, + SingleWithAutomaticDeletion, + EndlessLoop + }; + + //! Play notification + enum Notification + { + NotificationError = 0, + NotificationLogin, + NotificationLogoff, + NotificationTextMessage, + }; + }; +} + +#endif // guard diff --git a/src/blacksound/soundgenerator.cpp b/src/blacksound/soundgenerator.cpp index ba260872b..2acce37dc 100644 --- a/src/blacksound/soundgenerator.cpp +++ b/src/blacksound/soundgenerator.cpp @@ -21,7 +21,7 @@ namespace BlackSound { QDateTime CSoundGenerator::s_selcalStarted = QDateTime::currentDateTimeUtc(); - CSoundGenerator::CSoundGenerator(const QAudioDeviceInfo &device, const QAudioFormat &format, const QList &tones, PlayMode mode, QObject *parent) + CSoundGenerator::CSoundGenerator(const QAudioDeviceInfo &device, const QAudioFormat &format, const QList &tones, CNotificationSounds::PlayMode mode, QObject *parent) : 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)), @@ -30,7 +30,7 @@ namespace BlackSound Q_ASSERT(tones.size() > 0); } - CSoundGenerator::CSoundGenerator(const QList &tones, PlayMode mode, QObject *parent) + CSoundGenerator::CSoundGenerator(const QList &tones, CNotificationSounds::PlayMode mode, QObject *parent) : 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()), @@ -84,7 +84,7 @@ namespace BlackSound connect(this, &CSoundGenerator::stopping, this->m_ownThread, &QThread::quit); // in auto delete mode force deleteLater when thread is finished - if (this->m_playMode == SingleWithAutomaticDeletion) + if (this->m_playMode == CNotificationSounds::SingleWithAutomaticDeletion) connect(this->m_ownThread, &QThread::finished, this, &CSoundGenerator::deleteLater); // start thread and begin processing by calling start via signal startThread @@ -107,7 +107,7 @@ namespace BlackSound if (destructor) return; // trigger own termination - if (this->m_playMode == SingleWithAutomaticDeletion) + if (this->m_playMode == CNotificationSounds::SingleWithAutomaticDeletion) { emit this->stopping(); if (!this->m_ownThread) this->deleteLater(); // with own thread, thread signal will call deleteLater @@ -134,7 +134,7 @@ namespace BlackSound { if (this->m_pushTimer) this->m_pushTimer->stop(); this->m_pushTimer->disconnect(this); - if (this->m_playMode == SingleWithAutomaticDeletion) this->stop(); + if (this->m_playMode == CNotificationSounds::SingleWithAutomaticDeletion) this->stop(); } } @@ -326,7 +326,7 @@ namespace BlackSound this->m_position = (m_position + chunkSize) % m_buffer.size(); total += chunkSize; if (m_position == 0 && - (m_playMode == Single || m_playMode == SingleWithAutomaticDeletion)) + (m_playMode == CNotificationSounds::Single || m_playMode == CNotificationSounds::SingleWithAutomaticDeletion)) { this->m_endReached = true; break; @@ -396,7 +396,7 @@ namespace BlackSound CSoundGenerator *CSoundGenerator::playSignal(qint32 volume, const QList &tones, QAudioDeviceInfo device) { - CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CSoundGenerator::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 @@ -408,7 +408,7 @@ namespace BlackSound CSoundGenerator *CSoundGenerator::playSignalInBackground(qint32 volume, const QList &tones, QAudioDeviceInfo device) { - CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CSoundGenerator::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 @@ -423,7 +423,7 @@ namespace BlackSound if (tones.isEmpty()) return; // that was easy if (volume < 1) return; - CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CSoundGenerator::SingleWithAutomaticDeletion); + CSoundGenerator *generator = new CSoundGenerator(device, CSoundGenerator::defaultAudioFormat(), tones, CNotificationSounds::SingleWithAutomaticDeletion); if (generator->singleCyleDurationMs() > 10) { // play, and maybe clean up when done @@ -460,7 +460,7 @@ namespace BlackSound CSoundGenerator::playSelcal(volume, selcal, CSoundGenerator::findClosestOutputDevice(audioDevice)); } - void CSoundGenerator::playNotificationSound(qint32 volume, CSoundGenerator::Notification notification) + void CSoundGenerator::playNotificationSound(qint32 volume, CNotificationSounds::Notification notification) { QMediaPlayer *mediaPlayer = CSoundGenerator::mediaPlayer(); if (mediaPlayer->state() == QMediaPlayer::PlayingState) return; diff --git a/src/blacksound/soundgenerator.h b/src/blacksound/soundgenerator.h index 36b441cca..9860b8f58 100644 --- a/src/blacksound/soundgenerator.h +++ b/src/blacksound/soundgenerator.h @@ -9,6 +9,7 @@ #include "blackmisc/avselcal.h" #include "blackmisc/audiodevice.h" #include "blackmisc/pqtime.h" +#include "blackmisc/notificationsounds.h" #include #include #include @@ -27,27 +28,6 @@ namespace BlackSound public: - /*! - * \brief How to play - */ - enum PlayMode - { - Single, - SingleWithAutomaticDeletion, - EndlessLoop - }; - - /*! - * \brief Play notification - */ - enum Notification - { - NotificationError = 0, - NotificationLogin, - NotificationLogoff, - NotificationTextMessage, - }; - //! Tone to be played struct Tone { @@ -81,7 +61,7 @@ namespace BlackSound * \param parent * \see PlayMode */ - CSoundGenerator(const QAudioDeviceInfo &device, const QAudioFormat &format, const QList &tones, PlayMode mode, QObject *parent = nullptr); + CSoundGenerator(const QAudioDeviceInfo &device, const QAudioFormat &format, const QList &tones, CNotificationSounds::PlayMode mode, QObject *parent = nullptr); /*! * \brief Constructor @@ -90,7 +70,7 @@ namespace BlackSound * \param parent * \see PlayMode */ - CSoundGenerator(const QList &tones, PlayMode mode, QObject *parent = nullptr); + CSoundGenerator(const QList &tones, CNotificationSounds::PlayMode mode, QObject *parent = nullptr); //! Destructor ~CSoundGenerator(); @@ -193,7 +173,7 @@ namespace BlackSound * \param volume 0-100 * \param notification */ - static void playNotificationSound(qint32 volume, Notification notification); + static void playNotificationSound(qint32 volume, CNotificationSounds::Notification notification); //! One cycle of tones takes t milliseconds BlackMisc::PhysicalQuantities::CTime oneCycleDurationMs() const @@ -246,7 +226,7 @@ namespace BlackSound private: QList m_tones; /*! tones to be played */ qint64 m_position; /*!< position in buffer */ - PlayMode m_playMode; /*!< end data provisioning after playing all tones, play endless loop */ + CNotificationSounds::PlayMode m_playMode; /*!< end data provisioning after playing all tones, play endless loop */ bool m_endReached; /*!< indicates end in combination with single play */ qint64 m_oneCycleDurationMs; /*!< how long is one cycle of tones */ QByteArray m_buffer; /*!< generated buffer for data */