saturation_protector_buffer.cc (2045B)
1 /* 2 * Copyright (c) 2021 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 #include "modules/audio_processing/agc2/saturation_protector_buffer.h" 12 13 #include <optional> 14 15 #include "rtc_base/checks.h" 16 #include "rtc_base/numerics/safe_compare.h" 17 18 namespace webrtc { 19 20 SaturationProtectorBuffer::SaturationProtectorBuffer() = default; 21 22 SaturationProtectorBuffer::~SaturationProtectorBuffer() = default; 23 24 bool SaturationProtectorBuffer::operator==( 25 const SaturationProtectorBuffer& b) const { 26 RTC_DCHECK_LE(size_, buffer_.size()); 27 RTC_DCHECK_LE(b.size_, b.buffer_.size()); 28 if (size_ != b.size_) { 29 return false; 30 } 31 for (int i = 0, i0 = FrontIndex(), i1 = b.FrontIndex(); i < size_; 32 ++i, ++i0, ++i1) { 33 if (buffer_[i0 % buffer_.size()] != b.buffer_[i1 % b.buffer_.size()]) { 34 return false; 35 } 36 } 37 return true; 38 } 39 40 int SaturationProtectorBuffer::Capacity() const { 41 return buffer_.size(); 42 } 43 44 int SaturationProtectorBuffer::Size() const { 45 return size_; 46 } 47 48 void SaturationProtectorBuffer::Reset() { 49 next_ = 0; 50 size_ = 0; 51 } 52 53 void SaturationProtectorBuffer::PushBack(float v) { 54 RTC_DCHECK_GE(next_, 0); 55 RTC_DCHECK_GE(size_, 0); 56 RTC_DCHECK_LT(next_, buffer_.size()); 57 RTC_DCHECK_LE(size_, buffer_.size()); 58 buffer_[next_++] = v; 59 if (SafeEq(next_, buffer_.size())) { 60 next_ = 0; 61 } 62 if (SafeLt(size_, buffer_.size())) { 63 size_++; 64 } 65 } 66 67 std::optional<float> SaturationProtectorBuffer::Front() const { 68 if (size_ == 0) { 69 return std::nullopt; 70 } 71 RTC_DCHECK_LT(FrontIndex(), buffer_.size()); 72 return buffer_[FrontIndex()]; 73 } 74 75 int SaturationProtectorBuffer::FrontIndex() const { 76 return SafeEq(size_, buffer_.size()) ? next_ : 0; 77 } 78 79 } // namespace webrtc