OLD | NEW |
---|---|
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() |
16 // TODO(ivoc): Add implementation. | 29 : render_buffer_(kRenderBufferSize), |
17 RTC_NOTREACHED(); | 30 render_power_(kLookbackFrames), |
31 render_power_mean_(kLookbackFrames), | |
32 render_power_std_dev_(kLookbackFrames), | |
33 covariances_(kLookbackFrames){}; | |
34 | |
35 EchoDetector::~EchoDetector() = default; | |
36 | |
37 constexpr size_t EchoDetector::kLookbackFrames; | |
38 constexpr size_t EchoDetector::kRenderBufferSize; | |
39 | |
40 void EchoDetector::BufferRender(rtc::ArrayView<const float> render) { | |
41 if (render_buffer_.Size() == 0) { | |
42 render_buffer_zero_ = 0; | |
43 } | |
44 if (render_buffer_zero_ >= kRenderBufferSize) { | |
peah-webrtc
2016/10/20 15:08:24
you can add an else if here, as the latter if-stat
ivoc
2016/10/21 12:21:15
Done.
| |
45 LOG(LS_ERROR) << "Clockdrift detected in residual echo detector. Ignoring " | |
46 << "a render frame."; | |
peah-webrtc
2016/10/20 15:08:24
Please add to the comment which frame you are igno
ivoc
2016/10/21 12:21:15
It is actually the oldest frame in the buffer that
| |
47 render_buffer_.Pop(); | |
48 render_buffer_zero_ = 0; | |
49 } | |
50 ++render_buffer_zero_; | |
51 float power = Power(render); | |
52 render_buffer_.Push(power); | |
18 } | 53 } |
19 | 54 |
20 void EchoDetector::Process(const rtc::ArrayView<const float>& /*nearend*/) { | 55 void EchoDetector::Process(rtc::ArrayView<const float> capture) { |
21 // TODO(ivoc): Add implementation. | 56 if (first_process_call_) { |
22 RTC_NOTREACHED(); | 57 // On the first process call (so the start of a call), we must flush the |
58 // render buffer, otherwise the render data will be delayed. | |
59 render_buffer_.Clear(); | |
60 first_process_call_ = false; | |
61 } | |
62 | |
63 // Get the next render value. | |
64 const rtc::Optional<float> buffered_render_power = render_buffer_.Pop(); | |
65 if (!buffered_render_power) { | |
66 // This should only happen in case of clock drift. For now we will just | |
peah-webrtc
2016/10/20 15:08:24
Please update the comment to include that it can a
ivoc
2016/10/21 12:21:15
Done.
| |
67 // ignore the "extra" capture value. | |
68 if (!first_process_call_) { | |
69 // Don't spam error messages before the first process call. | |
70 LOG(LS_ERROR) << "Clockdrift detected in residual echo detector. " | |
71 "Ignoring capture frame."; | |
72 } | |
73 return; | |
74 } | |
75 // Update the render statistics, and store the . | |
peah-webrtc
2016/10/20 15:08:24
The comment seems to have been ended in the middle
ivoc
2016/10/21 12:21:15
Oh, that was sloppy, I fixed it.
| |
76 render_statistics_.Update(*buffered_render_power); | |
77 RTC_DCHECK_LT(next_insertion_index_, kLookbackFrames); | |
78 render_power_[next_insertion_index_] = *buffered_render_power; | |
79 render_power_mean_[next_insertion_index_] = render_statistics_.mean(); | |
80 render_power_std_dev_[next_insertion_index_] = | |
81 render_statistics_.std_deviation(); | |
82 | |
83 // Get the next capture value, update capture statistics and add the relevant | |
84 // values to the buffers. | |
85 const float capture_power = Power(capture); | |
86 capture_statistics_.Update(capture_power); | |
87 const float capture_mean = capture_statistics_.mean(); | |
88 const float capture_std_deviation = capture_statistics_.std_deviation(); | |
89 | |
90 // Update the covariance values and determine the new echo likelihood. | |
91 echo_likelihood_ = 0.f; | |
92 for (size_t delay = 0; delay < covariances_.size(); ++delay) { | |
93 const size_t read_index = | |
94 (kLookbackFrames + next_insertion_index_ - delay) % kLookbackFrames; | |
95 RTC_DCHECK_LT(read_index, render_power_.size()); | |
96 covariances_[delay].Update(capture_power, capture_mean, | |
97 capture_std_deviation, render_power_[read_index], | |
98 render_power_mean_[read_index], | |
99 render_power_std_dev_[read_index]); | |
100 echo_likelihood_ = std::max( | |
peah-webrtc
2016/10/20 15:08:24
Do you take any account of the reliability on the
ivoc
2016/10/21 12:21:15
That's an interesting idea. I think one additional
| |
101 echo_likelihood_, covariances_[delay].normalized_cross_correlation()); | |
102 } | |
103 | |
104 // Update the next insertion index. | |
105 ++next_insertion_index_; | |
106 next_insertion_index_ %= kLookbackFrames; | |
23 } | 107 } |
24 | 108 |
25 void EchoDetector::Initialize(int /*sample_rate_hz*/) { | 109 void EchoDetector::Initialize() { |
26 // TODO(ivoc): Add implementation. | 110 render_buffer_.Clear(); |
27 RTC_NOTREACHED(); | 111 std::fill(render_power_.begin(), render_power_.end(), 0.f); |
112 std::fill(render_power_mean_.begin(), render_power_mean_.end(), 0.f); | |
113 std::fill(render_power_std_dev_.begin(), render_power_std_dev_.end(), 0.f); | |
114 render_statistics_.Clear(); | |
115 capture_statistics_.Clear(); | |
116 for (auto& cov : covariances_) { | |
117 cov.Clear(); | |
118 } | |
119 echo_likelihood_ = 0.f; | |
120 next_insertion_index_ = 0; | |
28 } | 121 } |
29 | 122 |
30 } // namespace webrtc | 123 } // namespace webrtc |
OLD | NEW |