diff --git a/src/blackcore/afv/audio/input.cpp b/src/blackcore/afv/audio/input.cpp index a10a36d63..92a47300d 100644 --- a/src/blackcore/afv/audio/input.cpp +++ b/src/blackcore/afv/audio/input.cpp @@ -79,10 +79,10 @@ namespace BlackCore m_encoder.setBitRate(16 * 1024); } - bool CInput::setVolume(double volume) + bool CInput::setGainRatio(double gainRatio) { - if (qFuzzyCompare(m_volume, volume)) { return false; } - m_volume = volume; + if (qFuzzyCompare(m_gainRatio, gainRatio)) { return false; } + m_gainRatio = gainRatio; return true; } @@ -176,9 +176,10 @@ namespace BlackCore samples = convertFromStereoToMono(samples); } + const double volume = m_gainRatio; for (qint16 &sample : samples) { - int value = qRound(sample * m_volume); + int value = qRound(sample * volume); if (value > std::numeric_limits::max()) value = std::numeric_limits::max(); if (value < std::numeric_limits::min()) value = std::numeric_limits::min(); sample = static_cast(value); diff --git a/src/blackcore/afv/audio/input.h b/src/blackcore/afv/audio/input.h index a8c96ddd7..82b65f354 100644 --- a/src/blackcore/afv/audio/input.h +++ b/src/blackcore/afv/audio/input.h @@ -101,10 +101,16 @@ namespace BlackCore void setOpusBytesEncoded(int opusBytesEncoded) { m_opusBytesEncoded = opusBytesEncoded; } //! @} - //! Volume 0..1 @{ - double volume() const { return m_volume; } - bool setVolume(double volume); + //! Gain ratio, value a amplitude need to be multiplied with + //! \see http://www.sengpielaudio.com/calculator-amplification.htm + //! \remark gain ratio is voltage ratio/or amplitude ratio, something between 0.001-7.95 for -60dB to 80dB + //! @{ + double getGainRatio() const { return m_gainRatio; } + bool setGainRatio(double gainRatio); //! @} + // those used to be the original function names + // double volume() const { return m_volume; } + // bool setVolume(double volume); /* disabled as not needed //! The device's volume 0..1 @{ @@ -146,7 +152,7 @@ namespace BlackCore bool m_started = false; int m_opusBytesEncoded = 0; int m_sampleCount = 0; - double m_volume = 1.0; + double m_gainRatio = 1.0; qint16 m_maxSampleInput = 0.0; const int SampleCountPerEvent = 4800; diff --git a/src/blackcore/afv/clients/afvclient.cpp b/src/blackcore/afv/clients/afvclient.cpp index b94c64d08..5be2fe5e7 100644 --- a/src/blackcore/afv/clients/afvclient.cpp +++ b/src/blackcore/afv/clients/afvclient.cpp @@ -290,7 +290,7 @@ namespace BlackCore this->initTransceivers(); // threadsafe block - const double outputVolume = this->getOutputVolume(); + const double outputVolume = this->getOutputGainRatio(); { // lock block 1 { @@ -305,7 +305,7 @@ namespace BlackCore if (m_outputSampleProvider) { m_outputSampleProvider->deleteLater(); } m_outputSampleProvider = new CVolumeSampleProvider(m_soundcardSampleProvider, this); - m_outputSampleProvider->setVolume(outputVolume); + m_outputSampleProvider->setGainRatio(outputVolume); } // lock block 2 @@ -726,7 +726,8 @@ namespace BlackCore m_inputVolumeDb = valueDb; if (m_input) { - changed = m_input->setVolume(qPow(10, valueDb / 20.0)); + const double gainRatio = qPow(10, valueDb / 20.0); + changed = m_input->setGainRatio(gainRatio); } } return changed; @@ -738,10 +739,10 @@ namespace BlackCore return m_outputVolumeDb; } - double CAfvClient::getOutputVolume() const + double CAfvClient::getOutputGainRatio() const { QMutexLocker lock(&m_mutexVolume); - return m_outputVolume; + return m_outputGainRatio; } int CAfvClient::getNormalizedInputVolume() const @@ -773,6 +774,8 @@ namespace BlackCore else if (volume > 100) { volume = 100; } const double range = MaxDbIn - MinDbIn; const double dB = MinDbIn + (volume * range / 100.0); + + // converted to MinDbIn-MaxDbIn return this->setInputVolumeDb(dB); } @@ -794,6 +797,8 @@ namespace BlackCore range = qAbs(MinDbOut); } dB += (volume * range / 50.0); + + // converted to MinDbOut-MaxDbOut this->setOutputVolumeDb(dB); } @@ -1291,17 +1296,16 @@ namespace BlackCore if (valueDb > MaxDbOut) { valueDb = MaxDbOut; } else if (valueDb < MinDbOut) { valueDb = MinDbOut; } - double outputVolume = 0; + const double gainRatio = qPow(10, valueDb / 20.0); bool changed = false; { QMutexLocker lock(&m_mutexVolume); changed = !qFuzzyCompare(m_outputVolumeDb, valueDb); if (changed) { - m_outputVolumeDb = valueDb; - m_outputVolume = qPow(10, m_outputVolumeDb / 20.0); + m_outputVolumeDb = valueDb; + m_outputGainRatio = gainRatio; } - outputVolume = m_outputVolume; // outside changed if needed for m_outputSampleProvider !! } // do NOT check on "changed", can be false, but "m_outputSampleProvider" is initialized @@ -1314,7 +1318,7 @@ namespace BlackCore if (m_outputSampleProvider) { - changed = m_outputSampleProvider->setVolume(outputVolume); + changed = m_outputSampleProvider->setGainRatio(gainRatio); } m_mutexSampleProviders.unlock(); return changed; diff --git a/src/blackcore/afv/clients/afvclient.h b/src/blackcore/afv/clients/afvclient.h index dff2a3ac6..8097ef3f0 100644 --- a/src/blackcore/afv/clients/afvclient.h +++ b/src/blackcore/afv/clients/afvclient.h @@ -223,21 +223,24 @@ namespace BlackCore Q_INVOKABLE bool isLoopback() const { return m_loopbackOn; } //! @} - //! Input volume in dB, +-18dB + //! Input volume in dB, [MinDbIn, MaxDbIn]dB //! \threadsafe //! @{ double getInputVolumeDb() const; Q_INVOKABLE bool setInputVolumeDb(double valueDb); //! @} - //! Output volume in dB, +-18dB + //! Output volume in dB, [MinDbOut, MaxDbOut]dB //! \threadsafe //! @{ double getOutputVolumeDb() const; - double getOutputVolume() const; Q_INVOKABLE bool setOutputVolumeDb(double valueDb); //! @} + //! Gain ratio + //! \threadsafe + double getOutputGainRatio() const; + //! Normalized volumes 0..100 //! \threadsafe //! @{ @@ -381,9 +384,9 @@ namespace BlackCore QDateTime m_startDateTimeUtc; - double m_inputVolumeDb = 0.0; - double m_outputVolumeDb = 0.0; - double m_outputVolume = 1.0; + double m_inputVolumeDb = 0.0; + double m_outputVolumeDb = 0.0; + double m_outputGainRatio = 1.0; //!< 0dB double m_maxDbReadingInPTTInterval = -100; QTimer *m_voiceServerTimer = nullptr; diff --git a/src/blacksound/sampleprovider/volumesampleprovider.cpp b/src/blacksound/sampleprovider/volumesampleprovider.cpp index c8fc59778..2730758b0 100644 --- a/src/blacksound/sampleprovider/volumesampleprovider.cpp +++ b/src/blacksound/sampleprovider/volumesampleprovider.cpp @@ -29,20 +29,20 @@ namespace BlackSound int CVolumeSampleProvider::readSamples(QVector &samples, qint64 count) { const int samplesRead = m_sourceProvider->readSamples(samples, count); - if (!qFuzzyCompare(m_volume, 1.0)) + if (!qFuzzyCompare(m_gainRatio, 1.0)) { for (int n = 0; n < samplesRead; n++) { - samples[n] = static_cast(m_volume * samples[n]); + samples[n] = static_cast(m_gainRatio * samples[n]); } } return samplesRead; } - bool CVolumeSampleProvider::setVolume(double volume) + bool CVolumeSampleProvider::setGainRatio(double volume) { - const bool changed = !qFuzzyCompare(m_volume, volume); - if (changed) { m_volume = volume; } + const bool changed = !qFuzzyCompare(m_gainRatio, volume); + if (changed) { m_gainRatio = volume; } return changed; } } // ns diff --git a/src/blacksound/sampleprovider/volumesampleprovider.h b/src/blacksound/sampleprovider/volumesampleprovider.h index f1581d441..8742c3c06 100644 --- a/src/blacksound/sampleprovider/volumesampleprovider.h +++ b/src/blacksound/sampleprovider/volumesampleprovider.h @@ -30,14 +30,20 @@ namespace BlackSound //! \copydoc ISampleProvider::readSamples virtual int readSamples(QVector &samples, qint64 count) override; - //! Volume @{ - double volume() const { return m_volume; } - bool setVolume(double volume); + //! Gain ratio, value a amplitude need to be multiplied with + //! \see http://www.sengpielaudio.com/calculator-amplification.htm + //! \remark gain ratio is voltage ratio/or amplitude ratio, something between 0.001-7.95 for -60dB to 80dB + //! @{ + double getGainRatio() const { return m_gainRatio; } + bool setGainRatio(double gainRatio); //! @} + // those used to be the original function names + // double volume() const { return m_volume; } + // bool setVolume(double volume); private: ISampleProvider *m_sourceProvider = nullptr; - double m_volume = 1.0; + double m_gainRatio = 1.0; }; } // ns } // ns