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

@ -61,6 +61,17 @@ std::unique_ptr<AdaptiveDigitalGainController> CreateAdaptiveDigitalController(
return nullptr;
}
// Creates an input volume controller if `enabled` is true.
std::unique_ptr<InputVolumeController> CreateInputVolumeController(
bool enabled,
int num_channels) {
if (enabled) {
return std::make_unique<InputVolumeController>(
num_channels, InputVolumeController::Config{.enabled = enabled});
}
return nullptr;
}
} // namespace
std::atomic<int> GainController2::instance_count_(0);
@ -79,6 +90,9 @@ GainController2::GainController2(const Agc2Config& config,
sample_rate_hz,
num_channels,
&data_dumper_)),
input_volume_controller_(
CreateInputVolumeController(config.input_volume_controller.enabled,
num_channels)),
limiter_(sample_rate_hz, &data_dumper_, /*histogram_name_prefix=*/"Agc2"),
calls_since_last_limiter_log_(0) {
RTC_DCHECK(Validate(config));
@ -91,10 +105,21 @@ GainController2::GainController2(const Agc2Config& config,
config.adaptive_digital.vad_reset_period_ms, cpu_features_,
sample_rate_hz);
}
if (input_volume_controller_) {
input_volume_controller_->Initialize();
}
}
GainController2::~GainController2() = default;
// TODO(webrtc:7494): Pass the flag also to the other components.
void GainController2::SetCaptureOutputUsed(bool capture_output_used) {
if (input_volume_controller_) {
input_volume_controller_->HandleCaptureOutputUsedChange(
capture_output_used);
}
}
void GainController2::SetFixedGainDb(float gain_db) {
const float gain_factor = DbToRatio(gain_db);
if (fixed_gain_applier_.GetGainFactor() != gain_factor) {
@ -105,6 +130,24 @@ void GainController2::SetFixedGainDb(float gain_db) {
fixed_gain_applier_.SetGainFactor(gain_factor);
}
void GainController2::Analyze(int applied_input_volume,
const AudioBuffer& audio_buffer) {
RTC_DCHECK_GE(applied_input_volume, 0);
RTC_DCHECK_LE(applied_input_volume, 255);
if (input_volume_controller_) {
input_volume_controller_->set_stream_analog_level(applied_input_volume);
input_volume_controller_->AnalyzePreProcess(audio_buffer);
}
}
absl::optional<int> GainController2::GetRecommendedInputVolume() const {
return input_volume_controller_
? absl::optional<int>(
input_volume_controller_->recommended_analog_level())
: absl::nullopt;
}
void GainController2::Process(absl::optional<float> speech_probability,
bool input_volume_changed,
AudioBuffer* audio) {
@ -125,6 +168,16 @@ void GainController2::Process(absl::optional<float> speech_probability,
if (speech_probability.has_value()) {
data_dumper_.DumpRaw("agc2_speech_probability", speech_probability.value());
}
if (input_volume_controller_) {
absl::optional<float> speech_level;
if (adaptive_digital_controller_) {
speech_level =
adaptive_digital_controller_->GetSpeechLevelDbfsIfConfident();
}
input_volume_controller_->Process(speech_probability, speech_level);
}
fixed_gain_applier_.ApplyGain(float_frame);
if (adaptive_digital_controller_) {
RTC_DCHECK(speech_probability.has_value());