mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
further improved audio metering
Give qTox time to do more important things -> do not measure with 100%.
This commit is contained in:
parent
535bb212f5
commit
ace3662757
|
@ -36,7 +36,6 @@
|
|||
#include <QMutexLocker>
|
||||
#include <QPointer>
|
||||
#include <QThread>
|
||||
#include <QWaitCondition>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <QMutexLocker>
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <QWaitCondition>
|
||||
|
||||
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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user