[AFV] Renamed to "setGainRatio" as the value does not represent a volume, but a multiplier

This commit is contained in:
Klaus Basan
2020-04-24 00:49:48 +02:00
committed by Mat Sutcliffe
parent 8848faadfa
commit f2e0e7b22e
6 changed files with 53 additions and 33 deletions

View File

@@ -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<qint16>::max()) value = std::numeric_limits<qint16>::max();
if (value < std::numeric_limits<qint16>::min()) value = std::numeric_limits<qint16>::min();
sample = static_cast<qint16>(value);

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -29,20 +29,20 @@ namespace BlackSound
int CVolumeSampleProvider::readSamples(QVector<float> &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<float>(m_volume * samples[n]);
samples[n] = static_cast<float>(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

View File

@@ -30,14 +30,20 @@ namespace BlackSound
//! \copydoc ISampleProvider::readSamples
virtual int readSamples(QVector<float> &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