xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/block_delay_buffer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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     const size_t delay = delay_;
45 
46     for (size_t band = 0; band < num_bands; ++band) {
47       RTC_DCHECK_EQ(delay_, buf_[ch][band].size());
48       i = i_start;
49 
50       // Offloading these pointers and class variables to local variables allows
51       // the compiler to optimize the below loop when compiling with
52       // '-fno-strict-aliasing'.
53       float* buf_ch_band = buf_[ch][band].data();
54       float* frame_ch_band = frame_ch[band];
55 
56       for (size_t k = 0, frame_length = frame_length_; k < frame_length; ++k) {
57         const float tmp = buf_ch_band[i];
58         buf_ch_band[i] = frame_ch_band[k];
59         frame_ch_band[k] = tmp;
60 
61         i = i < delay - 1 ? i + 1 : 0;
62       }
63     }
64   }
65 
66   last_insert_ = i;
67 }
68 
69 }  // namespace webrtc
70