aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_audio/channel_buffer.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <cstdint> |
| 14 | |
| 15 | #include "common_audio/include/audio_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 17 | |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 20 | IFChannelBuffer::IFChannelBuffer(size_t num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 21 | size_t num_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 22 | size_t num_bands) |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 23 | : ivalid_(true), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 24 | ibuf_(num_frames, num_channels, num_bands), |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 25 | fvalid_(true), |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 26 | fbuf_(num_frames, num_channels, num_bands) {} |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 27 | |
kwiberg | 942c851 | 2016-08-29 13:10:29 -0700 | [diff] [blame] | 28 | IFChannelBuffer::~IFChannelBuffer() = default; |
| 29 | |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 30 | ChannelBuffer<int16_t>* IFChannelBuffer::ibuf() { |
| 31 | RefreshI(); |
| 32 | fvalid_ = false; |
| 33 | return &ibuf_; |
| 34 | } |
| 35 | |
| 36 | ChannelBuffer<float>* IFChannelBuffer::fbuf() { |
| 37 | RefreshF(); |
| 38 | ivalid_ = false; |
| 39 | return &fbuf_; |
| 40 | } |
| 41 | |
| 42 | const ChannelBuffer<int16_t>* IFChannelBuffer::ibuf_const() const { |
| 43 | RefreshI(); |
| 44 | return &ibuf_; |
| 45 | } |
| 46 | |
| 47 | const ChannelBuffer<float>* IFChannelBuffer::fbuf_const() const { |
| 48 | RefreshF(); |
| 49 | return &fbuf_; |
| 50 | } |
| 51 | |
| 52 | void IFChannelBuffer::RefreshF() const { |
| 53 | if (!fvalid_) { |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 54 | RTC_DCHECK(ivalid_); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 55 | fbuf_.set_num_channels(ibuf_.num_channels()); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 56 | const int16_t* const* int_channels = ibuf_.channels(); |
| 57 | float* const* float_channels = fbuf_.channels(); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 58 | for (size_t i = 0; i < ibuf_.num_channels(); ++i) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 59 | for (size_t j = 0; j < ibuf_.num_frames(); ++j) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 60 | float_channels[i][j] = int_channels[i][j]; |
| 61 | } |
| 62 | } |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 63 | fvalid_ = true; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void IFChannelBuffer::RefreshI() const { |
| 68 | if (!ivalid_) { |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 69 | RTC_DCHECK(fvalid_); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 70 | int16_t* const* int_channels = ibuf_.channels(); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 71 | ibuf_.set_num_channels(fbuf_.num_channels()); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 72 | const float* const* float_channels = fbuf_.channels(); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 73 | for (size_t i = 0; i < fbuf_.num_channels(); ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 74 | FloatS16ToS16(float_channels[i], ibuf_.num_frames(), int_channels[i]); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 75 | } |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 76 | ivalid_ = true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | } // namespace webrtc |