xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/adaptive_digital_gain_applier.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_APPLIER_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_APPLIER_H_
13 
14 #include <vector>
15 
16 #include "modules/audio_processing/agc2/gain_applier.h"
17 #include "modules/audio_processing/include/audio_frame_view.h"
18 #include "modules/audio_processing/include/audio_processing.h"
19 
20 namespace webrtc {
21 
22 class ApmDataDumper;
23 
24 // TODO(bugs.webrtc.org/7494): Split into `GainAdaptor` and `GainApplier`.
25 // Selects the target digital gain, decides when and how quickly to adapt to the
26 // target and applies the current gain to 10 ms frames.
27 class AdaptiveDigitalGainApplier {
28  public:
29   // Information about a frame to process.
30   struct FrameInfo {
31     float speech_probability;     // Probability of speech in the [0, 1] range.
32     float speech_level_dbfs;      // Estimated speech level (dBFS).
33     bool speech_level_reliable;   // True with reliable speech level estimation.
34     float noise_rms_dbfs;         // Estimated noise RMS level (dBFS).
35     float headroom_db;            // Headroom (dB).
36     float limiter_envelope_dbfs;  // Envelope level from the limiter (dBFS).
37   };
38 
39   AdaptiveDigitalGainApplier(
40       ApmDataDumper* apm_data_dumper,
41       const AudioProcessing::Config::GainController2::AdaptiveDigital& config,
42       int sample_rate_hz,
43       int num_channels);
44   AdaptiveDigitalGainApplier(const AdaptiveDigitalGainApplier&) = delete;
45   AdaptiveDigitalGainApplier& operator=(const AdaptiveDigitalGainApplier&) =
46       delete;
47 
48   void Initialize(int sample_rate_hz, int num_channels);
49 
50   // Analyzes `info`, updates the digital gain and applies it to a 10 ms
51   // `frame`. Supports any sample rate supported by APM.
52   void Process(const FrameInfo& info, AudioFrameView<float> frame);
53 
54  private:
55   ApmDataDumper* const apm_data_dumper_;
56   GainApplier gain_applier_;
57 
58   const AudioProcessing::Config::GainController2::AdaptiveDigital config_;
59   const float max_gain_change_db_per_10ms_;
60 
61   int calls_since_last_gain_log_;
62   int frames_to_gain_increase_allowed_;
63   float last_gain_db_;
64 
65   std::vector<std::vector<float>> dry_run_frame_;
66   std::vector<float*> dry_run_channels_;
67 };
68 
69 }  // namespace webrtc
70 
71 #endif  // MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_APPLIER_H_
72