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): NotificationAudio::NotificationAudio(QObject *parent):
QObject(parent) QObject(parent)
{ {
m_file = new BWFFile(QAudioFormat{}, this);
m_stream = new SoundOutput(); 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(){ NotificationAudio::~NotificationAudio(){
stop(); 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){ void NotificationAudio::setDevice(const QAudioDeviceInfo &device, unsigned channels, unsigned msBuffer){
m_device = device; m_device = device;
m_channels = channels; m_channels = channels;
m_msBuffer = msBuffer; m_msBuffer = msBuffer;
m_stream->setFormat(device, channels, msBuffer); m_stream->setFormat(device, channels, msBuffer);
//m_decoder->init(m_stream->format());
} }
void NotificationAudio::play(const QString &filePath){ void NotificationAudio::play(const QString &filePath){
//m_decoder->start(filePath);
//m_stream->restart(m_decoder);
if(m_file->isOpen()){ if(m_file->isOpen()){
m_file->close(); m_file->close();
} }
if(m_file->open(filePath)){ m_file->setFileName(filePath);
m_file->seek(0); if(m_file->open(BWFFile::ReadOnly)){
m_stream->setDeviceFormat(m_device, m_file->fileFormat(), m_channels, m_msBuffer); auto format = m_file->format();
m_stream->restart(m_file); 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(){ void NotificationAudio::stop(){
//m_decoder->stop();
m_stream->stop(); m_stream->stop();
if(m_file->isOpen()){ if(m_file->isOpen()){

View File

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