1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include "NotificationAudio.h"
#include "BWFFile.h"
#include "soundout.h"
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(notificationaudio_js8)
/******************************************************************************/
// Public Implementation
/******************************************************************************/
NotificationAudio::NotificationAudio(QObject *parent)
: QObject{parent}, m_stream{new SoundOutput} {
connect(m_stream.data(), &SoundOutput::status, this,
&NotificationAudio::status);
connect(m_stream.data(), &SoundOutput::error, this,
&NotificationAudio::error);
}
NotificationAudio::~NotificationAudio() { stop(); }
void NotificationAudio::status(QString const message) {
if (message == "Idle")
stop();
}
void NotificationAudio::error(QString const message) {
qCDebug(notificationaudio_js8) << "notification error:" << message;
}
void NotificationAudio::setDevice(QAudioDevice const &device,
unsigned const msBuffer) {
m_device = device;
m_msBuffer = msBuffer;
}
void NotificationAudio::play(QString const &filePath) {
if (auto const it = m_cache.constFind(filePath); it != m_cache.constEnd()) {
playEntry(it);
} else if (auto file = BWFFile(QAudioFormat{}, filePath);
file.open(QIODevice::ReadOnly)) {
if (auto data = file.readAll(); !data.isEmpty()) {
playEntry(m_cache.emplace(filePath, file.format(), data));
}
}
}
void NotificationAudio::stop() { m_stream->stop(); }
/******************************************************************************/
// Private Implementation
/******************************************************************************/
void NotificationAudio::playEntry(Cache::const_iterator const it) {
if (m_buffer.isOpen())
m_buffer.close();
auto const &[format, data] = *it;
m_buffer.setData(data);
if (m_buffer.open(QIODevice::ReadOnly)) {
m_stream->setDeviceFormat(m_device, format, m_msBuffer);
m_stream->restart(&m_buffer);
}
}
/******************************************************************************/
Q_LOGGING_CATEGORY(notificationaudio_js8, "notificationaudio.js8", QtWarningMsg)
|