1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(audio): Move getVolume and volumeAvailable to qreal from float

Reduces casting back and forth between types.
This commit is contained in:
Anthony Bilinski 2022-03-21 18:58:34 -07:00
parent 8337b935f9
commit 270286b38c
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
5 changed files with 14 additions and 14 deletions

View File

@ -40,6 +40,6 @@ public:
signals:
void frameAvailable(const int16_t* pcm, size_t sample_count, uint8_t channels,
uint32_t sampling_rate);
void volumeAvailable(float value);
void volumeAvailable(qreal value);
void invalidated();
};

View File

@ -588,19 +588,19 @@ void OpenAL::cleanupOutput()
*
* @return normalized volume between 0-1
*/
float OpenAL::getVolume()
qreal OpenAL::getVolume()
{
const quint32 samples = AUDIO_FRAME_SAMPLE_COUNT_TOTAL;
const float rootTwo = 1.414213562; // sqrt(2), but sqrt is not constexpr
const qreal rootTwo = 1.414213562; // sqrt(2), but sqrt is not constexpr
// calculate volume as the root mean squared of amplitudes in the sample
float sumOfSquares = 0;
qreal sumOfSquares = 0;
for (quint32 i = 0; i < samples; i++) {
float sample = static_cast<float>(inputBuffer[i]) / std::numeric_limits<int16_t>::max();
auto sample = static_cast<qreal>(inputBuffer[i]) / std::numeric_limits<int16_t>::max();
sumOfSquares += std::pow(sample, 2);
}
const float rms = std::sqrt(sumOfSquares / samples);
const qreal rms = std::sqrt(sumOfSquares / samples);
// our calculated normalized volume could possibly be above 1 because our RMS assumes a sinusoidal wave
const float normalizedVolume = std::min(rms * rootTwo, 1.0f);
const qreal normalizedVolume = std::min(rms * rootTwo, 1.0);
return normalizedVolume;
}
@ -627,7 +627,7 @@ void OpenAL::doInput()
applyGain(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT_TOTAL, gainFactor);
float volume = getVolume();
auto volume = getVolume();
if (volume >= inputThreshold) {
isActive = true;
emit startActive(voiceHold);
@ -693,7 +693,7 @@ QStringList OpenAL::outDeviceNames()
if (pDeviceList) {
while (*pDeviceList) {
int len = static_cast<int>(strlen(pDeviceList));
auto len = strlen(pDeviceList);
list << QString::fromUtf8(pDeviceList, len);
pDeviceList += len + 1;
}
@ -709,7 +709,7 @@ QStringList OpenAL::inDeviceNames()
if (pDeviceList) {
while (*pDeviceList) {
int len = static_cast<int>(strlen(pDeviceList));
auto len = strlen(pDeviceList);
list << QString::fromUtf8(pDeviceList, len);
pDeviceList += len + 1;
}

View File

@ -127,7 +127,7 @@ private:
void cleanupBuffers(uint sourceId);
void cleanupSound();
float getVolume();
qreal getVolume();
protected:
IAudioSettings& settings;

View File

@ -171,9 +171,9 @@ void AVForm::rescanDevices()
getVideoDevices();
}
void AVForm::setVolume(float value)
void AVForm::setVolume(qreal value)
{
volumeDisplay->setValue(getStepsFromValue(static_cast<qreal>(value), audio.minOutputVolume(), audio.maxOutputVolume()));
volumeDisplay->setValue(getStepsFromValue(value, audio.minOutputVolume(), audio.maxOutputVolume()));
}
void AVForm::on_videoModescomboBox_currentIndexChanged(int index)

View File

@ -82,7 +82,7 @@ private slots:
void on_videoModescomboBox_currentIndexChanged(int index);
void rescanDevices();
void setVolume(float value);
void setVolume(qreal value);
protected:
void updateVideoModes(int curIndex);