Per Åhgren | 0cbb58e | 2019-10-29 22:59:44 +0100 | [diff] [blame] | 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 | |
Per Åhgren | 15179a9 | 2021-03-12 14:12:44 +0000 | [diff] [blame^] | 44 | // Specifies whether the capture output will be used. The purpose of this is |
| 45 | // to allow the noise suppressor to deactivate some of the processing when the |
| 46 | // resulting output is anyway not used, for instance when the endpoint is |
| 47 | // muted. |
| 48 | void SetCaptureOutputUsage(bool capture_output_used) { |
| 49 | capture_output_used_ = capture_output_used; |
| 50 | } |
| 51 | |
Per Åhgren | 0cbb58e | 2019-10-29 22:59:44 +0100 | [diff] [blame] | 52 | private: |
| 53 | const size_t num_bands_; |
| 54 | const size_t num_channels_; |
| 55 | const SuppressionParams suppression_params_; |
| 56 | int32_t num_analyzed_frames_ = -1; |
| 57 | NrFft fft_; |
Per Åhgren | 15179a9 | 2021-03-12 14:12:44 +0000 | [diff] [blame^] | 58 | bool capture_output_used_ = true; |
Per Åhgren | 0cbb58e | 2019-10-29 22:59:44 +0100 | [diff] [blame] | 59 | |
| 60 | struct ChannelState { |
| 61 | ChannelState(const SuppressionParams& suppression_params, size_t num_bands); |
| 62 | |
| 63 | SpeechProbabilityEstimator speech_probability_estimator; |
| 64 | WienerFilter wiener_filter; |
| 65 | NoiseEstimator noise_estimator; |
| 66 | std::array<float, kFftSizeBy2Plus1> prev_analysis_signal_spectrum; |
| 67 | std::array<float, kFftSize - kNsFrameSize> analyze_analysis_memory; |
| 68 | std::array<float, kOverlapSize> process_analysis_memory; |
| 69 | std::array<float, kOverlapSize> process_synthesis_memory; |
| 70 | std::vector<std::array<float, kOverlapSize>> process_delay_memory; |
| 71 | }; |
| 72 | |
| 73 | struct FilterBankState { |
| 74 | std::array<float, kFftSize> real; |
| 75 | std::array<float, kFftSize> imag; |
| 76 | std::array<float, kFftSize> extended_frame; |
| 77 | }; |
| 78 | |
| 79 | std::vector<FilterBankState> filter_bank_states_heap_; |
| 80 | std::vector<float> upper_band_gains_heap_; |
| 81 | std::vector<float> energies_before_filtering_heap_; |
| 82 | std::vector<float> gain_adjustments_heap_; |
| 83 | std::vector<std::unique_ptr<ChannelState>> channels_; |
| 84 | |
| 85 | // Aggregates the Wiener filters into a single filter to use. |
| 86 | void AggregateWienerFilters( |
| 87 | rtc::ArrayView<float, kFftSizeBy2Plus1> filter) const; |
| 88 | }; |
| 89 | |
| 90 | } // namespace webrtc |
| 91 | |
| 92 | #endif // MODULES_AUDIO_PROCESSING_NS_NOISE_SUPPRESSOR_H_ |