xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/adaptive_digital_gain_controller.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_CONTROLLER_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_CONTROLLER_H_
13 
14 #include <memory>
15 
16 #include "absl/types/optional.h"
17 #include "modules/audio_processing/agc2/adaptive_digital_gain_applier.h"
18 #include "modules/audio_processing/agc2/noise_level_estimator.h"
19 #include "modules/audio_processing/agc2/saturation_protector.h"
20 #include "modules/audio_processing/agc2/speech_level_estimator.h"
21 #include "modules/audio_processing/include/audio_frame_view.h"
22 #include "modules/audio_processing/include/audio_processing.h"
23 
24 namespace webrtc {
25 class ApmDataDumper;
26 
27 // Gain controller that adapts and applies a variable digital gain to meet the
28 // target level, which is determined by the given configuration.
29 class AdaptiveDigitalGainController {
30  public:
31   AdaptiveDigitalGainController(
32       ApmDataDumper* apm_data_dumper,
33       const AudioProcessing::Config::GainController2::AdaptiveDigital& config,
34       int sample_rate_hz,
35       int num_channels);
36   AdaptiveDigitalGainController(const AdaptiveDigitalGainController&) = delete;
37   AdaptiveDigitalGainController& operator=(
38       const AdaptiveDigitalGainController&) = delete;
39   ~AdaptiveDigitalGainController();
40 
41   // Detects and handles changes of sample rate and or number of channels.
42   void Initialize(int sample_rate_hz, int num_channels);
43 
44   // Analyzes `frame`, adapts the current digital gain and applies it to
45   // `frame`.
46   // TODO(bugs.webrtc.org/7494): Remove `limiter_envelope`.
47   void Process(AudioFrameView<float> frame,
48                float speech_probability,
49                float limiter_envelope);
50 
51   // Handles a gain change applied to the input signal (e.g., analog gain).
52   void HandleInputGainChange();
53 
54   // Returns the most recent speech level (dBFs) if the estimator is confident.
55   // Otherwise returns absl::nullopt.
56   absl::optional<float> GetSpeechLevelDbfsIfConfident() const;
57 
58  private:
59   SpeechLevelEstimator speech_level_estimator_;
60   AdaptiveDigitalGainApplier gain_controller_;
61   ApmDataDumper* const apm_data_dumper_;
62   std::unique_ptr<NoiseLevelEstimator> noise_level_estimator_;
63   std::unique_ptr<SaturationProtector> saturation_protector_;
64 };
65 
66 }  // namespace webrtc
67 
68 #endif  // MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_CONTROLLER_H_
69