block_delay_buffer.cc (2395B)
1 /* 2 * Copyright (c) 2018 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 #include "modules/audio_processing/aec3/block_delay_buffer.h" 11 12 #include <cstddef> 13 #include <vector> 14 15 #include "api/array_view.h" 16 #include "modules/audio_processing/audio_buffer.h" 17 #include "rtc_base/checks.h" 18 19 namespace webrtc { 20 21 BlockDelayBuffer::BlockDelayBuffer(size_t num_channels, 22 size_t num_bands, 23 size_t frame_length, 24 size_t delay_samples) 25 : frame_length_(frame_length), 26 delay_(delay_samples), 27 buf_(num_channels, 28 std::vector<std::vector<float>>(num_bands, 29 std::vector<float>(delay_, 0.f))) {} 30 31 BlockDelayBuffer::~BlockDelayBuffer() = default; 32 33 void BlockDelayBuffer::DelaySignal(AudioBuffer* frame) { 34 RTC_DCHECK_EQ(buf_.size(), frame->num_channels()); 35 if (delay_ == 0) { 36 return; 37 } 38 39 const size_t num_bands = buf_[0].size(); 40 const size_t num_channels = buf_.size(); 41 42 const size_t i_start = last_insert_; 43 size_t i = 0; 44 for (size_t ch = 0; ch < num_channels; ++ch) { 45 RTC_DCHECK_EQ(buf_[ch].size(), frame->num_bands()); 46 RTC_DCHECK_EQ(buf_[ch].size(), num_bands); 47 ArrayView<float* const> frame_ch(frame->split_bands(ch), num_bands); 48 const size_t delay = delay_; 49 50 for (size_t band = 0; band < num_bands; ++band) { 51 RTC_DCHECK_EQ(delay_, buf_[ch][band].size()); 52 i = i_start; 53 54 // Offloading these pointers and class variables to local variables allows 55 // the compiler to optimize the below loop when compiling with 56 // '-fno-strict-aliasing'. 57 float* buf_ch_band = buf_[ch][band].data(); 58 float* frame_ch_band = frame_ch[band]; 59 60 for (size_t k = 0, frame_length = frame_length_; k < frame_length; ++k) { 61 const float tmp = buf_ch_band[i]; 62 buf_ch_band[i] = frame_ch_band[k]; 63 frame_ch_band[k] = tmp; 64 65 i = i < delay - 1 ? i + 1 : 0; 66 } 67 } 68 } 69 70 last_insert_ = i; 71 } 72 73 } // namespace webrtc