blob: 0c38a4fe82b854de906f06e675fc694842c9b61b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 "modules/audio_processing/audio_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
14#include <cstdint>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "common_audio/channel_buffer.h"
17#include "common_audio/include/audio_util.h"
18#include "common_audio/resampler/push_sinc_resampler.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "modules/audio_processing/splitting_filter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000021
niklase@google.com470e71d2011-07-07 08:21:25 +000022namespace webrtc {
23namespace {
24
Peter Kastingdce40cf2015-08-24 14:52:23 -070025const size_t kSamplesPer16kHzChannel = 160;
26const size_t kSamplesPer32kHzChannel = 320;
27const size_t kSamplesPer48kHzChannel = 480;
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070028
Michael Graczyk86c6d332015-07-23 11:41:39 -070029int KeyboardChannelIndex(const StreamConfig& stream_config) {
30 if (!stream_config.has_keyboard()) {
kwiberg9e2be5f2016-09-14 05:23:22 -070031 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -080032 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +000033 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +000034
Michael Graczyk86c6d332015-07-23 11:41:39 -070035 return stream_config.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000036}
37
Peter Kastingdce40cf2015-08-24 14:52:23 -070038size_t NumBandsFromSamplesPerChannel(size_t num_frames) {
39 size_t num_bands = 1;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000040 if (num_frames == kSamplesPer32kHzChannel ||
41 num_frames == kSamplesPer48kHzChannel) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000043 }
44 return num_bands;
45}
46
niklase@google.com470e71d2011-07-07 08:21:25 +000047} // namespace
48
Peter Kastingdce40cf2015-08-24 14:52:23 -070049AudioBuffer::AudioBuffer(size_t input_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080050 size_t num_input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070051 size_t process_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080052 size_t num_process_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070053 size_t output_num_frames)
Yves Gerey665174f2018-06-19 15:03:05 +020054 : input_num_frames_(input_num_frames),
55 num_input_channels_(num_input_channels),
56 proc_num_frames_(process_num_frames),
57 num_proc_channels_(num_process_channels),
58 output_num_frames_(output_num_frames),
59 num_channels_(num_process_channels),
60 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
61 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
62 mixed_low_pass_valid_(false),
63 reference_copied_(false),
64 activity_(AudioFrame::kVadUnknown),
65 keyboard_data_(NULL),
66 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)),
67 output_buffer_(new IFChannelBuffer(output_num_frames_, num_channels_)) {
kwibergaf476c72016-11-28 15:21:39 -080068 RTC_DCHECK_GT(input_num_frames_, 0);
69 RTC_DCHECK_GT(proc_num_frames_, 0);
70 RTC_DCHECK_GT(output_num_frames_, 0);
71 RTC_DCHECK_GT(num_input_channels_, 0);
72 RTC_DCHECK_GT(num_proc_channels_, 0);
kwiberg9e2be5f2016-09-14 05:23:22 -070073 RTC_DCHECK_LE(num_proc_channels_, num_input_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000075 if (input_num_frames_ != proc_num_frames_ ||
76 output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000077 // Create an intermediate buffer for resampling.
Yves Gerey665174f2018-06-19 15:03:05 +020078 process_buffer_.reset(
79 new ChannelBuffer<float>(proc_num_frames_, num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080082 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070083 input_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
84 new PushSincResampler(input_num_frames_, proc_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000085 }
86 }
87
88 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080089 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070090 output_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
91 new PushSincResampler(proc_num_frames_, output_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000092 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093 }
94 }
95
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000096 if (num_bands_ > 1) {
Yves Gerey665174f2018-06-19 15:03:05 +020097 split_data_.reset(
98 new IFChannelBuffer(proc_num_frames_, num_proc_channels_, num_bands_));
99 splitting_filter_.reset(
100 new SplittingFilter(num_proc_channels_, num_bands_, proc_num_frames_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000101 }
102}
103
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000104AudioBuffer::~AudioBuffer() {}
105
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000106void AudioBuffer::CopyFrom(const float* const* data,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700107 const StreamConfig& stream_config) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700108 RTC_DCHECK_EQ(stream_config.num_frames(), input_num_frames_);
109 RTC_DCHECK_EQ(stream_config.num_channels(), num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000110 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700111 // Initialized lazily because there's a different condition in
112 // DeinterleaveFrom.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700113 const bool need_to_downmix =
114 num_input_channels_ > 1 && num_proc_channels_ == 1;
115 if (need_to_downmix && !input_buffer_) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700116 input_buffer_.reset(
117 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
118 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000119
Michael Graczyk86c6d332015-07-23 11:41:39 -0700120 if (stream_config.has_keyboard()) {
121 keyboard_data_ = data[KeyboardChannelIndex(stream_config)];
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000122 }
123
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000124 // Downmix.
125 const float* const* data_ptr = data;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700126 if (need_to_downmix) {
127 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
128 input_buffer_->fbuf()->channels()[0]);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700129 data_ptr = input_buffer_->fbuf_const()->channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000130 }
131
132 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000133 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800134 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200135 input_resamplers_[i]->Resample(data_ptr[i], input_num_frames_,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000136 process_buffer_->channels()[i],
137 proc_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000138 }
139 data_ptr = process_buffer_->channels();
140 }
141
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000142 // Convert to the S16 range.
Peter Kasting69558702016-01-12 16:26:35 -0800143 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200144 FloatToFloatS16(data_ptr[i], proc_num_frames_,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000145 data_->fbuf()->channels()[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000146 }
147}
148
Michael Graczyk86c6d332015-07-23 11:41:39 -0700149void AudioBuffer::CopyTo(const StreamConfig& stream_config,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000150 float* const* data) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700151 RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_);
152 RTC_DCHECK(stream_config.num_channels() == num_channels_ ||
153 num_channels_ == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000154
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000155 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000156 float* const* data_ptr = data;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000157 if (output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000158 // Convert to an intermediate buffer for subsequent resampling.
159 data_ptr = process_buffer_->channels();
160 }
Peter Kasting69558702016-01-12 16:26:35 -0800161 for (size_t i = 0; i < num_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200162 FloatS16ToFloat(data_->fbuf()->channels()[i], proc_num_frames_,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000163 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000164 }
165
166 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000167 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800168 for (size_t i = 0; i < num_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200169 output_resamplers_[i]->Resample(data_ptr[i], proc_num_frames_, data[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000170 output_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000171 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 }
aluebsb2328d12016-01-11 20:32:29 -0800173
174 // Upmix.
Peter Kasting69558702016-01-12 16:26:35 -0800175 for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
aluebsb2328d12016-01-11 20:32:29 -0800176 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
177 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000178}
179
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000180void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000181 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000182 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000183 reference_copied_ = false;
184 activity_ = AudioFrame::kVadUnknown;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000185 num_channels_ = num_proc_channels_;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700186 data_->set_num_channels(num_proc_channels_);
187 if (split_data_.get()) {
188 split_data_->set_num_channels(num_proc_channels_);
189 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000190}
191
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000192const int16_t* const* AudioBuffer::channels_const() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000193 return data_->ibuf_const()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000194}
195
196int16_t* const* AudioBuffer::channels() {
197 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000198 return data_->ibuf()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000199}
200
Peter Kasting69558702016-01-12 16:26:35 -0800201const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200202 return split_data_.get() ? split_data_->ibuf_const()->bands(channel)
203 : data_->ibuf_const()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000204}
205
Peter Kasting69558702016-01-12 16:26:35 -0800206int16_t* const* AudioBuffer::split_bands(size_t channel) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000207 mixed_low_pass_valid_ = false;
Yves Gerey665174f2018-06-19 15:03:05 +0200208 return split_data_.get() ? split_data_->ibuf()->bands(channel)
209 : data_->ibuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000210}
211
212const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000213 if (split_data_.get()) {
214 return split_data_->ibuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000215 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000216 return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000217 }
218}
219
220int16_t* const* AudioBuffer::split_channels(Band band) {
221 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000222 if (split_data_.get()) {
223 return split_data_->ibuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000224 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000225 return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000226 }
227}
228
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000229ChannelBuffer<int16_t>* AudioBuffer::data() {
230 mixed_low_pass_valid_ = false;
231 return data_->ibuf();
232}
233
234const ChannelBuffer<int16_t>* AudioBuffer::data() const {
235 return data_->ibuf_const();
236}
237
238ChannelBuffer<int16_t>* AudioBuffer::split_data() {
239 mixed_low_pass_valid_ = false;
240 return split_data_.get() ? split_data_->ibuf() : data_->ibuf();
241}
242
243const ChannelBuffer<int16_t>* AudioBuffer::split_data() const {
244 return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const();
245}
246
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000247const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000248 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000249}
250
251float* const* AudioBuffer::channels_f() {
252 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000253 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000254}
255
Peter Kasting69558702016-01-12 16:26:35 -0800256const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200257 return split_data_.get() ? split_data_->fbuf_const()->bands(channel)
258 : data_->fbuf_const()->bands(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000259}
260
Peter Kasting69558702016-01-12 16:26:35 -0800261float* const* AudioBuffer::split_bands_f(size_t channel) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000262 mixed_low_pass_valid_ = false;
Yves Gerey665174f2018-06-19 15:03:05 +0200263 return split_data_.get() ? split_data_->fbuf()->bands(channel)
264 : data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000265}
266
267const float* const* AudioBuffer::split_channels_const_f(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000268 if (split_data_.get()) {
269 return split_data_->fbuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000270 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000271 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000272 }
273}
274
275float* const* AudioBuffer::split_channels_f(Band band) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000276 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000277 if (split_data_.get()) {
278 return split_data_->fbuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000279 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000280 return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000281 }
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000282}
283
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000284ChannelBuffer<float>* AudioBuffer::data_f() {
285 mixed_low_pass_valid_ = false;
286 return data_->fbuf();
287}
288
289const ChannelBuffer<float>* AudioBuffer::data_f() const {
290 return data_->fbuf_const();
291}
292
293ChannelBuffer<float>* AudioBuffer::split_data_f() {
294 mixed_low_pass_valid_ = false;
295 return split_data_.get() ? split_data_->fbuf() : data_->fbuf();
296}
297
298const ChannelBuffer<float>* AudioBuffer::split_data_f() const {
299 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const();
300}
301
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000302const int16_t* AudioBuffer::mixed_low_pass_data() {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000303 if (num_proc_channels_ == 1) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000304 return split_bands_const(0)[kBand0To8kHz];
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000305 }
306
307 if (!mixed_low_pass_valid_) {
308 if (!mixed_low_pass_channels_.get()) {
309 mixed_low_pass_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000310 new ChannelBuffer<int16_t>(num_split_frames_, 1));
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000311 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700312
313 DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz),
314 num_split_frames_, num_channels_,
315 mixed_low_pass_channels_->channels()[0]);
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000316 mixed_low_pass_valid_ = true;
317 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000318 return mixed_low_pass_channels_->channels()[0];
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000321const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000322 if (!reference_copied_) {
323 return NULL;
324 }
325
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000326 return low_pass_reference_channels_->channels()[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000327}
328
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000329const float* AudioBuffer::keyboard_data() const {
330 return keyboard_data_;
331}
332
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000333void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
334 activity_ = activity;
335}
336
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000337AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000338 return activity_;
339}
340
Peter Kasting69558702016-01-12 16:26:35 -0800341size_t AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000342 return num_channels_;
343}
344
Peter Kasting69558702016-01-12 16:26:35 -0800345void AudioBuffer::set_num_channels(size_t num_channels) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000346 num_channels_ = num_channels;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700347 data_->set_num_channels(num_channels);
348 if (split_data_.get()) {
349 split_data_->set_num_channels(num_channels);
350 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000351}
352
Peter Kastingdce40cf2015-08-24 14:52:23 -0700353size_t AudioBuffer::num_frames() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000354 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000355}
356
Peter Kastingdce40cf2015-08-24 14:52:23 -0700357size_t AudioBuffer::num_frames_per_band() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000358 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000359}
360
Peter Kastingdce40cf2015-08-24 14:52:23 -0700361size_t AudioBuffer::num_keyboard_frames() const {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000362 // We don't resample the keyboard channel.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000363 return input_num_frames_;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000364}
365
Peter Kastingdce40cf2015-08-24 14:52:23 -0700366size_t AudioBuffer::num_bands() const {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000367 return num_bands_;
368}
369
Alejandro Luebs05c76052015-05-20 14:39:39 -0700370// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000371void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
kwiberg9e2be5f2016-09-14 05:23:22 -0700372 RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_);
373 RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000374 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700375 // Initialized lazily because there's a different condition in CopyFrom.
376 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
377 input_buffer_.reset(
378 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
379 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000380 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381
Alejandro Luebs05c76052015-05-20 14:39:39 -0700382 int16_t* const* deinterleaved;
383 if (input_num_frames_ == proc_num_frames_) {
384 deinterleaved = data_->ibuf()->channels();
385 } else {
386 deinterleaved = input_buffer_->ibuf()->channels();
387 }
yujo36b1a5f2017-06-12 12:45:32 -0700388 // TODO(yujo): handle muted frames more efficiently.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700389 if (num_proc_channels_ == 1) {
390 // Downmix and deinterleave simultaneously.
yujo36b1a5f2017-06-12 12:45:32 -0700391 DownmixInterleavedToMono(frame->data(), input_num_frames_,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700392 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000393 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700394 RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_);
Yves Gerey665174f2018-06-19 15:03:05 +0200395 Deinterleave(frame->data(), input_num_frames_, num_proc_channels_,
Alejandro Luebs05c76052015-05-20 14:39:39 -0700396 deinterleaved);
397 }
398
399 // Resample.
400 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800401 for (size_t i = 0; i < num_proc_channels_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200402 input_resamplers_[i]->Resample(
403 input_buffer_->fbuf_const()->channels()[i], input_num_frames_,
404 data_->fbuf()->channels()[i], proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405 }
406 }
407}
408
kthelgasonc7daea82017-03-14 03:10:07 -0700409void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000410 frame->vad_activity_ = activity_;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000411 if (!data_changed) {
412 return;
413 }
414
kwiberg9e2be5f2016-09-14 05:23:22 -0700415 RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1);
416 RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700417
418 // Resample if necessary.
419 IFChannelBuffer* data_ptr = data_.get();
420 if (proc_num_frames_ != output_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800421 for (size_t i = 0; i < num_channels_; ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -0700422 output_resamplers_[i]->Resample(
423 data_->fbuf()->channels()[i], proc_num_frames_,
424 output_buffer_->fbuf()->channels()[i], output_num_frames_);
425 }
426 data_ptr = output_buffer_.get();
427 }
428
yujo36b1a5f2017-06-12 12:45:32 -0700429 // TODO(yujo): handle muted frames more efficiently.
ekmeyerson60d9b332015-08-14 10:35:55 -0700430 if (frame->num_channels_ == num_channels_) {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700431 Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_,
yujo36b1a5f2017-06-12 12:45:32 -0700432 frame->mutable_data());
ekmeyerson60d9b332015-08-14 10:35:55 -0700433 } else {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700434 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_,
yujo36b1a5f2017-06-12 12:45:32 -0700435 frame->num_channels_, frame->mutable_data());
ekmeyerson60d9b332015-08-14 10:35:55 -0700436 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000437}
438
niklase@google.com470e71d2011-07-07 08:21:25 +0000439void AudioBuffer::CopyLowPassToReference() {
440 reference_copied_ = true;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000441 if (!low_pass_reference_channels_.get() ||
442 low_pass_reference_channels_->num_channels() != num_channels_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000443 low_pass_reference_channels_.reset(
Yves Gerey665174f2018-06-19 15:03:05 +0200444 new ChannelBuffer<int16_t>(num_split_frames_, num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000445 }
Peter Kasting69558702016-01-12 16:26:35 -0800446 for (size_t i = 0; i < num_proc_channels_; i++) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000447 memcpy(low_pass_reference_channels_->channels()[i],
448 split_bands_const(i)[kBand0To8kHz],
449 low_pass_reference_channels_->num_frames_per_band() *
450 sizeof(split_bands_const(i)[kBand0To8kHz][0]));
niklase@google.com470e71d2011-07-07 08:21:25 +0000451 }
452}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000453
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000454void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000455 splitting_filter_->Analysis(data_.get(), split_data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000456}
457
458void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000459 splitting_filter_->Synthesis(split_data_.get(), data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000460}
461
niklase@google.com470e71d2011-07-07 08:21:25 +0000462} // namespace webrtc