xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/comfort_noise_generator.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 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_COMFORT_NOISE_GENERATOR_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_COMFORT_NOISE_GENERATOR_H_
13 
14 #include <stdint.h>
15 
16 #include <array>
17 #include <memory>
18 
19 #include "modules/audio_processing/aec3/aec3_common.h"
20 #include "modules/audio_processing/aec3/aec_state.h"
21 #include "modules/audio_processing/aec3/fft_data.h"
22 #include "rtc_base/system/arch.h"
23 
24 namespace webrtc {
25 namespace aec3 {
26 #if defined(WEBRTC_ARCH_X86_FAMILY)
27 
28 void EstimateComfortNoise_SSE2(const std::array<float, kFftLengthBy2Plus1>& N2,
29                                uint32_t* seed,
30                                FftData* lower_band_noise,
31                                FftData* upper_band_noise);
32 #endif
33 void EstimateComfortNoise(const std::array<float, kFftLengthBy2Plus1>& N2,
34                           uint32_t* seed,
35                           FftData* lower_band_noise,
36                           FftData* upper_band_noise);
37 
38 }  // namespace aec3
39 
40 // Generates the comfort noise.
41 class ComfortNoiseGenerator {
42  public:
43   ComfortNoiseGenerator(const EchoCanceller3Config& config,
44                         Aec3Optimization optimization,
45                         size_t num_capture_channels);
46   ComfortNoiseGenerator() = delete;
47   ~ComfortNoiseGenerator();
48   ComfortNoiseGenerator(const ComfortNoiseGenerator&) = delete;
49 
50   // Computes the comfort noise.
51   void Compute(bool saturated_capture,
52                rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>>
53                    capture_spectrum,
54                rtc::ArrayView<FftData> lower_band_noise,
55                rtc::ArrayView<FftData> upper_band_noise);
56 
57   // Returns the estimate of the background noise spectrum.
NoiseSpectrum()58   rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> NoiseSpectrum()
59       const {
60     return N2_;
61   }
62 
63  private:
64   const Aec3Optimization optimization_;
65   uint32_t seed_;
66   const size_t num_capture_channels_;
67   const float noise_floor_;
68   std::unique_ptr<std::vector<std::array<float, kFftLengthBy2Plus1>>>
69       N2_initial_;
70   std::vector<std::array<float, kFftLengthBy2Plus1>> Y2_smoothed_;
71   std::vector<std::array<float, kFftLengthBy2Plus1>> N2_;
72   int N2_counter_ = 0;
73 };
74 
75 }  // namespace webrtc
76 
77 #endif  // MODULES_AUDIO_PROCESSING_AEC3_COMFORT_NOISE_GENERATOR_H_
78