blob: 97736a3096fff0ae9d4d8e9e76e4890e3da588e4 [file] [log] [blame]
Per Åhgren8ba58612017-12-01 23:01:44 +01001/*
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_MATRIX_BUFFER_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_MATRIX_BUFFER_H_
13
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Per Åhgren8ba58612017-12-01 23:01:44 +010016#include <vector>
17
18#include "rtc_base/checks.h"
19
20namespace webrtc {
21
22// Struct for bundling a circular buffer of two dimensional vector objects
23// together with the read and write indices.
Per Åhgrence202a02019-09-02 17:01:19 +020024// TODO(peah): Change name of this class to be more specific to what it does.
Per Åhgren8ba58612017-12-01 23:01:44 +010025struct MatrixBuffer {
Per Åhgrence202a02019-09-02 17:01:19 +020026 MatrixBuffer(size_t size,
27 size_t num_bands,
28 size_t num_channels,
29 size_t frame_length);
Per Åhgren8ba58612017-12-01 23:01:44 +010030 ~MatrixBuffer();
31
Per Åhgrenc59a5762017-12-11 21:34:19 +010032 int IncIndex(int index) const {
33 RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
34 return index < size - 1 ? index + 1 : 0;
Per Åhgren8ba58612017-12-01 23:01:44 +010035 }
36
Per Åhgrenc59a5762017-12-11 21:34:19 +010037 int DecIndex(int index) const {
38 RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
39 return index > 0 ? index - 1 : size - 1;
Per Åhgren8ba58612017-12-01 23:01:44 +010040 }
41
Per Åhgrenc59a5762017-12-11 21:34:19 +010042 int OffsetIndex(int index, int offset) const {
43 RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
44 RTC_DCHECK_GE(size, offset);
45 return (size + index + offset) % size;
Per Åhgren8ba58612017-12-01 23:01:44 +010046 }
47
48 void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); }
49 void IncWriteIndex() { write = IncIndex(write); }
50 void DecWriteIndex() { write = DecIndex(write); }
51 void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); }
52 void IncReadIndex() { read = IncIndex(read); }
53 void DecReadIndex() { read = DecIndex(read); }
54
Per Åhgrenc59a5762017-12-11 21:34:19 +010055 const int size;
Per Åhgrence202a02019-09-02 17:01:19 +020056 std::vector<std::vector<std::vector<std::vector<float>>>> buffer;
Per Åhgrenc59a5762017-12-11 21:34:19 +010057 int write = 0;
58 int read = 0;
Per Åhgren8ba58612017-12-01 23:01:44 +010059};
60
61} // namespace webrtc
62
63#endif // MODULES_AUDIO_PROCESSING_AEC3_MATRIX_BUFFER_H_