Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 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_VECTOR_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_VECTOR_BUFFER_H_ |
| 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 15 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
| 18 | #include "rtc_base/checks.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // Struct for bundling a circular buffer of one dimensional vector objects |
| 23 | // together with the read and write indices. |
| 24 | struct VectorBuffer { |
| 25 | VectorBuffer(size_t size, size_t height); |
| 26 | ~VectorBuffer(); |
| 27 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 28 | int IncIndex(int index) const { |
| 29 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
| 30 | return index < size - 1 ? index + 1 : 0; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 33 | int DecIndex(int index) const { |
| 34 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
| 35 | return index > 0 ? index - 1 : size - 1; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 38 | int OffsetIndex(int index, int offset) const { |
| 39 | RTC_DCHECK_GE(size, offset); |
| 40 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
Jesús de Vicente Peña | dd09287 | 2018-05-25 16:55:11 +0200 | [diff] [blame] | 41 | RTC_DCHECK_GE(size + index + offset, 0); |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 42 | return (size + index + offset) % size; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); } |
| 46 | void IncWriteIndex() { write = IncIndex(write); } |
| 47 | void DecWriteIndex() { write = DecIndex(write); } |
| 48 | void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); } |
| 49 | void IncReadIndex() { read = IncIndex(read); } |
| 50 | void DecReadIndex() { read = DecIndex(read); } |
| 51 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 52 | const int size; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 53 | std::vector<std::vector<float>> buffer; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 54 | int write = 0; |
| 55 | int read = 0; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | } // namespace webrtc |
| 59 | |
| 60 | #endif // MODULES_AUDIO_PROCESSING_AEC3_VECTOR_BUFFER_H_ |