Add support for InputVolumeController in GainController2

Add InputVolumeController as a member in GainController2 (not created
by default). Add a method GainController2::Analyze() to update the
applied input volume and run the pre-processing steps in
InputVolumeController. Add a call InputVolumeController::Process() in
GainController2::Process().

Bug: webrtc:7494
Change-Id: Idf4111ac5e19a620b6421c7f23fd642f169c7b5a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/279822
Reviewed-by: Per Åhgren <peah@webrtc.org>
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Commit-Queue: Hanna Silen <silen@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38548}
This commit is contained in:
Hanna Silen
2022-11-02 19:12:20 +01:00
committed by WebRTC LUCI CQ
parent 01c2c325bd
commit d7cfbe3843
5 changed files with 146 additions and 2 deletions

View File

@ -22,12 +22,16 @@
#include "modules/audio_processing/test/audio_buffer_tools.h"
#include "modules/audio_processing/test/bitexactness_tools.h"
#include "rtc_base/checks.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace webrtc {
namespace test {
namespace {
using ::testing::Eq;
using ::testing::Optional;
using Agc2Config = AudioProcessing::Config::GainController2;
// Sets all the samples in `ab` to `value`.
@ -40,13 +44,20 @@ void SetAudioBufferSamples(float value, AudioBuffer& ab) {
float RunAgc2WithConstantInput(GainController2& agc2,
float input_level,
int num_frames,
int sample_rate_hz) {
int sample_rate_hz,
int num_channels = 1,
int applied_initial_volume = 0) {
const int num_samples = rtc::CheckedDivExact(sample_rate_hz, 100);
AudioBuffer ab(sample_rate_hz, 1, sample_rate_hz, 1, sample_rate_hz, 1);
AudioBuffer ab(sample_rate_hz, num_channels, sample_rate_hz, num_channels,
sample_rate_hz, num_channels);
// Give time to the level estimator to converge.
for (int i = 0; i < num_frames + 1; ++i) {
SetAudioBufferSamples(input_level, ab);
const auto applied_volume = agc2.GetRecommendedInputVolume();
agc2.Analyze(i > 0 && applied_volume.has_value() ? *applied_volume
: applied_initial_volume,
ab);
agc2.Process(/*speech_probability=*/absl::nullopt,
/*input_volume_changed=*/false, &ab);
}
@ -137,6 +148,66 @@ TEST(GainController2, CheckAdaptiveDigitalMaxOutputNoiseLevelConfig) {
EXPECT_TRUE(GainController2::Validate(config));
}
TEST(GainController2,
CheckGetRecommendedInputVolumeWhenInputVolumeControllerNotEnabled) {
constexpr float kHighInputLevel = 32767.0f;
constexpr float kLowInputLevel = 1000.0f;
constexpr int kInitialInputVolume = 100;
constexpr int kNumChannels = 2;
constexpr int kNumFrames = 5;
constexpr int kSampleRateHz = 16000;
Agc2Config config;
config.input_volume_controller.enabled = false;
auto gain_controller =
std::make_unique<GainController2>(config, kSampleRateHz, kNumChannels,
/*use_internal_vad=*/true);
EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value());
// Run AGC for a signal with no clipping or detected speech.
RunAgc2WithConstantInput(*gain_controller, kLowInputLevel, kNumFrames,
kSampleRateHz, kNumChannels, kInitialInputVolume);
EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value());
// Run AGC for a signal with clipping.
RunAgc2WithConstantInput(*gain_controller, kHighInputLevel, kNumFrames,
kSampleRateHz, kNumChannels, kInitialInputVolume);
EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value());
}
TEST(GainController2,
CheckGetRecommendedInputVolumeWhenInputVolumeControllerEnabled) {
constexpr float kHighInputLevel = 32767.0f;
constexpr float kLowInputLevel = 1000.0f;
constexpr int kInitialInputVolume = 100;
constexpr int kNumChannels = 2;
constexpr int kNumFrames = 5;
constexpr int kSampleRateHz = 16000;
Agc2Config config;
config.input_volume_controller.enabled = true;
auto gain_controller =
std::make_unique<GainController2>(config, kSampleRateHz, kNumChannels,
/*use_internal_vad=*/true);
EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value());
// Run AGC for a signal with no clipping or detected speech.
RunAgc2WithConstantInput(*gain_controller, kLowInputLevel, kNumFrames,
kSampleRateHz, kNumChannels, kInitialInputVolume);
EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value());
// Run AGC for a signal with clipping.
RunAgc2WithConstantInput(*gain_controller, kHighInputLevel, kNumFrames,
kSampleRateHz, kNumChannels, kInitialInputVolume);
EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value());
}
// Checks that the default config is applied.
TEST(GainController2, ApplyDefaultConfig) {
auto gain_controller2 = std::make_unique<GainController2>(