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, size_t S, size_t N, size_t 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: SequenceBuffer()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; size()48 size_t size() const { return S; } chunks_size()49 size_t chunks_size() const { return N; } 50 // Sets the sequence buffer values to zero. Reset()51 void Reset() { std::fill(buffer_.begin(), buffer_.end(), 0); } 52 // Returns a view on the whole buffer. GetBufferView()53 rtc::ArrayView<const T, S> GetBufferView() const { 54 return {buffer_.data(), S}; 55 } 56 // Returns a view on the M most recent values of the buffer. GetMostRecentValuesView()57 rtc::ArrayView<const T, M> GetMostRecentValuesView() const { 58 static_assert(M <= S, 59 "The number of most recent values cannot be larger than the " 60 "sequence buffer size."); 61 return {buffer_.data() + S - M, M}; 62 } 63 // Shifts left the buffer by N items and add new N items at the end. Push(rtc::ArrayView<const T,N> new_values)64 void Push(rtc::ArrayView<const T, N> new_values) { 65 // Make space for the new values. 66 if (S > N) 67 std::memmove(buffer_.data(), buffer_.data() + N, (S - N) * sizeof(T)); 68 // Copy the new values at the end of the buffer. 69 std::memcpy(buffer_.data() + S - N, new_values.data(), N * sizeof(T)); 70 } 71 72 private: 73 std::vector<T> buffer_; 74 }; 75 76 } // namespace rnn_vad 77 } // namespace webrtc 78 79 #endif // MODULES_AUDIO_PROCESSING_AGC2_RNN_VAD_SEQUENCE_BUFFER_H_ 80