blob: 65dbc8fc2b4610c261e2b35e0246c07ea4e43fb0 [file] [log] [blame]
peahcf02cf12017-04-05 14:18:07 -07001/*
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 "webrtc/modules/audio_processing/aec3/render_buffer.h"
12
13#include <algorithm>
14
15#include "webrtc/base/checks.h"
16#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
17
18namespace webrtc {
19
20RenderBuffer::RenderBuffer(Aec3Optimization optimization,
21 size_t num_bands,
22 size_t num_partitions,
23 const std::vector<size_t> num_ffts_for_spectral_sums)
24 : optimization_(optimization),
25 fft_buffer_(num_partitions),
26 spectrum_buffer_(num_partitions, std::array<float, kFftLengthBy2Plus1>()),
27 spectral_sums_(num_ffts_for_spectral_sums.size(),
28 std::array<float, kFftLengthBy2Plus1>()),
guidou854e5072017-04-05 21:28:05 -070029 last_block_(num_bands, std::vector<float>(kBlockSize, 0.f)) {
peahcf02cf12017-04-05 14:18:07 -070030 // Current implementation only allows a maximum of one spectral sum lengths.
31 RTC_DCHECK_EQ(1, num_ffts_for_spectral_sums.size());
32 spectral_sums_length_ = num_ffts_for_spectral_sums[0];
33 RTC_DCHECK_GE(fft_buffer_.size(), spectral_sums_length_);
34
35 Clear();
36}
37
38RenderBuffer::~RenderBuffer() = default;
39
40void RenderBuffer::Clear() {
41 position_ = 0;
42 for (auto& sum : spectral_sums_) {
43 sum.fill(0.f);
44 }
45
46 for (auto& spectrum : spectrum_buffer_) {
47 spectrum.fill(0.f);
48 }
49
50 for (auto& fft : fft_buffer_) {
51 fft.Clear();
52 }
53
54 for (auto& b : last_block_) {
55 std::fill(b.begin(), b.end(), 0.f);
56 }
57}
58
59void RenderBuffer::Insert(const std::vector<std::vector<float>>& block) {
60 // Compute the FFT of the data in the lowest band.
61 FftData X;
62 fft_.PaddedFft(block[0], last_block_[0], &X);
63
64 // Copy the last render frame.
65 RTC_DCHECK_EQ(last_block_.size(), block.size());
66 for (size_t k = 0; k < block.size(); ++k) {
67 RTC_DCHECK_EQ(last_block_[k].size(), block[k].size());
68 std::copy(block[k].begin(), block[k].end(), last_block_[k].begin());
69 }
70
71 // Insert X into the buffer.
72 position_ = position_ > 0 ? position_ - 1 : fft_buffer_.size() - 1;
73 fft_buffer_[position_].Assign(X);
74
75 // Compute and insert the spectrum for the FFT into the spectrum buffer.
76 X.Spectrum(optimization_, &spectrum_buffer_[position_]);
77
78 // Pre-compute and cache the spectral sums.
79 std::copy(spectrum_buffer_[position_].begin(),
80 spectrum_buffer_[position_].end(), spectral_sums_[0].begin());
81 size_t position = (position_ + 1) % fft_buffer_.size();
82 for (size_t j = 1; j < spectral_sums_length_; ++j) {
83 const std::array<float, kFftLengthBy2Plus1>& spectrum =
84 spectrum_buffer_[position];
85
86 for (size_t k = 0; k < spectral_sums_[0].size(); ++k) {
87 spectral_sums_[0][k] += spectrum[k];
88 }
89
90 position = position < (fft_buffer_.size() - 1) ? position + 1 : 0;
91 }
92}
93
94} // namespace webrtc