| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_processing/utility/block_mean_calculator.h" | 11 #include "webrtc/modules/audio_processing/utility/block_fraction_calculator.h" |
| 12 | 12 |
| 13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 14 | 14 |
| 15 namespace webrtc { | 15 namespace webrtc { |
| 16 | 16 |
| 17 BlockMeanCalculator::BlockMeanCalculator(size_t block_length) | 17 BlockFractionCalculator::BlockFractionCalculator(size_t block_length) |
| 18 : block_length_(block_length), | 18 : block_length_(block_length), |
| 19 count_(0), | 19 count_(0), |
| 20 sum_(0.0), | 20 occurrence_(0), |
| 21 mean_(0.0) { | 21 fraction_(-1.0) { |
| 22 RTC_DCHECK(block_length_ != 0); | 22 RTC_DCHECK(block_length_ != 0); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void BlockMeanCalculator::Reset() { | 25 void BlockFractionCalculator::Reset() { |
| 26 Clear(); | 26 Clear(); |
| 27 mean_ = 0.0; | 27 fraction_ = -1.0; |
| 28 } | 28 } |
| 29 | 29 |
| 30 void BlockMeanCalculator::AddValue(float value) { | 30 void BlockFractionCalculator::AddObservation(bool occurred) { |
| 31 sum_ += value; | 31 if (occurred) |
| 32 occurrence_++; |
| 32 ++count_; | 33 ++count_; |
| 33 if (count_ == block_length_) { | 34 if (count_ == block_length_) { |
| 34 mean_ = sum_ / block_length_; | 35 fraction_ = static_cast<float>(occurrence_) / block_length_; |
| 35 Clear(); | 36 Clear(); |
| 36 } | 37 } |
| 37 } | 38 } |
| 38 | 39 |
| 39 bool BlockMeanCalculator::EndOfBlock() const { | 40 float BlockFractionCalculator::GetLatestFraction() const { |
| 40 return count_ == 0; | 41 return fraction_; |
| 41 } | |
| 42 | |
| 43 float BlockMeanCalculator::GetLatestMean() const { | |
| 44 return mean_; | |
| 45 } | 42 } |
| 46 | 43 |
| 47 // Flush all samples added. | 44 // Flush all samples added. |
| 48 void BlockMeanCalculator::Clear() { | 45 void BlockFractionCalculator::Clear() { |
| 49 count_ = 0; | 46 count_ = 0; |
| 50 sum_ = 0.0; | 47 occurrence_ = 0; |
| 51 } | 48 } |
| 52 | 49 |
| 53 } // namespace webrtc | 50 } // namespace webrtc |
| OLD | NEW |