2019-10-08 14:33:38 -04:00
|
|
|
#include "NotificationAudio.h"
|
2025-10-12 03:34:27 +02:00
|
|
|
#include <QLoggingCategory>
|
2024-09-16 08:42:58 -07:00
|
|
|
#include "Audio/BWFFile.hpp"
|
|
|
|
|
#include "soundout.h"
|
2019-10-08 14:33:38 -04:00
|
|
|
|
2025-10-12 03:34:27 +02:00
|
|
|
Q_DECLARE_LOGGING_CATEGORY(notificationaudio_js8)
|
|
|
|
|
|
2024-09-16 16:54:29 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
// Public Implementation
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2024-09-16 08:42:58 -07:00
|
|
|
NotificationAudio::NotificationAudio(QObject * parent)
|
|
|
|
|
: QObject {parent}
|
|
|
|
|
, m_stream {new SoundOutput}
|
2019-10-08 14:33:38 -04:00
|
|
|
{
|
2024-09-16 08:42:58 -07:00
|
|
|
connect(m_stream.data(), &SoundOutput::status, this, &NotificationAudio::status);
|
|
|
|
|
connect(m_stream.data(), &SoundOutput::error, this, &NotificationAudio::error);
|
2019-10-08 14:33:38 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 08:42:58 -07:00
|
|
|
NotificationAudio::~NotificationAudio()
|
|
|
|
|
{
|
2019-10-08 21:26:17 -04:00
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 08:42:58 -07:00
|
|
|
void NotificationAudio::status(QString const message)
|
|
|
|
|
{
|
|
|
|
|
if (message == "Idle") stop();
|
2019-11-21 11:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 08:42:58 -07:00
|
|
|
void NotificationAudio::error(QString const message)
|
|
|
|
|
{
|
2025-10-12 03:34:27 +02:00
|
|
|
qCDebug(notificationaudio_js8) << "notification error:" << message;
|
2019-11-21 11:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 08:42:58 -07:00
|
|
|
void
|
|
|
|
|
NotificationAudio::setDevice(QAudioDevice const & device,
|
|
|
|
|
unsigned const msBuffer)
|
|
|
|
|
{
|
2024-09-14 09:25:49 -07:00
|
|
|
m_device = device;
|
2019-10-16 22:28:45 -04:00
|
|
|
m_msBuffer = msBuffer;
|
2019-10-08 14:33:38 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 08:42:58 -07:00
|
|
|
void
|
|
|
|
|
NotificationAudio::play(QString const & filePath)
|
|
|
|
|
{
|
|
|
|
|
if (auto const it = m_cache.constFind(filePath);
|
|
|
|
|
it != m_cache.constEnd())
|
|
|
|
|
{
|
|
|
|
|
playEntry(it);
|
2019-10-16 22:28:45 -04:00
|
|
|
}
|
2024-09-16 16:54:29 -07:00
|
|
|
else if (auto file = BWFFile(QAudioFormat{}, filePath);
|
|
|
|
|
file.open(QIODevice::ReadOnly))
|
2024-09-16 08:42:58 -07:00
|
|
|
{
|
2024-09-16 16:54:29 -07:00
|
|
|
if (auto data = file.readAll();
|
|
|
|
|
!data.isEmpty())
|
2024-09-16 08:42:58 -07:00
|
|
|
{
|
2024-09-16 16:54:29 -07:00
|
|
|
playEntry(m_cache.emplace(filePath, file.format(), data));
|
2024-09-16 08:42:58 -07:00
|
|
|
}
|
2019-11-26 14:23:39 -05:00
|
|
|
}
|
2019-10-08 14:33:38 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-16 08:42:58 -07:00
|
|
|
void
|
|
|
|
|
NotificationAudio::stop()
|
|
|
|
|
{
|
2019-10-15 13:52:30 -04:00
|
|
|
m_stream->stop();
|
2019-10-11 23:34:31 -04:00
|
|
|
}
|
2024-09-16 16:54:29 -07:00
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2025-10-12 03:34:27 +02:00
|
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(notificationaudio_js8, "notificationaudio.js8", QtWarningMsg)
|