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