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 27 namespace webrtc { 28 29 // Provides a buffer of the render data for the echo remover. 30 class RenderBuffer { 31 public: 32 RenderBuffer(BlockBuffer* block_buffer, 33 SpectrumBuffer* spectrum_buffer, 34 FftBuffer* fft_buffer); 35 36 RenderBuffer() = delete; 37 RenderBuffer(const RenderBuffer&) = delete; 38 RenderBuffer& operator=(const RenderBuffer&) = delete; 39 40 ~RenderBuffer(); 41 42 // Get a block. GetBlock(int buffer_offset_blocks)43 const Block& GetBlock(int buffer_offset_blocks) const { 44 int position = 45 block_buffer_->OffsetIndex(block_buffer_->read, buffer_offset_blocks); 46 return block_buffer_->buffer[position]; 47 } 48 49 // Get the spectrum from one of the FFTs in the buffer. Spectrum(int buffer_offset_ffts)50 rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> Spectrum( 51 int buffer_offset_ffts) const { 52 int position = spectrum_buffer_->OffsetIndex(spectrum_buffer_->read, 53 buffer_offset_ffts); 54 return spectrum_buffer_->buffer[position]; 55 } 56 57 // Returns the circular fft buffer. GetFftBuffer()58 rtc::ArrayView<const std::vector<FftData>> GetFftBuffer() const { 59 return fft_buffer_->buffer; 60 } 61 62 // Returns the current position in the circular buffer. Position()63 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; 72 73 // 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 79 // Gets the recent activity seen in the render signal. GetRenderActivity()80 bool GetRenderActivity() const { return render_activity_; } 81 82 // Specifies the recent activity seen in the render signal. SetRenderActivity(bool activity)83 void SetRenderActivity(bool activity) { render_activity_ = activity; } 84 85 // Returns the headroom between the write and the read positions in the 86 // buffer. Headroom()87 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 100 // Returns a reference to the spectrum buffer. GetSpectrumBuffer()101 const SpectrumBuffer& GetSpectrumBuffer() const { return *spectrum_buffer_; } 102 103 // Returns a reference to the block buffer. GetBlockBuffer()104 const BlockBuffer& GetBlockBuffer() const { return *block_buffer_; } 105 106 private: 107 const BlockBuffer* const block_buffer_; 108 const SpectrumBuffer* const spectrum_buffer_; 109 const FftBuffer* const fft_buffer_; 110 bool render_activity_ = false; 111 }; 112 113 } // namespace webrtc 114 115 #endif // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_ 116