blob: 16f11742e3fbb6dd891c674aa877d8a79f6665f6 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
andrew@webrtc.org63a50982012-05-02 23:56:372 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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 04:47:3111#include "modules/audio_processing/audio_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:2512
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "common_audio/channel_buffer.h"
14#include "common_audio/include/audio_util.h"
15#include "common_audio/resampler/push_sinc_resampler.h"
16#include "common_audio/signal_processing/include/signal_processing_library.h"
17#include "modules/audio_processing/common.h"
18#include "rtc_base/checks.h"
andrew@webrtc.org755b04a2011-11-15 16:57:5619
niklase@google.com470e71d2011-07-07 08:21:2520namespace webrtc {
21namespace {
22
Peter Kastingdce40cf2015-08-24 21:52:2323const size_t kSamplesPer16kHzChannel = 160;
24const size_t kSamplesPer32kHzChannel = 320;
25const size_t kSamplesPer48kHzChannel = 480;
Alejandro Luebs5a92aa82015-04-27 18:34:4526
Michael Graczyk86c6d332015-07-23 18:41:3927int KeyboardChannelIndex(const StreamConfig& stream_config) {
28 if (!stream_config.has_keyboard()) {
kwiberg9e2be5f2016-09-14 12:23:2229 RTC_NOTREACHED();
pkasting25702cb2016-01-08 21:50:2730 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:5631 }
andrew@webrtc.org103657b2014-04-24 18:28:5632
Michael Graczyk86c6d332015-07-23 18:41:3933 return stream_config.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:0434}
35
Peter Kastingdce40cf2015-08-24 21:52:2336size_t NumBandsFromSamplesPerChannel(size_t num_frames) {
37 size_t num_bands = 1;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1538 if (num_frames == kSamplesPer32kHzChannel ||
39 num_frames == kSamplesPer48kHzChannel) {
Peter Kastingdce40cf2015-08-24 21:52:2340 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1541 }
42 return num_bands;
43}
44
niklase@google.com470e71d2011-07-07 08:21:2545} // namespace
46
Peter Kastingdce40cf2015-08-24 21:52:2347AudioBuffer::AudioBuffer(size_t input_num_frames,
Peter Kasting69558702016-01-13 00:26:3548 size_t num_input_channels,
Peter Kastingdce40cf2015-08-24 21:52:2349 size_t process_num_frames,
Peter Kasting69558702016-01-13 00:26:3550 size_t num_process_channels,
Peter Kastingdce40cf2015-08-24 21:52:2351 size_t output_num_frames)
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1552 : input_num_frames_(input_num_frames),
andrew@webrtc.orgddbb8a22014-04-22 21:00:0453 num_input_channels_(num_input_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1554 proc_num_frames_(process_num_frames),
andrew@webrtc.orgddbb8a22014-04-22 21:00:0455 num_proc_channels_(num_process_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1556 output_num_frames_(output_num_frames),
aluebs@webrtc.org27d106b2014-12-11 17:09:2157 num_channels_(num_process_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1558 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
Peter Kasting728d9032015-06-11 21:31:3859 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
aluebs@webrtc.org2561d522014-07-17 08:27:3960 mixed_low_pass_valid_(false),
andrew@webrtc.orged083d42011-09-19 15:28:5161 reference_copied_(false),
62 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:5663 keyboard_data_(NULL),
kthelgasonc7daea82017-03-14 10:10:0764 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)),
65 output_buffer_(new IFChannelBuffer(output_num_frames_, num_channels_)) {
kwibergaf476c72016-11-28 23:21:3966 RTC_DCHECK_GT(input_num_frames_, 0);
67 RTC_DCHECK_GT(proc_num_frames_, 0);
68 RTC_DCHECK_GT(output_num_frames_, 0);
69 RTC_DCHECK_GT(num_input_channels_, 0);
70 RTC_DCHECK_GT(num_proc_channels_, 0);
kwiberg9e2be5f2016-09-14 12:23:2271 RTC_DCHECK_LE(num_proc_channels_, num_input_channels_);
niklase@google.com470e71d2011-07-07 08:21:2572
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1573 if (input_num_frames_ != proc_num_frames_ ||
74 output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:0475 // Create an intermediate buffer for resampling.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1576 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:0477 num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:0478
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1579 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-13 00:26:3580 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 17:24:2681 input_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
82 new PushSincResampler(input_num_frames_, proc_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1583 }
84 }
85
86 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-13 00:26:3587 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 17:24:2688 output_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
89 new PushSincResampler(proc_num_frames_, output_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1590 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:0491 }
92 }
93
aluebs@webrtc.orgd35a5c32015-02-10 22:52:1594 if (num_bands_ > 1) {
95 split_data_.reset(new IFChannelBuffer(proc_num_frames_,
96 num_proc_channels_,
97 num_bands_));
Alejandro Luebs5a92aa82015-04-27 18:34:4598 splitting_filter_.reset(new SplittingFilter(num_proc_channels_,
99 num_bands_,
100 proc_num_frames_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04101 }
102}
103
andrew@webrtc.org103657b2014-04-24 18:28:56104AudioBuffer::~AudioBuffer() {}
105
andrew@webrtc.orgddbb8a22014-04-22 21:00:04106void AudioBuffer::CopyFrom(const float* const* data,
Michael Graczyk86c6d332015-07-23 18:41:39107 const StreamConfig& stream_config) {
kwiberg9e2be5f2016-09-14 12:23:22108 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:04110 InitForNewData();
Alejandro Luebs05c76052015-05-20 21:39:39111 // Initialized lazily because there's a different condition in
112 // DeinterleaveFrom.
Michael Graczyk86c6d332015-07-23 18:41:39113 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 21:39:39116 input_buffer_.reset(
117 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
118 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04119
Michael Graczyk86c6d332015-07-23 18:41:39120 if (stream_config.has_keyboard()) {
121 keyboard_data_ = data[KeyboardChannelIndex(stream_config)];
andrew@webrtc.org103657b2014-04-24 18:28:56122 }
123
andrew@webrtc.orgddbb8a22014-04-22 21:00:04124 // Downmix.
125 const float* const* data_ptr = data;
Michael Graczyk86c6d332015-07-23 18:41:39126 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 21:39:39129 data_ptr = input_buffer_->fbuf_const()->channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04130 }
131
132 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15133 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-13 00:26:35134 for (size_t i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04135 input_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15136 input_num_frames_,
137 process_buffer_->channels()[i],
138 proc_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04139 }
140 data_ptr = process_buffer_->channels();
141 }
142
andrew@webrtc.org8328e7c2014-10-31 04:58:14143 // Convert to the S16 range.
Peter Kasting69558702016-01-13 00:26:35144 for (size_t i = 0; i < num_proc_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15145 FloatToFloatS16(data_ptr[i],
146 proc_num_frames_,
147 data_->fbuf()->channels()[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04148 }
149}
150
Michael Graczyk86c6d332015-07-23 18:41:39151void AudioBuffer::CopyTo(const StreamConfig& stream_config,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04152 float* const* data) {
kwiberg9e2be5f2016-09-14 12:23:22153 RTC_DCHECK_EQ(stream_config.num_frames(), output_num_frames_);
154 RTC_DCHECK(stream_config.num_channels() == num_channels_ ||
155 num_channels_ == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04156
andrew@webrtc.org8328e7c2014-10-31 04:58:14157 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04158 float* const* data_ptr = data;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15159 if (output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04160 // Convert to an intermediate buffer for subsequent resampling.
161 data_ptr = process_buffer_->channels();
162 }
Peter Kasting69558702016-01-13 00:26:35163 for (size_t i = 0; i < num_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15164 FloatS16ToFloat(data_->fbuf()->channels()[i],
165 proc_num_frames_,
andrew@webrtc.org8328e7c2014-10-31 04:58:14166 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04167 }
168
169 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15170 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-13 00:26:35171 for (size_t i = 0; i < num_channels_; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04172 output_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15173 proc_num_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04174 data[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15175 output_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04176 }
niklase@google.com470e71d2011-07-07 08:21:25177 }
aluebsb2328d12016-01-12 04:32:29178
179 // Upmix.
Peter Kasting69558702016-01-13 00:26:35180 for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
aluebsb2328d12016-01-12 04:32:29181 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
182 }
niklase@google.com470e71d2011-07-07 08:21:25183}
184
andrew@webrtc.orgddbb8a22014-04-22 21:00:04185void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56186 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39187 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13188 reference_copied_ = false;
189 activity_ = AudioFrame::kVadUnknown;
aluebs@webrtc.org27d106b2014-12-11 17:09:21190 num_channels_ = num_proc_channels_;
Alejandro Luebsa181c9a2016-06-30 22:33:37191 data_->set_num_channels(num_proc_channels_);
192 if (split_data_.get()) {
193 split_data_->set_num_channels(num_proc_channels_);
194 }
andrew@webrtc.org17e40642014-03-04 20:58:13195}
196
aluebs@webrtc.orga7384a12014-12-03 01:06:35197const int16_t* const* AudioBuffer::channels_const() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15198 return data_->ibuf_const()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10199}
200
201int16_t* const* AudioBuffer::channels() {
202 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15203 return data_->ibuf()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10204}
205
Peter Kasting69558702016-01-13 00:26:35206const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15207 return split_data_.get() ?
208 split_data_->ibuf_const()->bands(channel) :
209 data_->ibuf_const()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35210}
211
Peter Kasting69558702016-01-13 00:26:35212int16_t* const* AudioBuffer::split_bands(size_t channel) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57213 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15214 return split_data_.get() ?
215 split_data_->ibuf()->bands(channel) :
216 data_->ibuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35217}
218
219const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15220 if (split_data_.get()) {
221 return split_data_->ibuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35222 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15223 return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35224 }
225}
226
227int16_t* const* AudioBuffer::split_channels(Band band) {
228 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15229 if (split_data_.get()) {
230 return split_data_->ibuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35231 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15232 return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35233 }
234}
235
aluebs@webrtc.org3aca0b02015-02-26 21:52:20236ChannelBuffer<int16_t>* AudioBuffer::data() {
237 mixed_low_pass_valid_ = false;
238 return data_->ibuf();
239}
240
241const ChannelBuffer<int16_t>* AudioBuffer::data() const {
242 return data_->ibuf_const();
243}
244
245ChannelBuffer<int16_t>* AudioBuffer::split_data() {
246 mixed_low_pass_valid_ = false;
247 return split_data_.get() ? split_data_->ibuf() : data_->ibuf();
248}
249
250const ChannelBuffer<int16_t>* AudioBuffer::split_data() const {
251 return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const();
252}
253
aluebs@webrtc.orga7384a12014-12-03 01:06:35254const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15255 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08256}
257
258float* const* AudioBuffer::channels_f() {
259 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15260 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08261}
262
Peter Kasting69558702016-01-13 00:26:35263const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15264 return split_data_.get() ?
265 split_data_->fbuf_const()->bands(channel) :
266 data_->fbuf_const()->bands(channel);
niklase@google.com470e71d2011-07-07 08:21:25267}
268
Peter Kasting69558702016-01-13 00:26:35269float* const* AudioBuffer::split_bands_f(size_t channel) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57270 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15271 return split_data_.get() ?
272 split_data_->fbuf()->bands(channel) :
273 data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35274}
275
276const float* const* AudioBuffer::split_channels_const_f(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15277 if (split_data_.get()) {
278 return split_data_->fbuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35279 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15280 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35281 }
282}
283
284float* const* AudioBuffer::split_channels_f(Band band) {
aluebs@webrtc.org2561d522014-07-17 08:27:39285 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15286 if (split_data_.get()) {
287 return split_data_->fbuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35288 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15289 return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35290 }
aluebs@webrtc.org087da132014-11-17 23:01:23291}
292
aluebs@webrtc.org3aca0b02015-02-26 21:52:20293ChannelBuffer<float>* AudioBuffer::data_f() {
294 mixed_low_pass_valid_ = false;
295 return data_->fbuf();
296}
297
298const ChannelBuffer<float>* AudioBuffer::data_f() const {
299 return data_->fbuf_const();
300}
301
302ChannelBuffer<float>* AudioBuffer::split_data_f() {
303 mixed_low_pass_valid_ = false;
304 return split_data_.get() ? split_data_->fbuf() : data_->fbuf();
305}
306
307const ChannelBuffer<float>* AudioBuffer::split_data_f() const {
308 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const();
309}
310
aluebs@webrtc.org2561d522014-07-17 08:27:39311const int16_t* AudioBuffer::mixed_low_pass_data() {
aluebs@webrtc.org2561d522014-07-17 08:27:39312 if (num_proc_channels_ == 1) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57313 return split_bands_const(0)[kBand0To8kHz];
aluebs@webrtc.org2561d522014-07-17 08:27:39314 }
315
316 if (!mixed_low_pass_valid_) {
317 if (!mixed_low_pass_channels_.get()) {
318 mixed_low_pass_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15319 new ChannelBuffer<int16_t>(num_split_frames_, 1));
aluebs@webrtc.org2561d522014-07-17 08:27:39320 }
Michael Graczyk86c6d332015-07-23 18:41:39321
322 DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz),
323 num_split_frames_, num_channels_,
324 mixed_low_pass_channels_->channels()[0]);
aluebs@webrtc.org2561d522014-07-17 08:27:39325 mixed_low_pass_valid_ = true;
326 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15327 return mixed_low_pass_channels_->channels()[0];
niklase@google.com470e71d2011-07-07 08:21:25328}
329
andrew@webrtc.org65f93382014-04-30 16:44:13330const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25331 if (!reference_copied_) {
332 return NULL;
333 }
334
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15335 return low_pass_reference_channels_->channels()[channel];
niklase@google.com470e71d2011-07-07 08:21:25336}
337
andrew@webrtc.org103657b2014-04-24 18:28:56338const float* AudioBuffer::keyboard_data() const {
339 return keyboard_data_;
340}
341
andrew@webrtc.orged083d42011-09-19 15:28:51342void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
343 activity_ = activity;
344}
345
andrew@webrtc.org755b04a2011-11-15 16:57:56346AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51347 return activity_;
348}
349
Peter Kasting69558702016-01-13 00:26:35350size_t AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21351 return num_channels_;
352}
353
Peter Kasting69558702016-01-13 00:26:35354void AudioBuffer::set_num_channels(size_t num_channels) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21355 num_channels_ = num_channels;
Alejandro Luebsa181c9a2016-06-30 22:33:37356 data_->set_num_channels(num_channels);
357 if (split_data_.get()) {
358 split_data_->set_num_channels(num_channels);
359 }
niklase@google.com470e71d2011-07-07 08:21:25360}
361
Peter Kastingdce40cf2015-08-24 21:52:23362size_t AudioBuffer::num_frames() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15363 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25364}
365
Peter Kastingdce40cf2015-08-24 21:52:23366size_t AudioBuffer::num_frames_per_band() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15367 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25368}
369
Peter Kastingdce40cf2015-08-24 21:52:23370size_t AudioBuffer::num_keyboard_frames() const {
andrew@webrtc.org103657b2014-04-24 18:28:56371 // We don't resample the keyboard channel.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15372 return input_num_frames_;
andrew@webrtc.org103657b2014-04-24 18:28:56373}
374
Peter Kastingdce40cf2015-08-24 21:52:23375size_t AudioBuffer::num_bands() const {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57376 return num_bands_;
377}
378
Alejandro Luebs05c76052015-05-20 21:39:39379// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
andrew@webrtc.orged083d42011-09-19 15:28:51380void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
kwiberg9e2be5f2016-09-14 12:23:22381 RTC_DCHECK_EQ(frame->num_channels_, num_input_channels_);
382 RTC_DCHECK_EQ(frame->samples_per_channel_, input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04383 InitForNewData();
Alejandro Luebs05c76052015-05-20 21:39:39384 // Initialized lazily because there's a different condition in CopyFrom.
385 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
386 input_buffer_.reset(
387 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
388 }
andrew@webrtc.org63a50982012-05-02 23:56:37389 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25390
Alejandro Luebs05c76052015-05-20 21:39:39391 int16_t* const* deinterleaved;
392 if (input_num_frames_ == proc_num_frames_) {
393 deinterleaved = data_->ibuf()->channels();
394 } else {
395 deinterleaved = input_buffer_->ibuf()->channels();
396 }
yujo36b1a5f2017-06-12 19:45:32397 // TODO(yujo): handle muted frames more efficiently.
Michael Graczyk86c6d332015-07-23 18:41:39398 if (num_proc_channels_ == 1) {
399 // Downmix and deinterleave simultaneously.
yujo36b1a5f2017-06-12 19:45:32400 DownmixInterleavedToMono(frame->data(), input_num_frames_,
Michael Graczyk86c6d332015-07-23 18:41:39401 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23402 } else {
kwiberg9e2be5f2016-09-14 12:23:22403 RTC_DCHECK_EQ(num_proc_channels_, num_input_channels_);
yujo36b1a5f2017-06-12 19:45:32404 Deinterleave(frame->data(),
Alejandro Luebs05c76052015-05-20 21:39:39405 input_num_frames_,
406 num_proc_channels_,
407 deinterleaved);
408 }
409
410 // Resample.
411 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-13 00:26:35412 for (size_t i = 0; i < num_proc_channels_; ++i) {
Alejandro Luebs05c76052015-05-20 21:39:39413 input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i],
414 input_num_frames_,
415 data_->fbuf()->channels()[i],
416 proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25417 }
418 }
419}
420
kthelgasonc7daea82017-03-14 10:10:07421void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.org63a50982012-05-02 23:56:37422 frame->vad_activity_ = activity_;
andrew@webrtc.org755b04a2011-11-15 16:57:56423 if (!data_changed) {
424 return;
425 }
426
kwiberg9e2be5f2016-09-14 12:23:22427 RTC_DCHECK(frame->num_channels_ == num_channels_ || num_channels_ == 1);
428 RTC_DCHECK_EQ(frame->samples_per_channel_, output_num_frames_);
ekmeyerson60d9b332015-08-14 17:35:55429
430 // Resample if necessary.
431 IFChannelBuffer* data_ptr = data_.get();
432 if (proc_num_frames_ != output_num_frames_) {
Peter Kasting69558702016-01-13 00:26:35433 for (size_t i = 0; i < num_channels_; ++i) {
ekmeyerson60d9b332015-08-14 17:35:55434 output_resamplers_[i]->Resample(
435 data_->fbuf()->channels()[i], proc_num_frames_,
436 output_buffer_->fbuf()->channels()[i], output_num_frames_);
437 }
438 data_ptr = output_buffer_.get();
439 }
440
yujo36b1a5f2017-06-12 19:45:32441 // TODO(yujo): handle muted frames more efficiently.
ekmeyerson60d9b332015-08-14 17:35:55442 if (frame->num_channels_ == num_channels_) {
Alejandro Luebs40cbec52016-04-06 00:29:19443 Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_,
yujo36b1a5f2017-06-12 19:45:32444 frame->mutable_data());
ekmeyerson60d9b332015-08-14 17:35:55445 } else {
Alejandro Luebs40cbec52016-04-06 00:29:19446 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_,
yujo36b1a5f2017-06-12 19:45:32447 frame->num_channels_, frame->mutable_data());
ekmeyerson60d9b332015-08-14 17:35:55448 }
niklase@google.com470e71d2011-07-07 08:21:25449}
450
niklase@google.com470e71d2011-07-07 08:21:25451void AudioBuffer::CopyLowPassToReference() {
452 reference_copied_ = true;
aluebs@webrtc.org27d106b2014-12-11 17:09:21453 if (!low_pass_reference_channels_.get() ||
454 low_pass_reference_channels_->num_channels() != num_channels_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04455 low_pass_reference_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15456 new ChannelBuffer<int16_t>(num_split_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04457 num_proc_channels_));
458 }
Peter Kasting69558702016-01-13 00:26:35459 for (size_t i = 0; i < num_proc_channels_; i++) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15460 memcpy(low_pass_reference_channels_->channels()[i],
461 split_bands_const(i)[kBand0To8kHz],
462 low_pass_reference_channels_->num_frames_per_band() *
463 sizeof(split_bands_const(i)[kBand0To8kHz][0]));
niklase@google.com470e71d2011-07-07 08:21:25464 }
465}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04466
aluebs@webrtc.orgbe05c742014-11-14 22:18:10467void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15468 splitting_filter_->Analysis(data_.get(), split_data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10469}
470
471void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15472 splitting_filter_->Synthesis(split_data_.get(), data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10473}
474
niklase@google.com470e71d2011-07-07 08:21:25475} // namespace webrtc