peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 15 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 16 | #include <array> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | #include <vector> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/array_view.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/aec3/aec3_common.h" |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 21 | #include "modules/audio_processing/aec3/fft_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/aec3/fft_data.h" |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/matrix_buffer.h" |
| 24 | #include "modules/audio_processing/aec3/vector_buffer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "rtc_base/constructor_magic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 30 | // Provides a buffer of the render data for the echo remover. |
| 31 | class RenderBuffer { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 32 | public: |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 33 | RenderBuffer(MatrixBuffer* block_buffer, |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 34 | VectorBuffer* spectrum_buffer, |
| 35 | FftBuffer* fft_buffer); |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 36 | ~RenderBuffer(); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 37 | |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 38 | // Get a block. |
| 39 | const std::vector<std::vector<float>>& Block(int buffer_offset_blocks) const { |
| 40 | int position = |
| 41 | block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks); |
| 42 | return block_buffer_->buffer[position]; |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 43 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 44 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 45 | // Get the spectrum from one of the FFTs in the buffer. |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 46 | rtc::ArrayView<const float> Spectrum(int buffer_offset_ffts) const { |
| 47 | int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read, |
| 48 | buffer_offset_ffts); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 49 | return spectrum_buffer_->buffer[position]; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 52 | // Returns the circular fft buffer. |
| 53 | rtc::ArrayView<const FftData> GetFftBuffer() const { |
| 54 | return fft_buffer_->buffer; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 57 | // Returns the current position in the circular buffer. |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 58 | size_t Position() const { |
| 59 | RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read); |
| 60 | RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write); |
| 61 | return fft_buffer_->read; |
| 62 | } |
| 63 | |
| 64 | // Returns the sum of the spectrums for a certain number of FFTs. |
| 65 | void SpectralSum(size_t num_spectra, |
| 66 | std::array<float, kFftLengthBy2Plus1>* X2) const; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 67 | |
Per Åhgren | ee8ad5f | 2018-08-10 21:15:48 +0200 | [diff] [blame] | 68 | // Returns the sums of the spectrums for two numbers of FFTs. |
| 69 | void SpectralSums(size_t num_spectra_shorter, |
| 70 | size_t num_spectra_longer, |
| 71 | std::array<float, kFftLengthBy2Plus1>* X2_shorter, |
| 72 | std::array<float, kFftLengthBy2Plus1>* X2_longer) const; |
| 73 | |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 74 | // Gets the recent activity seen in the render signal. |
| 75 | bool GetRenderActivity() const { return render_activity_; } |
| 76 | |
| 77 | // Specifies the recent activity seen in the render signal. |
| 78 | void SetRenderActivity(bool activity) { render_activity_ = activity; } |
| 79 | |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 80 | // Returns the headroom between the write and the read positions in the |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 81 | // buffer. |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 82 | int Headroom() const { |
| 83 | // The write and read indices are decreased over time. |
| 84 | int headroom = |
| 85 | fft_buffer_->write < fft_buffer_->read |
| 86 | ? fft_buffer_->read - fft_buffer_->write |
| 87 | : fft_buffer_->size - fft_buffer_->write + fft_buffer_->read; |
| 88 | |
| 89 | RTC_DCHECK_LE(0, headroom); |
| 90 | RTC_DCHECK_GE(fft_buffer_->size, headroom); |
| 91 | |
| 92 | return headroom; |
| 93 | } |
| 94 | |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 95 | // Returns a reference to the spectrum buffer. |
| 96 | const VectorBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; } |
| 97 | |
Jesús de Vicente Peña | 2f2633d | 2018-05-02 09:47:22 +0200 | [diff] [blame] | 98 | // Returns a reference to the block buffer. |
| 99 | const MatrixBuffer& GetBlockBuffer() const { return *block_buffer_; } |
| 100 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 101 | private: |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 102 | const MatrixBuffer* const block_buffer_; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 103 | const VectorBuffer* const spectrum_buffer_; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 104 | const FftBuffer* const fft_buffer_; |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 105 | bool render_activity_ = false; |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 106 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderBuffer); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace webrtc |
| 110 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 111 | #endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ |