Chromium Code Reviews| 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 namespace { | |
| 17 | |
| 18 float Power(rtc::ArrayView<const float> input) { | |
| 19 return std::inner_product(input.begin(), input.end(), input.begin(), 0.f); | |
| 20 } | |
| 21 | |
| 22 constexpr size_t kLookbackFrames = 650; | |
| 23 // TODO(ivoc): Verify the size of this buffer. | |
| 24 constexpr size_t kRenderBufferSize = 30; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 13 namespace webrtc { | 28 namespace webrtc { |
| 14 | 29 |
| 15 void EchoDetector::BufferFarend(const rtc::ArrayView<const float>& /*farend*/) { | 30 EchoDetector::EchoDetector() |
| 16 // TODO(ivoc): Add implementation. | 31 : render_buffer_(kRenderBufferSize), |
| 17 RTC_NOTREACHED(); | 32 render_power_(kLookbackFrames), |
| 33 render_power_mean_(kLookbackFrames), | |
| 34 render_power_std_dev_(kLookbackFrames), | |
| 35 covariances_(kLookbackFrames){}; | |
| 36 | |
| 37 EchoDetector::~EchoDetector() = default; | |
| 38 | |
| 39 void EchoDetector::BufferRender(rtc::ArrayView<const float> render) { | |
| 40 if (render_buffer_.Size() == 0) { | |
| 41 frames_since_zero_buffer_size_ = 0; | |
| 42 } else if (frames_since_zero_buffer_size_ >= kRenderBufferSize) { | |
| 43 // This can happen in a few cases: at the start of a call, due to a glitch | |
| 44 // or due to clock drift. The excess capture value will be ignored. | |
| 45 // TODO(ivoc): Include how often this happens in APM stats. | |
| 46 render_buffer_.Pop(); | |
| 47 frames_since_zero_buffer_size_ = 0; | |
|
hlundin-webrtc
2016/10/24 00:42:14
I don't understand the logic of frames_since_zero_
ivoc
2016/10/24 15:25:19
The idea is to count how long ago it was since the
hlundin-webrtc
2016/10/27 13:05:48
Oh, it was an if--elseif statement. I read it as i
ivoc
2016/10/27 13:55:29
Added more comments in the header, because naming
| |
| 48 } | |
| 49 ++frames_since_zero_buffer_size_; | |
| 50 float power = Power(render); | |
| 51 render_buffer_.Push(power); | |
| 18 } | 52 } |
| 19 | 53 |
| 20 void EchoDetector::Process(const rtc::ArrayView<const float>& /*nearend*/) { | 54 void EchoDetector::Process(rtc::ArrayView<const float> capture) { |
| 21 // TODO(ivoc): Add implementation. | 55 if (first_process_call_) { |
| 22 RTC_NOTREACHED(); | 56 // On the first process call (so the start of a call), we must flush the |
| 57 // render buffer, otherwise the render data will be delayed. | |
| 58 render_buffer_.Clear(); | |
| 59 first_process_call_ = false; | |
| 60 } | |
| 61 | |
| 62 // Get the next render value. | |
| 63 const rtc::Optional<float> buffered_render_power = render_buffer_.Pop(); | |
| 64 if (!buffered_render_power) { | |
| 65 // This can happen in a few cases: at the start of a call, due to a glitch | |
| 66 // or due to clock drift. The excess capture value will be ignored. | |
| 67 // TODO(ivoc): Include how often this happens in APM stats. | |
| 68 return; | |
| 69 } | |
| 70 // Update the render statistics, and store the statistics in circular buffers. | |
| 71 render_statistics_.Update(*buffered_render_power); | |
| 72 RTC_DCHECK_LT(next_insertion_index_, kLookbackFrames); | |
| 73 render_power_[next_insertion_index_] = *buffered_render_power; | |
| 74 render_power_mean_[next_insertion_index_] = render_statistics_.mean(); | |
| 75 render_power_std_dev_[next_insertion_index_] = | |
| 76 render_statistics_.std_deviation(); | |
| 77 | |
| 78 // Get the next capture value, update capture statistics and add the relevant | |
| 79 // values to the buffers. | |
| 80 const float capture_power = Power(capture); | |
| 81 capture_statistics_.Update(capture_power); | |
| 82 const float capture_mean = capture_statistics_.mean(); | |
| 83 const float capture_std_deviation = capture_statistics_.std_deviation(); | |
| 84 | |
| 85 // Update the covariance values and determine the new echo likelihood. | |
| 86 echo_likelihood_ = 0.f; | |
| 87 for (size_t delay = 0; delay < covariances_.size(); ++delay) { | |
| 88 const size_t read_index = | |
| 89 (kLookbackFrames + next_insertion_index_ - delay) % kLookbackFrames; | |
| 90 RTC_DCHECK_LT(read_index, render_power_.size()); | |
| 91 covariances_[delay].Update(capture_power, capture_mean, | |
| 92 capture_std_deviation, render_power_[read_index], | |
| 93 render_power_mean_[read_index], | |
| 94 render_power_std_dev_[read_index]); | |
| 95 echo_likelihood_ = std::max( | |
| 96 echo_likelihood_, covariances_[delay].normalized_cross_correlation()); | |
| 97 } | |
| 98 | |
| 99 // Update the next insertion index. | |
| 100 ++next_insertion_index_; | |
| 101 next_insertion_index_ %= kLookbackFrames; | |
| 23 } | 102 } |
| 24 | 103 |
| 25 void EchoDetector::Initialize(int /*sample_rate_hz*/) { | 104 void EchoDetector::Initialize() { |
| 26 // TODO(ivoc): Add implementation. | 105 render_buffer_.Clear(); |
| 27 RTC_NOTREACHED(); | 106 std::fill(render_power_.begin(), render_power_.end(), 0.f); |
| 107 std::fill(render_power_mean_.begin(), render_power_mean_.end(), 0.f); | |
| 108 std::fill(render_power_std_dev_.begin(), render_power_std_dev_.end(), 0.f); | |
| 109 render_statistics_.Clear(); | |
| 110 capture_statistics_.Clear(); | |
| 111 for (auto& cov : covariances_) { | |
| 112 cov.Clear(); | |
| 113 } | |
| 114 echo_likelihood_ = 0.f; | |
| 115 next_insertion_index_ = 0; | |
| 28 } | 116 } |
| 29 | 117 |
| 30 } // namespace webrtc | 118 } // namespace webrtc |
| OLD | NEW |