Per Åhgren | 39f491e | 2018-02-22 00:39:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #include "modules/audio_processing/aec3/skew_estimator.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <numeric> |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | SkewEstimator::SkewEstimator(size_t skew_history_size_log2) |
| 18 | : skew_history_size_log2_(static_cast<int>(skew_history_size_log2)), |
| 19 | skew_history_(1ULL << skew_history_size_log2_, 0) {} |
| 20 | |
| 21 | SkewEstimator::~SkewEstimator() = default; |
| 22 | |
| 23 | void SkewEstimator::Reset() { |
| 24 | skew_ = 0; |
| 25 | skew_sum_ = 0; |
| 26 | next_index_ = 0; |
| 27 | sufficient_skew_stored_ = false; |
| 28 | std::fill(skew_history_.begin(), skew_history_.end(), 0); |
| 29 | } |
| 30 | |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 31 | absl::optional<int> SkewEstimator::GetSkewFromCapture() { |
Per Åhgren | 39f491e | 2018-02-22 00:39:23 +0100 | [diff] [blame] | 32 | --skew_; |
| 33 | |
| 34 | skew_sum_ += skew_ - skew_history_[next_index_]; |
| 35 | skew_history_[next_index_] = skew_; |
| 36 | if (++next_index_ == skew_history_.size()) { |
| 37 | next_index_ = 0; |
| 38 | sufficient_skew_stored_ = true; |
| 39 | } |
| 40 | |
Gustaf Ullberg | 41c11e4 | 2018-05-22 08:59:29 +0200 | [diff] [blame] | 41 | const int bias = static_cast<int>(skew_history_.size()) >> 1; |
| 42 | const int average = (skew_sum_ + bias) >> skew_history_size_log2_; |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 43 | return sufficient_skew_stored_ ? absl::optional<int>(average) : absl::nullopt; |
Per Åhgren | 39f491e | 2018-02-22 00:39:23 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | } // namespace webrtc |