blob: 9d6b8c980267670109ad7a1fd271b6c55b728b91 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_
peah522d71b2017-02-23 05:16:26 -080013
Per Åhgren8ba58612017-12-01 23:01:44 +010014#include <array>
peah522d71b2017-02-23 05:16:26 -080015#include <memory>
peah522d71b2017-02-23 05:16:26 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/array_view.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010018#include "modules/audio_processing/aec3/fft_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/aec3/fft_data.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010020#include "modules/audio_processing/aec3/matrix_buffer.h"
21#include "modules/audio_processing/aec3/vector_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/constructormagic.h"
peah522d71b2017-02-23 05:16:26 -080023
24namespace webrtc {
25
peahcf02cf12017-04-05 14:18:07 -070026// Provides a buffer of the render data for the echo remover.
27class RenderBuffer {
peah522d71b2017-02-23 05:16:26 -080028 public:
Per Åhgrenec22e3f2017-12-20 15:20:37 +010029 RenderBuffer(MatrixBuffer* block_buffer,
Per Åhgren8ba58612017-12-01 23:01:44 +010030 VectorBuffer* spectrum_buffer,
31 FftBuffer* fft_buffer);
peahcf02cf12017-04-05 14:18:07 -070032 ~RenderBuffer();
peah522d71b2017-02-23 05:16:26 -080033
Per Åhgrenec22e3f2017-12-20 15:20:37 +010034 // Get a block.
35 const std::vector<std::vector<float>>& Block(int buffer_offset_blocks) const {
36 int position =
37 block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks);
38 return block_buffer_->buffer[position];
peahcf02cf12017-04-05 14:18:07 -070039 }
peah522d71b2017-02-23 05:16:26 -080040
Per Åhgren8ba58612017-12-01 23:01:44 +010041 // Get the spectrum from one of the FFTs in the buffer.
Per Åhgrenec22e3f2017-12-20 15:20:37 +010042 rtc::ArrayView<const float> Spectrum(int buffer_offset_ffts) const {
43 int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read,
44 buffer_offset_ffts);
Per Åhgren8ba58612017-12-01 23:01:44 +010045 return spectrum_buffer_->buffer[position];
peah522d71b2017-02-23 05:16:26 -080046 }
47
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020048 // Get the spectrum directly from a position in the buffer.
49 rtc::ArrayView<const float> SpectrumFromPosition(int position) const {
50 RTC_CHECK_LT(position, spectrum_buffer_->size);
51 int position_bound = std::min(position, spectrum_buffer_->size - 1);
52 position_bound = std::max(0, position_bound);
53 return spectrum_buffer_->buffer[position_bound];
54 }
55
Per Åhgrenec22e3f2017-12-20 15:20:37 +010056 // Returns the circular fft buffer.
57 rtc::ArrayView<const FftData> GetFftBuffer() const {
58 return fft_buffer_->buffer;
peah522d71b2017-02-23 05:16:26 -080059 }
60
Per Åhgren8ba58612017-12-01 23:01:44 +010061 // Returns the current position in the circular buffer.
Per Åhgrenec22e3f2017-12-20 15:20:37 +010062 size_t Position() const {
63 RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read);
64 RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write);
65 return fft_buffer_->read;
66 }
67
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020068 // Returns the write postion in the circular buffer.
69 int GetWritePositionSpectrum() const { return spectrum_buffer_->write; }
70
Per Åhgrenec22e3f2017-12-20 15:20:37 +010071 // Returns the sum of the spectrums for a certain number of FFTs.
72 void SpectralSum(size_t num_spectra,
73 std::array<float, kFftLengthBy2Plus1>* X2) const;
peah522d71b2017-02-23 05:16:26 -080074
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010075 // Gets the recent activity seen in the render signal.
76 bool GetRenderActivity() const { return render_activity_; }
77
78 // Specifies the recent activity seen in the render signal.
79 void SetRenderActivity(bool activity) { render_activity_ = activity; }
80
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020081 // Returns the headroom between the write and the read positions in the
82 // buffer;
83 int Headroom() const {
84 // The write and read indices are decreased over time.
85 int headroom =
86 fft_buffer_->write < fft_buffer_->read
87 ? fft_buffer_->read - fft_buffer_->write
88 : fft_buffer_->size - fft_buffer_->write + fft_buffer_->read;
89
90 RTC_DCHECK_LE(0, headroom);
91 RTC_DCHECK_GE(fft_buffer_->size, headroom);
92
93 return headroom;
94 }
95
Jesús de Vicente Peñadc872b62018-04-25 16:11:42 +020096 // Decrease an index that is used for accessing the buffer.
97 int DecIdx(int idx) const { return spectrum_buffer_->DecIndex(idx); }
98
peah522d71b2017-02-23 05:16:26 -080099 private:
Per Åhgren8ba58612017-12-01 23:01:44 +0100100 const MatrixBuffer* const block_buffer_;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100101 const VectorBuffer* const spectrum_buffer_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100102 const FftBuffer* const fft_buffer_;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100103 bool render_activity_ = false;
peahcf02cf12017-04-05 14:18:07 -0700104 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderBuffer);
peah522d71b2017-02-23 05:16:26 -0800105};
106
107} // namespace webrtc
108
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200109#endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_