Make possible to activate adaptive AGC2 in the APM.

We update the configuration settings for AGC2. We also update their
effects. Now, 'gain_controller2.enable=true' means 'first run Adaptive
AGC2; then run AGC2 limiter'.

Previously, only the AGC2 limiter was implemented. To run that, one
had to set both 'gain_controller2.enable=true' and
'gain_controller2.enable_limiter=true'.

This setting also enables adaptive AGC2 in the test tool 'audioproc_f'.

Bug: webrtc:7494
Change-Id: I0d5dfe443f2cdc0ecf3aa4054442dab6276d284d
Reviewed-on: https://webrtc-review.googlesource.com/64990
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Commit-Queue: Alex Loiko <aleloi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22669}
diff --git a/modules/audio_processing/agc2/fixed_gain_controller.cc b/modules/audio_processing/agc2/fixed_gain_controller.cc
index a565613..ceaa10b 100644
--- a/modules/audio_processing/agc2/fixed_gain_controller.cc
+++ b/modules/audio_processing/agc2/fixed_gain_controller.cc
@@ -54,10 +54,6 @@
   gain_curve_applier_.SetSampleRate(sample_rate_hz);
 }
 
-void FixedGainController::EnableLimiter(bool enable_limiter) {
-  enable_limiter_ = enable_limiter;
-}
-
 void FixedGainController::Process(AudioFrameView<float> signal) {
   // Apply fixed digital gain; interpolate if necessary. One of the
   // planned usages of the FGC is to only use the limiter. In that
@@ -72,16 +68,13 @@
     }
   }
 
-  // Use the limiter (if configured to).
-  if (enable_limiter_) {
-    gain_curve_applier_.Process(signal);
+  // Use the limiter.
+  gain_curve_applier_.Process(signal);
 
-    // Dump data for debug.
-    const auto channel_view = signal.channel(0);
-    apm_data_dumper_->DumpRaw("agc2_fixed_digital_gain_curve_applier",
-                              channel_view.size(), channel_view.data());
-  }
-
+  // Dump data for debug.
+  const auto channel_view = signal.channel(0);
+  apm_data_dumper_->DumpRaw("agc2_fixed_digital_gain_curve_applier",
+                            channel_view.size(), channel_view.data());
   // Hard-clipping.
   for (size_t k = 0; k < signal.num_channels(); ++k) {
     rtc::ArrayView<float> channel_view = signal.channel(k);
diff --git a/modules/audio_processing/agc2/fixed_gain_controller.h b/modules/audio_processing/agc2/fixed_gain_controller.h
index fd80348..2b92cfc 100644
--- a/modules/audio_processing/agc2/fixed_gain_controller.h
+++ b/modules/audio_processing/agc2/fixed_gain_controller.h
@@ -27,13 +27,11 @@
   // with any other method call).
   void SetGain(float gain_to_apply_db);
   void SetSampleRate(size_t sample_rate_hz);
-  void EnableLimiter(bool enable_limiter);
 
  private:
   float gain_to_apply_ = 1.f;
   ApmDataDumper* apm_data_dumper_ = nullptr;
   GainCurveApplier gain_curve_applier_;
-  bool enable_limiter_ = true;
 };
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc b/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc
index 1d6c2ae..d688f6a 100644
--- a/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc
+++ b/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc
@@ -49,34 +49,20 @@
 
 std::unique_ptr<FixedGainController> CreateFixedGainController(
     float gain_to_apply,
-    size_t rate,
-    bool enable_limiter) {
+    size_t rate) {
   std::unique_ptr<FixedGainController> fgc =
       rtc::MakeUnique<FixedGainController>(&test_data_dumper);
   fgc->SetGain(gain_to_apply);
   fgc->SetSampleRate(rate);
-  fgc->EnableLimiter(enable_limiter);
   return fgc;
 }
 
 }  // namespace
 
-TEST(AutomaticGainController2FixedDigital, CreateUseWithoutLimiter) {
-  const int kSampleRate = 48000;
-  std::unique_ptr<FixedGainController> fixed_gc =
-      CreateFixedGainController(kGainToApplyDb, kSampleRate, false);
-  VectorFloatFrame vectors_with_float_frame(
-      1, rtc::CheckedDivExact(kSampleRate, 100), kInputLevelLinear);
-  auto float_frame = vectors_with_float_frame.float_frame_view();
-  fixed_gc->Process(float_frame);
-  const auto channel = float_frame.channel(0);
-  EXPECT_LT(kInputLevelLinear, channel[0]);
-}
-
-TEST(AutomaticGainController2FixedDigital, CreateUseWithLimiter) {
+TEST(AutomaticGainController2FixedDigital, CreateUse) {
   const int kSampleRate = 44000;
   std::unique_ptr<FixedGainController> fixed_gc =
-      CreateFixedGainController(kGainToApplyDb, kSampleRate, true);
+      CreateFixedGainController(kGainToApplyDb, kSampleRate);
   VectorFloatFrame vectors_with_float_frame(
       1, rtc::CheckedDivExact(kSampleRate, 100), kInputLevelLinear);
   auto float_frame = vectors_with_float_frame.float_frame_view();
@@ -96,7 +82,7 @@
     // Since |test::kLimiterMaxInputLevelDbFs| > |gain_db|, the
     // limiter will not saturate the signal.
     std::unique_ptr<FixedGainController> fixed_gc_no_saturation =
-        CreateFixedGainController(gain_db, kSampleRate, true);
+        CreateFixedGainController(gain_db, kSampleRate);
 
     // Saturation not expected.
     SCOPED_TRACE(std::to_string(gain_db));
@@ -112,7 +98,7 @@
     // Since |test::kLimiterMaxInputLevelDbFs| < |gain|, the limiter
     // will saturate the signal.
     std::unique_ptr<FixedGainController> fixed_gc_saturation =
-        CreateFixedGainController(gain_db, kSampleRate, true);
+        CreateFixedGainController(gain_db, kSampleRate);
 
     // Saturation expected.
     SCOPED_TRACE(std::to_string(gain_db));
@@ -135,7 +121,7 @@
     // Since |gain| > |test::kLimiterMaxInputLevelDbFs|, the limiter will
     // not saturate the signal.
     std::unique_ptr<FixedGainController> fixed_gc_no_saturation =
-        CreateFixedGainController(gain_db, kSampleRate, true);
+        CreateFixedGainController(gain_db, kSampleRate);
 
     // Saturation not expected.
     SCOPED_TRACE(std::to_string(gain_db));
@@ -151,7 +137,7 @@
     // Singe |gain| < |test::kLimiterMaxInputLevelDbFs|, the limiter will
     // saturate the signal.
     std::unique_ptr<FixedGainController> fixed_gc_saturation =
-        CreateFixedGainController(gain_db, kSampleRate, true);
+        CreateFixedGainController(gain_db, kSampleRate);
 
     // Saturation expected.
     SCOPED_TRACE(std::to_string(gain_db));
@@ -170,7 +156,7 @@
   constexpr float kGainDbFactor10 = 20.f;
 
   std::unique_ptr<FixedGainController> fixed_gc_no_saturation =
-      CreateFixedGainController(kGainDbNoChange, kSampleRate, false);
+      CreateFixedGainController(kGainDbNoChange, kSampleRate);
 
   // Signal level is unchanged with 0 db gain.
   EXPECT_FLOAT_EQ(
diff --git a/modules/audio_processing/gain_controller2.cc b/modules/audio_processing/gain_controller2.cc
index aa866c1..6c0149a 100644
--- a/modules/audio_processing/gain_controller2.cc
+++ b/modules/audio_processing/gain_controller2.cc
@@ -23,7 +23,8 @@
 GainController2::GainController2()
     : data_dumper_(
           new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
-      gain_controller_(data_dumper_.get()) {}
+      fixed_gain_controller_(data_dumper_.get()),
+      adaptive_agc_(data_dumper_.get()) {}
 
 GainController2::~GainController2() = default;
 
@@ -32,7 +33,7 @@
              sample_rate_hz == AudioProcessing::kSampleRate16kHz ||
              sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
              sample_rate_hz == AudioProcessing::kSampleRate48kHz);
-  gain_controller_.SetSampleRate(sample_rate_hz);
+  fixed_gain_controller_.SetSampleRate(sample_rate_hz);
   data_dumper_->InitiateNewSetOfRecordings();
   data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz);
 }
@@ -40,15 +41,15 @@
 void GainController2::Process(AudioBuffer* audio) {
   AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(),
                                     audio->num_frames());
-  gain_controller_.Process(float_frame);
+  adaptive_agc_.Process(float_frame);
+  fixed_gain_controller_.Process(float_frame);
 }
 
 void GainController2::ApplyConfig(
     const AudioProcessing::Config::GainController2& config) {
   RTC_DCHECK(Validate(config));
   config_ = config;
-  gain_controller_.SetGain(config_.fixed_gain_db);
-  gain_controller_.EnableLimiter(config_.enable_limiter);
+  fixed_gain_controller_.SetGain(config_.fixed_gain_db);
 }
 
 bool GainController2::Validate(
@@ -60,8 +61,7 @@
     const AudioProcessing::Config::GainController2& config) {
   std::stringstream ss;
   ss << "{enabled: " << (config.enabled ? "true" : "false") << ", "
-     << "fixed_gain_dB: " << config.fixed_gain_db << ", "
-     << "enable_limiter: " << (config.enable_limiter ? "true" : "false") << "}";
+     << "fixed_gain_dB: " << config.fixed_gain_db << "}";
   return ss.str();
 }
 
diff --git a/modules/audio_processing/gain_controller2.h b/modules/audio_processing/gain_controller2.h
index 6de4564..e2ca05e 100644
--- a/modules/audio_processing/gain_controller2.h
+++ b/modules/audio_processing/gain_controller2.h
@@ -14,6 +14,7 @@
 #include <memory>
 #include <string>
 
+#include "modules/audio_processing/agc2/adaptive_agc.h"
 #include "modules/audio_processing/agc2/fixed_gain_controller.h"
 #include "modules/audio_processing/include/audio_processing.h"
 #include "rtc_base/constructormagic.h"
@@ -41,8 +42,9 @@
  private:
   static int instance_count_;
   std::unique_ptr<ApmDataDumper> data_dumper_;
-  FixedGainController gain_controller_;
+  FixedGainController fixed_gain_controller_;
   AudioProcessing::Config::GainController2 config_;
+  AdaptiveAgc adaptive_agc_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(GainController2);
 };
diff --git a/modules/audio_processing/gain_controller2_unittest.cc b/modules/audio_processing/gain_controller2_unittest.cc
index 0c3b383..5b5bd03 100644
--- a/modules/audio_processing/gain_controller2_unittest.cc
+++ b/modules/audio_processing/gain_controller2_unittest.cc
@@ -61,12 +61,11 @@
   config.fixed_gain_db = 5.f;
 
   config.enabled = false;
-  config.enable_limiter = true;
-  EXPECT_EQ("{enabled: false, fixed_gain_dB: 5, enable_limiter: true}",
+  EXPECT_EQ("{enabled: false, fixed_gain_dB: 5}",
             GainController2::ToString(config));
 
   config.enabled = true;
-  EXPECT_EQ("{enabled: true, fixed_gain_dB: 5, enable_limiter: true}",
+  EXPECT_EQ("{enabled: true, fixed_gain_dB: 5}",
             GainController2::ToString(config));
 }
 
diff --git a/modules/audio_processing/include/audio_processing.h b/modules/audio_processing/include/audio_processing.h
index d31174a..05eef5e 100644
--- a/modules/audio_processing/include/audio_processing.h
+++ b/modules/audio_processing/include/audio_processing.h
@@ -270,14 +270,13 @@
       bool enabled = false;
     } high_pass_filter;
 
-    // Enables the next generation AGC functionality. This feature replaces the
-    // standard methods of gain control in the previous AGC.
-    // The functionality is not yet activated in the code and turning this on
-    // does not yet have the desired behavior.
+    // Enables the next generation AGC functionality. This feature
+    // replaces the standard methods of gain control in the previous
+    // AGC. This functionality is currently only partially
+    // implemented.
     struct GainController2 {
       bool enabled = false;
       float fixed_gain_db = 0.f;
-      bool enable_limiter = true;
     } gain_controller2;
 
     // Explicit copy assignment implementation to avoid issues with memory