Index: webrtc/modules/audio_processing/echo_detector/echo_detector.cc |
diff --git a/webrtc/modules/audio_processing/echo_detector/echo_detector.cc b/webrtc/modules/audio_processing/echo_detector/echo_detector.cc |
index 842be9e2db3a5a999de4dd07c02c40ec41c879db..a7c344e302a0fc1f320fc30667bb70af181a974a 100644 |
--- a/webrtc/modules/audio_processing/echo_detector/echo_detector.cc |
+++ b/webrtc/modules/audio_processing/echo_detector/echo_detector.cc |
@@ -10,21 +10,75 @@ |
#include "webrtc/modules/audio_processing/echo_detector/echo_detector.h" |
+#include <algorithm> |
+#include <numeric> |
+ |
+namespace { |
+ |
+float Power(const rtc::ArrayView<const float>& input) { |
hlundin-webrtc
2016/10/17 18:49:40
Pass the ArrayView by value, not as ref.
ivoc
2016/10/18 15:20:19
Done.
|
+ return std::inner_product(input.begin(), input.end(), input.begin(), 0.f); |
+} |
+ |
+} // namespace |
+ |
namespace webrtc { |
-void EchoDetector::BufferFarend(const rtc::ArrayView<const float>& /*farend*/) { |
- // TODO(ivoc): Add implementation. |
- RTC_NOTREACHED(); |
+EchoDetector::EchoDetector() = default; |
+ |
+EchoDetector::~EchoDetector() = default; |
+ |
+constexpr size_t EchoDetector::kLookbackFrames; |
+constexpr size_t EchoDetector::kRenderBufferSize; |
+ |
+void EchoDetector::BufferRender(const rtc::ArrayView<const float>& render) { |
+ float power = Power(render); |
+ render_buffer_.Insert(power); |
} |
-void EchoDetector::Process(const rtc::ArrayView<const float>& /*nearend*/) { |
- // TODO(ivoc): Add implementation. |
- RTC_NOTREACHED(); |
+void EchoDetector::Process(const rtc::ArrayView<const float>& nearend) { |
+ const float capture_power = Power(nearend); |
+ capture_variance_.UpdateEstimate(capture_power); |
+ const float capture_mean = capture_variance_.mean(); |
+ const float capture_std_deviation = capture_variance_.std_deviation(); |
+ echo_likelihood_ = 0.f; |
+ |
+ const float latest_render_power = render_buffer_.GetValue(); |
+ render_variance_.UpdateEstimate(latest_render_power); |
+ render_power_[next_insertion_index_] = latest_render_power; |
+ render_power_mean_[next_insertion_index_] = render_variance_.mean(); |
+ render_power_std_dev_[next_insertion_index_] = |
+ render_variance_.std_deviation(); |
+ for (size_t delay = 0; delay < covariance_.size(); ++delay) { |
+ const size_t read_index = |
+ (kLookbackFrames + next_insertion_index_ - delay) % kLookbackFrames; |
+ RTC_DCHECK_LT(read_index, render_power_.size()); |
+ const float render_power = render_power_[read_index]; |
+ const float render_mean = render_power_mean_[read_index]; |
+ const float render_std_dev = render_power_std_dev_[read_index]; |
+ covariance_[delay].UpdateCovarianceEstimate( |
+ capture_power, capture_mean, capture_std_deviation, render_power, |
+ render_mean, render_std_dev); |
+ echo_likelihood_ = std::max( |
+ echo_likelihood_, covariance_[delay].normalized_cross_correlation()); |
+ } |
+ // Update the next insertion index. |
+ ++next_insertion_index_; |
+ next_insertion_index_ %= kLookbackFrames; |
+ RTC_DCHECK_LT(next_insertion_index_, kLookbackFrames); |
hlundin-webrtc
2016/10/17 18:49:40
DCHECKing immediately after the modulo is maybe no
ivoc
2016/10/18 15:20:19
Good idea.
|
} |
-void EchoDetector::Initialize(int /*sample_rate_hz*/) { |
- // TODO(ivoc): Add implementation. |
- RTC_NOTREACHED(); |
+void EchoDetector::Initialize() { |
+ render_buffer_.Clear(); |
+ std::fill(render_power_.begin(), render_power_.end(), 0.f); |
+ std::fill(render_power_mean_.begin(), render_power_mean_.end(), 0.f); |
+ std::fill(render_power_std_dev_.begin(), render_power_std_dev_.end(), 0.f); |
+ render_variance_.Clear(); |
+ capture_variance_.Clear(); |
+ for (auto& cov : covariance_) { |
+ cov.Clear(); |
+ } |
+ echo_likelihood_ = 0.f; |
+ next_insertion_index_ = 0; |
} |
} // namespace webrtc |