blob: b9b8c25e37506ed3899f84f0a9f9dce7c9f01b1c [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <cstdint>
14
15#include "common_audio/include/audio_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
kwibergc2b785d2016-02-24 05:22:32 -080017
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000018namespace webrtc {
19
Peter Kastingdce40cf2015-08-24 14:52:23 -070020IFChannelBuffer::IFChannelBuffer(size_t num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080021 size_t num_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070022 size_t num_bands)
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000023 : ivalid_(true),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000024 ibuf_(num_frames, num_channels, num_bands),
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000025 fvalid_(true),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000026 fbuf_(num_frames, num_channels, num_bands) {}
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000027
kwiberg942c8512016-08-29 13:10:29 -070028IFChannelBuffer::~IFChannelBuffer() = default;
29
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000030ChannelBuffer<int16_t>* IFChannelBuffer::ibuf() {
31 RefreshI();
32 fvalid_ = false;
33 return &ibuf_;
34}
35
36ChannelBuffer<float>* IFChannelBuffer::fbuf() {
37 RefreshF();
38 ivalid_ = false;
39 return &fbuf_;
40}
41
42const ChannelBuffer<int16_t>* IFChannelBuffer::ibuf_const() const {
43 RefreshI();
44 return &ibuf_;
45}
46
47const ChannelBuffer<float>* IFChannelBuffer::fbuf_const() const {
48 RefreshF();
49 return &fbuf_;
50}
51
52void IFChannelBuffer::RefreshF() const {
53 if (!fvalid_) {
kwibergc2b785d2016-02-24 05:22:32 -080054 RTC_DCHECK(ivalid_);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070055 fbuf_.set_num_channels(ibuf_.num_channels());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000056 const int16_t* const* int_channels = ibuf_.channels();
57 float* const* float_channels = fbuf_.channels();
Peter Kasting69558702016-01-12 16:26:35 -080058 for (size_t i = 0; i < ibuf_.num_channels(); ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070059 for (size_t j = 0; j < ibuf_.num_frames(); ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000060 float_channels[i][j] = int_channels[i][j];
61 }
62 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000063 fvalid_ = true;
64 }
65}
66
67void IFChannelBuffer::RefreshI() const {
68 if (!ivalid_) {
kwibergc2b785d2016-02-24 05:22:32 -080069 RTC_DCHECK(fvalid_);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000070 int16_t* const* int_channels = ibuf_.channels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070071 ibuf_.set_num_channels(fbuf_.num_channels());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000072 const float* const* float_channels = fbuf_.channels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070073 for (size_t i = 0; i < fbuf_.num_channels(); ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +020074 FloatS16ToS16(float_channels[i], ibuf_.num_frames(), int_channels[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000075 }
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000076 ivalid_ = true;
77 }
78}
79
80} // namespace webrtc