summaryrefslogtreecommitdiff
path: root/indra/llwebrtc
diff options
context:
space:
mode:
authorRoxie Linden <roxie@lindenlab.com>2023-09-22 16:55:13 -0700
committerRoxie Linden <roxie@lindenlab.com>2024-02-08 18:34:01 -0800
commit78228d4dc423478c153aeeab0d92acf9ce5de78b (patch)
tree9af60e659f5c99da946e0e060ed4236b68cda000 /indra/llwebrtc
parent2ad058aa38485ffc48910b3bfe9679bdd9d692b3 (diff)
Smooth voice power level reporting.
Diffstat (limited to 'indra/llwebrtc')
-rw-r--r--indra/llwebrtc/llwebrtc.cpp21
-rw-r--r--indra/llwebrtc/llwebrtc_impl.h5
2 files changed, 21 insertions, 5 deletions
diff --git a/indra/llwebrtc/llwebrtc.cpp b/indra/llwebrtc/llwebrtc.cpp
index a3dadd696c..7b5ca7fb16 100644
--- a/indra/llwebrtc/llwebrtc.cpp
+++ b/indra/llwebrtc/llwebrtc.cpp
@@ -39,6 +39,7 @@ namespace llwebrtc
const float VOLUME_SCALE_WEBRTC = 3.0f;
+LLAudioDeviceObserver::LLAudioDeviceObserver() : mMicrophoneEnergy(0.0), mSumVector {0} {}
double LLAudioDeviceObserver::getMicrophoneEnergy() { return mMicrophoneEnergy; }
@@ -48,14 +49,26 @@ void LLAudioDeviceObserver::OnCaptureData(const void *audio_samples,
const size_t num_channels,
const uint32_t samples_per_sec)
{
- double energy = 0;
+ float energy = 0;
const short *samples = (const short *) audio_samples;
for (size_t index = 0; index < num_samples * num_channels; index++)
{
- double sample = (static_cast<double>(samples[index]) / (double) 32768);
+ float sample = (static_cast<float>(samples[index]) / (float) 32768);
energy += sample * sample;
}
- mMicrophoneEnergy = std::sqrt(energy);
+
+ // smooth it.
+ size_t buffer_size = sizeof(mSumVector) / sizeof(mSumVector[0]);
+ float totalSum = 0;
+ int i;
+ for (i = 0; i < (buffer_size - 1); i++)
+ {
+ mSumVector[i] = mSumVector[i + 1];
+ totalSum += mSumVector[i];
+ }
+ mSumVector[i] = energy;
+ totalSum += energy;
+ mMicrophoneEnergy = std::sqrt(totalSum / (num_samples * buffer_size));
}
void LLAudioDeviceObserver::OnRenderData(const void *audio_samples,
@@ -479,7 +492,7 @@ void LLWebRTCImpl::setSpeakerVolume(float volume)
double LLWebRTCImpl::getAudioLevel()
{
- return mAudioDeviceObserver->getMicrophoneEnergy();
+ return 20*mAudioDeviceObserver->getMicrophoneEnergy();
}
//
diff --git a/indra/llwebrtc/llwebrtc_impl.h b/indra/llwebrtc/llwebrtc_impl.h
index da829e52af..70586f9013 100644
--- a/indra/llwebrtc/llwebrtc_impl.h
+++ b/indra/llwebrtc/llwebrtc_impl.h
@@ -66,6 +66,8 @@ namespace llwebrtc
class LLAudioDeviceObserver : public webrtc::AudioDeviceDataObserver
{
public:
+ LLAudioDeviceObserver();
+
double getMicrophoneEnergy();
void OnCaptureData(const void *audio_samples,
@@ -81,7 +83,8 @@ class LLAudioDeviceObserver : public webrtc::AudioDeviceDataObserver
const uint32_t samples_per_sec) override;
protected:
- double mMicrophoneEnergy;
+ float mSumVector[30]; // 300 ms of smoothing
+ float mMicrophoneEnergy;
};
class LLWebRTCImpl : public LLWebRTCDeviceInterface,