diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index a88e2ce..65c1b1a 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -60,12 +60,12 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu std::cout << "Audio buffer underflow.." << (src->underflowCount++) << std::endl; } - if (!src->boundThreads.load()->empty()) { - src->gain = 1.0 / src->boundThreads.load()->size(); - } else { + if (src->boundThreads.load()->empty()) { return 0; } + float peak = 0.0; + for (int j = 0; j < src->boundThreads.load()->size(); j++) { AudioThread *srcmix = (*(src->boundThreads.load()))[j]; if (srcmix->terminated || !srcmix->inputQueue || srcmix->inputQueue->empty() || !srcmix->isActive()) { @@ -104,6 +104,8 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu continue; } + peak += srcmix->currentInput->peak; + if (srcmix->currentInput->channels == 1) { for (int i = 0; i < nBufferFrames; i++) { if (srcmix->audioQueuePtr >= srcmix->currentInput->data.size()) { @@ -121,7 +123,7 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu srcmix->audioQueuePtr = 0; } if (srcmix->currentInput && srcmix->currentInput->data.size()) { - float v = srcmix->currentInput->data[srcmix->audioQueuePtr] * src->gain * srcmix->gain; + float v = srcmix->currentInput->data[srcmix->audioQueuePtr] * srcmix->gain; out[i * 2] += v; out[i * 2 + 1] += v; } @@ -144,12 +146,17 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu srcmix->audioQueuePtr = 0; } if (srcmix->currentInput && srcmix->currentInput->data.size()) { - out[i] = out[i] + srcmix->currentInput->data[srcmix->audioQueuePtr] * src->gain * srcmix->gain; + out[i] = out[i] + srcmix->currentInput->data[srcmix->audioQueuePtr] * srcmix->gain; } srcmix->audioQueuePtr++; } } + if (peak > 1.0) { + for (int i = 0 ; i < nBufferFrames * 2; i ++) { + out[i] /= peak; + } + } } return 0; diff --git a/src/audio/AudioThread.h b/src/audio/AudioThread.h index 26d9e43..914daa1 100644 --- a/src/audio/AudioThread.h +++ b/src/audio/AudioThread.h @@ -35,10 +35,11 @@ public: long long frequency; int sampleRate; int channels; + float peak; std::vector data; AudioThreadInput() : - frequency(0), sampleRate(0), channels(0) { + frequency(0), sampleRate(0), channels(0), peak(0) { } diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index 9c7d07e..efd03c3 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -313,6 +313,14 @@ void DemodulatorThread::threadMain() { ati->data.assign(resampledOutputData.begin(), resampledOutputData.begin() + numAudioWritten); } + std::vector::iterator data_i; + ati->peak = 0; + for (data_i = ati->data.begin(); data_i != ati->data.end(); data_i++) { + if (float p = fabs(*data_i) > ati->peak) { + ati->peak = p; + } + } + audioOutputQueue->push(ati); } }