tor-browser

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

sequence_buffer.h (2891B)


      1 /*
      2 *  Copyright (c) 2018 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_RNN_VAD_SEQUENCE_BUFFER_H_
     12 #define MODULES_AUDIO_PROCESSING_AGC2_RNN_VAD_SEQUENCE_BUFFER_H_
     13 
     14 #include <algorithm>
     15 #include <cstring>
     16 #include <type_traits>
     17 #include <vector>
     18 
     19 #include "api/array_view.h"
     20 #include "rtc_base/checks.h"
     21 
     22 namespace webrtc {
     23 namespace rnn_vad {
     24 
     25 // Linear buffer implementation to (i) push fixed size chunks of sequential data
     26 // and (ii) view contiguous parts of the buffer. The buffer and the pushed
     27 // chunks have size S and N respectively. For instance, when S = 2N the first
     28 // half of the sequence buffer is replaced with its second half, and the new N
     29 // values are written at the end of the buffer.
     30 // The class also provides a view on the most recent M values, where 0 < M <= S
     31 // and by default M = N.
     32 template <typename T, int S, int N, int M = N>
     33 class SequenceBuffer {
     34  static_assert(N <= S,
     35                "The new chunk size cannot be larger than the sequence buffer "
     36                "size.");
     37  static_assert(std::is_arithmetic<T>::value,
     38                "Integral or floating point required.");
     39 
     40 public:
     41  SequenceBuffer() : buffer_(S) {
     42    RTC_DCHECK_EQ(S, buffer_.size());
     43    Reset();
     44  }
     45  SequenceBuffer(const SequenceBuffer&) = delete;
     46  SequenceBuffer& operator=(const SequenceBuffer&) = delete;
     47  ~SequenceBuffer() = default;
     48  int size() const { return S; }
     49  int chunks_size() const { return N; }
     50  // Sets the sequence buffer values to zero.
     51  void Reset() { std::fill(buffer_.begin(), buffer_.end(), 0); }
     52  // Returns a view on the whole buffer.
     53  ArrayView<const T, S> GetBufferView() const { return {buffer_.data(), S}; }
     54  // Returns a view on the M most recent values of the buffer.
     55  ArrayView<const T, M> GetMostRecentValuesView() const {
     56    static_assert(M <= S,
     57                  "The number of most recent values cannot be larger than the "
     58                  "sequence buffer size.");
     59    return {buffer_.data() + S - M, M};
     60  }
     61  // Shifts left the buffer by N items and add new N items at the end.
     62  void Push(ArrayView<const T, N> new_values) {
     63    // Make space for the new values.
     64    if (S > N)
     65      std::memmove(buffer_.data(), buffer_.data() + N, (S - N) * sizeof(T));
     66    // Copy the new values at the end of the buffer.
     67    std::memcpy(buffer_.data() + S - N, new_values.data(), N * sizeof(T));
     68  }
     69 
     70 private:
     71  std::vector<T> buffer_;
     72 };
     73 
     74 }  // namespace rnn_vad
     75 }  // namespace webrtc
     76 
     77 #endif  // MODULES_AUDIO_PROCESSING_AGC2_RNN_VAD_SEQUENCE_BUFFER_H_