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