xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/echo_audibility.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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_ECHO_AUDIBILITY_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_
13 
14 #include <stddef.h>
15 
16 #include "absl/types/optional.h"
17 #include "api/array_view.h"
18 #include "modules/audio_processing/aec3/block_buffer.h"
19 #include "modules/audio_processing/aec3/render_buffer.h"
20 #include "modules/audio_processing/aec3/spectrum_buffer.h"
21 #include "modules/audio_processing/aec3/stationarity_estimator.h"
22 
23 namespace webrtc {
24 
25 class EchoAudibility {
26  public:
27   explicit EchoAudibility(bool use_render_stationarity_at_init);
28   ~EchoAudibility();
29 
30   EchoAudibility(const EchoAudibility&) = delete;
31   EchoAudibility& operator=(const EchoAudibility&) = delete;
32 
33   // Feed new render data to the echo audibility estimator.
34   void Update(const RenderBuffer& render_buffer,
35               rtc::ArrayView<const float> average_reverb,
36               int min_channel_delay_blocks,
37               bool external_delay_seen);
38   // Get the residual echo scaling.
GetResidualEchoScaling(bool filter_has_had_time_to_converge,rtc::ArrayView<float> residual_scaling)39   void GetResidualEchoScaling(bool filter_has_had_time_to_converge,
40                               rtc::ArrayView<float> residual_scaling) const {
41     for (size_t band = 0; band < residual_scaling.size(); ++band) {
42       if (render_stationarity_.IsBandStationary(band) &&
43           (filter_has_had_time_to_converge ||
44            use_render_stationarity_at_init_)) {
45         residual_scaling[band] = 0.f;
46       } else {
47         residual_scaling[band] = 1.0f;
48       }
49     }
50   }
51 
52   // Returns true if the current render block is estimated as stationary.
IsBlockStationary()53   bool IsBlockStationary() const {
54     return render_stationarity_.IsBlockStationary();
55   }
56 
57  private:
58   // Reset the EchoAudibility class.
59   void Reset();
60 
61   // Updates the render stationarity flags for the current frame.
62   void UpdateRenderStationarityFlags(const RenderBuffer& render_buffer,
63                                      rtc::ArrayView<const float> average_reverb,
64                                      int delay_blocks);
65 
66   // Updates the noise estimator with the new render data since the previous
67   // call to this method.
68   void UpdateRenderNoiseEstimator(const SpectrumBuffer& spectrum_buffer,
69                                   const BlockBuffer& block_buffer,
70                                   bool external_delay_seen);
71 
72   // Returns a bool being true if the render signal contains just close to zero
73   // values.
74   bool IsRenderTooLow(const BlockBuffer& block_buffer);
75 
76   absl::optional<int> render_spectrum_write_prev_;
77   int render_block_write_prev_;
78   bool non_zero_render_seen_;
79   const bool use_render_stationarity_at_init_;
80   StationarityEstimator render_stationarity_;
81 };
82 
83 }  // namespace webrtc
84 
85 #endif  // MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_
86