circular_buffer.h (1309B)
1 /* 2 * Copyright (c) 2016 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_ECHO_DETECTOR_CIRCULAR_BUFFER_H_ 12 #define MODULES_AUDIO_PROCESSING_ECHO_DETECTOR_CIRCULAR_BUFFER_H_ 13 14 #include <stddef.h> 15 16 #include <optional> 17 #include <vector> 18 19 namespace webrtc { 20 21 // Ring buffer containing floating point values. 22 struct CircularBuffer { 23 public: 24 explicit CircularBuffer(size_t size); 25 ~CircularBuffer(); 26 27 void Push(float value); 28 std::optional<float> Pop(); 29 size_t Size() const { return nr_elements_in_buffer_; } 30 // This function fills the buffer with zeros, but does not change its size. 31 void Clear(); 32 33 private: 34 std::vector<float> buffer_; 35 size_t next_insertion_index_ = 0; 36 // This is the number of elements that have been pushed into the circular 37 // buffer, not the allocated buffer size. 38 size_t nr_elements_in_buffer_ = 0; 39 }; 40 41 } // namespace webrtc 42 43 #endif // MODULES_AUDIO_PROCESSING_ECHO_DETECTOR_CIRCULAR_BUFFER_H_