tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

circular_buffer.cc (1545B)


      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 #include "modules/audio_processing/echo_detector/circular_buffer.h"
     12 
     13 #include <algorithm>
     14 #include <cstddef>
     15 #include <optional>
     16 
     17 #include "rtc_base/checks.h"
     18 
     19 namespace webrtc {
     20 
     21 CircularBuffer::CircularBuffer(size_t size) : buffer_(size) {}
     22 CircularBuffer::~CircularBuffer() = default;
     23 
     24 void CircularBuffer::Push(float value) {
     25  buffer_[next_insertion_index_] = value;
     26  ++next_insertion_index_;
     27  next_insertion_index_ %= buffer_.size();
     28  RTC_DCHECK_LT(next_insertion_index_, buffer_.size());
     29  nr_elements_in_buffer_ = std::min(nr_elements_in_buffer_ + 1, buffer_.size());
     30  RTC_DCHECK_LE(nr_elements_in_buffer_, buffer_.size());
     31 }
     32 
     33 std::optional<float> CircularBuffer::Pop() {
     34  if (nr_elements_in_buffer_ == 0) {
     35    return std::nullopt;
     36  }
     37  const size_t index =
     38      (buffer_.size() + next_insertion_index_ - nr_elements_in_buffer_) %
     39      buffer_.size();
     40  RTC_DCHECK_LT(index, buffer_.size());
     41  --nr_elements_in_buffer_;
     42  return buffer_[index];
     43 }
     44 
     45 void CircularBuffer::Clear() {
     46  std::fill(buffer_.begin(), buffer_.end(), 0.f);
     47  next_insertion_index_ = 0;
     48  nr_elements_in_buffer_ = 0;
     49 }
     50 
     51 }  // namespace webrtc