blob: 7b853219fb0123ba16d17f9b3973d4ab68753b0d [file] [log] [blame]
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +02001/*
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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020015
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020016#include "absl/types/optional.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020017#include "api/array_view.h"
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020018#include "modules/audio_processing/aec3/matrix_buffer.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020019#include "modules/audio_processing/aec3/render_buffer.h"
20#include "modules/audio_processing/aec3/stationarity_estimator.h"
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020021#include "modules/audio_processing/aec3/vector_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/constructor_magic.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020023
24namespace webrtc {
25
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020026class EchoAudibility {
27 public:
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020028 explicit EchoAudibility(bool use_render_stationarity_at_init);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020029 ~EchoAudibility();
30
31 // Feed new render data to the echo audibility estimator.
32 void Update(const RenderBuffer& render_buffer,
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +020033 rtc::ArrayView<const float> render_reverb_contribution_spectrum,
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020034 int delay_blocks,
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +020035 bool external_delay_seen);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020036 // Get the residual echo scaling.
Per Åhgrenb2d71162018-09-10 14:10:34 +020037 void GetResidualEchoScaling(bool filter_has_had_time_to_converge,
38 rtc::ArrayView<float> residual_scaling) const {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020039 for (size_t band = 0; band < residual_scaling.size(); ++band) {
Per Åhgrenb2d71162018-09-10 14:10:34 +020040 if (render_stationarity_.IsBandStationary(band) &&
41 filter_has_had_time_to_converge) {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020042 residual_scaling[band] = 0.f;
43 } else {
44 residual_scaling[band] = 1.0f;
45 }
46 }
47 }
48
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020049 // Returns true if the current render block is estimated as stationary.
50 bool IsBlockStationary() const {
51 return render_stationarity_.IsBlockStationary();
52 }
53
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020054 private:
55 // Reset the EchoAudibility class.
56 void Reset();
57
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020058 // Updates the render stationarity flags for the current frame.
Jesús de Vicente Peñac98849c2018-10-22 11:41:05 +020059 void UpdateRenderStationarityFlags(
60 const RenderBuffer& render_buffer,
61 rtc::ArrayView<const float> render_reverb_contribution_spectrum,
62 int delay_blocks);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020063
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020064 // Updates the noise estimator with the new render data since the previous
65 // call to this method.
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020066 void UpdateRenderNoiseEstimator(const VectorBuffer& spectrum_buffer,
67 const MatrixBuffer& block_buffer,
68 bool external_delay_seen);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020069
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020070 // Returns a bool being true if the render signal contains just close to zero
71 // values.
72 bool IsRenderTooLow(const MatrixBuffer& block_buffer);
73
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020074 absl::optional<int> render_spectrum_write_prev_;
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020075 int render_block_write_prev_;
76 bool non_zero_render_seen_;
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020077 const bool use_render_stationarity_at_init_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020078 StationarityEstimator render_stationarity_;
79 RTC_DISALLOW_COPY_AND_ASSIGN(EchoAudibility);
80};
81
82} // namespace webrtc
83
84#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_