diff options
Diffstat (limited to 'pica-client/audiovideocallcontroller.cpp')
| -rw-r--r-- | pica-client/audiovideocallcontroller.cpp | 150 |
1 files changed, 131 insertions, 19 deletions
diff --git a/pica-client/audiovideocallcontroller.cpp b/pica-client/audiovideocallcontroller.cpp index 763f1b1..405746d 100644 --- a/pica-client/audiovideocallcontroller.cpp +++ b/pica-client/audiovideocallcontroller.cpp @@ -50,7 +50,9 @@ static const int kCallSampleRate = 16000; AudioVideoCallController::AudioVideoCallController(QObject *parent) : QObject(parent), callwindow(0), ringdevice(0), is_active(false), m_captureGain(nullptr), m_savedCaptureGain(0.0), m_haveSavedCaptureGain(false), - m_captureGainSteps(0), m_audioSeq(0), + m_gainCalibrating(false), m_captureGainSteps(0), m_gainStartDb(0.0), + m_haveGainDb(false), m_gainIneffectiveSteps(0), m_lastPinnedPercent(0.0), + m_gainSettleReports(0), m_audioSeq(0), m_videoSeq(0), m_videoFrameTimestamp(0), m_videoFrameStarted(false) { connect(skynet, SIGNAL(IncomingCall(QByteArray)), this, SLOT(call_from(QByteArray))); @@ -476,16 +478,71 @@ void AudioVideoCallController::capture_clipping(double pinnedPercent, double pea // Either the setting is off, or nothing on this platform would let us at // the control. The warning above is then all we can do - and it is still // worth having, since the distortion cannot be undone anywhere else. - if (!m_captureGain) + if (!m_captureGain || !m_gainCalibrating) + return; + + // Calibration is a thing that happens at the start of a call, not a gain + // rider. Once the window has passed the level is whatever it is: moving it + // mid-conversation would keep dragging the echo path out from under the + // canceller, which is worse than a bit of distortion. + if (m_callClock.elapsed() > kGainCalibrationMs) + { + closeGainCalibration("calibration window has passed"); + return; + } + + // The audio in this report may predate the last adjustment. + if (m_gainSettleReports > 0) + { + m_gainSettleReports--; return; + } - if (m_captureGainSteps >= kMaxCaptureGainSteps) + // Without a dB scale there is no safe way to know how far down we have + // come, so the step count has to be the bound - and a much tighter one, + // since six steps of 0.7 is over 50 dB on a perceptual scale. + const int stepLimit = m_haveGainDb ? kMaxCaptureGainSteps : kMaxBlindGainSteps; + if (m_captureGainSteps >= stepLimit) + { + closeGainCalibration("step limit reached"); return; + } + + // Did the previous adjustment achieve anything? Two that did not means the + // stage overloading is not the one we are holding. + if (m_captureGainSteps > 0 && pinnedPercent >= m_lastPinnedPercent) + { + m_gainIneffectiveSteps++; + if (m_gainIneffectiveSteps >= kMaxGainIneffectiveSteps) + { + closeGainCalibration("lowering this control is not reducing the clipping - " + "something ahead of it (an analog mic boost, or Windows' " + "separate Microphone Boost) is what is overloading"); + return; + } + } + else + { + m_gainIneffectiveSteps = 0; + } double current = 0.0; if (!m_captureGain->gain(¤t)) return; + // The bound that matters, and the one an earlier version of this got + // wrong: how far down we have come in decibels, not where the slider sits. + double currentDb = 0.0; + if (m_haveGainDb && m_captureGain->gainDb(¤tDb)) + { + if (m_gainStartDb - currentDb >= kMaxGainReductionDb) + { + closeGainCalibration(QString("already %1 dB down and still clipping") + .arg(m_gainStartDb - currentDb, 0, 'f', 1)); + return; + } + } + // Size the step to how badly it is clipping. A microphone left at maximum // is 20-30dB too hot and wants covering quickly; one that only tips over // on the loudest syllable wants nudging, not slamming. @@ -498,22 +555,61 @@ void AudioVideoCallController::capture_clipping(double pinnedPercent, double pea factor = 0.95; double next = current * factor; - if (next < kMinCaptureGain) - next = kMinCaptureGain; - if (next >= current) - return; // already at the floor, nothing left to give + { + closeGainCalibration("control is already at its minimum"); + return; + } if (!m_captureGain->setGain(next)) { - qWarning() << "Could not lower the capture gain - leaving it alone"; - m_captureGainSteps = kMaxCaptureGainSteps; + closeGainCalibration("the control would not move"); return; } m_captureGainSteps++; - qDebug("Capture gain %.0f%% -> %.0f%% (step %d of %d)", - current * 100.0, next * 100.0, m_captureGainSteps, kMaxCaptureGainSteps); + m_lastPinnedPercent = pinnedPercent; + m_gainSettleReports = kGainSettleReports; + + double achieved = 0.0; + double newDb = 0.0; + if (m_haveGainDb && m_captureGain->gainDb(&newDb)) + { + achieved = currentDb - newDb; + qDebug("Capture gain %.0f%% -> %.0f%% (%.1f dB, %.1f dB below the user's setting)", + current * 100.0, next * 100.0, -achieved, m_gainStartDb - newDb); + + // A step that moved the slider but not the level means we are pushing + // against the end of a control that no longer does anything. + if (achieved < kMinEffectiveStepDb) + m_gainIneffectiveSteps++; + + // Close on the step that crosses the ceiling rather than waiting for + // another report, which would let one more step through first. + if (m_gainStartDb - newDb >= kMaxGainReductionDb) + closeGainCalibration(QString("reached the %1 dB limit") + .arg(kMaxGainReductionDb, 0, 'f', 0)); + } + else + { + qDebug("Capture gain %.0f%% -> %.0f%% (step %d of %d)", + current * 100.0, next * 100.0, m_captureGainSteps, kMaxCaptureGainSteps); + } + + // The echo path just changed by however many dB that was, so everything + // the filter has learned about it is now wrong. Telling it to start over + // costs a second of convergence; not telling it costs the rest of the call. + if (m_echoCanceller) + m_echoCanceller->reset(); +} + +void AudioVideoCallController::closeGainCalibration(const QString &why) +{ + if (!m_gainCalibrating) + return; + + m_gainCalibrating = false; + qDebug() << "Microphone gain calibration finished:" << why; } void AudioVideoCallController::startCaptureGainControl(const QString &captureDevice) @@ -522,9 +618,15 @@ void AudioVideoCallController::startCaptureGainControl(const QString &captureDev m_savedCaptureGain = 0.0; m_haveSavedCaptureGain = false; m_captureGainSteps = 0; + m_gainCalibrating = false; + m_gainStartDb = 0.0; + m_haveGainDb = false; + m_gainIneffectiveSteps = 0; + m_lastPinnedPercent = 0.0; + m_gainSettleReports = 0; Settings st(config_dbname); - if (!st.loadValue("audio.auto_capture_gain", 1).toBool()) + if (!st.loadValue("audio.auto_capture_gain", 0).toBool()) return; // Whichever driver the capture side is about to open the device with - @@ -535,20 +637,29 @@ void AudioVideoCallController::startCaptureGainControl(const QString &captureDev if (!m_captureGain) return; - if (m_captureGain->gain(&m_savedCaptureGain)) - { - m_haveSavedCaptureGain = true; - qDebug() << "Automatic microphone gain using" << m_captureGain->description() - << "- currently at" << qRound(m_savedCaptureGain * 100.0) << "%"; - } - else + if (!m_captureGain->gain(&m_savedCaptureGain)) { // Without a reading there is nothing to put back afterwards, and // moving a control we cannot restore is not a trade worth making. qWarning() << "Could not read the capture gain - not taking it over"; delete m_captureGain; m_captureGain = nullptr; + return; } + + m_haveSavedCaptureGain = true; + m_haveGainDb = m_captureGain->gainDb(&m_gainStartDb); + m_gainCalibrating = true; + + if (m_haveGainDb) + qDebug("Microphone gain calibration open on %s - at %.0f%% (%.1f dB), will cut at most %.0f dB", + qPrintable(m_captureGain->description()), m_savedCaptureGain * 100.0, + m_gainStartDb, kMaxGainReductionDb); + else + qDebug("Microphone gain calibration open on %s - at %.0f%%, no dB scale so bounded " + "to %d steps", + qPrintable(m_captureGain->description()), m_savedCaptureGain * 100.0, + kMaxCaptureGainSteps); } void AudioVideoCallController::stopCaptureGainControl() @@ -572,6 +683,7 @@ void AudioVideoCallController::stopCaptureGainControl() m_captureGain = nullptr; m_haveSavedCaptureGain = false; m_captureGainSteps = 0; + m_gainCalibrating = false; } void AudioVideoCallController::incoming_audio_params(QByteArray peer_id, QString codec, quint16 sample_rate) |
