Fixed lag in notifications by added a byte stream cache

This commit is contained in:
Jordan Sherer 2019-11-26 14:23:39 -05:00
parent 08d1f007fa
commit f332177034
2 changed files with 52 additions and 8 deletions

View File

@ -14,7 +14,13 @@ NotificationAudio::NotificationAudio(QObject *parent):
} }
NotificationAudio::~NotificationAudio(){ NotificationAudio::~NotificationAudio(){
// stop the audio
stop(); stop();
// clean cache
foreach(auto pair, m_cache){
delete pair.second;
}
} }
void NotificationAudio::status(QString message){ void NotificationAudio::status(QString message){
@ -36,22 +42,51 @@ void NotificationAudio::setDevice(const QAudioDeviceInfo &device, unsigned chann
} }
void NotificationAudio::play(const QString &filePath){ void NotificationAudio::play(const QString &filePath){
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;
}
if(m_file->isOpen()){ if(m_file->isOpen()){
m_file->close(); m_file->close();
} }
m_file->setFileName(filePath); m_file->setFileName(filePath);
if(m_file->open(BWFFile::ReadOnly)){ if(m_file->open(BWFFile::ReadOnly)){
auto format = m_file->format(); QAudioFormat format = m_file->format();
auto channels = format.channelCount(); QByteArray *bytes = new QByteArray(m_file->readAll());
auto bytes = m_file->readAll();
auto buffer = new QBuffer(new QByteArray(bytes)); qDebug() << "notification: playing" << filePath << "with format" << format << "from disk";
if(buffer->open(QIODevice::ReadOnly)){ playBytes(format, bytes);
m_stream->setDeviceFormat(m_device, format, channels, m_msBuffer);
m_stream->restart(buffer); // 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();
}
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);
}
void NotificationAudio::stop(){ void NotificationAudio::stop(){
m_stream->stop(); m_stream->stop();

View File

@ -7,7 +7,11 @@
#include <QAudioFormat> #include <QAudioFormat>
#include <QAudioOutput> #include <QAudioOutput>
#include <QFile> #include <QFile>
#include <QPair>
#include <QPointer> #include <QPointer>
#include <QByteArray>
#include <QCache>
#include <QScopedPointer>
#include "AudioDevice.hpp" #include "AudioDevice.hpp"
#include "AudioDecoder.h" #include "AudioDecoder.h"
@ -32,10 +36,15 @@ public slots:
void stop(); void stop();
private: private:
void playBytes(const QAudioFormat &format, QByteArray *bytes);
private:
QMap<QString, QPair<QAudioFormat, QByteArray*>> m_cache;
QPointer<SoundOutput> m_stream; QPointer<SoundOutput> m_stream;
QPointer<AudioDecoder> m_decoder; QPointer<AudioDecoder> m_decoder;
QPointer<BWFFile> m_file; QPointer<BWFFile> m_file;
QAudioDeviceInfo m_device; QAudioDeviceInfo m_device;
QBuffer m_buffer;
unsigned m_channels; unsigned m_channels;
unsigned m_msBuffer; unsigned m_msBuffer;
}; };