blob: b9eb3c9f93a86f5ee16bbb287de85f276c972d22 [file] [log] [blame]
Per Åhgren398689f2018-08-23 11:38:27 +02001/*
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
Per Åhgren260c7882020-01-28 15:38:41 +010012#include "api/array_view.h"
Per Åhgren398689f2018-08-23 11:38:27 +020013#include "rtc_base/checks.h"
14
15namespace webrtc {
16
Per Åhgren260c7882020-01-28 15:38:41 +010017BlockDelayBuffer::BlockDelayBuffer(size_t num_channels,
18 size_t num_bands,
Per Åhgren398689f2018-08-23 11:38:27 +020019 size_t frame_length,
20 size_t delay_samples)
21 : frame_length_(frame_length),
22 delay_(delay_samples),
Per Åhgren260c7882020-01-28 15:38:41 +010023 buf_(num_channels,
24 std::vector<std::vector<float>>(num_bands,
25 std::vector<float>(delay_, 0.f))) {}
Per Åhgren398689f2018-08-23 11:38:27 +020026
27BlockDelayBuffer::~BlockDelayBuffer() = default;
28
29void BlockDelayBuffer::DelaySignal(AudioBuffer* frame) {
Per Åhgren260c7882020-01-28 15:38:41 +010030 RTC_DCHECK_EQ(buf_.size(), frame->num_channels());
Per Åhgren398689f2018-08-23 11:38:27 +020031 if (delay_ == 0) {
32 return;
33 }
34
Per Åhgren260c7882020-01-28 15:38:41 +010035 const size_t num_bands = buf_[0].size();
36 const size_t num_channels = buf_.size();
37
Per Åhgren398689f2018-08-23 11:38:27 +020038 const size_t i_start = last_insert_;
39 size_t i = 0;
Per Åhgren260c7882020-01-28 15:38:41 +010040 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 }
Per Åhgren398689f2018-08-23 11:38:27 +020056 }
57 }
58
59 last_insert_ = i;
60}
61
62} // namespace webrtc