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(){
// stop the audio
stop();
// clean cache
foreach(auto pair, m_cache){
delete pair.second;
}
}
void NotificationAudio::status(QString message){
@ -36,20 +42,49 @@ void NotificationAudio::setDevice(const QAudioDeviceInfo &device, unsigned chann
}
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()){
m_file->close();
}
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);
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();
}
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(){

View File

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