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() = 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 float power = Power(render); |
| 37 render_buffer_.Push(power); |
18 } | 38 } |
19 | 39 |
20 void EchoDetector::Process(const rtc::ArrayView<const float>& /*nearend*/) { | 40 void EchoDetector::Process(rtc::ArrayView<const float> nearend) { |
21 // TODO(ivoc): Add implementation. | 41 const float capture_power = Power(nearend); |
22 RTC_NOTREACHED(); | 42 const float capture_mean = capture_variance_.mean(); |
| 43 const float capture_std_deviation = capture_variance_.std_deviation(); |
| 44 |
| 45 const rtc::Optional<float> buffered_render_power = render_buffer_.Pop(); |
| 46 if (!buffered_render_power) { |
| 47 // This should only happen in case of clock drift. For now we will just |
| 48 // ignore the "extra" capture value. |
| 49 LOG(LS_ERROR) << "Clockdrift detected in residual echo detector. Ignoring " |
| 50 "capture frame."; |
| 51 return; |
| 52 } |
| 53 const float latest_render_power = *buffered_render_power; |
| 54 capture_variance_.UpdateEstimate(capture_power); |
| 55 render_variance_.UpdateEstimate(latest_render_power); |
| 56 RTC_DCHECK_LT(next_insertion_index_, kLookbackFrames); |
| 57 render_power_[next_insertion_index_] = latest_render_power; |
| 58 render_power_mean_[next_insertion_index_] = render_variance_.mean(); |
| 59 render_power_std_dev_[next_insertion_index_] = |
| 60 render_variance_.std_deviation(); |
| 61 echo_likelihood_ = 0.f; |
| 62 for (size_t delay = 0; delay < covariance_.size(); ++delay) { |
| 63 const size_t read_index = |
| 64 (kLookbackFrames + next_insertion_index_ - delay) % kLookbackFrames; |
| 65 RTC_DCHECK_LT(read_index, render_power_.size()); |
| 66 const float render_power = render_power_[read_index]; |
| 67 const float render_mean = render_power_mean_[read_index]; |
| 68 const float render_std_dev = render_power_std_dev_[read_index]; |
| 69 covariance_[delay].UpdateCovarianceEstimate( |
| 70 capture_power, capture_mean, capture_std_deviation, render_power, |
| 71 render_mean, render_std_dev); |
| 72 echo_likelihood_ = std::max( |
| 73 echo_likelihood_, covariance_[delay].normalized_cross_correlation()); |
| 74 } |
| 75 // Update the next insertion index. |
| 76 ++next_insertion_index_; |
| 77 next_insertion_index_ %= kLookbackFrames; |
23 } | 78 } |
24 | 79 |
25 void EchoDetector::Initialize(int /*sample_rate_hz*/) { | 80 void EchoDetector::Initialize() { |
26 // TODO(ivoc): Add implementation. | 81 render_buffer_.Clear(); |
27 RTC_NOTREACHED(); | 82 std::fill(render_power_.begin(), render_power_.end(), 0.f); |
| 83 std::fill(render_power_mean_.begin(), render_power_mean_.end(), 0.f); |
| 84 std::fill(render_power_std_dev_.begin(), render_power_std_dev_.end(), 0.f); |
| 85 render_variance_.Clear(); |
| 86 capture_variance_.Clear(); |
| 87 for (auto& cov : covariance_) { |
| 88 cov.Clear(); |
| 89 } |
| 90 echo_likelihood_ = 0.f; |
| 91 next_insertion_index_ = 0; |
28 } | 92 } |
29 | 93 |
30 } // namespace webrtc | 94 } // namespace webrtc |
OLD | NEW |