Line data Source code
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 "webrtc/modules/audio_processing/aec3/block_framer.h"
12 :
13 : #include <algorithm>
14 :
15 : #include "webrtc/base/checks.h"
16 :
17 : namespace webrtc {
18 :
19 0 : BlockFramer::BlockFramer(size_t num_bands)
20 : : num_bands_(num_bands),
21 0 : 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.
29 0 : void BlockFramer::InsertBlock(const std::vector<std::vector<float>>& block) {
30 0 : RTC_DCHECK_EQ(num_bands_, block.size());
31 0 : for (size_t i = 0; i < num_bands_; ++i) {
32 0 : RTC_DCHECK_EQ(kBlockSize, block[i].size());
33 0 : RTC_DCHECK_EQ(0, buffer_[i].size());
34 0 : buffer_[i].insert(buffer_[i].begin(), block[i].begin(), block[i].end());
35 : }
36 0 : }
37 :
38 0 : void BlockFramer::InsertBlockAndExtractSubFrame(
39 : const std::vector<std::vector<float>>& block,
40 : std::vector<rtc::ArrayView<float>>* sub_frame) {
41 0 : RTC_DCHECK(sub_frame);
42 0 : RTC_DCHECK_EQ(num_bands_, block.size());
43 0 : RTC_DCHECK_EQ(num_bands_, sub_frame->size());
44 0 : for (size_t i = 0; i < num_bands_; ++i) {
45 0 : RTC_DCHECK_LE(kSubFrameLength, buffer_[i].size() + kBlockSize);
46 0 : RTC_DCHECK_EQ(kBlockSize, block[i].size());
47 0 : RTC_DCHECK_GE(kBlockSize, buffer_[i].size());
48 0 : RTC_DCHECK_EQ(kSubFrameLength, (*sub_frame)[i].size());
49 0 : const int samples_to_frame = kSubFrameLength - buffer_[i].size();
50 0 : std::copy(buffer_[i].begin(), buffer_[i].end(), (*sub_frame)[i].begin());
51 0 : std::copy(block[i].begin(), block[i].begin() + samples_to_frame,
52 0 : (*sub_frame)[i].begin() + buffer_[i].size());
53 0 : buffer_[i].clear();
54 0 : buffer_[i].insert(buffer_[i].begin(), block[i].begin() + samples_to_frame,
55 0 : block[i].end());
56 : }
57 0 : }
58 :
59 : } // namespace webrtc
|