Browse Source

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

Reduces casting back and forth between types.
reviewable/pr6581/r3
Anthony Bilinski 3 years ago
parent
commit
270286b38c
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 2
      audio/include/audio/iaudiosource.h
  2. 18
      audio/src/backend/openal.cpp
  3. 2
      audio/src/backend/openal.h
  4. 4
      src/widget/form/settings/avform.cpp
  5. 2
      src/widget/form/settings/avform.h

2
audio/include/audio/iaudiosource.h

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

18
audio/src/backend/openal.cpp

@ -588,19 +588,19 @@ void OpenAL::cleanupOutput()
* *
* @return normalized volume between 0-1 * @return normalized volume between 0-1
*/ */
float OpenAL::getVolume() qreal OpenAL::getVolume()
{ {
const quint32 samples = AUDIO_FRAME_SAMPLE_COUNT_TOTAL; 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 // 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++) { 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); 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 // 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; return normalizedVolume;
} }
@ -627,7 +627,7 @@ void OpenAL::doInput()
applyGain(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT_TOTAL, gainFactor); applyGain(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT_TOTAL, gainFactor);
float volume = getVolume(); auto volume = getVolume();
if (volume >= inputThreshold) { if (volume >= inputThreshold) {
isActive = true; isActive = true;
emit startActive(voiceHold); emit startActive(voiceHold);
@ -693,7 +693,7 @@ QStringList OpenAL::outDeviceNames()
if (pDeviceList) { if (pDeviceList) {
while (*pDeviceList) { while (*pDeviceList) {
int len = static_cast<int>(strlen(pDeviceList)); auto len = strlen(pDeviceList);
list << QString::fromUtf8(pDeviceList, len); list << QString::fromUtf8(pDeviceList, len);
pDeviceList += len + 1; pDeviceList += len + 1;
} }
@ -709,7 +709,7 @@ QStringList OpenAL::inDeviceNames()
if (pDeviceList) { if (pDeviceList) {
while (*pDeviceList) { while (*pDeviceList) {
int len = static_cast<int>(strlen(pDeviceList)); auto len = strlen(pDeviceList);
list << QString::fromUtf8(pDeviceList, len); list << QString::fromUtf8(pDeviceList, len);
pDeviceList += len + 1; pDeviceList += len + 1;
} }

2
audio/src/backend/openal.h

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

4
src/widget/form/settings/avform.cpp

@ -171,9 +171,9 @@ void AVForm::rescanDevices()
getVideoDevices(); 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) void AVForm::on_videoModescomboBox_currentIndexChanged(int index)

2
src/widget/form/settings/avform.h

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

Loading…
Cancel
Save