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 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 14 | #include <array> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 15 | #include <memory> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/array_view.h" |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 18 | #include "modules/audio_processing/aec3/fft_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/aec3/fft_data.h" |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 20 | #include "modules/audio_processing/aec3/matrix_buffer.h" |
| 21 | #include "modules/audio_processing/aec3/vector_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/constructormagic.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 26 | // Provides a buffer of the render data for the echo remover. |
| 27 | class RenderBuffer { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 28 | public: |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 29 | RenderBuffer(MatrixBuffer* block_buffer, |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 30 | VectorBuffer* spectrum_buffer, |
| 31 | FftBuffer* fft_buffer); |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 32 | ~RenderBuffer(); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 33 | |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 34 | // 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]; |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 39 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 40 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 41 | // Get the spectrum from one of the FFTs in the buffer. |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 42 | rtc::ArrayView<const float> Spectrum(int buffer_offset_ffts) const { |
| 43 | int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read, |
| 44 | buffer_offset_ffts); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 45 | return spectrum_buffer_->buffer[position]; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 48 | // Get the spectrum directly from an index in the buffer. |
| 49 | rtc::ArrayView<const float> SpectrumAtIndex(int index) const { |
| 50 | RTC_CHECK_LT(index, spectrum_buffer_->size); |
| 51 | int position_bound = std::min(index, spectrum_buffer_->size - 1); |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 52 | position_bound = std::max(0, position_bound); |
| 53 | return spectrum_buffer_->buffer[position_bound]; |
| 54 | } |
| 55 | |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 56 | // Returns the circular fft buffer. |
| 57 | rtc::ArrayView<const FftData> GetFftBuffer() const { |
| 58 | return fft_buffer_->buffer; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 61 | // Returns the current position in the circular buffer. |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 62 | 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ña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 68 | // Applies an offset to a buffer index and returns it. |
| 69 | int OffsetSpectrumIndex(int index, int offset) const { |
| 70 | return spectrum_buffer_->OffsetIndex(index, offset); |
| 71 | } |
| 72 | |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 73 | // Returns the write postion in the circular buffer. |
| 74 | int GetWritePositionSpectrum() const { return spectrum_buffer_->write; } |
| 75 | |
Per Åhgren | ec22e3f | 2017-12-20 15:20:37 +0100 | [diff] [blame] | 76 | // Returns the sum of the spectrums for a certain number of FFTs. |
| 77 | void SpectralSum(size_t num_spectra, |
| 78 | std::array<float, kFftLengthBy2Plus1>* X2) const; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 79 | |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 80 | // 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ña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 86 | // 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] | 87 | // buffer. |
Jesús de Vicente Peña | d5cb477 | 2018-04-25 13:58:45 +0200 | [diff] [blame] | 88 | 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ña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 101 | // Decreases an index that is used for accessing the buffer. |
Jesús de Vicente Peña | dc872b6 | 2018-04-25 16:11:42 +0200 | [diff] [blame] | 102 | int DecIdx(int idx) const { return spectrum_buffer_->DecIndex(idx); } |
| 103 | |
Jesús de Vicente Peña | 9558192 | 2018-04-30 08:37:57 +0200 | [diff] [blame] | 104 | // Returns a reference to the spectrum buffer. |
| 105 | const VectorBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; } |
| 106 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 107 | private: |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 108 | const MatrixBuffer* const block_buffer_; |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 109 | const VectorBuffer* const spectrum_buffer_; |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 110 | const FftBuffer* const fft_buffer_; |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 111 | bool render_activity_ = false; |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 112 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderBuffer); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace webrtc |
| 116 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 117 | #endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ |