1 /*
2 * Copyright (c) 2016 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
11 #include "modules/audio_processing/aec3/block_framer.h"
12
13 #include <algorithm>
14
15 #include "rtc_base/checks.h"
16
17 namespace webrtc {
18
BlockFramer(size_t num_bands)19 BlockFramer::BlockFramer(size_t num_bands)
20 : num_bands_(num_bands),
21 buffer_(num_bands_, std::vector<float>(kBlockSize, 0.f)) {}
22
23 BlockFramer::~BlockFramer() = default;
24
25 // All the constants are chosen so that the buffer is either empty or has enough
26 // samples for InsertBlockAndExtractSubFrame to produce a frame. In order to
27 // achieve this, the InsertBlockAndExtractSubFrame and InsertBlock methods need
28 // to be called in the correct order.
InsertBlock(const std::vector<std::vector<float>> & block)29 void BlockFramer::InsertBlock(const std::vector<std::vector<float>>& block) {
30 RTC_DCHECK_EQ(num_bands_, block.size());
31 for (size_t i = 0; i < num_bands_; ++i) {
32 RTC_DCHECK_EQ(kBlockSize, block[i].size());
33 RTC_DCHECK_EQ(0, buffer_[i].size());
34 buffer_[i].insert(buffer_[i].begin(), block[i].begin(), block[i].end());
35 }
36 }
37
InsertBlockAndExtractSubFrame(const std::vector<std::vector<float>> & block,std::vector<rtc::ArrayView<float>> * sub_frame)38 void BlockFramer::InsertBlockAndExtractSubFrame(
39 const std::vector<std::vector<float>>& block,
40 std::vector<rtc::ArrayView<float>>* sub_frame) {
41 RTC_DCHECK(sub_frame);
42 RTC_DCHECK_EQ(num_bands_, block.size());
43 RTC_DCHECK_EQ(num_bands_, sub_frame->size());
44 for (size_t i = 0; i < num_bands_; ++i) {
45 RTC_DCHECK_LE(kSubFrameLength, buffer_[i].size() + kBlockSize);
46 RTC_DCHECK_EQ(kBlockSize, block[i].size());
47 RTC_DCHECK_GE(kBlockSize, buffer_[i].size());
48 RTC_DCHECK_EQ(kSubFrameLength, (*sub_frame)[i].size());
49 const int samples_to_frame = kSubFrameLength - buffer_[i].size();
50 std::copy(buffer_[i].begin(), buffer_[i].end(), (*sub_frame)[i].begin());
51 std::copy(block[i].begin(), block[i].begin() + samples_to_frame,
52 (*sub_frame)[i].begin() + buffer_[i].size());
53 buffer_[i].clear();
54 buffer_[i].insert(buffer_[i].begin(), block[i].begin() + samples_to_frame,
55 block[i].end());
56 }
57 }
58
59 } // namespace webrtc
60