tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

speech_level_estimator.h (2641B)


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