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 #include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
11
12 #include <algorithm>
13 #include <array>
14
15 #include "modules/audio_processing/aec3/aec3_common.h"
16 #include "modules/audio_processing/include/audio_processing.h"
17 #include "modules/audio_processing/logging/apm_data_dumper.h"
18 #include "rtc_base/checks.h"
19
20 namespace webrtc {
21
EchoPathDelayEstimator(ApmDataDumper * data_dumper,const EchoCanceller3Config & config)22 EchoPathDelayEstimator::EchoPathDelayEstimator(
23 ApmDataDumper* data_dumper,
24 const EchoCanceller3Config& config)
25 : data_dumper_(data_dumper),
26 down_sampling_factor_(config.delay.down_sampling_factor),
27 sub_block_size_(down_sampling_factor_ != 0
28 ? kBlockSize / down_sampling_factor_
29 : kBlockSize),
30 capture_decimator_(down_sampling_factor_),
31 matched_filter_(data_dumper_,
32 DetectOptimization(),
33 sub_block_size_,
34 kMatchedFilterWindowSizeSubBlocks,
35 config.delay.num_filters,
36 kMatchedFilterAlignmentShiftSizeSubBlocks,
37 config.render_levels.poor_excitation_render_limit),
38 matched_filter_lag_aggregator_(data_dumper_,
39 matched_filter_.GetMaxFilterLag()) {
40 RTC_DCHECK(data_dumper);
41 RTC_DCHECK(down_sampling_factor_ > 0);
42 }
43
44 EchoPathDelayEstimator::~EchoPathDelayEstimator() = default;
45
Reset()46 void EchoPathDelayEstimator::Reset() {
47 matched_filter_lag_aggregator_.Reset();
48 matched_filter_.Reset();
49 }
50
EstimateDelay(const DownsampledRenderBuffer & render_buffer,rtc::ArrayView<const float> capture)51 rtc::Optional<size_t> EchoPathDelayEstimator::EstimateDelay(
52 const DownsampledRenderBuffer& render_buffer,
53 rtc::ArrayView<const float> capture) {
54 RTC_DCHECK_EQ(kBlockSize, capture.size());
55
56 std::array<float, kBlockSize> downsampled_capture_data;
57 rtc::ArrayView<float> downsampled_capture(downsampled_capture_data.data(),
58 sub_block_size_);
59 data_dumper_->DumpWav("aec3_capture_decimator_input", capture.size(),
60 capture.data(), 16000, 1);
61 capture_decimator_.Decimate(capture, downsampled_capture);
62 data_dumper_->DumpWav("aec3_capture_decimator_output",
63 downsampled_capture.size(), downsampled_capture.data(),
64 16000 / down_sampling_factor_, 1);
65 matched_filter_.Update(render_buffer, downsampled_capture);
66
67 rtc::Optional<size_t> aggregated_matched_filter_lag =
68 matched_filter_lag_aggregator_.Aggregate(
69 matched_filter_.GetLagEstimates());
70
71 // TODO(peah): Move this logging outside of this class once EchoCanceller3
72 // development is done.
73 data_dumper_->DumpRaw("aec3_echo_path_delay_estimator_delay",
74 aggregated_matched_filter_lag
75 ? static_cast<int>(*aggregated_matched_filter_lag *
76 down_sampling_factor_)
77 : -1);
78
79 // Return the detected delay in samples as the aggregated matched filter lag
80 // compensated by the down sampling factor for the signal being correlated.
81 return aggregated_matched_filter_lag
82 ? rtc::Optional<size_t>(*aggregated_matched_filter_lag *
83 down_sampling_factor_)
84 : rtc::nullopt;
85 }
86
87 } // namespace webrtc
88