xref: /aosp_15_r20/external/webrtc/modules/audio_processing/gain_controller2.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_
12*d9f75844SAndroid Build Coastguard Worker #define MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <atomic>
15*d9f75844SAndroid Build Coastguard Worker #include <memory>
16*d9f75844SAndroid Build Coastguard Worker #include <string>
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc2/adaptive_digital_gain_controller.h"
19*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc2/cpu_features.h"
20*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc2/gain_applier.h"
21*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc2/input_volume_controller.h"
22*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc2/limiter.h"
23*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc2/vad_wrapper.h"
24*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/include/audio_processing.h"
25*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/logging/apm_data_dumper.h"
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
28*d9f75844SAndroid Build Coastguard Worker 
29*d9f75844SAndroid Build Coastguard Worker class AudioBuffer;
30*d9f75844SAndroid Build Coastguard Worker 
31*d9f75844SAndroid Build Coastguard Worker // Gain Controller 2 aims to automatically adjust levels by acting on the
32*d9f75844SAndroid Build Coastguard Worker // microphone gain and/or applying digital gain.
33*d9f75844SAndroid Build Coastguard Worker class GainController2 {
34*d9f75844SAndroid Build Coastguard Worker  public:
35*d9f75844SAndroid Build Coastguard Worker   // Ctor. If `use_internal_vad` is true, an internal voice activity
36*d9f75844SAndroid Build Coastguard Worker   // detector is used for digital adaptive gain.
37*d9f75844SAndroid Build Coastguard Worker   GainController2(
38*d9f75844SAndroid Build Coastguard Worker       const AudioProcessing::Config::GainController2& config,
39*d9f75844SAndroid Build Coastguard Worker       const InputVolumeController::Config& input_volume_controller_config,
40*d9f75844SAndroid Build Coastguard Worker       int sample_rate_hz,
41*d9f75844SAndroid Build Coastguard Worker       int num_channels,
42*d9f75844SAndroid Build Coastguard Worker       bool use_internal_vad);
43*d9f75844SAndroid Build Coastguard Worker   GainController2(const GainController2&) = delete;
44*d9f75844SAndroid Build Coastguard Worker   GainController2& operator=(const GainController2&) = delete;
45*d9f75844SAndroid Build Coastguard Worker   ~GainController2();
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker   // Sets the fixed digital gain.
48*d9f75844SAndroid Build Coastguard Worker   void SetFixedGainDb(float gain_db);
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker   // Updates the input volume controller about whether the capture output is
51*d9f75844SAndroid Build Coastguard Worker   // used or not.
52*d9f75844SAndroid Build Coastguard Worker   void SetCaptureOutputUsed(bool capture_output_used);
53*d9f75844SAndroid Build Coastguard Worker 
54*d9f75844SAndroid Build Coastguard Worker   // Analyzes `audio_buffer` before `Process()` is called so that the analysis
55*d9f75844SAndroid Build Coastguard Worker   // can be performed before digital processing operations take place (e.g.,
56*d9f75844SAndroid Build Coastguard Worker   // echo cancellation). The analysis consists of input clipping detection and
57*d9f75844SAndroid Build Coastguard Worker   // prediction (if enabled). The value of `applied_input_volume` is limited to
58*d9f75844SAndroid Build Coastguard Worker   // [0, 255].
59*d9f75844SAndroid Build Coastguard Worker   void Analyze(int applied_input_volume, const AudioBuffer& audio_buffer);
60*d9f75844SAndroid Build Coastguard Worker 
61*d9f75844SAndroid Build Coastguard Worker   // Applies fixed and adaptive digital gains to `audio` and runs a limiter.
62*d9f75844SAndroid Build Coastguard Worker   // If the internal VAD is used, `speech_probability` is ignored. Otherwise
63*d9f75844SAndroid Build Coastguard Worker   // `speech_probability` is used for digital adaptive gain if it's available
64*d9f75844SAndroid Build Coastguard Worker   // (limited to values [0.0, 1.0]). Handles input volume changes; if the caller
65*d9f75844SAndroid Build Coastguard Worker   // cannot determine whether an input volume change occurred, set
66*d9f75844SAndroid Build Coastguard Worker   // `input_volume_changed` to false.
67*d9f75844SAndroid Build Coastguard Worker   void Process(absl::optional<float> speech_probability,
68*d9f75844SAndroid Build Coastguard Worker                bool input_volume_changed,
69*d9f75844SAndroid Build Coastguard Worker                AudioBuffer* audio);
70*d9f75844SAndroid Build Coastguard Worker 
71*d9f75844SAndroid Build Coastguard Worker   static bool Validate(const AudioProcessing::Config::GainController2& config);
72*d9f75844SAndroid Build Coastguard Worker 
GetCpuFeatures()73*d9f75844SAndroid Build Coastguard Worker   AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; }
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker   // Returns the recommended input volume if input volume controller is enabled
76*d9f75844SAndroid Build Coastguard Worker   // and if a volume recommendation is available.
77*d9f75844SAndroid Build Coastguard Worker   absl::optional<int> GetRecommendedInputVolume() const;
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker  private:
80*d9f75844SAndroid Build Coastguard Worker   static std::atomic<int> instance_count_;
81*d9f75844SAndroid Build Coastguard Worker   const AvailableCpuFeatures cpu_features_;
82*d9f75844SAndroid Build Coastguard Worker   ApmDataDumper data_dumper_;
83*d9f75844SAndroid Build Coastguard Worker   GainApplier fixed_gain_applier_;
84*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<VoiceActivityDetectorWrapper> vad_;
85*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_;
86*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<InputVolumeController> input_volume_controller_;
87*d9f75844SAndroid Build Coastguard Worker   Limiter limiter_;
88*d9f75844SAndroid Build Coastguard Worker   int calls_since_last_limiter_log_;
89*d9f75844SAndroid Build Coastguard Worker };
90*d9f75844SAndroid Build Coastguard Worker 
91*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker #endif  // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_
94