From 6fee18df9a177f33b99ef2bdcf9fe040df699056 Mon Sep 17 00:00:00 2001 From: Elliott Liggett Date: Mon, 8 Feb 2021 00:31:48 -0800 Subject: [PATCH] Moved audio to separate thread. --- rxaudiohandler.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++ rxaudiohandler.h | 44 ++++++++++++++++++++++++++++++++++ udphandler.cpp | 33 ++++++++++++++------------ udphandler.h | 12 ++++++++++ wfview.pro | 6 +++-- 5 files changed, 137 insertions(+), 17 deletions(-) create mode 100644 rxaudiohandler.cpp create mode 100644 rxaudiohandler.h diff --git a/rxaudiohandler.cpp b/rxaudiohandler.cpp new file mode 100644 index 0000000..dee494b --- /dev/null +++ b/rxaudiohandler.cpp @@ -0,0 +1,59 @@ +#include "rxaudiohandler.h" + +rxAudioHandler::rxAudioHandler() +{ + +} + +rxAudioHandler::~rxAudioHandler() +{ + audio->stop(); + delete audio; + delete buffer; +} + +void rxAudioHandler::process() +{ + qDebug() << "rxAudio Handler created."; +} + +void rxAudioHandler::setup(const QAudioFormat format, const int bufferSize) +{ + this->format = format; + this->bufferSize = bufferSize; + buffer = new QBuffer(); + buffer->open(QIODevice::ReadWrite); + audio = new QAudioOutput(format); + audio->setBufferSize(bufferSize); + buffer->seek(0); + audio->start(buffer); +} + + +void rxAudioHandler::incomingAudio(const QByteArray data, const int size) +{ + buffer->buffer().remove(0,buffer->pos()); + buffer->seek(buffer->size()); + + buffer->write(data.constData(), size); + buffer->seek(0); +} + +void rxAudioHandler::changeBufferSize(const int newSize) +{ + // TODO: make a way to change the buffer size. + // possibly deleting the buffer and re-creating + + audio->setBufferSize(newSize); +} + +void rxAudioHandler::getBufferSize() +{ + emit sendBufferSize(buffer->size()); +} + + +void rxAudioHandler::getAudioBufferSize() +{ + emit sendAudioBufferSize(audio->bufferSize()); +} diff --git a/rxaudiohandler.h b/rxaudiohandler.h new file mode 100644 index 0000000..4cf0ab2 --- /dev/null +++ b/rxaudiohandler.h @@ -0,0 +1,44 @@ +#ifndef RXAUDIOHANDLER_H +#define RXAUDIOHANDLER_H + +#include + +#include +#include + +#include + +class rxAudioHandler : public QObject +{ + Q_OBJECT + +public: + rxAudioHandler(); + ~rxAudioHandler(); + + +public slots: + void process(); + void setup(const QAudioFormat format, const int bufferSize); + + void incomingAudio(const QByteArray data, const int size); + void changeBufferSize(const int newSize); + void getBufferSize(); + void getAudioBufferSize(); + +signals: + void audioMessage(QString message); + void sendBufferSize(int newSize); + void sendAudioBufferSize(int newSize); + + +private: + QBuffer* buffer; + QAudioOutput* audio; + QAudioFormat format; + int bufferSize; + + +}; + +#endif // RXAUDIOHANDLER_H diff --git a/udphandler.cpp b/udphandler.cpp index 76cbe18..db1859f 100644 --- a/udphandler.cpp +++ b/udphandler.cpp @@ -576,12 +576,15 @@ udpAudio::udpAudio(QHostAddress local, QHostAddress ip, int aport) qDebug() << "----- done with audio info -----"; } - buffer = new QBuffer(); - buffer->open(QIODevice::ReadWrite); - audio = new QAudioOutput(format); - audio->setBufferSize(10000); // TODO: add preference, maybe UI too. 20210205: connection was wifi --> cellular --> internet --> rig - buffer->seek(0); - audio->start(buffer); + rxaudio = new rxAudioHandler(); + rxAudioThread = new QThread(this); + + rxaudio->moveToThread(rxAudioThread); + + connect(this,SIGNAL(setupAudio(QAudioFormat,int)), rxaudio, SLOT(setup(QAudioFormat,int))); + connect(this, SIGNAL(haveAudioData(QByteArray,int)), rxaudio, SLOT(incomingAudio(QByteArray,int))); + + rxaudio->setup(format, 10000); } @@ -596,6 +599,14 @@ udpAudio::~udpAudio() { delete buffer; } + if(rxaudio != Q_NULLPTR) + { + delete rxaudio; + } + if(rxAudioThread != Q_NULLPTR) + { + delete rxAudioThread; + } } void udpAudio::DataReceived() @@ -638,15 +649,7 @@ void udpAudio::DataReceived() lastReceivedSeq = gotSeq; - //qDebug() << "Got Audio Sequence: (" << r.length() << ") " << gotSeq; - // Delete contents of buffer up to existing pos() - buffer->buffer().remove(0,buffer->pos()); - // Seek to end of curent buffer - buffer->seek(buffer->size()); - // Append to end of buffer - buffer->write(r.mid(24).constData(), r.mid(24).length()); - // Seek to start of buffer. - buffer->seek(0); + emit haveAudioData(r.mid(24), r.mid(24).length()); } break; } diff --git a/udphandler.h b/udphandler.h index d90e1af..49b4bdb 100644 --- a/udphandler.h +++ b/udphandler.h @@ -15,10 +15,14 @@ // Needed for audio #include #include +#include #include +#include "rxaudiohandler.h" + + // Parent class that contains all common items. class udpBase : public QObject { @@ -118,6 +122,10 @@ public: udpAudio(QHostAddress local, QHostAddress ip, int aport); ~udpAudio(); QAudioOutput* audio; + +signals: + void haveAudioData(QByteArray data, int length); + void setupAudio(const QAudioFormat format, const int bufferSize); private: void DataReceived(); @@ -128,6 +136,10 @@ private: bool sentPacketConnect2 = false; uint16_t sendAudioSeq = 0; + rxAudioHandler* rxaudio; + QThread* rxAudioThread; + + }; diff --git a/wfview.pro b/wfview.pro index cb03a00..cfd89d0 100644 --- a/wfview.pro +++ b/wfview.pro @@ -80,7 +80,8 @@ SOURCES += main.cpp\ freqmemory.cpp \ rigidentities.cpp \ udphandler.cpp \ - logcategories.cpp + logcategories.cpp \ + rxaudiohandler.cpp HEADERS += wfmain.h \ commhandler.h \ @@ -88,7 +89,8 @@ HEADERS += wfmain.h \ freqmemory.h \ rigidentities.h \ udphandler.h \ - logcategories.h + logcategories.h \ + rxaudiohandler.h FORMS += wfmain.ui