2019-10-08 14:33:38 -04:00
|
|
|
#include "NotificationAudio.h"
|
2019-10-16 22:28:45 -04:00
|
|
|
#include "WaveFile.h"
|
2019-10-08 14:33:38 -04:00
|
|
|
|
|
|
|
|
2019-10-15 13:52:30 -04:00
|
|
|
NotificationAudio::NotificationAudio(QObject *parent):
|
|
|
|
QObject(parent)
|
2019-10-08 14:33:38 -04:00
|
|
|
{
|
2019-11-21 11:49:17 -05:00
|
|
|
m_file = new BWFFile(QAudioFormat{}, this);
|
|
|
|
|
2019-10-15 13:52:30 -04:00
|
|
|
m_stream = new SoundOutput();
|
2019-11-21 11:49:17 -05:00
|
|
|
|
|
|
|
connect(m_stream, &SoundOutput::status, this, &NotificationAudio::status);
|
|
|
|
connect(m_stream, &SoundOutput::error, this, &NotificationAudio::error);
|
2019-10-08 14:33:38 -04:00
|
|
|
}
|
|
|
|
|
2019-10-08 21:26:17 -04:00
|
|
|
NotificationAudio::~NotificationAudio(){
|
2019-11-26 14:23:39 -05:00
|
|
|
// stop the audio
|
2019-10-08 21:26:17 -04:00
|
|
|
stop();
|
2019-11-26 14:23:39 -05:00
|
|
|
|
|
|
|
// clean cache
|
|
|
|
foreach(auto pair, m_cache){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
2019-10-08 21:26:17 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 11:49:17 -05:00
|
|
|
void NotificationAudio::status(QString message){
|
|
|
|
if(message == "Idle"){
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationAudio::error(QString message){
|
|
|
|
qDebug() << "notification error:" << message;
|
|
|
|
}
|
|
|
|
|
2019-10-15 13:52:30 -04:00
|
|
|
void NotificationAudio::setDevice(const QAudioDeviceInfo &device, unsigned channels, unsigned msBuffer){
|
2019-10-16 22:28:45 -04:00
|
|
|
m_device = device;
|
|
|
|
m_channels = channels;
|
|
|
|
m_msBuffer = msBuffer;
|
2019-10-15 13:52:30 -04:00
|
|
|
m_stream->setFormat(device, channels, msBuffer);
|
2019-10-08 14:33:38 -04:00
|
|
|
}
|
|
|
|
|
2019-10-12 14:12:16 -04:00
|
|
|
void NotificationAudio::play(const QString &filePath){
|
2019-11-26 14:23:39 -05:00
|
|
|
if(m_cache.contains(filePath)){
|
|
|
|
auto pair = m_cache.value(filePath);
|
|
|
|
auto format = pair.first;
|
|
|
|
auto bytes = pair.second;
|
|
|
|
|
|
|
|
qDebug() << "notification: playing" << filePath << "with format" << format << "from cache";
|
|
|
|
playBytes(format, bytes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-16 22:28:45 -04:00
|
|
|
if(m_file->isOpen()){
|
|
|
|
m_file->close();
|
|
|
|
}
|
2019-11-21 11:49:17 -05:00
|
|
|
m_file->setFileName(filePath);
|
|
|
|
if(m_file->open(BWFFile::ReadOnly)){
|
2019-11-26 14:23:39 -05:00
|
|
|
QAudioFormat format = m_file->format();
|
|
|
|
QByteArray *bytes = new QByteArray(m_file->readAll());
|
|
|
|
|
|
|
|
qDebug() << "notification: playing" << filePath << "with format" << format << "from disk";
|
|
|
|
playBytes(format, bytes);
|
|
|
|
|
|
|
|
// cache the buffer
|
|
|
|
m_cache.insert(filePath, {format, bytes});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationAudio::playBytes(const QAudioFormat &format, QByteArray *bytes){
|
|
|
|
if(bytes == nullptr){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_buffer.isOpen()){
|
|
|
|
m_buffer.close();
|
2019-10-16 22:28:45 -04:00
|
|
|
}
|
2019-11-26 14:23:39 -05:00
|
|
|
|
|
|
|
m_buffer.setBuffer(bytes);
|
|
|
|
|
|
|
|
if(!m_buffer.open(QIODevice::ReadOnly)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stream->setDeviceFormat(m_device, format, format.channelCount(), m_msBuffer);
|
|
|
|
m_stream->restart(&m_buffer);
|
2019-10-08 14:33:38 -04:00
|
|
|
}
|
|
|
|
|
2019-10-15 13:52:30 -04:00
|
|
|
void NotificationAudio::stop(){
|
|
|
|
m_stream->stop();
|
2019-10-16 22:28:45 -04:00
|
|
|
|
|
|
|
if(m_file->isOpen()){
|
|
|
|
m_file->close();
|
|
|
|
}
|
2019-10-11 23:34:31 -04:00
|
|
|
}
|