blob: 3b252f3ea2b7e5cbd9065376c2769887a76298b7 [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
14#include <algorithm>
15#include <array>
16#include <limits>
17#include <memory>
18#include <vector>
19
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020020#include "absl/types/optional.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020021#include "api/array_view.h"
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020022#include "modules/audio_processing/aec3/matrix_buffer.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020023#include "modules/audio_processing/aec3/render_buffer.h"
24#include "modules/audio_processing/aec3/stationarity_estimator.h"
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020025#include "modules/audio_processing/aec3/vector_buffer.h"
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020026#include "rtc_base/constructormagic.h"
27
28namespace webrtc {
29
30class ApmDataDumper;
31
32class EchoAudibility {
33 public:
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020034 explicit EchoAudibility(bool use_render_stationarity_at_init);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020035 ~EchoAudibility();
36
37 // Feed new render data to the echo audibility estimator.
38 void Update(const RenderBuffer& render_buffer,
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020039 int delay_blocks,
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +020040 bool external_delay_seen,
41 float reverb_decay);
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020042 // Get the residual echo scaling.
Per Åhgrenb2d71162018-09-10 14:10:34 +020043 void GetResidualEchoScaling(bool filter_has_had_time_to_converge,
44 rtc::ArrayView<float> residual_scaling) const {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020045 for (size_t band = 0; band < residual_scaling.size(); ++band) {
Per Åhgrenb2d71162018-09-10 14:10:34 +020046 if (render_stationarity_.IsBandStationary(band) &&
47 filter_has_had_time_to_converge) {
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020048 residual_scaling[band] = 0.f;
49 } else {
50 residual_scaling[band] = 1.0f;
51 }
52 }
53 }
54
Jesús de Vicente Peña496cedf2018-07-04 11:02:09 +020055 // Returns true if the current render block is estimated as stationary.
56 bool IsBlockStationary() const {
57 return render_stationarity_.IsBlockStationary();
58 }
59
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020060 private:
61 // Reset the EchoAudibility class.
62 void Reset();
63
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020064 // Updates the render stationarity flags for the current frame.
65 void UpdateRenderStationarityFlags(const RenderBuffer& render_buffer,
Jesús de Vicente Peña075cb2b2018-06-13 15:13:55 +020066 int delay_blocks,
67 float reverb_decay);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020068
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020069 // Updates the noise estimator with the new render data since the previous
70 // call to this method.
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020071 void UpdateRenderNoiseEstimator(const VectorBuffer& spectrum_buffer,
72 const MatrixBuffer& block_buffer,
73 bool external_delay_seen);
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020074
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020075 // Returns a bool being true if the render signal contains just close to zero
76 // values.
77 bool IsRenderTooLow(const MatrixBuffer& block_buffer);
78
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020079 absl::optional<int> render_spectrum_write_prev_;
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020080 int render_block_write_prev_;
81 bool non_zero_render_seen_;
Jesús de Vicente Peña836a7a22018-08-31 15:03:04 +020082 const bool use_render_stationarity_at_init_;
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020083 StationarityEstimator render_stationarity_;
84 RTC_DISALLOW_COPY_AND_ASSIGN(EchoAudibility);
85};
86
87} // namespace webrtc
88
89#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_AUDIBILITY_H_