From 1393571de62a27ab176cd5b6ca3ca71000ec795d Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Mon, 24 May 2021 09:27:18 +0100 Subject: [PATCH] Add mutex for audio buffer --- audiohandler.cpp | 15 ++++++--------- audiohandler.h | 4 +++- wfview.vcxproj | 2 +- wfview.vcxproj.filters | 1 + 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/audiohandler.cpp b/audiohandler.cpp index 031d70a..3997fdb 100644 --- a/audiohandler.cpp +++ b/audiohandler.cpp @@ -138,11 +138,13 @@ int audioHandler::readData(void* outputBuffer, void* inputBuffer, unsigned int n int sentlen = 0; qint16* buffer = (qint16*)outputBuffer; //qDebug(logAudio()) << "looking for: " << nFrames << this->audioBuffer.size(); + if (status == RTAUDIO_OUTPUT_UNDERFLOW) + qDebug(logAudio()) << "Underflow detected"; if (!audioBuffer.isEmpty()) { - + mutex.lock(); // Output buffer is ALWAYS 16 bit. auto packet = audioBuffer.begin(); @@ -193,13 +195,8 @@ int audioHandler::readData(void* outputBuffer, void* inputBuffer, unsigned int n lastSeq = packet->seq; } } + mutex.unlock(); } - else { - // Fool audio system into thinking it has valid data, this seems to be required - // for MacOS Built in audio but shouldn't cause any issues with other platforms. - return 0; - } - return 0; } @@ -331,9 +328,9 @@ void audioHandler::incomingAudio(audioPacket data) data.dataout = data.datain; } - //memcpy(buf, data.dataout.constData(), data.dataout.length()); - //qDebug(logAudio()) << "Got data: " << data.dataout.length(); + mutex.lock(); audioBuffer.insert( data.seq, data ); + mutex.unlock(); } void audioHandler::changeLatency(const quint16 newSize) diff --git a/audiohandler.h b/audiohandler.h index 1b3d3d9..54bb044 100644 --- a/audiohandler.h +++ b/audiohandler.h @@ -5,7 +5,7 @@ #include #include - +#include #include "rtaudio/RtAudio.h" typedef signed short MY_TYPE; @@ -836,6 +836,8 @@ private: unsigned int ratioNum; unsigned int ratioDen; + QMutex mutex; + volatile bool lock=false; }; #endif // AUDIOHANDLER_H diff --git a/wfview.vcxproj b/wfview.vcxproj index 36bfb87..816a3a0 100644 --- a/wfview.vcxproj +++ b/wfview.vcxproj @@ -95,7 +95,7 @@ wfview true - $(IncludePath) + C:\Users\Phil\source\repos\qcustomplot;$(IncludePath) $(LibraryPath) diff --git a/wfview.vcxproj.filters b/wfview.vcxproj.filters index f16a5ae..94c1d1e 100644 --- a/wfview.vcxproj.filters +++ b/wfview.vcxproj.filters @@ -357,6 +357,7 @@ +