diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 38e9516b9..b7ab14274 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -36,7 +36,6 @@ #include #include #include -#include #include @@ -169,30 +168,27 @@ private: mNewMaxGain = 0.f; + QMutexLocker locker(&mMeterLock); while (!isInterruptionRequested()) { + mListenerReady.wait(locker.mutex()); + locker.relock(); + int16_t buff[framesize] = {0}; if (audio.tryCaptureSamples(buff, AUDIO_FRAME_SAMPLE_COUNT)) { - mMeterLock.lock(); mNewMaxGain = 0.f; for (int i = 0; i < framesize; ++i) { mNewMaxGain = qMax(mNewMaxGain, qAbs(buff[i]) / 32767.0); } - mMeterLock.unlock(); - } else if (mNewMaxGain > 0.f) { - mNewMaxGain -= 0.01f; } - mMeterLock.lock(); - mCheckGainChanged.wakeAll(); - mDoMonitoring.wait(&mMeterLock); - mMeterLock.unlock(); + mGainMeasured.wakeAll(); } } public: QMutex mMeterLock; - QWaitCondition mDoMonitoring; - QWaitCondition mCheckGainChanged; + QWaitCondition mListenerReady; + QWaitCondition mGainMeasured; qreal mNewMaxGain; }; @@ -205,13 +201,20 @@ AudioMeterListener::AudioMeterListener(AudioMeter* measureThread) void AudioMeterListener::start() { + if (!mAudioMeter->isRunning()) { + mAudioMeter->start(); + // TODO: ensure that audiometer is running + // -> Start listeners from AudioMeter::started signal + while (!mAudioMeter->isRunning()) + QThread::msleep(10); + } + QThread* listener = new QThread; connect(listener, &QThread::started, this, &AudioMeterListener::doListen); connect(listener, &QThread::finished, listener, &QThread::deleteLater); moveToThread(listener); listener->start(); - mAudioMeter->start(); } void AudioMeterListener::stop() @@ -219,6 +222,11 @@ void AudioMeterListener::stop() mActive = false; } +void AudioMeterListener::processed() +{ + mGainProcessed.wakeAll(); +} + void AudioMeterListener::doListen() { mMaxGain = 0.f; @@ -226,29 +234,25 @@ void AudioMeterListener::doListen() QMutexLocker locker(&mAudioMeter->mMeterLock); while (mActive) { - mAudioMeter->waitForData(locker.mutex()); - - locker.unlock(); - //qDebug() << "GAIN:" << mAudioMeter->mNewMaxGain << "/" << mMaxGain; - if (mAudioMeter->mNewMaxGain != mMaxGain) { - if (mAudioMeter->mNewMaxGain > mMaxGain) - { - mMaxGain = mAudioMeter->mNewMaxGain; - emit gainChanged(mMaxGain); - } else if (mMaxGain > 0.02f) { - mMaxGain -= 0.0005f; - emit gainChanged(mMaxGain); - } - } + mAudioMeter->mListenerReady.wakeAll(); + mAudioMeter->mGainMeasured.wait(&mAudioMeter->mMeterLock); locker.relock(); - mAudioMeter->monitorFrame(); + if (mAudioMeter->mNewMaxGain > mMaxGain) { + mMaxGain = mAudioMeter->mNewMaxGain; + emit gainChanged(mMaxGain); + } else if (mMaxGain > 0.02f) { + mMaxGain -= 0.008f; + emit gainChanged(mMaxGain); + } + + mGainProcessed.wait(locker.mutex(), 10); + locker.relock(); } mAudioMeter->requestInterruption(); } - /** Returns the singleton's instance. Will construct on first call. */ diff --git a/src/audio/audio.h b/src/audio/audio.h index 7aa397ed3..8a70382f7 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -25,6 +25,7 @@ #include #include #include +#include struct Tox; class AudioFilterer; @@ -117,6 +118,8 @@ public: void start(); void stop(); + void processed(); + signals: void gainChanged(qreal newMaxGain); @@ -127,6 +130,7 @@ private: bool mActive; AudioMeter* mAudioMeter; qreal mMaxGain; + QWaitCondition mGainProcessed; }; #endif // AUDIO_H diff --git a/src/widget/tool/micfeedbackwidget.cpp b/src/widget/tool/micfeedbackwidget.cpp index f18c59219..60766653e 100644 --- a/src/widget/tool/micfeedbackwidget.cpp +++ b/src/widget/tool/micfeedbackwidget.cpp @@ -61,7 +61,8 @@ void MicFeedbackWidget::paintEvent(QPaintEvent*) void MicFeedbackWidget::showEvent(QShowEvent*) { mMeterListener = Audio::getInstance().createAudioMeterListener(); - connect(mMeterListener, &AudioMeterListener::gainChanged, this, &MicFeedbackWidget::onGainMetered); + connect(mMeterListener, &AudioMeterListener::gainChanged, + this, &MicFeedbackWidget::onGainMetered); mMeterListener->start(); } @@ -73,6 +74,6 @@ void MicFeedbackWidget::hideEvent(QHideEvent*) void MicFeedbackWidget::onGainMetered(qreal value) { current = value; - //qDebug("Gain metered at %.3f", current); update(); + mMeterListener->processed(); }