blob: 9f81a910a8fb7094f87d0515aa2baf30e1d8040b [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>
Per Åhgren8ba58612017-12-01 23:01:44 +010015#include <vector>
16
17#include "modules/audio_processing/aec3/fft_data.h"
18#include "rtc_base/checks.h"
19
20namespace webrtc {
21
22// Struct for bundling a circular buffer of FftData objects together with the
23// read and write indices.
24struct FftBuffer {
25 explicit FftBuffer(size_t size);
26 ~FftBuffer();
27
Per Åhgrenc59a5762017-12-11 21:34:19 +010028 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 Åhgren8ba58612017-12-01 23:01:44 +010031 }
32
Per Åhgrenc59a5762017-12-11 21:34:19 +010033 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 Åhgren8ba58612017-12-01 23:01:44 +010036 }
37
Per Åhgrenc59a5762017-12-11 21:34:19 +010038 int OffsetIndex(int index, int offset) const {
Per Åhgren8ba58612017-12-01 23:01:44 +010039 RTC_DCHECK_GE(buffer.size(), offset);
Per Åhgrenc59a5762017-12-11 21:34:19 +010040 RTC_DCHECK_EQ(buffer.size(), static_cast<size_t>(size));
41 return (size + index + offset) % size;
Per Åhgren8ba58612017-12-01 23:01:44 +010042 }
43
44 void UpdateWriteIndex(int offset) { write = OffsetIndex(write, offset); }
45 void IncWriteIndex() { write = IncIndex(write); }
46 void DecWriteIndex() { write = DecIndex(write); }
47 void UpdateReadIndex(int offset) { read = OffsetIndex(read, offset); }
48 void IncReadIndex() { read = IncIndex(read); }
49 void DecReadIndex() { read = DecIndex(read); }
50
Per Åhgrenc59a5762017-12-11 21:34:19 +010051 const int size;
Per Åhgren8ba58612017-12-01 23:01:44 +010052 std::vector<FftData> buffer;
Per Åhgrenc59a5762017-12-11 21:34:19 +010053 int write = 0;
54 int read = 0;
Per Åhgren8ba58612017-12-01 23:01:44 +010055};
56
57} // namespace webrtc
58
59#endif // MODULES_AUDIO_PROCESSING_AEC3_FFT_BUFFER_H_