Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_processing/aec3/fft_buffer.h" | 11 #include "webrtc/modules/audio_processing/aec3/render_buffer.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/modules/audio_processing/aec3/aec3_common.h" | 16 #include "webrtc/modules/audio_processing/aec3/aec3_common.h" |
| 17 | 17 |
| 18 namespace webrtc { | 18 namespace webrtc { |
| 19 | 19 |
| 20 FftBuffer::FftBuffer(Aec3Optimization optimization, | 20 RenderBuffer::RenderBuffer(Aec3Optimization optimization, |
| 21 size_t num_partitions, | 21 size_t num_bands, |
| 22 const std::vector<size_t> num_ffts_for_spectral_sums) | 22 size_t num_partitions, |
| 23 const std::vector<size_t> num_ffts_for_spectral_sums) | |
| 23 : optimization_(optimization), | 24 : optimization_(optimization), |
| 24 fft_buffer_(num_partitions), | 25 fft_buffer_(num_partitions), |
| 25 spectrum_buffer_(num_partitions, std::array<float, kFftLengthBy2Plus1>()), | 26 spectrum_buffer_(num_partitions, std::array<float, kFftLengthBy2Plus1>()), |
| 26 spectral_sums_(num_ffts_for_spectral_sums.size(), | 27 spectral_sums_(num_ffts_for_spectral_sums.size(), |
| 27 std::array<float, kFftLengthBy2Plus1>()) { | 28 std::array<float, kFftLengthBy2Plus1>()), |
| 29 last_block_(num_bands, std::vector<float>(kBlockSize, 0.f)) { | |
| 28 // Current implementation only allows a maximum of one spectral sum lengths. | 30 // Current implementation only allows a maximum of one spectral sum lengths. |
| 29 RTC_DCHECK_EQ(1, num_ffts_for_spectral_sums.size()); | 31 RTC_DCHECK_EQ(1, num_ffts_for_spectral_sums.size()); |
| 30 spectral_sums_length_ = num_ffts_for_spectral_sums[0]; | 32 spectral_sums_length_ = num_ffts_for_spectral_sums[0]; |
| 31 RTC_DCHECK_GE(fft_buffer_.size(), spectral_sums_length_); | 33 RTC_DCHECK_GE(fft_buffer_.size(), spectral_sums_length_); |
| 32 | 34 |
| 35 Clear(); | |
| 36 } | |
| 37 | |
| 38 RenderBuffer::~RenderBuffer() = default; | |
| 39 | |
| 40 void RenderBuffer::Clear() { | |
| 41 position_ = 0; | |
| 33 for (auto& sum : spectral_sums_) { | 42 for (auto& sum : spectral_sums_) { |
| 34 sum.fill(0.f); | 43 sum.fill(0.f); |
| 35 } | 44 } |
| 36 | 45 |
| 37 for (auto& spectrum : spectrum_buffer_) { | 46 for (auto& spectrum : spectrum_buffer_) { |
| 38 spectrum.fill(0.f); | 47 spectrum.fill(0.f); |
| 39 } | 48 } |
| 40 | 49 |
| 41 for (auto& fft : fft_buffer_) { | 50 for (auto& fft : fft_buffer_) { |
| 42 fft.Clear(); | 51 fft.Clear(); |
| 43 } | 52 } |
| 53 | |
| 54 for (auto& b : last_block_) { | |
| 55 std::fill(b.begin(), b.end(), 0.f); | |
| 56 } | |
| 44 } | 57 } |
| 45 | 58 |
| 46 FftBuffer::~FftBuffer() = default; | 59 void RenderBuffer::Insert(const std::vector<std::vector<float>>& block) { |
| 60 // Compute the FFT of the data in the lowest band. | |
|
ivoc
2017/03/31 13:58:31
A DCHECK to verify the size of block would be usef
peah-webrtc
2017/04/03 08:02:33
Done.
| |
| 61 FftData X; | |
| 62 fft_.PaddedFft(block[0], last_block_[0], &X); | |
| 47 | 63 |
| 48 void FftBuffer::Insert(const FftData& fft) { | 64 // Copy the last render frame. |
| 49 // Insert the fft into the buffer. | 65 for (size_t k = 0; k < block.size(); ++k) { |
| 66 std::copy(block[k].begin(), block[k].end(), last_block_[k].begin()); | |
|
ivoc
2017/03/31 13:58:31
A DHECK for the size of block[k] would be good.
peah-webrtc
2017/04/03 08:02:33
Done.
| |
| 67 } | |
| 68 | |
| 69 // Insert X into the buffer. | |
| 50 position_ = (position_ - 1 + fft_buffer_.size()) % fft_buffer_.size(); | 70 position_ = (position_ - 1 + fft_buffer_.size()) % fft_buffer_.size(); |
| 51 fft_buffer_[position_].Assign(fft); | 71 fft_buffer_[position_].Assign(X); |
| 52 | 72 |
| 53 // Compute and insert the spectrum for the FFT into the spectrum buffer. | 73 // Compute and insert the spectrum for the FFT into the spectrum buffer. |
| 54 fft.Spectrum(optimization_, &spectrum_buffer_[position_]); | 74 X.Spectrum(optimization_, &spectrum_buffer_[position_]); |
| 55 | 75 |
| 56 // Pre-compute and cachec the spectral sums. | 76 // Pre-compute and cachec the spectral sums. |
|
ivoc
2017/03/31 13:58:31
cache
peah-webrtc
2017/04/03 08:02:33
Done.
| |
| 57 std::copy(spectrum_buffer_[position_].begin(), | 77 std::copy(spectrum_buffer_[position_].begin(), |
| 58 spectrum_buffer_[position_].end(), spectral_sums_[0].begin()); | 78 spectrum_buffer_[position_].end(), spectral_sums_[0].begin()); |
| 59 size_t position = (position_ + 1) % fft_buffer_.size(); | 79 size_t position = (position_ + 1) % fft_buffer_.size(); |
| 60 for (size_t j = 1; j < spectral_sums_length_; ++j) { | 80 for (size_t j = 1; j < spectral_sums_length_; ++j) { |
| 61 const std::array<float, kFftLengthBy2Plus1>& spectrum = | 81 const std::array<float, kFftLengthBy2Plus1>& spectrum = |
| 62 spectrum_buffer_[position]; | 82 spectrum_buffer_[position]; |
| 63 | 83 |
| 64 for (size_t k = 0; k < spectral_sums_[0].size(); ++k) { | 84 for (size_t k = 0; k < spectral_sums_[0].size(); ++k) { |
| 65 spectral_sums_[0][k] += spectrum[k]; | 85 spectral_sums_[0][k] += spectrum[k]; |
| 66 } | 86 } |
| 67 | 87 |
| 68 position = position < (fft_buffer_.size() - 1) ? position + 1 : 0; | 88 position = position < (fft_buffer_.size() - 1) ? position + 1 : 0; |
|
ivoc
2017/03/31 13:58:31
I think it would be nicer to update position in a
peah-webrtc
2017/04/03 08:02:33
I agree that the code becomes nicer. However, the
ivoc
2017/04/03 16:09:07
Ok, let's keep it this way then.
| |
| 69 } | 89 } |
| 70 } | 90 } |
| 71 | 91 |
| 72 } // namespace webrtc | 92 } // namespace webrtc |
| OLD | NEW |