LCOV - code coverage report
Current view: top level - media/webrtc/trunk/webrtc/modules/audio_processing - residual_echo_detector.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 70 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 6 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
       3             :  *
       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
       6             :  *  tree. An additional intellectual property rights grant can be found
       7             :  *  in the file PATENTS.  All contributing project authors may
       8             :  *  be found in the AUTHORS file in the root of the source tree.
       9             :  */
      10             : 
      11             : #include "webrtc/modules/audio_processing/residual_echo_detector.h"
      12             : 
      13             : #include <algorithm>
      14             : #include <numeric>
      15             : 
      16             : #include "webrtc/modules/audio_processing/audio_buffer.h"
      17             : #include "webrtc/system_wrappers/include/metrics.h"
      18             : 
      19             : namespace {
      20             : 
      21           0 : float Power(rtc::ArrayView<const float> input) {
      22           0 :   return std::inner_product(input.begin(), input.end(), input.begin(), 0.f);
      23             : }
      24             : 
      25             : constexpr size_t kLookbackFrames = 650;
      26             : // TODO(ivoc): Verify the size of this buffer.
      27             : constexpr size_t kRenderBufferSize = 30;
      28             : constexpr float kAlpha = 0.001f;
      29             : // 10 seconds of data, updated every 10 ms.
      30             : constexpr size_t kAggregationBufferSize = 10 * 100;
      31             : 
      32             : }  // namespace
      33             : 
      34             : namespace webrtc {
      35             : 
      36           0 : ResidualEchoDetector::ResidualEchoDetector()
      37             :     : render_buffer_(kRenderBufferSize),
      38             :       render_power_(kLookbackFrames),
      39             :       render_power_mean_(kLookbackFrames),
      40             :       render_power_std_dev_(kLookbackFrames),
      41             :       covariances_(kLookbackFrames),
      42           0 :       recent_likelihood_max_(kAggregationBufferSize) {}
      43             : 
      44             : ResidualEchoDetector::~ResidualEchoDetector() = default;
      45             : 
      46           0 : void ResidualEchoDetector::AnalyzeRenderAudio(
      47             :     rtc::ArrayView<const float> render_audio) {
      48           0 :   if (render_buffer_.Size() == 0) {
      49           0 :     frames_since_zero_buffer_size_ = 0;
      50           0 :   } else if (frames_since_zero_buffer_size_ >= kRenderBufferSize) {
      51             :     // This can happen in a few cases: at the start of a call, due to a glitch
      52             :     // or due to clock drift. The excess capture value will be ignored.
      53             :     // TODO(ivoc): Include how often this happens in APM stats.
      54           0 :     render_buffer_.Pop();
      55           0 :     frames_since_zero_buffer_size_ = 0;
      56             :   }
      57           0 :   ++frames_since_zero_buffer_size_;
      58           0 :   float power = Power(render_audio);
      59           0 :   render_buffer_.Push(power);
      60           0 : }
      61             : 
      62           0 : void ResidualEchoDetector::AnalyzeCaptureAudio(
      63             :     rtc::ArrayView<const float> capture_audio) {
      64           0 :   if (first_process_call_) {
      65             :     // On the first process call (so the start of a call), we must flush the
      66             :     // render buffer, otherwise the render data will be delayed.
      67           0 :     render_buffer_.Clear();
      68           0 :     first_process_call_ = false;
      69             :   }
      70             : 
      71             :   // Get the next render value.
      72           0 :   const rtc::Optional<float> buffered_render_power = render_buffer_.Pop();
      73           0 :   if (!buffered_render_power) {
      74             :     // This can happen in a few cases: at the start of a call, due to a glitch
      75             :     // or due to clock drift. The excess capture value will be ignored.
      76             :     // TODO(ivoc): Include how often this happens in APM stats.
      77           0 :     return;
      78             :   }
      79             :   // Update the render statistics, and store the statistics in circular buffers.
      80           0 :   render_statistics_.Update(*buffered_render_power);
      81           0 :   RTC_DCHECK_LT(next_insertion_index_, kLookbackFrames);
      82           0 :   render_power_[next_insertion_index_] = *buffered_render_power;
      83           0 :   render_power_mean_[next_insertion_index_] = render_statistics_.mean();
      84           0 :   render_power_std_dev_[next_insertion_index_] =
      85           0 :       render_statistics_.std_deviation();
      86             : 
      87             :   // Get the next capture value, update capture statistics and add the relevant
      88             :   // values to the buffers.
      89           0 :   const float capture_power = Power(capture_audio);
      90           0 :   capture_statistics_.Update(capture_power);
      91           0 :   const float capture_mean = capture_statistics_.mean();
      92           0 :   const float capture_std_deviation = capture_statistics_.std_deviation();
      93             : 
      94             :   // Update the covariance values and determine the new echo likelihood.
      95           0 :   echo_likelihood_ = 0.f;
      96           0 :   for (size_t delay = 0; delay < covariances_.size(); ++delay) {
      97             :     const size_t read_index =
      98           0 :         (kLookbackFrames + next_insertion_index_ - delay) % kLookbackFrames;
      99           0 :     RTC_DCHECK_LT(read_index, render_power_.size());
     100           0 :     covariances_[delay].Update(capture_power, capture_mean,
     101           0 :                                capture_std_deviation, render_power_[read_index],
     102           0 :                                render_power_mean_[read_index],
     103           0 :                                render_power_std_dev_[read_index]);
     104           0 :     echo_likelihood_ = std::max(
     105           0 :         echo_likelihood_, covariances_[delay].normalized_cross_correlation());
     106             :   }
     107           0 :   reliability_ = (1.0f - kAlpha) * reliability_ + kAlpha * 1.0f;
     108           0 :   echo_likelihood_ *= reliability_;
     109           0 :   int echo_percentage = static_cast<int>(echo_likelihood_ * 100);
     110           0 :   RTC_HISTOGRAM_COUNTS("WebRTC.Audio.ResidualEchoDetector.EchoLikelihood",
     111             :                        echo_percentage, 0, 100, 100 /* number of bins */);
     112             : 
     113             :   // Update the buffer of recent likelihood values.
     114           0 :   recent_likelihood_max_.Update(echo_likelihood_);
     115             : 
     116             :   // Update the next insertion index.
     117           0 :   ++next_insertion_index_;
     118           0 :   next_insertion_index_ %= kLookbackFrames;
     119             : }
     120             : 
     121           0 : void ResidualEchoDetector::Initialize() {
     122           0 :   render_buffer_.Clear();
     123           0 :   std::fill(render_power_.begin(), render_power_.end(), 0.f);
     124           0 :   std::fill(render_power_mean_.begin(), render_power_mean_.end(), 0.f);
     125           0 :   std::fill(render_power_std_dev_.begin(), render_power_std_dev_.end(), 0.f);
     126           0 :   render_statistics_.Clear();
     127           0 :   capture_statistics_.Clear();
     128           0 :   recent_likelihood_max_.Clear();
     129           0 :   for (auto& cov : covariances_) {
     130           0 :     cov.Clear();
     131             :   }
     132           0 :   echo_likelihood_ = 0.f;
     133           0 :   next_insertion_index_ = 0;
     134           0 :   reliability_ = 0.f;
     135           0 : }
     136             : 
     137           0 : void ResidualEchoDetector::PackRenderAudioBuffer(
     138             :     AudioBuffer* audio,
     139             :     std::vector<float>* packed_buffer) {
     140           0 :   RTC_DCHECK_GE(160, audio->num_frames_per_band());
     141             : 
     142           0 :   packed_buffer->clear();
     143           0 :   packed_buffer->insert(packed_buffer->end(),
     144           0 :                         audio->split_bands_const_f(0)[kBand0To8kHz],
     145           0 :                         (audio->split_bands_const_f(0)[kBand0To8kHz] +
     146           0 :                          audio->num_frames_per_band()));
     147           0 : }
     148             : 
     149             : }  // namespace webrtc

Generated by: LCOV version 1.13