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 11 #ifndef MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ 12 #define MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ 13 14 #include <stddef.h> 15 16 #include <array> 17 #include <vector> 18 19 #include "api/array_view.h" 20 #include "modules/audio_processing/aec3/aec3_common.h" 21 #include "modules/audio_processing/aec3/block_buffer.h" 22 #include "modules/audio_processing/aec3/fft_buffer.h" 23 #include "modules/audio_processing/aec3/fft_data.h" 24 #include "modules/audio_processing/aec3/spectrum_buffer.h" 25 #include "rtc_base/checks.h" 26 #include "rtc_base/constructor_magic.h" 27 28 namespace webrtc { 29 30 // Provides a buffer of the render data for the echo remover. 31 class RenderBuffer { 32 public: 33 RenderBuffer(BlockBuffer* block_buffer, 34 SpectrumBuffer* spectrum_buffer, 35 FftBuffer* fft_buffer); 36 ~RenderBuffer(); 37 38 // Get a block. Block(int buffer_offset_blocks)39 const std::vector<std::vector<std::vector<float>>>& Block( 40 int buffer_offset_blocks) const { 41 int position = 42 block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks); 43 return block_buffer_->buffer[position]; 44 } 45 46 // Get the spectrum from one of the FFTs in the buffer. Spectrum(int buffer_offset_ffts)47 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Spectrum( 48 int buffer_offset_ffts) const { 49 int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read, 50 buffer_offset_ffts); 51 return spectrum_buffer_->buffer[position]; 52 } 53 54 // Returns the circular fft buffer. GetFftBuffer()55 rtc::ArrayView<const std::vector<FftData>> GetFftBuffer() const { 56 return fft_buffer_->buffer; 57 } 58 59 // Returns the current position in the circular buffer. Position()60 size_t Position() const { 61 RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read); 62 RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write); 63 return fft_buffer_->read; 64 } 65 66 // Returns the sum of the spectrums for a certain number of FFTs. 67 void SpectralSum(size_t num_spectra, 68 std::array<float, kFftLengthBy2Plus1>* X2) const; 69 70 // Returns the sums of the spectrums for two numbers of FFTs. 71 void SpectralSums(size_t num_spectra_shorter, 72 size_t num_spectra_longer, 73 std::array<float, kFftLengthBy2Plus1>* X2_shorter, 74 std::array<float, kFftLengthBy2Plus1>* X2_longer) const; 75 76 // Gets the recent activity seen in the render signal. GetRenderActivity()77 bool GetRenderActivity() const { return render_activity_; } 78 79 // Specifies the recent activity seen in the render signal. SetRenderActivity(bool activity)80 void SetRenderActivity(bool activity) { render_activity_ = activity; } 81 82 // Returns the headroom between the write and the read positions in the 83 // buffer. Headroom()84 int Headroom() const { 85 // The write and read indices are decreased over time. 86 int headroom = 87 fft_buffer_->write < fft_buffer_->read 88 ? fft_buffer_->read - fft_buffer_->write 89 : fft_buffer_->size - fft_buffer_->write + fft_buffer_->read; 90 91 RTC_DCHECK_LE(0, headroom); 92 RTC_DCHECK_GE(fft_buffer_->size, headroom); 93 94 return headroom; 95 } 96 97 // Returns a reference to the spectrum buffer. GetSpectrumBuffer()98 const SpectrumBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; } 99 100 // Returns a reference to the block buffer. GetBlockBuffer()101 const BlockBuffer& GetBlockBuffer() const { return *block_buffer_; } 102 103 private: 104 const BlockBuffer* const block_buffer_; 105 const SpectrumBuffer* const spectrum_buffer_; 106 const FftBuffer* const fft_buffer_; 107 bool render_activity_ = false; 108 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderBuffer); 109 }; 110 111 } // namespace webrtc 112 113 #endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ 114