From 4e13b625ca7dae1a970bdbf1aa747a232023efc7 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Sun, 1 Mar 2015 10:30:02 -0500 Subject: [PATCH] Fix for mixer clipping bug that's causing audio to crackle --- src/audio/AudioThread.cpp | 23 ++++++++++++++++------- src/demod/DemodulatorThread.cpp | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index 4bf6ee2..dab66f3 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -40,7 +40,7 @@ void AudioThread::removeThread(AudioThread *other) { } void AudioThread::deviceCleanup() { - std::map::iterator i; + std::map::iterator i; for (i = deviceController.begin(); i != deviceController.end(); i++) { i->second->terminate(); @@ -105,7 +105,7 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu continue; } - peak += srcmix->currentInput->peak * srcmix->gain; + float mixPeak = srcmix->currentInput->peak * srcmix->gain; if (srcmix->currentInput->channels == 1) { for (int i = 0; i < nBufferFrames; i++) { @@ -122,6 +122,10 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu continue; } srcmix->audioQueuePtr = 0; + float srcPeak = srcmix->currentInput->peak * srcmix->gain; + if (mixPeak < srcPeak) { + mixPeak = srcPeak; + } } if (srcmix->currentInput && srcmix->currentInput->data.size()) { float v = srcmix->currentInput->data[srcmix->audioQueuePtr] * srcmix->gain; @@ -145,6 +149,10 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu continue; } srcmix->audioQueuePtr = 0; + float srcPeak = srcmix->currentInput->peak * srcmix->gain; + if (mixPeak < srcPeak) { + mixPeak = srcPeak; + } } if (srcmix->currentInput && srcmix->currentInput->data.size()) { out[i] = out[i] + srcmix->currentInput->data[srcmix->audioQueuePtr] * srcmix->gain; @@ -153,13 +161,14 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu } } - if (peak > 1.0) { - for (int i = 0 ; i < nBufferFrames * 2; i ++) { - out[i] /= peak; - } - } + peak += mixPeak; } + if (peak > 1.0) { + for (int i = 0; i < nBufferFrames * 2; i++) { + out[i] /= peak; + } + } return 0; } diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index bd5a521..169137f 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -191,8 +191,8 @@ void DemodulatorThread::threadMain() { break; } - amOutputCeilMA = amOutputCeilMA + (amOutputCeil - amOutputCeilMA) * 0.05; - amOutputCeilMAA = amOutputCeilMAA + (amOutputCeilMA - amOutputCeilMAA) * 0.05; + amOutputCeilMA = amOutputCeilMA + (amOutputCeil - amOutputCeilMA) * 0.025; + amOutputCeilMAA = amOutputCeilMAA + (amOutputCeilMA - amOutputCeilMAA) * 0.025; amOutputCeil = 0;