Index: webrtc/modules/audio_processing/utility/mean_calculator.h |
diff --git a/webrtc/modules/audio_processing/utility/mean_calculator.h b/webrtc/modules/audio_processing/utility/mean_calculator.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c9184ab211688ffec9ae3b4febfc9fd3af68f402 |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/utility/mean_calculator.h |
@@ -0,0 +1,60 @@ |
+/* |
+ * 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. |
+ */ |
+ |
+#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_MEAN_CALCULATOR_H_ |
+#define WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_MEAN_CALCULATOR_H_ |
+ |
+#include <stddef.h> |
+#include <vector> |
+ |
+#include "webrtc/base/criticalsection.h" |
+#include "webrtc/base/constructormagic.h" |
+#include "webrtc/base/optional.h" |
+ |
+namespace webrtc { |
+ |
+// MeanCalculator calculates the mean of a sample sequence of a certain length. |
+class MeanCalculator { |
+ public: |
+ explicit MeanCalculator(size_t window_length); |
+ |
+ // Add one sample to the sequence. |
+ void AddSample(float sample); |
+ |
+ // 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> GetMean() const; |
+ |
+ // Clear all samples added. |
+ void Clear(); |
+ |
+ // Determines if the window is full. This is a quick way of checking if the |
+ // mean is ready. |
+ bool IsWindowFull() const; |
+ |
+ private: |
+ // Update |sum_| and |compensation_| using Kahan algorithm. |
+ void KahanSum(float sample); |
+ |
+ rtc::CriticalSection crit_; |
+ size_t window_length_ GUARDED_BY(crit_); |
+ std::vector<float> buffer_ GUARDED_BY(crit_); |
+ size_t head_ GUARDED_BY(crit_); |
+ bool full_ GUARDED_BY(crit_); |
+ float sum_ GUARDED_BY(crit_); |
+ float compensation_ GUARDED_BY(crit_); |
+ |
+ RTC_DISALLOW_COPY_AND_ASSIGN(MeanCalculator); |
+}; |
+ |
+} // namespace webrtc |
+ |
+#endif // WEBRTC_MODULES_AUDIO_PROCESSING_UTILITY_MEAN_CALCULATOR_H |