Line data Source code
1 : /*
2 : * Copyright (c) 2014 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/common_audio/channel_buffer.h"
12 :
13 : #include "webrtc/base/checks.h"
14 :
15 : namespace webrtc {
16 :
17 0 : IFChannelBuffer::IFChannelBuffer(size_t num_frames,
18 : size_t num_channels,
19 0 : size_t num_bands)
20 : : ivalid_(true),
21 : ibuf_(num_frames, num_channels, num_bands),
22 : fvalid_(true),
23 0 : fbuf_(num_frames, num_channels, num_bands) {}
24 :
25 : IFChannelBuffer::~IFChannelBuffer() = default;
26 :
27 0 : ChannelBuffer<int16_t>* IFChannelBuffer::ibuf() {
28 0 : RefreshI();
29 0 : fvalid_ = false;
30 0 : return &ibuf_;
31 : }
32 :
33 0 : ChannelBuffer<float>* IFChannelBuffer::fbuf() {
34 0 : RefreshF();
35 0 : ivalid_ = false;
36 0 : return &fbuf_;
37 : }
38 :
39 0 : const ChannelBuffer<int16_t>* IFChannelBuffer::ibuf_const() const {
40 0 : RefreshI();
41 0 : return &ibuf_;
42 : }
43 :
44 0 : const ChannelBuffer<float>* IFChannelBuffer::fbuf_const() const {
45 0 : RefreshF();
46 0 : return &fbuf_;
47 : }
48 :
49 0 : void IFChannelBuffer::RefreshF() const {
50 0 : if (!fvalid_) {
51 0 : RTC_DCHECK(ivalid_);
52 0 : fbuf_.set_num_channels(ibuf_.num_channels());
53 0 : const int16_t* const* int_channels = ibuf_.channels();
54 0 : float* const* float_channels = fbuf_.channels();
55 0 : for (size_t i = 0; i < ibuf_.num_channels(); ++i) {
56 0 : for (size_t j = 0; j < ibuf_.num_frames(); ++j) {
57 0 : float_channels[i][j] = int_channels[i][j];
58 : }
59 : }
60 0 : fvalid_ = true;
61 : }
62 0 : }
63 :
64 0 : void IFChannelBuffer::RefreshI() const {
65 0 : if (!ivalid_) {
66 0 : RTC_DCHECK(fvalid_);
67 0 : int16_t* const* int_channels = ibuf_.channels();
68 0 : ibuf_.set_num_channels(fbuf_.num_channels());
69 0 : const float* const* float_channels = fbuf_.channels();
70 0 : for (size_t i = 0; i < fbuf_.num_channels(); ++i) {
71 0 : FloatS16ToS16(float_channels[i],
72 : ibuf_.num_frames(),
73 0 : int_channels[i]);
74 : }
75 0 : ivalid_ = true;
76 : }
77 0 : }
78 :
79 : } // namespace webrtc
|