• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
15 #include <vector>
16 
17 #include "api/array_view.h"
18 #include "modules/audio_processing/audio_buffer.h"
19 #include "modules/audio_processing/ns/noise_estimator.h"
20 #include "modules/audio_processing/ns/ns_common.h"
21 #include "modules/audio_processing/ns/ns_config.h"
22 #include "modules/audio_processing/ns/ns_fft.h"
23 #include "modules/audio_processing/ns/speech_probability_estimator.h"
24 #include "modules/audio_processing/ns/wiener_filter.h"
25 
26 namespace webrtc {
27 
28 // Class for suppressing noise in a signal.
29 class NoiseSuppressor {
30  public:
31   NoiseSuppressor(const NsConfig& config,
32                   size_t sample_rate_hz,
33                   size_t num_channels);
34   NoiseSuppressor(const NoiseSuppressor&) = delete;
35   NoiseSuppressor& operator=(const NoiseSuppressor&) = delete;
36 
37   // Analyses the signal (typically applied before the AEC to avoid analyzing
38   // any comfort noise signal).
39   void Analyze(const AudioBuffer& audio);
40 
41   // Applies noise suppression.
42   void Process(AudioBuffer* audio);
43 
44  private:
45   const size_t num_bands_;
46   const size_t num_channels_;
47   const SuppressionParams suppression_params_;
48   int32_t num_analyzed_frames_ = -1;
49   NrFft fft_;
50 
51   struct ChannelState {
52     ChannelState(const SuppressionParams& suppression_params, size_t num_bands);
53 
54     SpeechProbabilityEstimator speech_probability_estimator;
55     WienerFilter wiener_filter;
56     NoiseEstimator noise_estimator;
57     std::array<float, kFftSizeBy2Plus1> prev_analysis_signal_spectrum;
58     std::array<float, kFftSize - kNsFrameSize> analyze_analysis_memory;
59     std::array<float, kOverlapSize> process_analysis_memory;
60     std::array<float, kOverlapSize> process_synthesis_memory;
61     std::vector<std::array<float, kOverlapSize>> process_delay_memory;
62   };
63 
64   struct FilterBankState {
65     std::array<float, kFftSize> real;
66     std::array<float, kFftSize> imag;
67     std::array<float, kFftSize> extended_frame;
68   };
69 
70   std::vector<FilterBankState> filter_bank_states_heap_;
71   std::vector<float> upper_band_gains_heap_;
72   std::vector<float> energies_before_filtering_heap_;
73   std::vector<float> gain_adjustments_heap_;
74   std::vector<std::unique_ptr<ChannelState>> channels_;
75 
76   // Aggregates the Wiener filters into a single filter to use.
77   void AggregateWienerFilters(
78       rtc::ArrayView<float, kFftSizeBy2Plus1> filter) const;
79 };
80 
81 }  // namespace webrtc
82 
83 #endif  // MODULES_AUDIO_PROCESSING_NS_NOISE_SUPPRESSOR_H_
84