tor-browser

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

noise_suppressor.h (3433B)


      1 /*
      2 *  Copyright (c) 2012 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_NS_NOISE_SUPPRESSOR_H_
     12 #define MODULES_AUDIO_PROCESSING_NS_NOISE_SUPPRESSOR_H_
     13 
     14 #include <array>
     15 #include <cstddef>
     16 #include <cstdint>
     17 #include <memory>
     18 #include <vector>
     19 
     20 #include "api/array_view.h"
     21 #include "modules/audio_processing/audio_buffer.h"
     22 #include "modules/audio_processing/ns/noise_estimator.h"
     23 #include "modules/audio_processing/ns/ns_common.h"
     24 #include "modules/audio_processing/ns/ns_config.h"
     25 #include "modules/audio_processing/ns/ns_fft.h"
     26 #include "modules/audio_processing/ns/speech_probability_estimator.h"
     27 #include "modules/audio_processing/ns/suppression_params.h"
     28 #include "modules/audio_processing/ns/wiener_filter.h"
     29 
     30 namespace webrtc {
     31 
     32 // Class for suppressing noise in a signal.
     33 class NoiseSuppressor {
     34 public:
     35  NoiseSuppressor(const NsConfig& config,
     36                  size_t sample_rate_hz,
     37                  size_t num_channels);
     38  NoiseSuppressor(const NoiseSuppressor&) = delete;
     39  NoiseSuppressor& operator=(const NoiseSuppressor&) = delete;
     40 
     41  // Analyses the signal (typically applied before the AEC to avoid analyzing
     42  // any comfort noise signal).
     43  void Analyze(const AudioBuffer& audio);
     44 
     45  // Applies noise suppression.
     46  void Process(AudioBuffer* audio);
     47 
     48  // Specifies whether the capture output will be used. The purpose of this is
     49  // to allow the noise suppressor to deactivate some of the processing when the
     50  // resulting output is anyway not used, for instance when the endpoint is
     51  // muted.
     52  void SetCaptureOutputUsage(bool capture_output_used) {
     53    capture_output_used_ = capture_output_used;
     54  }
     55 
     56 private:
     57  const size_t num_bands_;
     58  const size_t num_channels_;
     59  const SuppressionParams suppression_params_;
     60  int32_t num_analyzed_frames_ = -1;
     61  NrFft fft_;
     62  bool capture_output_used_ = true;
     63 
     64  struct ChannelState {
     65    ChannelState(const SuppressionParams& suppression_params, size_t num_bands);
     66 
     67    SpeechProbabilityEstimator speech_probability_estimator;
     68    WienerFilter wiener_filter;
     69    NoiseEstimator noise_estimator;
     70    std::array<float, kFftSizeBy2Plus1> prev_analysis_signal_spectrum;
     71    std::array<float, kFftSize - kNsFrameSize> analyze_analysis_memory;
     72    std::array<float, kOverlapSize> process_analysis_memory;
     73    std::array<float, kOverlapSize> process_synthesis_memory;
     74    std::vector<std::array<float, kOverlapSize>> process_delay_memory;
     75  };
     76 
     77  struct FilterBankState {
     78    std::array<float, kFftSize> real;
     79    std::array<float, kFftSize> imag;
     80    std::array<float, kFftSize> extended_frame;
     81  };
     82 
     83  std::vector<FilterBankState> filter_bank_states_heap_;
     84  std::vector<float> upper_band_gains_heap_;
     85  std::vector<float> energies_before_filtering_heap_;
     86  std::vector<float> gain_adjustments_heap_;
     87  std::vector<std::unique_ptr<ChannelState>> channels_;
     88 
     89  // Aggregates the Wiener filters into a single filter to use.
     90  void AggregateWienerFilters(ArrayView<float, kFftSizeBy2Plus1> filter) const;
     91 };
     92 
     93 }  // namespace webrtc
     94 
     95 #endif  // MODULES_AUDIO_PROCESSING_NS_NOISE_SUPPRESSOR_H_