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 <memory>
15 #include <vector>
16 
17 #include "api/array_view.h"
18 #include "modules/audio_processing/aec3/aec3_fft.h"
19 #include "modules/audio_processing/aec3/fft_data.h"
20 #include "rtc_base/constructormagic.h"
21 
22 namespace webrtc {
23 
24 // Provides a buffer of the render data for the echo remover.
25 class RenderBuffer {
26  public:
27   // The constructor takes, besides from the other parameters, a vector
28   // containing the number of FFTs that will be included in the spectral sums in
29   // the call to SpectralSum.
30   RenderBuffer(Aec3Optimization optimization,
31                size_t num_bands,
32                size_t size,
33                const std::vector<size_t> num_ffts_for_spectral_sums);
34   ~RenderBuffer();
35 
36   // Clears the buffer.
37   void Clear();
38 
39   // Insert a block into the buffer.
40   void Insert(const std::vector<std::vector<float>>& block);
41 
42   // Gets the last inserted block.
MostRecentBlock()43   const std::vector<std::vector<float>>& MostRecentBlock() const {
44     return last_block_;
45   }
46 
47   // Get the spectrum from one of the FFTs in the buffer
Spectrum(size_t buffer_offset_ffts)48   const std::array<float, kFftLengthBy2Plus1>& Spectrum(
49       size_t buffer_offset_ffts) const {
50     return spectrum_buffer_[(position_ + buffer_offset_ffts) %
51                             fft_buffer_.size()];
52   }
53 
54   // Returns the sum of the spectrums for a certain number of FFTs.
SpectralSum(size_t num_ffts)55   const std::array<float, kFftLengthBy2Plus1>& SpectralSum(
56       size_t num_ffts) const {
57     RTC_DCHECK_EQ(spectral_sums_length_, num_ffts);
58     return spectral_sums_[0];
59   }
60 
61   // Returns the circular buffer.
Buffer()62   rtc::ArrayView<const FftData> Buffer() const { return fft_buffer_; }
63 
64   // Returns the current position in the circular buffer
Position()65   size_t Position() const { return position_; }
66 
67  private:
68   const Aec3Optimization optimization_;
69   std::vector<FftData> fft_buffer_;
70   std::vector<std::array<float, kFftLengthBy2Plus1>> spectrum_buffer_;
71   size_t spectral_sums_length_;
72   std::vector<std::array<float, kFftLengthBy2Plus1>> spectral_sums_;
73   size_t position_ = 0;
74   std::vector<std::vector<float>> last_block_;
75   const Aec3Fft fft_;
76   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderBuffer);
77 };
78 
79 }  // namespace webrtc
80 
81 #endif  // MODULES_AUDIO_PROCESSING_AEC3_RENDER_BUFFER_H_
82