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 "api/array_view.h"
13 #include "rtc_base/checks.h"
14
15 namespace webrtc {
16
BlockDelayBuffer(size_t num_channels,size_t num_bands,size_t frame_length,size_t delay_samples)17 BlockDelayBuffer::BlockDelayBuffer(size_t num_channels,
18 size_t num_bands,
19 size_t frame_length,
20 size_t delay_samples)
21 : frame_length_(frame_length),
22 delay_(delay_samples),
23 buf_(num_channels,
24 std::vector<std::vector<float>>(num_bands,
25 std::vector<float>(delay_, 0.f))) {}
26
27 BlockDelayBuffer::~BlockDelayBuffer() = default;
28
DelaySignal(AudioBuffer * frame)29 void BlockDelayBuffer::DelaySignal(AudioBuffer* frame) {
30 RTC_DCHECK_EQ(buf_.size(), frame->num_channels());
31 if (delay_ == 0) {
32 return;
33 }
34
35 const size_t num_bands = buf_[0].size();
36 const size_t num_channels = buf_.size();
37
38 const size_t i_start = last_insert_;
39 size_t i = 0;
40 for (size_t ch = 0; ch < num_channels; ++ch) {
41 RTC_DCHECK_EQ(buf_[ch].size(), frame->num_bands());
42 RTC_DCHECK_EQ(buf_[ch].size(), num_bands);
43 rtc::ArrayView<float* const> frame_ch(frame->split_bands(ch), num_bands);
44
45 for (size_t band = 0; band < num_bands; ++band) {
46 RTC_DCHECK_EQ(delay_, buf_[ch][band].size());
47 i = i_start;
48
49 for (size_t k = 0; k < frame_length_; ++k) {
50 const float tmp = buf_[ch][band][i];
51 buf_[ch][band][i] = frame_ch[band][k];
52 frame_ch[band][k] = tmp;
53
54 i = i < delay_ - 1 ? i + 1 : 0;
55 }
56 }
57 }
58
59 last_insert_ = i;
60 }
61
62 } // namespace webrtc
63