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 | |
Sam Zackrisson | f3f6159 | 2019-09-05 11:30:49 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AEC3_SPECTRUM_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_SPECTRUM_BUFFER_H_ |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 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 | |
Sam Zackrisson | 98872dc | 2019-10-18 08:20:09 +0200 | [diff] [blame] | 16 | #include <array> |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Sam Zackrisson | 98872dc | 2019-10-18 08:20:09 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/aec3/aec3_common.h" |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | // Struct for bundling a circular buffer of one dimensional vector objects |
| 25 | // together with the read and write indices. |
Sam Zackrisson | f3f6159 | 2019-09-05 11:30:49 +0200 | [diff] [blame] | 26 | struct SpectrumBuffer { |
Sam Zackrisson | 98872dc | 2019-10-18 08:20:09 +0200 | [diff] [blame] | 27 | SpectrumBuffer(size_t size, size_t num_channels); |
Sam Zackrisson | f3f6159 | 2019-09-05 11:30:49 +0200 | [diff] [blame] | 28 | ~SpectrumBuffer(); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 29 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 30 | int IncIndex(int index) const { |
| 31 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
| 32 | return index < size - 1 ? index + 1 : 0; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 35 | int DecIndex(int index) const { |
| 36 | RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size)); |
| 37 | return index > 0 ? index - 1 : size - 1; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 40 | int OffsetIndex(int index, int offset) const { |
| 41 | RTC_DCHECK_GE(size, offset); |
| 42 | 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] | 43 | RTC_DCHECK_GE(size + index + offset, 0); |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 44 | return (size + index + offset) % size; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); } |
| 48 | void IncWriteIndex() { write = IncIndex(write); } |
| 49 | void DecWriteIndex() { write = DecIndex(write); } |
| 50 | void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); } |
| 51 | void IncReadIndex() { read = IncIndex(read); } |
| 52 | void DecReadIndex() { read = DecIndex(read); } |
| 53 | |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 54 | const int size; |
Sam Zackrisson | 98872dc | 2019-10-18 08:20:09 +0200 | [diff] [blame] | 55 | std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>> buffer; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 56 | int write = 0; |
| 57 | int read = 0; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } // namespace webrtc |
| 61 | |
Sam Zackrisson | f3f6159 | 2019-09-05 11:30:49 +0200 | [diff] [blame] | 62 | #endif // MODULES_AUDIO_PROCESSING_AEC3_SPECTRUM_BUFFER_H_ |