blob: cc6cd1c12a5808cd8d1bbe18514ab5c05d9a28e5 [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Per Åhgren8ba58612017-12-01 23:01:44 +010015#include <array>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <vector>
peah522d71b2017-02-23 05:16:26 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "modules/audio_processing/aec3/aec3_common.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010020#include "modules/audio_processing/aec3/fft_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/aec3/fft_data.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010022#include "modules/audio_processing/aec3/matrix_buffer.h"
23#include "modules/audio_processing/aec3/vector_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/constructor_magic.h"
peah522d71b2017-02-23 05:16:26 -080026
27namespace webrtc {
28
peahcf02cf12017-04-05 14:18:07 -070029// Provides a buffer of the render data for the echo remover.
30class RenderBuffer {
peah522d71b2017-02-23 05:16:26 -080031 public:
Per Åhgrenec22e3f2017-12-20 15:20:37 +010032 RenderBuffer(MatrixBuffer* block_buffer,
Per Åhgren8ba58612017-12-01 23:01:44 +010033 VectorBuffer* spectrum_buffer,
34 FftBuffer* fft_buffer);
peahcf02cf12017-04-05 14:18:07 -070035 ~RenderBuffer();
peah522d71b2017-02-23 05:16:26 -080036
Per Åhgrenec22e3f2017-12-20 15:20:37 +010037 // Get a block.
38 const std::vector<std::vector<float>>& Block(int buffer_offset_blocks) const {
39 int position =
40 block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks);
41 return block_buffer_->buffer[position];
peahcf02cf12017-04-05 14:18:07 -070042 }
peah522d71b2017-02-23 05:16:26 -080043
Per Åhgren8ba58612017-12-01 23:01:44 +010044 // Get the spectrum from one of the FFTs in the buffer.
Per Åhgrenec22e3f2017-12-20 15:20:37 +010045 rtc::ArrayView<const float> Spectrum(int buffer_offset_ffts) const {
46 int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read,
47 buffer_offset_ffts);
Per Åhgren8ba58612017-12-01 23:01:44 +010048 return spectrum_buffer_->buffer[position];
peah522d71b2017-02-23 05:16:26 -080049 }
50
Per Åhgrenec22e3f2017-12-20 15:20:37 +010051 // Returns the circular fft buffer.
52 rtc::ArrayView<const FftData> GetFftBuffer() const {
53 return fft_buffer_->buffer;
peah522d71b2017-02-23 05:16:26 -080054 }
55
Per Åhgren8ba58612017-12-01 23:01:44 +010056 // Returns the current position in the circular buffer.
Per Åhgrenec22e3f2017-12-20 15:20:37 +010057 size_t Position() const {
58 RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read);
59 RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write);
60 return fft_buffer_->read;
61 }
62
63 // Returns the sum of the spectrums for a certain number of FFTs.
64 void SpectralSum(size_t num_spectra,
65 std::array<float, kFftLengthBy2Plus1>* X2) const;
peah522d71b2017-02-23 05:16:26 -080066
Per Åhgrenee8ad5f2018-08-10 21:15:48 +020067 // Returns the sums of the spectrums for two numbers of FFTs.
68 void SpectralSums(size_t num_spectra_shorter,
69 size_t num_spectra_longer,
70 std::array<float, kFftLengthBy2Plus1>* X2_shorter,
71 std::array<float, kFftLengthBy2Plus1>* X2_longer) const;
72
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010073 // Gets the recent activity seen in the render signal.
74 bool GetRenderActivity() const { return render_activity_; }
75
76 // Specifies the recent activity seen in the render signal.
77 void SetRenderActivity(bool activity) { render_activity_ = activity; }
78
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020079 // Returns the headroom between the write and the read positions in the
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020080 // buffer.
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020081 int Headroom() const {
82 // The write and read indices are decreased over time.
83 int headroom =
84 fft_buffer_->write < fft_buffer_->read
85 ? fft_buffer_->read - fft_buffer_->write
86 : fft_buffer_->size - fft_buffer_->write + fft_buffer_->read;
87
88 RTC_DCHECK_LE(0, headroom);
89 RTC_DCHECK_GE(fft_buffer_->size, headroom);
90
91 return headroom;
92 }
93
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020094 // Returns a reference to the spectrum buffer.
95 const VectorBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; }
96
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +020097 // Returns a reference to the block buffer.
98 const MatrixBuffer& GetBlockBuffer() const { return *block_buffer_; }
99
peah522d71b2017-02-23 05:16:26 -0800100 private:
Per Åhgren8ba58612017-12-01 23:01:44 +0100101 const MatrixBuffer* const block_buffer_;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100102 const VectorBuffer* const spectrum_buffer_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100103 const FftBuffer* const fft_buffer_;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100104 bool render_activity_ = false;
peahcf02cf12017-04-05 14:18:07 -0700105 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderBuffer);
peah522d71b2017-02-23 05:16:26 -0800106};
107
108} // namespace webrtc
109
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200110#endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_