• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "modules/audio_processing/aec3/render_buffer.h"
12 
13 #include <algorithm>
14 #include <functional>
15 
16 #include "modules/audio_processing/aec3/aec3_common.h"
17 #include "rtc_base/checks.h"
18 
19 namespace webrtc {
20 
RenderBuffer(BlockBuffer * block_buffer,SpectrumBuffer * spectrum_buffer,FftBuffer * fft_buffer)21 RenderBuffer::RenderBuffer(BlockBuffer* block_buffer,
22                            SpectrumBuffer* spectrum_buffer,
23                            FftBuffer* fft_buffer)
24     : block_buffer_(block_buffer),
25       spectrum_buffer_(spectrum_buffer),
26       fft_buffer_(fft_buffer) {
27   RTC_DCHECK(block_buffer_);
28   RTC_DCHECK(spectrum_buffer_);
29   RTC_DCHECK(fft_buffer_);
30   RTC_DCHECK_EQ(block_buffer_->buffer.size(), fft_buffer_->buffer.size());
31   RTC_DCHECK_EQ(spectrum_buffer_->buffer.size(), fft_buffer_->buffer.size());
32   RTC_DCHECK_EQ(spectrum_buffer_->read, fft_buffer_->read);
33   RTC_DCHECK_EQ(spectrum_buffer_->write, fft_buffer_->write);
34 }
35 
36 RenderBuffer::~RenderBuffer() = default;
37 
SpectralSum(size_t num_spectra,std::array<float,kFftLengthBy2Plus1> * X2) const38 void RenderBuffer::SpectralSum(
39     size_t num_spectra,
40     std::array<float, kFftLengthBy2Plus1>* X2) const {
41   X2->fill(0.f);
42   int position = spectrum_buffer_->read;
43   for (size_t j = 0; j < num_spectra; ++j) {
44     for (const auto& channel_spectrum : spectrum_buffer_->buffer[position]) {
45       std::transform(X2->begin(), X2->end(), channel_spectrum.begin(),
46                      X2->begin(), std::plus<float>());
47     }
48     position = spectrum_buffer_->IncIndex(position);
49   }
50 }
51 
SpectralSums(size_t num_spectra_shorter,size_t num_spectra_longer,std::array<float,kFftLengthBy2Plus1> * X2_shorter,std::array<float,kFftLengthBy2Plus1> * X2_longer) const52 void RenderBuffer::SpectralSums(
53     size_t num_spectra_shorter,
54     size_t num_spectra_longer,
55     std::array<float, kFftLengthBy2Plus1>* X2_shorter,
56     std::array<float, kFftLengthBy2Plus1>* X2_longer) const {
57   RTC_DCHECK_LE(num_spectra_shorter, num_spectra_longer);
58   X2_shorter->fill(0.f);
59   int position = spectrum_buffer_->read;
60   size_t j = 0;
61   for (; j < num_spectra_shorter; ++j) {
62     for (const auto& channel_spectrum : spectrum_buffer_->buffer[position]) {
63       std::transform(X2_shorter->begin(), X2_shorter->end(),
64                      channel_spectrum.begin(), X2_shorter->begin(),
65                      std::plus<float>());
66     }
67     position = spectrum_buffer_->IncIndex(position);
68   }
69   std::copy(X2_shorter->begin(), X2_shorter->end(), X2_longer->begin());
70   for (; j < num_spectra_longer; ++j) {
71     for (const auto& channel_spectrum : spectrum_buffer_->buffer[position]) {
72       std::transform(X2_longer->begin(), X2_longer->end(),
73                      channel_spectrum.begin(), X2_longer->begin(),
74                      std::plus<float>());
75     }
76     position = spectrum_buffer_->IncIndex(position);
77   }
78 }
79 
80 }  // namespace webrtc
81