xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/speech_level_estimator.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_SPEECH_LEVEL_ESTIMATOR_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_SPEECH_LEVEL_ESTIMATOR_H_
13 
14 #include <stddef.h>
15 
16 #include <type_traits>
17 
18 #include "modules/audio_processing/agc2/agc2_common.h"
19 #include "modules/audio_processing/include/audio_processing.h"
20 
21 namespace webrtc {
22 class ApmDataDumper;
23 
24 // Active speech level estimator based on the analysis of the following
25 // framewise properties: RMS level (dBFS), peak level (dBFS), speech
26 // probability.
27 class SpeechLevelEstimator {
28  public:
29   SpeechLevelEstimator(
30       ApmDataDumper* apm_data_dumper,
31       const AudioProcessing::Config::GainController2::AdaptiveDigital& config);
32   SpeechLevelEstimator(const SpeechLevelEstimator&) = delete;
33   SpeechLevelEstimator& operator=(const SpeechLevelEstimator&) = delete;
34 
35   // Updates the level estimation.
36   void Update(float rms_dbfs, float peak_dbfs, float speech_probability);
37   // Returns the estimated speech plus noise level.
level_dbfs()38   float level_dbfs() const { return level_dbfs_; }
39   // Returns true if the estimator is confident on its current estimate.
40   bool IsConfident() const;
41 
42   void Reset();
43 
44  private:
45   // Part of the level estimator state used for check-pointing and restore ops.
46   struct LevelEstimatorState {
47     bool operator==(const LevelEstimatorState& s) const;
48     inline bool operator!=(const LevelEstimatorState& s) const {
49       return !(*this == s);
50     }
51     // TODO(bugs.webrtc.org/7494): Remove `time_to_confidence_ms` if redundant.
52     int time_to_confidence_ms;
53     struct Ratio {
54       float numerator;
55       float denominator;
56       float GetRatio() const;
57     } level_dbfs;
58   };
59   static_assert(std::is_trivially_copyable<LevelEstimatorState>::value, "");
60 
61   void ResetLevelEstimatorState(LevelEstimatorState& state) const;
62 
63   void DumpDebugData() const;
64 
65   ApmDataDumper* const apm_data_dumper_;
66 
67   const float initial_speech_level_dbfs_;
68   const int adjacent_speech_frames_threshold_;
69   LevelEstimatorState preliminary_state_;
70   LevelEstimatorState reliable_state_;
71   float level_dbfs_;
72   int num_adjacent_speech_frames_;
73 };
74 
75 }  // namespace webrtc
76 
77 #endif  // MODULES_AUDIO_PROCESSING_AGC2_SPEECH_LEVEL_ESTIMATOR_H_
78