blob: 349d4245ccdb6e472db3cb60ee3a951c1e333b3b [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_);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000050 const int16_t* const* int_channels = ibuf_.channels();
51 float* const* float_channels = fbuf_.channels();
Peter Kasting69558702016-01-12 16:26:35 -080052 for (size_t i = 0; i < ibuf_.num_channels(); ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070053 for (size_t j = 0; j < ibuf_.num_frames(); ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000054 float_channels[i][j] = int_channels[i][j];
55 }
56 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000057 fvalid_ = true;
58 }
59}
60
61void IFChannelBuffer::RefreshI() const {
62 if (!ivalid_) {
kwibergc2b785d2016-02-24 05:22:32 -080063 RTC_DCHECK(fvalid_);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000064 int16_t* const* int_channels = ibuf_.channels();
65 const float* const* float_channels = fbuf_.channels();
Peter Kasting69558702016-01-12 16:26:35 -080066 for (size_t i = 0; i < ibuf_.num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000067 FloatS16ToS16(float_channels[i],
68 ibuf_.num_frames(),
69 int_channels[i]);
70 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000071 ivalid_ = true;
72 }
73}
74
75} // namespace webrtc