Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: webrtc/modules/audio_processing/echo_detector/echo_detector.cc

Issue 2419563003: Add algorithm for Residual Echo Detector. (Closed)
Patch Set: Added better handling of clock drift on render side. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_processing/echo_detector/echo_detector.h" 11 #include "webrtc/modules/audio_processing/echo_detector/echo_detector.h"
12 12
13 #include <algorithm>
14 #include <numeric>
15
16 #include "webrtc/base/logging.h"
17
18 namespace {
19
20 float Power(rtc::ArrayView<const float> input) {
21 return std::inner_product(input.begin(), input.end(), input.begin(), 0.f);
22 }
23
24 } // namespace
25
13 namespace webrtc { 26 namespace webrtc {
14 27
15 void EchoDetector::BufferFarend(const rtc::ArrayView<const float>& /*farend*/) { 28 EchoDetector::EchoDetector() = default;
16 // TODO(ivoc): Add implementation. 29
17 RTC_NOTREACHED(); 30 EchoDetector::~EchoDetector() = default;
31
32 constexpr size_t EchoDetector::kLookbackFrames;
33 constexpr size_t EchoDetector::kRenderBufferSize;
34
35 void EchoDetector::BufferRender(rtc::ArrayView<const float> render) {
36 minimum_render_buffer_size_.AddValue(render_buffer_.buffer_size());
37 if (minimum_render_buffer_size_.GetMinimum() > 0 &&
peah-webrtc 2016/10/19 14:45:26 Do you need a sliding window buffer for this? You
ivoc 2016/10/20 14:04:36 That's a good idea. My solution is a bit too gener
38 render_buffer_.buffer_size() ==
39 minimum_render_buffer_size_.GetMinimum()) {
40 LOG(LS_ERROR) << "Clockdrift detected in residual echo detector. Ignoring "
41 << render_buffer_.buffer_size() << " render frame(s).";
42 render_buffer_.Clear();
43 }
44 float power = Power(render);
45 render_buffer_.Push(power);
18 } 46 }
19 47
20 void EchoDetector::Process(const rtc::ArrayView<const float>& /*nearend*/) { 48 void EchoDetector::Process(rtc::ArrayView<const float> nearend) {
peah-webrtc 2016/10/19 14:45:26 I think you should use a name with something of ca
ivoc 2016/10/20 14:04:36 Done.
21 // TODO(ivoc): Add implementation. 49 const float capture_power = Power(nearend);
peah-webrtc 2016/10/19 14:45:26 Either more this variable closer to where it is fi
ivoc 2016/10/20 14:04:36 It is used in two places, to update the statistics
22 RTC_NOTREACHED(); 50 const float capture_mean = capture_variance_.mean();
peah-webrtc 2016/10/19 14:45:26 Move capture_mean closer to where it is used. Or i
ivoc 2016/10/20 14:04:35 That does seem to be a bug, I fixed it now, thanks
51 const float capture_std_deviation = capture_variance_.std_deviation();
peah-webrtc 2016/10/19 14:45:26 Same comment as for capture_mean.
ivoc 2016/10/20 14:04:35 Done.
52
53 const rtc::Optional<float> buffered_render_power = render_buffer_.Pop();
peah-webrtc 2016/10/19 14:45:25 I think the function would benefit in readability
ivoc 2016/10/20 14:04:36 I added a few blank lines and some comments.
54 if (!buffered_render_power) {
55 // This should only happen in case of clock drift. For now we will just
56 // ignore the "extra" capture value.
57 LOG(LS_ERROR) << "Clockdrift detected in residual echo detector. Ignoring "
peah-webrtc 2016/10/19 14:45:26 This will flood the log with messages if Capture i
ivoc 2016/10/20 14:04:35 Good point, but isn't it typically the render side
peah-webrtc 2016/10/20 15:08:24 I don't think we can make any assumptions on that.
ivoc 2016/10/21 12:21:15 I removed the log messages for now to avoid this.
ivoc 2016/10/21 15:42:08 Sorry, I mistyped, I meant that I will add it to t
58 "capture frame.";
59 return;
60 }
61 const float latest_render_power = *buffered_render_power;
peah-webrtc 2016/10/19 14:45:26 I think you can skip this local copy. Use *buffere
ivoc 2016/10/20 14:04:35 Done.
62 capture_variance_.UpdateEstimate(capture_power);
63 render_variance_.UpdateEstimate(latest_render_power);
64 RTC_DCHECK_LT(next_insertion_index_, kLookbackFrames);
65 render_power_[next_insertion_index_] = latest_render_power;
66 render_power_mean_[next_insertion_index_] = render_variance_.mean();
67 render_power_std_dev_[next_insertion_index_] =
68 render_variance_.std_deviation();
69 echo_likelihood_ = 0.f;
70 for (size_t delay = 0; delay < covariance_.size(); ++delay) {
71 const size_t read_index =
72 (kLookbackFrames + next_insertion_index_ - delay) % kLookbackFrames;
73 RTC_DCHECK_LT(read_index, render_power_.size());
74 const float render_power = render_power_[read_index];
peah-webrtc 2016/10/19 14:45:25 I think you can skip storing the buffer values in
ivoc 2016/10/20 14:04:36 Done.
75 const float render_mean = render_power_mean_[read_index];
76 const float render_std_dev = render_power_std_dev_[read_index];
77 covariance_[delay].UpdateCovarianceEstimate(
78 capture_power, capture_mean, capture_std_deviation, render_power,
79 render_mean, render_std_dev);
80 echo_likelihood_ = std::max(
81 echo_likelihood_, covariance_[delay].normalized_cross_correlation());
82 }
83 // Update the next insertion index.
84 ++next_insertion_index_;
85 next_insertion_index_ %= kLookbackFrames;
23 } 86 }
24 87
25 void EchoDetector::Initialize(int /*sample_rate_hz*/) { 88 void EchoDetector::Initialize() {
26 // TODO(ivoc): Add implementation. 89 render_buffer_.Clear();
27 RTC_NOTREACHED(); 90 std::fill(render_power_.begin(), render_power_.end(), 0.f);
91 std::fill(render_power_mean_.begin(), render_power_mean_.end(), 0.f);
92 std::fill(render_power_std_dev_.begin(), render_power_std_dev_.end(), 0.f);
93 render_variance_.Clear();
94 capture_variance_.Clear();
95 for (auto& cov : covariance_) {
96 cov.Clear();
97 }
98 echo_likelihood_ = 0.f;
99 next_insertion_index_ = 0;
28 } 100 }
29 101
30 } // namespace webrtc 102 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698