blob: b8be6f517ab2676fd14414b1a93d00eccf713a9c [file] [log] [blame]
peah522d71b2017-02-23 13:16:261/*
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 04:47:3111#ifndef MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_
peah522d71b2017-02-23 13:16:2613
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Per Åhgren8ba58612017-12-01 22:01:4416#include <array>
Yves Gerey988cc082018-10-23 10:03:0117#include <vector>
peah522d71b2017-02-23 13:16:2618
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "api/array_view.h"
Yves Gerey988cc082018-10-23 10:03:0120#include "modules/audio_processing/aec3/aec3_common.h"
Sam Zackrissonf3f61592019-09-05 09:30:4921#include "modules/audio_processing/aec3/block_buffer.h"
Per Åhgren8ba58612017-12-01 22:01:4422#include "modules/audio_processing/aec3/fft_buffer.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "modules/audio_processing/aec3/fft_data.h"
Sam Zackrissonf3f61592019-09-05 09:30:4924#include "modules/audio_processing/aec3/spectrum_buffer.h"
Yves Gerey988cc082018-10-23 10:03:0125#include "rtc_base/checks.h"
peah522d71b2017-02-23 13:16:2626
27namespace webrtc {
28
peahcf02cf12017-04-05 21:18:0729// Provides a buffer of the render data for the echo remover.
30class RenderBuffer {
peah522d71b2017-02-23 13:16:2631 public:
Sam Zackrissonf3f61592019-09-05 09:30:4932 RenderBuffer(BlockBuffer* block_buffer,
33 SpectrumBuffer* spectrum_buffer,
Per Åhgren8ba58612017-12-01 22:01:4434 FftBuffer* fft_buffer);
Niels Möllerde953292020-09-29 07:46:2135
36 RenderBuffer() = delete;
37 RenderBuffer(const RenderBuffer&) = delete;
38 RenderBuffer& operator=(const RenderBuffer&) = delete;
39
peahcf02cf12017-04-05 21:18:0740 ~RenderBuffer();
peah522d71b2017-02-23 13:16:2641
Per Åhgrenec22e3f2017-12-20 14:20:3742 // Get a block.
Per Åhgrence202a02019-09-02 15:01:1943 const std::vector<std::vector<std::vector<float>>>& Block(
44 int buffer_offset_blocks) const {
Per Åhgrenec22e3f2017-12-20 14:20:3745 int position =
46 block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks);
47 return block_buffer_->buffer[position];
peahcf02cf12017-04-05 21:18:0748 }
peah522d71b2017-02-23 13:16:2649
Per Åhgren8ba58612017-12-01 22:01:4450 // Get the spectrum from one of the FFTs in the buffer.
Sam Zackrisson98872dc2019-10-18 06:20:0951 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Spectrum(
52 int buffer_offset_ffts) const {
Per Åhgrenec22e3f2017-12-20 14:20:3753 int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read,
54 buffer_offset_ffts);
Sam Zackrisson98872dc2019-10-18 06:20:0955 return spectrum_buffer_->buffer[position];
peah522d71b2017-02-23 13:16:2656 }
57
Per Åhgrenec22e3f2017-12-20 14:20:3758 // Returns the circular fft buffer.
Sam Zackrissoncfb94972019-09-05 13:03:0759 rtc::ArrayView<const std::vector<FftData>> GetFftBuffer() const {
Per Åhgrenec22e3f2017-12-20 14:20:3760 return fft_buffer_->buffer;
peah522d71b2017-02-23 13:16:2661 }
62
Per Åhgren8ba58612017-12-01 22:01:4463 // Returns the current position in the circular buffer.
Per Åhgrenec22e3f2017-12-20 14:20:3764 size_t Position() const {
65 RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read);
66 RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write);
67 return fft_buffer_->read;
68 }
69
70 // Returns the sum of the spectrums for a certain number of FFTs.
71 void SpectralSum(size_t num_spectra,
72 std::array<float, kFftLengthBy2Plus1>* X2) const;
peah522d71b2017-02-23 13:16:2673
Per Åhgrenee8ad5f2018-08-10 19:15:4874 // Returns the sums of the spectrums for two numbers of FFTs.
75 void SpectralSums(size_t num_spectra_shorter,
76 size_t num_spectra_longer,
77 std::array<float, kFftLengthBy2Plus1>* X2_shorter,
78 std::array<float, kFftLengthBy2Plus1>* X2_longer) const;
79
Per Åhgrenb6b00dc2018-02-20 21:18:2780 // Gets the recent activity seen in the render signal.
81 bool GetRenderActivity() const { return render_activity_; }
82
83 // Specifies the recent activity seen in the render signal.
84 void SetRenderActivity(bool activity) { render_activity_ = activity; }
85
Jesús de Vicente Peñad5cb4772018-04-25 11:58:4586 // Returns the headroom between the write and the read positions in the
Jesús de Vicente Peña95581922018-04-30 06:37:5787 // buffer.
Jesús de Vicente Peñad5cb4772018-04-25 11:58:4588 int Headroom() const {
89 // The write and read indices are decreased over time.
90 int headroom =
91 fft_buffer_->write < fft_buffer_->read
92 ? fft_buffer_->read - fft_buffer_->write
93 : fft_buffer_->size - fft_buffer_->write + fft_buffer_->read;
94
95 RTC_DCHECK_LE(0, headroom);
96 RTC_DCHECK_GE(fft_buffer_->size, headroom);
97
98 return headroom;
99 }
100
Jesús de Vicente Peña95581922018-04-30 06:37:57101 // Returns a reference to the spectrum buffer.
Sam Zackrissonf3f61592019-09-05 09:30:49102 const SpectrumBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; }
Jesús de Vicente Peña95581922018-04-30 06:37:57103
Jesús de Vicente Peña2f2633d2018-05-02 07:47:22104 // Returns a reference to the block buffer.
Sam Zackrissonf3f61592019-09-05 09:30:49105 const BlockBuffer& GetBlockBuffer() const { return *block_buffer_; }
Jesús de Vicente Peña2f2633d2018-05-02 07:47:22106
peah522d71b2017-02-23 13:16:26107 private:
Sam Zackrissonf3f61592019-09-05 09:30:49108 const BlockBuffer* const block_buffer_;
109 const SpectrumBuffer* const spectrum_buffer_;
Per Åhgren8ba58612017-12-01 22:01:44110 const FftBuffer* const fft_buffer_;
Per Åhgrenb6b00dc2018-02-20 21:18:27111 bool render_activity_ = false;
peah522d71b2017-02-23 13:16:26112};
113
114} // namespace webrtc
115
Mirko Bonadei92ea95e2017-09-15 04:47:31116#endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_