blob: 4187315863fca19f4216f1e8b61ab24c241ab7de [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_FFT_BUFFER_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_FFT_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 "modules/audio_processing/aec3/fft_data.h"
19#include "rtc_base/checks.h"
20
21namespace webrtc {
22
23// Struct for bundling a circular buffer of FftData objects together with the
24// read and write indices.
25struct FftBuffer {
Sam Zackrissoncfb94972019-09-05 15:03:07 +020026 FftBuffer(size_t size, size_t num_channels);
Per Åhgren8ba58612017-12-01 23:01:44 +010027 ~FftBuffer();
28
Per Åhgrenc59a5762017-12-11 21:34:19 +010029 int IncIndex(int index) const {
30 RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
31 return index < size - 1 ? index + 1 : 0;
Per Åhgren8ba58612017-12-01 23:01:44 +010032 }
33
Per Åhgrenc59a5762017-12-11 21:34:19 +010034 int DecIndex(int index) const {
35 RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
36 return index > 0 ? index - 1 : size - 1;
Per Åhgren8ba58612017-12-01 23:01:44 +010037 }
38
Per Åhgrenc59a5762017-12-11 21:34:19 +010039 int OffsetIndex(int index, int offset) const {
Per Åhgren8ba58612017-12-01 23:01:44 +010040 RTC_DCHECK_GE(buffer.size(), offset);
Per Åhgrenc59a5762017-12-11 21:34:19 +010041 RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
42 return (size + index + offset) % size;
Per Åhgren8ba58612017-12-01 23:01:44 +010043 }
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 Åhgrenc59a5762017-12-11 21:34:19 +010052 const int size;
Sam Zackrissoncfb94972019-09-05 15:03:07 +020053 std::vector<std::vector<FftData>> buffer;
Per Åhgrenc59a5762017-12-11 21:34:19 +010054 int write = 0;
55 int read = 0;
Per Åhgren8ba58612017-12-01 23:01:44 +010056};
57
58} // namespace webrtc
59
60#endif // MODULES_AUDIO_PROCESSING_AEC3_FFT_BUFFER_H_