OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 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 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_MEAN_CALCULATOR_H_ |
| 12 #define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_MEAN_CALCULATOR_H_ |
| 13 |
| 14 #include <stddef.h> |
| 15 #include <vector> |
| 16 |
| 17 #include "webrtc/base/criticalsection.h" |
| 18 #include "webrtc/base/constructormagic.h" |
| 19 #include "webrtc/base/optional.h" |
| 20 |
| 21 namespace webrtc { |
| 22 |
| 23 // MeanCalculator calculates the mean of a sample sequence of a certain length. |
| 24 class MeanCalculator { |
| 25 public: |
| 26 explicit MeanCalculator(size_t window_length); |
| 27 |
| 28 // Add one sample to the sequence. |
| 29 void AddSample(float sample); |
| 30 |
| 31 // Get the mean of the latest samples. Returns the mean if it is available, |
| 32 // otherwise null, which happens when the added samples have not fully filled |
| 33 // the window. |
| 34 rtc::Optional<float> GetMean() const; |
| 35 |
| 36 // Clear all samples added. |
| 37 void Clear(); |
| 38 |
| 39 // Determines if the window is full. This is a quick way of checking if the |
| 40 // mean is ready. |
| 41 bool IsWindowFull() const; |
| 42 |
| 43 private: |
| 44 // Update |sum_| and |compensation_| using Kahan algorithm. |
| 45 void KahanSum(float sample); |
| 46 |
| 47 rtc::CriticalSection crit_; |
| 48 size_t window_length_ GUARDED_BY(crit_); |
| 49 std::vector<float> buffer_ GUARDED_BY(crit_); |
| 50 size_t head_ GUARDED_BY(crit_); |
| 51 bool full_ GUARDED_BY(crit_); |
| 52 float sum_ GUARDED_BY(crit_); |
| 53 float compensation_ GUARDED_BY(crit_); |
| 54 |
| 55 RTC_DISALLOW_COPY_AND_ASSIGN(MeanCalculator); |
| 56 }; |
| 57 |
| 58 } // namespace webrtc |
| 59 |
| 60 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_MEAN_CALCULATOR_H |
OLD | NEW |