Fixed notification audio to not produce static by buffering the entire file ahead of time before playing

This commit is contained in:
Jordan Sherer 2019-11-21 11:49:17 -05:00
parent 35b478f791
commit c1fd31dc24
2 changed files with 30 additions and 12 deletions

View File

@ -5,38 +5,54 @@
NotificationAudio::NotificationAudio(QObject *parent):
QObject(parent)
{
m_file = new BWFFile(QAudioFormat{}, this);
m_stream = new SoundOutput();
//m_decoder = new AudioDecoder(this);
m_file = new WaveFile(this);
connect(m_stream, &SoundOutput::status, this, &NotificationAudio::status);
connect(m_stream, &SoundOutput::error, this, &NotificationAudio::error);
}
NotificationAudio::~NotificationAudio(){
stop();
}
void NotificationAudio::status(QString message){
if(message == "Idle"){
stop();
}
}
void NotificationAudio::error(QString message){
qDebug() << "notification error:" << message;
stop();
}
void NotificationAudio::setDevice(const QAudioDeviceInfo &device, unsigned channels, unsigned msBuffer){
m_device = device;
m_channels = channels;
m_msBuffer = msBuffer;
m_stream->setFormat(device, channels, msBuffer);
//m_decoder->init(m_stream->format());
}
void NotificationAudio::play(const QString &filePath){
//m_decoder->start(filePath);
//m_stream->restart(m_decoder);
if(m_file->isOpen()){
m_file->close();
}
if(m_file->open(filePath)){
m_file->seek(0);
m_stream->setDeviceFormat(m_device, m_file->fileFormat(), m_channels, m_msBuffer);
m_stream->restart(m_file);
m_file->setFileName(filePath);
if(m_file->open(BWFFile::ReadOnly)){
auto format = m_file->format();
auto channels = format.channelCount();
auto bytes = m_file->readAll();
auto buffer = new QBuffer(new QByteArray(bytes));
if(buffer->open(QIODevice::ReadOnly)){
m_stream->setDeviceFormat(m_device, format, channels, m_msBuffer);
m_stream->restart(buffer);
}
}
}
void NotificationAudio::stop(){
//m_decoder->stop();
m_stream->stop();
if(m_file->isOpen()){

View File

@ -11,7 +11,7 @@
#include "AudioDevice.hpp"
#include "AudioDecoder.h"
#include "WaveFile.h"
#include "Audio/BWFFile.hpp"
#include "soundout.h"
@ -25,6 +25,8 @@ public:
~NotificationAudio();
public slots:
void status(QString message);
void error(QString message);
void setDevice(const QAudioDeviceInfo &device, unsigned channels, unsigned msBuffer=0);
void play(const QString &filePath);
void stop();
@ -32,7 +34,7 @@ public slots:
private:
QPointer<SoundOutput> m_stream;
QPointer<AudioDecoder> m_decoder;
QPointer<WaveFile> m_file;
QPointer<BWFFile> m_file;
QAudioDeviceInfo m_device;
unsigned m_channels;
unsigned m_msBuffer;