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