blob: 310e4e9dd1da22c541ba3a0708b4a44fef4b05fb [file] [log] [blame]
Per Åhgren39f491e2018-02-22 00:39:23 +01001/*
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
15namespace webrtc {
16
17SkewEstimator::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
21SkewEstimator::~SkewEstimator() = default;
22
23void 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 Chapovalovdb9f7ab2018-06-19 10:50:11 +020031absl::optional<int> SkewEstimator::GetSkewFromCapture() {
Per Åhgren39f491e2018-02-22 00:39:23 +010032 --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 Ullberg41c11e42018-05-22 08:59:29 +020041 const int bias = static_cast<int>(skew_history_.size()) >> 1;
42 const int average = (skew_sum_ + bias) >> skew_history_size_log2_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020043 return sufficient_skew_stored_ ? absl::optional<int>(average) : absl::nullopt;
Per Åhgren39f491e2018-02-22 00:39:23 +010044}
45
46} // namespace webrtc