| Index: webrtc/modules/audio_processing/utility/mean_calculator.cc
|
| diff --git a/webrtc/modules/audio_processing/utility/mean_calculator.cc b/webrtc/modules/audio_processing/utility/mean_calculator.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0b3b46d8cb189d57e2e7540bc55ae5ee771ecacd
|
| --- /dev/null
|
| +++ b/webrtc/modules/audio_processing/utility/mean_calculator.cc
|
| @@ -0,0 +1,74 @@
|
| +/*
|
| + * Copyright 2016 The WebRTC Project Authors. All rights reserved.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license
|
| + * that can be found in the LICENSE file in the root of the source
|
| + * tree. An additional intellectual property rights grant can be found
|
| + * in the file PATENTS. All contributing project authors may
|
| + * be found in the AUTHORS file in the root of the source tree.
|
| + */
|
| +
|
| +#include "webrtc/modules/audio_processing/utility/mean_calculator.h"
|
| +
|
| +namespace webrtc {
|
| +
|
| +MeanCalculator::MeanCalculator(size_t window_length)
|
| + : window_length_(window_length),
|
| + head_(0),
|
| + full_(false),
|
| + sum_(0.0f),
|
| + compensation_(0.0f) {
|
| + buffer_.resize(window_length);
|
| +}
|
| +
|
| +// MeanCalculator::~MeanCalculator() = default;
|
| +
|
| +// Add one sample to the sequence.
|
| +void MeanCalculator::AddSample(float sample) {
|
| + rtc::CritScope cs(&crit_);
|
| + if (full_)
|
| + KahanSum(-buffer_[head_]);
|
| + buffer_[head_] = sample;
|
| + KahanSum(buffer_[head_]);
|
| + head_ = (head_ + 1) % window_length_;
|
| + if (!full_ && head_ == 0)
|
| + full_ = true;
|
| +}
|
| +
|
| +// Get the mean of the latest samples. Returns the mean if it is available,
|
| +// otherwise null, which happens when the added samples have not fully filled
|
| +// the window.
|
| +rtc::Optional<float> MeanCalculator::GetMean() const {
|
| + rtc::CritScope cs(&crit_);
|
| + if (full_) {
|
| + return rtc::Optional<float>(sum_ / window_length_);
|
| + } else {
|
| + return rtc::Optional<float>();
|
| + }
|
| +}
|
| +
|
| +// Flush all samples added.
|
| +void MeanCalculator::Clear() {
|
| + rtc::CritScope cs(&crit_);
|
| + head_ = 0;
|
| + full_ = false;
|
| + sum_ = 0.0f;
|
| + compensation_ = 0.0f;
|
| +}
|
| +
|
| +// Determines if the window is full. This is a quick way of checking if the
|
| +// mean is ready.
|
| +bool MeanCalculator::IsWindowFull() const {
|
| + rtc::CritScope cs(&crit_);
|
| + return full_;
|
| +}
|
| +
|
| +void MeanCalculator::KahanSum(float sample) {
|
| + rtc::CritScope cs(&crit_);
|
| + const float compensated_sample = sample - compensation_;
|
| + const float temp_sum = sum_ + compensated_sample;
|
| + compensation_ = (temp_sum - sum_) - compensated_sample;
|
| + sum_ = temp_sum;
|
| +}
|
| +
|
| +} // namespace webrtc
|
|
|