blob: 38d231e09d06471f99bd28ff2668f014dfa8e242 [file] [log] [blame]
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_audio/channel_buffer.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
kwibergc2b785d2016-02-24 05:22:32 -080014
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000015namespace webrtc {
16
Peter Kastingdce40cf2015-08-24 14:52:23 -070017IFChannelBuffer::IFChannelBuffer(size_t num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080018 size_t num_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070019 size_t num_bands)
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000020 : ivalid_(true),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000021 ibuf_(num_frames, num_channels, num_bands),
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000022 fvalid_(true),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000023 fbuf_(num_frames, num_channels, num_bands) {}
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000024
kwiberg942c8512016-08-29 13:10:29 -070025IFChannelBuffer::~IFChannelBuffer() = default;
26
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000027ChannelBuffer<int16_t>* IFChannelBuffer::ibuf() {
28 RefreshI();
29 fvalid_ = false;
30 return &ibuf_;
31}
32
33ChannelBuffer<float>* IFChannelBuffer::fbuf() {
34 RefreshF();
35 ivalid_ = false;
36 return &fbuf_;
37}
38
39const ChannelBuffer<int16_t>* IFChannelBuffer::ibuf_const() const {
40 RefreshI();
41 return &ibuf_;
42}
43
44const ChannelBuffer<float>* IFChannelBuffer::fbuf_const() const {
45 RefreshF();
46 return &fbuf_;
47}
48
49void IFChannelBuffer::RefreshF() const {
50 if (!fvalid_) {
kwibergc2b785d2016-02-24 05:22:32 -080051 RTC_DCHECK(ivalid_);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070052 fbuf_.set_num_channels(ibuf_.num_channels());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000053 const int16_t* const* int_channels = ibuf_.channels();
54 float* const* float_channels = fbuf_.channels();
Peter Kasting69558702016-01-12 16:26:35 -080055 for (size_t i = 0; i < ibuf_.num_channels(); ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070056 for (size_t j = 0; j < ibuf_.num_frames(); ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000057 float_channels[i][j] = int_channels[i][j];
58 }
59 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000060 fvalid_ = true;
61 }
62}
63
64void IFChannelBuffer::RefreshI() const {
65 if (!ivalid_) {
kwibergc2b785d2016-02-24 05:22:32 -080066 RTC_DCHECK(fvalid_);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000067 int16_t* const* int_channels = ibuf_.channels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070068 ibuf_.set_num_channels(fbuf_.num_channels());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000069 const float* const* float_channels = fbuf_.channels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070070 for (size_t i = 0; i < fbuf_.num_channels(); ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +020071 FloatS16ToS16(float_channels[i], ibuf_.num_frames(), int_channels[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000072 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000073 ivalid_ = true;
74 }
75}
76
77} // namespace webrtc