blob: 8adc996087e8daf8ee3426767cc869e314cdb66d [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Per Åhgren8ba58612017-12-01 23:01:44 +010016#include <array>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <vector>
peah522d71b2017-02-23 05:16:26 -080018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/array_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "modules/audio_processing/aec3/aec3_common.h"
Sam Zackrissonf3f61592019-09-05 11:30:49 +020021#include "modules/audio_processing/aec3/block_buffer.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010022#include "modules/audio_processing/aec3/fft_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/aec3/fft_data.h"
Sam Zackrissonf3f61592019-09-05 11:30:49 +020024#include "modules/audio_processing/aec3/spectrum_buffer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020025#include "rtc_base/checks.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:
Sam Zackrissonf3f61592019-09-05 11:30:49 +020032 RenderBuffer(BlockBuffer* block_buffer,
33 SpectrumBuffer* spectrum_buffer,
Per Åhgren8ba58612017-12-01 23:01:44 +010034 FftBuffer* fft_buffer);
Niels Möllerde953292020-09-29 09:46:21 +020035
36 RenderBuffer() = delete;
37 RenderBuffer(const RenderBuffer&) = delete;
38 RenderBuffer& operator=(const RenderBuffer&) = delete;
39
peahcf02cf12017-04-05 14:18:07 -070040 ~RenderBuffer();
peah522d71b2017-02-23 05:16:26 -080041
Per Åhgrenec22e3f2017-12-20 15:20:37 +010042 // Get a block.
Ivan Murashov1e8bb672022-05-24 10:49:50 +030043 const Block& GetBlock(int buffer_offset_blocks) const {
Per Åhgrenec22e3f2017-12-20 15:20:37 +010044 int position =
45 block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks);
46 return block_buffer_->buffer[position];
peahcf02cf12017-04-05 14:18:07 -070047 }
peah522d71b2017-02-23 05:16:26 -080048
Per Åhgren8ba58612017-12-01 23:01:44 +010049 // Get the spectrum from one of the FFTs in the buffer.
Sam Zackrisson98872dc2019-10-18 08:20:09 +020050 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Spectrum(
51 int buffer_offset_ffts) const {
Per Åhgrenec22e3f2017-12-20 15:20:37 +010052 int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read,
53 buffer_offset_ffts);
Sam Zackrisson98872dc2019-10-18 08:20:09 +020054 return spectrum_buffer_->buffer[position];
peah522d71b2017-02-23 05:16:26 -080055 }
56
Per Åhgrenec22e3f2017-12-20 15:20:37 +010057 // Returns the circular fft buffer.
Sam Zackrissoncfb94972019-09-05 15:03:07 +020058 rtc::ArrayView<const std::vector<FftData>> GetFftBuffer() const {
Per Åhgrenec22e3f2017-12-20 15:20:37 +010059 return fft_buffer_->buffer;
peah522d71b2017-02-23 05:16:26 -080060 }
61
Per Åhgren8ba58612017-12-01 23:01:44 +010062 // Returns the current position in the circular buffer.
Per Åhgrenec22e3f2017-12-20 15:20:37 +010063 size_t Position() const {
64 RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read);
65 RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write);
66 return fft_buffer_->read;
67 }
68
69 // Returns the sum of the spectrums for a certain number of FFTs.
70 void SpectralSum(size_t num_spectra,
71 std::array<float, kFftLengthBy2Plus1>* X2) const;
peah522d71b2017-02-23 05:16:26 -080072
Per Åhgrenee8ad5f2018-08-10 21:15:48 +020073 // Returns the sums of the spectrums for two numbers of FFTs.
74 void SpectralSums(size_t num_spectra_shorter,
75 size_t num_spectra_longer,
76 std::array<float, kFftLengthBy2Plus1>* X2_shorter,
77 std::array<float, kFftLengthBy2Plus1>* X2_longer) const;
78
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010079 // Gets the recent activity seen in the render signal.
80 bool GetRenderActivity() const { return render_activity_; }
81
82 // Specifies the recent activity seen in the render signal.
83 void SetRenderActivity(bool activity) { render_activity_ = activity; }
84
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020085 // Returns the headroom between the write and the read positions in the
Jesús de Vicente Peña95581922018-04-30 08:37:57 +020086 // buffer.
Jesús de Vicente Peñad5cb4772018-04-25 13:58:45 +020087 int Headroom() const {
88 // The write and read indices are decreased over time.
89 int headroom =
90 fft_buffer_->write < fft_buffer_->read
91 ? fft_buffer_->read - fft_buffer_->write
92 : fft_buffer_->size - fft_buffer_->write + fft_buffer_->read;
93
94 RTC_DCHECK_LE(0, headroom);
95 RTC_DCHECK_GE(fft_buffer_->size, headroom);
96
97 return headroom;
98 }
99
Jesús de Vicente Peña95581922018-04-30 08:37:57 +0200100 // Returns a reference to the spectrum buffer.
Sam Zackrissonf3f61592019-09-05 11:30:49 +0200101 const SpectrumBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; }
Jesús de Vicente Peña95581922018-04-30 08:37:57 +0200102
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +0200103 // Returns a reference to the block buffer.
Sam Zackrissonf3f61592019-09-05 11:30:49 +0200104 const BlockBuffer& GetBlockBuffer() const { return *block_buffer_; }
Jesús de Vicente Peña2f2633d2018-05-02 09:47:22 +0200105
peah522d71b2017-02-23 05:16:26 -0800106 private:
Sam Zackrissonf3f61592019-09-05 11:30:49 +0200107 const BlockBuffer* const block_buffer_;
108 const SpectrumBuffer* const spectrum_buffer_;
Per Åhgren8ba58612017-12-01 23:01:44 +0100109 const FftBuffer* const fft_buffer_;
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100110 bool render_activity_ = false;
peah522d71b2017-02-23 05:16:26 -0800111};
112
113} // namespace webrtc
114
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200115#endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_