From 270286b38c455f5330a85fd65ff62f613e7dfdb7 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Mon, 21 Mar 2022 18:58:34 -0700 Subject: [PATCH] refactor(audio): Move getVolume and volumeAvailable to qreal from float Reduces casting back and forth between types. --- audio/include/audio/iaudiosource.h | 2 +- audio/src/backend/openal.cpp | 18 +++++++++--------- audio/src/backend/openal.h | 2 +- src/widget/form/settings/avform.cpp | 4 ++-- src/widget/form/settings/avform.h | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/audio/include/audio/iaudiosource.h b/audio/include/audio/iaudiosource.h index c62f8628b..152bc9546 100644 --- a/audio/include/audio/iaudiosource.h +++ b/audio/include/audio/iaudiosource.h @@ -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(); }; diff --git a/audio/src/backend/openal.cpp b/audio/src/backend/openal.cpp index f17f15e46..13b4f1b88 100644 --- a/audio/src/backend/openal.cpp +++ b/audio/src/backend/openal.cpp @@ -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(inputBuffer[i]) / std::numeric_limits::max(); + auto sample = static_cast(inputBuffer[i]) / std::numeric_limits::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(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(strlen(pDeviceList)); + auto len = strlen(pDeviceList); list << QString::fromUtf8(pDeviceList, len); pDeviceList += len + 1; } diff --git a/audio/src/backend/openal.h b/audio/src/backend/openal.h index 772b00c2f..e60fc38ea 100644 --- a/audio/src/backend/openal.h +++ b/audio/src/backend/openal.h @@ -127,7 +127,7 @@ private: void cleanupBuffers(uint sourceId); void cleanupSound(); - float getVolume(); + qreal getVolume(); protected: IAudioSettings& settings; diff --git a/src/widget/form/settings/avform.cpp b/src/widget/form/settings/avform.cpp index a4cfa2f5a..c1cae733e 100644 --- a/src/widget/form/settings/avform.cpp +++ b/src/widget/form/settings/avform.cpp @@ -171,9 +171,9 @@ void AVForm::rescanDevices() getVideoDevices(); } -void AVForm::setVolume(float value) +void AVForm::setVolume(qreal value) { - volumeDisplay->setValue(getStepsFromValue(static_cast(value), audio.minOutputVolume(), audio.maxOutputVolume())); + volumeDisplay->setValue(getStepsFromValue(value, audio.minOutputVolume(), audio.maxOutputVolume())); } void AVForm::on_videoModescomboBox_currentIndexChanged(int index) diff --git a/src/widget/form/settings/avform.h b/src/widget/form/settings/avform.h index 52b03150c..3e4d9971e 100644 --- a/src/widget/form/settings/avform.h +++ b/src/widget/form/settings/avform.h @@ -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);