blob: a2099fcd01301c2b95848368a7341fcd6d9e7735 [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>
Per Åhgren39f491e2018-02-22 00:39:23 +010013
14namespace webrtc {
15
16SkewEstimator::SkewEstimator(size_t skew_history_size_log2)
17 : skew_history_size_log2_(static_cast<int>(skew_history_size_log2)),
18 skew_history_(1ULL << skew_history_size_log2_, 0) {}
19
20SkewEstimator::~SkewEstimator() = default;
21
22void SkewEstimator::Reset() {
23 skew_ = 0;
24 skew_sum_ = 0;
25 next_index_ = 0;
26 sufficient_skew_stored_ = false;
27 std::fill(skew_history_.begin(), skew_history_.end(), 0);
28}
29
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020030absl::optional<int> SkewEstimator::GetSkewFromCapture() {
Per Åhgren39f491e2018-02-22 00:39:23 +010031 --skew_;
32
33 skew_sum_ += skew_ - skew_history_[next_index_];
34 skew_history_[next_index_] = skew_;
35 if (++next_index_ == skew_history_.size()) {
36 next_index_ = 0;
37 sufficient_skew_stored_ = true;
38 }
39
Gustaf Ullberg41c11e42018-05-22 08:59:29 +020040 const int bias = static_cast<int>(skew_history_.size()) >> 1;
41 const int average = (skew_sum_ + bias) >> skew_history_size_log2_;
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020042 return sufficient_skew_stored_ ? absl::optional<int>(average) : absl::nullopt;
Per Åhgren39f491e2018-02-22 00:39:23 +010043}
44
45} // namespace webrtc