blob: cc0001b944d7f8dfd79ab9e55dabb23e7b06a715 [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
kjellander@webrtc.org035e9122015-01-28 19:57:00 +000011#include "webrtc/common_audio/channel_buffer.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000012
kwibergc2b785d2016-02-24 05:22:32 -080013#include "webrtc/base/checks.h"
14
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
25ChannelBuffer<int16_t>* IFChannelBuffer::ibuf() {
26 RefreshI();
27 fvalid_ = false;
28 return &ibuf_;
29}
30
31ChannelBuffer<float>* IFChannelBuffer::fbuf() {
32 RefreshF();
33 ivalid_ = false;
34 return &fbuf_;
35}
36
37const ChannelBuffer<int16_t>* IFChannelBuffer::ibuf_const() const {
38 RefreshI();
39 return &ibuf_;
40}
41
42const ChannelBuffer<float>* IFChannelBuffer::fbuf_const() const {
43 RefreshF();
44 return &fbuf_;
45}
46
47void IFChannelBuffer::RefreshF() const {
48 if (!fvalid_) {
kwibergc2b785d2016-02-24 05:22:32 -080049 RTC_DCHECK(ivalid_);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070050 fbuf_.set_num_channels(ibuf_.num_channels());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000051 const int16_t* const* int_channels = ibuf_.channels();
52 float* const* float_channels = fbuf_.channels();
Peter Kasting69558702016-01-12 16:26:35 -080053 for (size_t i = 0; i < ibuf_.num_channels(); ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070054 for (size_t j = 0; j < ibuf_.num_frames(); ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000055 float_channels[i][j] = int_channels[i][j];
56 }
57 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000058 fvalid_ = true;
59 }
60}
61
62void IFChannelBuffer::RefreshI() const {
63 if (!ivalid_) {
kwibergc2b785d2016-02-24 05:22:32 -080064 RTC_DCHECK(fvalid_);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000065 int16_t* const* int_channels = ibuf_.channels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070066 ibuf_.set_num_channels(fbuf_.num_channels());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000067 const float* const* float_channels = fbuf_.channels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070068 for (size_t i = 0; i < fbuf_.num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000069 FloatS16ToS16(float_channels[i],
70 ibuf_.num_frames(),
71 int_channels[i]);
72 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000073 ivalid_ = true;
74 }
75}
76
77} // namespace webrtc