1 /*
2  *  Copyright (c) 2017 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_AEC3_DOWNSAMPLED_RENDER_BUFFER_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_DOWNSAMPLED_RENDER_BUFFER_H_
13 
14 #include <stddef.h>
15 
16 #include <vector>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
22 // Holds the circular buffer of the downsampled render data.
23 struct DownsampledRenderBuffer {
24   explicit DownsampledRenderBuffer(size_t downsampled_buffer_size);
25   ~DownsampledRenderBuffer();
26 
IncIndexDownsampledRenderBuffer27   int IncIndex(int index) const {
28     RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
29     return index < size - 1 ? index + 1 : 0;
30   }
31 
DecIndexDownsampledRenderBuffer32   int DecIndex(int index) const {
33     RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
34     return index > 0 ? index - 1 : size - 1;
35   }
36 
OffsetIndexDownsampledRenderBuffer37   int OffsetIndex(int index, int offset) const {
38     RTC_DCHECK_GE(buffer.size(), offset);
39     RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
40     return (size + index + offset) % size;
41   }
42 
UpdateWriteIndexDownsampledRenderBuffer43   void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); }
IncWriteIndexDownsampledRenderBuffer44   void IncWriteIndex() { write = IncIndex(write); }
DecWriteIndexDownsampledRenderBuffer45   void DecWriteIndex() { write = DecIndex(write); }
UpdateReadIndexDownsampledRenderBuffer46   void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); }
IncReadIndexDownsampledRenderBuffer47   void IncReadIndex() { read = IncIndex(read); }
DecReadIndexDownsampledRenderBuffer48   void DecReadIndex() { read = DecIndex(read); }
49 
50   const int size;
51   std::vector<float> buffer;
52   int write = 0;
53   int read = 0;
54 };
55 
56 }  // namespace webrtc
57 
58 #endif  // MODULES_AUDIO_PROCESSING_AEC3_DOWNSAMPLED_RENDER_BUFFER_H_
59