niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 "modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include <cstdint> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "common_audio/channel_buffer.h" |
| 18 | #include "common_audio/include/audio_util.h" |
| 19 | #include "common_audio/resampler/push_sinc_resampler.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/splitting_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
andrew@webrtc.org | 755b04a | 2011-11-15 16:57:56 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | namespace { |
| 25 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 26 | const size_t kSamplesPer16kHzChannel = 160; |
| 27 | const size_t kSamplesPer32kHzChannel = 320; |
| 28 | const size_t kSamplesPer48kHzChannel = 480; |
Alejandro Luebs | 5a92aa8 | 2015-04-27 11:34:45 -0700 | [diff] [blame] | 29 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 30 | size_t NumBandsFromSamplesPerChannel(size_t num_frames) { |
| 31 | size_t num_bands = 1; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 32 | if (num_frames == kSamplesPer32kHzChannel || |
| 33 | num_frames == kSamplesPer48kHzChannel) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 34 | num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 35 | } |
| 36 | return num_bands; |
| 37 | } |
| 38 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } // namespace |
| 40 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 41 | AudioBuffer::AudioBuffer(size_t input_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 42 | size_t num_input_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 43 | size_t process_num_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 44 | size_t num_process_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 45 | size_t output_num_frames) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 46 | : input_num_frames_(input_num_frames), |
| 47 | num_input_channels_(num_input_channels), |
| 48 | proc_num_frames_(process_num_frames), |
| 49 | num_proc_channels_(num_process_channels), |
| 50 | output_num_frames_(output_num_frames), |
| 51 | num_channels_(num_process_channels), |
| 52 | num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), |
| 53 | num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 54 | data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)), |
| 55 | output_buffer_(new IFChannelBuffer(output_num_frames_, num_channels_)) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 56 | RTC_DCHECK_GT(input_num_frames_, 0); |
| 57 | RTC_DCHECK_GT(proc_num_frames_, 0); |
| 58 | RTC_DCHECK_GT(output_num_frames_, 0); |
| 59 | RTC_DCHECK_GT(num_input_channels_, 0); |
| 60 | RTC_DCHECK_GT(num_proc_channels_, 0); |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 61 | RTC_DCHECK_LE(num_proc_channels_, num_input_channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 63 | if (input_num_frames_ != proc_num_frames_ || |
| 64 | output_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 65 | // Create an intermediate buffer for resampling. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | process_buffer_.reset( |
| 67 | new ChannelBuffer<float>(proc_num_frames_, num_proc_channels_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 68 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 69 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 70 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 71 | input_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 72 | new PushSincResampler(input_num_frames_, proc_num_frames_))); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | if (output_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 77 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
kwiberg | 4a206a9 | 2016-03-31 10:24:26 -0700 | [diff] [blame] | 78 | output_resamplers_.push_back(std::unique_ptr<PushSincResampler>( |
| 79 | new PushSincResampler(proc_num_frames_, output_num_frames_))); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 80 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 84 | if (num_bands_ > 1) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 85 | split_data_.reset( |
| 86 | new IFChannelBuffer(proc_num_frames_, num_proc_channels_, num_bands_)); |
| 87 | splitting_filter_.reset( |
| 88 | new SplittingFilter(num_proc_channels_, num_bands_, proc_num_frames_)); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
andrew@webrtc.org | 103657b | 2014-04-24 18:28:56 +0000 | [diff] [blame] | 92 | AudioBuffer::~AudioBuffer() {} |
| 93 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 94 | void AudioBuffer::CopyFrom(const float* const* data, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 95 | const StreamConfig& stream_config) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 96 | RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_); |
| 97 | RTC_DCHECK_EQ(stream_config.num_channels(), num_input_channels_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 98 | InitForNewData(); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 99 | // Initialized lazily because there's a different condition in |
| 100 | // DeinterleaveFrom. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 101 | const bool need_to_downmix = |
| 102 | num_input_channels_ > 1 && num_proc_channels_ == 1; |
| 103 | if (need_to_downmix && !input_buffer_) { |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 104 | input_buffer_.reset( |
| 105 | new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
| 106 | } |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 107 | |
| 108 | // Downmix. |
| 109 | const float* const* data_ptr = data; |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 110 | if (need_to_downmix) { |
| 111 | DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_, |
| 112 | input_buffer_->fbuf()->channels()[0]); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 113 | data_ptr = input_buffer_->fbuf_const()->channels(); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Resample. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 117 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 118 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | input_resamplers_[i]->Resample(data_ptr[i], input_num_frames_, |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 120 | process_buffer_->channels()[i], |
| 121 | proc_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 122 | } |
| 123 | data_ptr = process_buffer_->channels(); |
| 124 | } |
| 125 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 126 | // Convert to the S16 range. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 127 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | FloatToFloatS16(data_ptr[i], proc_num_frames_, |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 129 | data_->fbuf()->channels()[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 133 | void AudioBuffer::CopyTo(const StreamConfig& stream_config, |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 134 | float* const* data) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 135 | RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_); |
| 136 | RTC_DCHECK(stream_config.num_channels() == num_channels_ || |
| 137 | num_channels_ == 1); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 138 | |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 139 | // Convert to the float range. |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 140 | float* const* data_ptr = data; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 141 | if (output_num_frames_ != proc_num_frames_) { |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 142 | // Convert to an intermediate buffer for subsequent resampling. |
| 143 | data_ptr = process_buffer_->channels(); |
| 144 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 145 | for (size_t i = 0; i < num_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | FloatS16ToFloat(data_->fbuf()->channels()[i], proc_num_frames_, |
andrew@webrtc.org | 8328e7c | 2014-10-31 04:58:14 +0000 | [diff] [blame] | 147 | data_ptr[i]); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Resample. |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 151 | if (output_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 152 | for (size_t i = 0; i < num_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 153 | output_resamplers_[i]->Resample(data_ptr[i], proc_num_frames_, data[i], |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 154 | output_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 155 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | } |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 157 | |
| 158 | // Upmix. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 159 | for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) { |
aluebs | b2328d1 | 2016-01-11 20:32:29 -0800 | [diff] [blame] | 160 | memcpy(data[i], data[0], output_num_frames_ * sizeof(**data)); |
| 161 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | } |
| 163 | |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 164 | void AudioBuffer::InitForNewData() { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 165 | num_channels_ = num_proc_channels_; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 166 | data_->set_num_channels(num_proc_channels_); |
| 167 | if (split_data_.get()) { |
| 168 | split_data_->set_num_channels(num_proc_channels_); |
| 169 | } |
andrew@webrtc.org | 17e4064 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 172 | const float* const* AudioBuffer::split_channels_const_f(Band band) const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 173 | if (split_data_.get()) { |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 174 | return split_data_->fbuf_const()->channels(band); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 175 | } else { |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 176 | return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr; |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 180 | const float* const* AudioBuffer::channels_const_f() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 181 | return data_->fbuf_const()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | float* const* AudioBuffer::channels_f() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 185 | return data_->fbuf()->channels(); |
claguna@google.com | bfacaab | 2014-09-25 20:52:08 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 188 | const float* const* AudioBuffer::split_bands_const_f(size_t channel) const { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 189 | return split_data_.get() ? split_data_->fbuf_const()->bands(channel) |
| 190 | : data_->fbuf_const()->bands(channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 193 | float* const* AudioBuffer::split_bands_f(size_t channel) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 194 | return split_data_.get() ? split_data_->fbuf()->bands(channel) |
| 195 | : data_->fbuf()->bands(channel); |
aluebs@webrtc.org | a7384a1 | 2014-12-03 01:06:35 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 198 | size_t AudioBuffer::num_channels() const { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 199 | return num_channels_; |
| 200 | } |
| 201 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 202 | void AudioBuffer::set_num_channels(size_t num_channels) { |
aluebs@webrtc.org | 27d106b | 2014-12-11 17:09:21 +0000 | [diff] [blame] | 203 | num_channels_ = num_channels; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 204 | data_->set_num_channels(num_channels); |
| 205 | if (split_data_.get()) { |
| 206 | split_data_->set_num_channels(num_channels); |
| 207 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 210 | size_t AudioBuffer::num_frames() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 211 | return proc_num_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 214 | size_t AudioBuffer::num_frames_per_band() const { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 215 | return num_split_frames_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 218 | size_t AudioBuffer::num_bands() const { |
aluebs@webrtc.org | c5ebbd9 | 2014-12-10 19:30:57 +0000 | [diff] [blame] | 219 | return num_bands_; |
| 220 | } |
| 221 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 222 | // The resampler is only for supporting 48kHz to 16kHz in the reverse stream. |
Per Åhgren | a135127 | 2019-08-15 12:15:46 +0200 | [diff] [blame] | 223 | void AudioBuffer::DeinterleaveFrom(const AudioFrame* frame) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 224 | RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_); |
| 225 | RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 226 | InitForNewData(); |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 227 | // Initialized lazily because there's a different condition in CopyFrom. |
| 228 | if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) { |
| 229 | input_buffer_.reset( |
| 230 | new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
| 231 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 233 | int16_t* const* deinterleaved; |
| 234 | if (input_num_frames_ == proc_num_frames_) { |
| 235 | deinterleaved = data_->ibuf()->channels(); |
| 236 | } else { |
| 237 | deinterleaved = input_buffer_->ibuf()->channels(); |
| 238 | } |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 239 | // TODO(yujo): handle muted frames more efficiently. |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 240 | if (num_proc_channels_ == 1) { |
| 241 | // Downmix and deinterleave simultaneously. |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 242 | DownmixInterleavedToMono(frame->data(), input_num_frames_, |
Michael Graczyk | 86c6d33 | 2015-07-23 11:41:39 -0700 | [diff] [blame] | 243 | num_input_channels_, deinterleaved[0]); |
andrew@webrtc.org | 30be827 | 2014-09-24 20:06:23 +0000 | [diff] [blame] | 244 | } else { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 245 | RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 246 | Deinterleave(frame->data(), input_num_frames_, num_proc_channels_, |
Alejandro Luebs | 05c7605 | 2015-05-20 14:39:39 -0700 | [diff] [blame] | 247 | deinterleaved); |
| 248 | } |
| 249 | |
| 250 | // Resample. |
| 251 | if (input_num_frames_ != proc_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 252 | for (size_t i = 0; i < num_proc_channels_; ++i) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 253 | input_resamplers_[i]->Resample( |
| 254 | input_buffer_->fbuf_const()->channels()[i], input_num_frames_, |
| 255 | data_->fbuf()->channels()[i], proc_num_frames_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Per Åhgren | a135127 | 2019-08-15 12:15:46 +0200 | [diff] [blame] | 260 | void AudioBuffer::InterleaveTo(AudioFrame* frame) const { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 261 | RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1); |
| 262 | RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 263 | |
| 264 | // Resample if necessary. |
| 265 | IFChannelBuffer* data_ptr = data_.get(); |
| 266 | if (proc_num_frames_ != output_num_frames_) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 267 | for (size_t i = 0; i < num_channels_; ++i) { |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 268 | output_resamplers_[i]->Resample( |
| 269 | data_->fbuf()->channels()[i], proc_num_frames_, |
| 270 | output_buffer_->fbuf()->channels()[i], output_num_frames_); |
| 271 | } |
| 272 | data_ptr = output_buffer_.get(); |
| 273 | } |
| 274 | |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 275 | // TODO(yujo): handle muted frames more efficiently. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 276 | if (frame->num_channels_ == num_channels_) { |
Alejandro Luebs | 40cbec5 | 2016-04-05 17:29:19 -0700 | [diff] [blame] | 277 | Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 278 | frame->mutable_data()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 279 | } else { |
Alejandro Luebs | 40cbec5 | 2016-04-05 17:29:19 -0700 | [diff] [blame] | 280 | UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 281 | frame->num_channels_, frame->mutable_data()); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 282 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | } |
| 284 | |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 285 | void AudioBuffer::SplitIntoFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 286 | splitting_filter_->Analysis(data_.get(), split_data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void AudioBuffer::MergeFrequencyBands() { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 290 | splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
aluebs@webrtc.org | be05c74 | 2014-11-14 22:18:10 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 293 | void AudioBuffer::CopySplitChannelDataTo(size_t channel, |
| 294 | int16_t* const* split_band_data) { |
| 295 | for (size_t k = 0; k < num_bands(); ++k) { |
| 296 | const float* band_data = split_bands_f(channel)[k]; |
| 297 | RTC_DCHECK(split_band_data[k]); |
| 298 | RTC_DCHECK(band_data); |
| 299 | for (size_t i = 0; i < num_frames_per_band(); ++i) { |
| 300 | split_band_data[k][i] = FloatS16ToS16(band_data[i]); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void AudioBuffer::CopySplitChannelDataFrom( |
| 306 | size_t channel, |
| 307 | const int16_t* const* split_band_data) { |
| 308 | for (size_t k = 0; k < num_bands(); ++k) { |
| 309 | float* band_data = split_bands_f(channel)[k]; |
| 310 | RTC_DCHECK(split_band_data[k]); |
| 311 | RTC_DCHECK(band_data); |
| 312 | for (size_t i = 0; i < num_frames_per_band(); ++i) { |
| 313 | band_data[i] = split_band_data[k][i]; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 318 | } // namespace webrtc |