tor-browser

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

fullband_erle_estimator.h (4150B)


      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_AEC3_FULLBAND_ERLE_ESTIMATOR_H_
     12 #define MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_
     13 
     14 #include <algorithm>
     15 #include <array>
     16 #include <cstddef>
     17 #include <memory>
     18 #include <optional>
     19 #include <vector>
     20 
     21 #include "api/array_view.h"
     22 #include "api/audio/echo_canceller3_config.h"
     23 #include "modules/audio_processing/aec3/aec3_common.h"
     24 #include "modules/audio_processing/logging/apm_data_dumper.h"
     25 
     26 namespace webrtc {
     27 
     28 // Estimates the echo return loss enhancement using the energy of all the
     29 // freuquency bands.
     30 class FullBandErleEstimator {
     31 public:
     32  FullBandErleEstimator(const EchoCanceller3Config::Erle& config,
     33                        size_t num_capture_channels);
     34  ~FullBandErleEstimator();
     35  // Resets the ERLE estimator.
     36  void Reset();
     37 
     38  // Updates the ERLE estimator.
     39  void Update(ArrayView<const float> X2,
     40              ArrayView<const std::array<float, kFftLengthBy2Plus1>> Y2,
     41              ArrayView<const std::array<float, kFftLengthBy2Plus1>> E2,
     42              const std::vector<bool>& converged_filters);
     43 
     44  // Returns the fullband ERLE estimates in log2 units.
     45  float FullbandErleLog2() const {
     46    float min_erle = erle_time_domain_log2_[0];
     47    for (size_t ch = 1; ch < erle_time_domain_log2_.size(); ++ch) {
     48      min_erle = std::min(min_erle, erle_time_domain_log2_[ch]);
     49    }
     50    return min_erle;
     51  }
     52 
     53  // Returns an estimation of the current linear filter quality. It returns a
     54  // float number between 0 and 1 mapping 1 to the highest possible quality.
     55  ArrayView<const std::optional<float>> GetInstLinearQualityEstimates() const {
     56    return linear_filters_qualities_;
     57  }
     58 
     59  void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
     60 
     61 private:
     62  void UpdateQualityEstimates();
     63 
     64  class ErleInstantaneous {
     65   public:
     66    explicit ErleInstantaneous(const EchoCanceller3Config::Erle& config);
     67    ~ErleInstantaneous();
     68 
     69    // Updates the estimator with a new point, returns true
     70    // if the instantaneous ERLE was updated due to having enough
     71    // points for performing the estimate.
     72    bool Update(float Y2_sum, float E2_sum);
     73    // Resets the instantaneous ERLE estimator to its initial state.
     74    void Reset();
     75    // Resets the members related with an instantaneous estimate.
     76    void ResetAccumulators();
     77    // Returns the instantaneous ERLE in log2 units.
     78    std::optional<float> GetInstErleLog2() const { return erle_log2_; }
     79    // Gets an indication between 0 and 1 of the performance of the linear
     80    // filter for the current time instant.
     81    std::optional<float> GetQualityEstimate() const {
     82      if (erle_log2_) {
     83        float value = inst_quality_estimate_;
     84        if (clamp_inst_quality_to_zero_) {
     85          value = std::max(0.f, value);
     86        }
     87        if (clamp_inst_quality_to_one_) {
     88          value = std::min(1.f, value);
     89        }
     90        return std::optional<float>(value);
     91      }
     92      return std::nullopt;
     93    }
     94    void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
     95 
     96   private:
     97    void UpdateMaxMin();
     98    void UpdateQualityEstimate();
     99    const bool clamp_inst_quality_to_zero_;
    100    const bool clamp_inst_quality_to_one_;
    101    std::optional<float> erle_log2_;
    102    float inst_quality_estimate_;
    103    float max_erle_log2_;
    104    float min_erle_log2_;
    105    float Y2_acum_;
    106    float E2_acum_;
    107    int num_points_;
    108  };
    109 
    110  const float min_erle_log2_;
    111  const float max_erle_lf_log2_;
    112  std::vector<int> hold_counters_instantaneous_erle_;
    113  std::vector<float> erle_time_domain_log2_;
    114  std::vector<ErleInstantaneous> instantaneous_erle_;
    115  std::vector<std::optional<float>> linear_filters_qualities_;
    116 };
    117 
    118 }  // namespace webrtc
    119 
    120 #endif  // MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_