noise_suppressor.h 2.9 KB

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