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 #ifndef MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_BUFFER_H_ 12 #define MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_BUFFER_H_ 13 14 #include <array> 15 16 #include "absl/types/optional.h" 17 #include "modules/audio_processing/agc2/agc2_common.h" 18 19 namespace webrtc { 20 21 // Ring buffer for the saturation protector which only supports (i) push back 22 // and (ii) read oldest item. 23 class SaturationProtectorBuffer { 24 public: 25 SaturationProtectorBuffer(); 26 ~SaturationProtectorBuffer(); 27 28 bool operator==(const SaturationProtectorBuffer& b) const; 29 inline bool operator!=(const SaturationProtectorBuffer& b) const { 30 return !(*this == b); 31 } 32 33 // Maximum number of values that the buffer can contain. 34 int Capacity() const; 35 36 // Number of values in the buffer. 37 int Size() const; 38 39 void Reset(); 40 41 // Pushes back `v`. If the buffer is full, the oldest value is replaced. 42 void PushBack(float v); 43 44 // Returns the oldest item in the buffer. Returns an empty value if the 45 // buffer is empty. 46 absl::optional<float> Front() const; 47 48 private: 49 int FrontIndex() const; 50 // `buffer_` has `size_` elements (up to the size of `buffer_`) and `next_` is 51 // the position where the next new value is written in `buffer_`. 52 std::array<float, kSaturationProtectorBufferSize> buffer_; 53 int next_ = 0; 54 int size_ = 0; 55 }; 56 57 } // namespace webrtc 58 59 #endif // MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_BUFFER_H_ 60