blob: f9b9f631d2b813523ed4a9935ecbc3c0af2ab3bd [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
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000011#include "webrtc/modules/audio_processing/audio_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Alejandro Luebs05c76052015-05-20 14:39:39 -070013#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
kjellander@webrtc.org035e9122015-01-28 19:57:00 +000016#include "webrtc/common_audio/channel_buffer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000017#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000018
niklase@google.com470e71d2011-07-07 08:21:25 +000019namespace webrtc {
20namespace {
21
Peter Kastingdce40cf2015-08-24 14:52:23 -070022const size_t kSamplesPer16kHzChannel = 160;
23const size_t kSamplesPer32kHzChannel = 320;
24const size_t kSamplesPer48kHzChannel = 480;
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070025
Michael Graczyk86c6d332015-07-23 11:41:39 -070026int KeyboardChannelIndex(const StreamConfig& stream_config) {
27 if (!stream_config.has_keyboard()) {
28 assert(false);
pkasting25702cb2016-01-08 13:50:27 -080029 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +000030 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +000031
Michael Graczyk86c6d332015-07-23 11:41:39 -070032 return stream_config.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000033}
34
Peter Kastingdce40cf2015-08-24 14:52:23 -070035size_t NumBandsFromSamplesPerChannel(size_t num_frames) {
36 size_t num_bands = 1;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000037 if (num_frames == kSamplesPer32kHzChannel ||
38 num_frames == kSamplesPer48kHzChannel) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070039 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000040 }
41 return num_bands;
42}
43
niklase@google.com470e71d2011-07-07 08:21:25 +000044} // namespace
45
Peter Kastingdce40cf2015-08-24 14:52:23 -070046AudioBuffer::AudioBuffer(size_t input_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080047 size_t num_input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070048 size_t process_num_frames,
Peter Kasting69558702016-01-12 16:26:35 -080049 size_t num_process_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070050 size_t output_num_frames)
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000051 : input_num_frames_(input_num_frames),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000052 num_input_channels_(num_input_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000053 proc_num_frames_(process_num_frames),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000054 num_proc_channels_(num_process_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000055 output_num_frames_(output_num_frames),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +000056 num_channels_(num_process_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000057 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
Peter Kasting728d9032015-06-11 14:31:38 -070058 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
aluebs@webrtc.org2561d522014-07-17 08:27:39 +000059 mixed_low_pass_valid_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +000060 reference_copied_(false),
61 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:56 +000062 keyboard_data_(NULL),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000063 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) {
64 assert(input_num_frames_ > 0);
65 assert(proc_num_frames_ > 0);
66 assert(output_num_frames_ > 0);
Michael Graczyk86c6d332015-07-23 11:41:39 -070067 assert(num_input_channels_ > 0);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000068 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000070 if (input_num_frames_ != proc_num_frames_ ||
71 output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000072 // Create an intermediate buffer for resampling.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000073 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000074 num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000076 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080077 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070078 input_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
79 new PushSincResampler(input_num_frames_, proc_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000080 }
81 }
82
83 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -080084 for (size_t i = 0; i < num_proc_channels_; ++i) {
kwiberg4a206a92016-03-31 10:24:26 -070085 output_resamplers_.push_back(std::unique_ptr<PushSincResampler>(
86 new PushSincResampler(proc_num_frames_, output_num_frames_)));
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000088 }
89 }
90
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000091 if (num_bands_ > 1) {
92 split_data_.reset(new IFChannelBuffer(proc_num_frames_,
93 num_proc_channels_,
94 num_bands_));
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070095 splitting_filter_.reset(new SplittingFilter(num_proc_channels_,
96 num_bands_,
97 proc_num_frames_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000098 }
99}
100
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000101AudioBuffer::~AudioBuffer() {}
102
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000103void AudioBuffer::CopyFrom(const float* const* data,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700104 const StreamConfig& stream_config) {
105 assert(stream_config.num_frames() == input_num_frames_);
106 assert(stream_config.num_channels() == num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000107 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700108 // Initialized lazily because there's a different condition in
109 // DeinterleaveFrom.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700110 const bool need_to_downmix =
111 num_input_channels_ > 1 && num_proc_channels_ == 1;
112 if (need_to_downmix && !input_buffer_) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700113 input_buffer_.reset(
114 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
115 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116
Michael Graczyk86c6d332015-07-23 11:41:39 -0700117 if (stream_config.has_keyboard()) {
118 keyboard_data_ = data[KeyboardChannelIndex(stream_config)];
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000119 }
120
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121 // Downmix.
122 const float* const* data_ptr = data;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700123 if (need_to_downmix) {
124 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
125 input_buffer_->fbuf()->channels()[0]);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700126 data_ptr = input_buffer_->fbuf_const()->channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000127 }
128
129 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000130 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800131 for (size_t i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000132 input_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000133 input_num_frames_,
134 process_buffer_->channels()[i],
135 proc_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000136 }
137 data_ptr = process_buffer_->channels();
138 }
139
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000140 // Convert to the S16 range.
Peter Kasting69558702016-01-12 16:26:35 -0800141 for (size_t i = 0; i < num_proc_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000142 FloatToFloatS16(data_ptr[i],
143 proc_num_frames_,
144 data_->fbuf()->channels()[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000145 }
146}
147
Michael Graczyk86c6d332015-07-23 11:41:39 -0700148void AudioBuffer::CopyTo(const StreamConfig& stream_config,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000149 float* const* data) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700150 assert(stream_config.num_frames() == output_num_frames_);
aluebsb2328d12016-01-11 20:32:29 -0800151 assert(stream_config.num_channels() == num_channels_ || num_channels_ == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000152
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000153 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000154 float* const* data_ptr = data;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000155 if (output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000156 // Convert to an intermediate buffer for subsequent resampling.
157 data_ptr = process_buffer_->channels();
158 }
Peter Kasting69558702016-01-12 16:26:35 -0800159 for (size_t i = 0; i < num_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000160 FloatS16ToFloat(data_->fbuf()->channels()[i],
161 proc_num_frames_,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000162 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000163 }
164
165 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000166 if (output_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800167 for (size_t i = 0; i < num_channels_; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000168 output_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000169 proc_num_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000170 data[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000171 output_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000172 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000173 }
aluebsb2328d12016-01-11 20:32:29 -0800174
175 // Upmix.
Peter Kasting69558702016-01-12 16:26:35 -0800176 for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
aluebsb2328d12016-01-11 20:32:29 -0800177 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
178 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
180
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000181void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000182 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000183 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184 reference_copied_ = false;
185 activity_ = AudioFrame::kVadUnknown;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000186 num_channels_ = num_proc_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000187}
188
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000189const int16_t* const* AudioBuffer::channels_const() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000190 return data_->ibuf_const()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000191}
192
193int16_t* const* AudioBuffer::channels() {
194 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000195 return data_->ibuf()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000196}
197
Peter Kasting69558702016-01-12 16:26:35 -0800198const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000199 return split_data_.get() ?
200 split_data_->ibuf_const()->bands(channel) :
201 data_->ibuf_const()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000202}
203
Peter Kasting69558702016-01-12 16:26:35 -0800204int16_t* const* AudioBuffer::split_bands(size_t channel) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000205 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000206 return split_data_.get() ?
207 split_data_->ibuf()->bands(channel) :
208 data_->ibuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000209}
210
211const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000212 if (split_data_.get()) {
213 return split_data_->ibuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000214 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000215 return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000216 }
217}
218
219int16_t* const* AudioBuffer::split_channels(Band band) {
220 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000221 if (split_data_.get()) {
222 return split_data_->ibuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000223 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000224 return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000225 }
226}
227
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000228ChannelBuffer<int16_t>* AudioBuffer::data() {
229 mixed_low_pass_valid_ = false;
230 return data_->ibuf();
231}
232
233const ChannelBuffer<int16_t>* AudioBuffer::data() const {
234 return data_->ibuf_const();
235}
236
237ChannelBuffer<int16_t>* AudioBuffer::split_data() {
238 mixed_low_pass_valid_ = false;
239 return split_data_.get() ? split_data_->ibuf() : data_->ibuf();
240}
241
242const ChannelBuffer<int16_t>* AudioBuffer::split_data() const {
243 return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const();
244}
245
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000246const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000247 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000248}
249
250float* const* AudioBuffer::channels_f() {
251 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000252 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000253}
254
Peter Kasting69558702016-01-12 16:26:35 -0800255const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000256 return split_data_.get() ?
257 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;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000263 return split_data_.get() ?
264 split_data_->fbuf()->bands(channel) :
265 data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000266}
267
268const float* const* AudioBuffer::split_channels_const_f(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000269 if (split_data_.get()) {
270 return split_data_->fbuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000271 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000272 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000273 }
274}
275
276float* const* AudioBuffer::split_channels_f(Band band) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000277 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000278 if (split_data_.get()) {
279 return split_data_->fbuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000280 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000281 return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000282 }
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000283}
284
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000285ChannelBuffer<float>* AudioBuffer::data_f() {
286 mixed_low_pass_valid_ = false;
287 return data_->fbuf();
288}
289
290const ChannelBuffer<float>* AudioBuffer::data_f() const {
291 return data_->fbuf_const();
292}
293
294ChannelBuffer<float>* AudioBuffer::split_data_f() {
295 mixed_low_pass_valid_ = false;
296 return split_data_.get() ? split_data_->fbuf() : data_->fbuf();
297}
298
299const ChannelBuffer<float>* AudioBuffer::split_data_f() const {
300 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const();
301}
302
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000303const int16_t* AudioBuffer::mixed_low_pass_data() {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000304 if (num_proc_channels_ == 1) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000305 return split_bands_const(0)[kBand0To8kHz];
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000306 }
307
308 if (!mixed_low_pass_valid_) {
309 if (!mixed_low_pass_channels_.get()) {
310 mixed_low_pass_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000311 new ChannelBuffer<int16_t>(num_split_frames_, 1));
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000312 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700313
314 DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz),
315 num_split_frames_, num_channels_,
316 mixed_low_pass_channels_->channels()[0]);
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000317 mixed_low_pass_valid_ = true;
318 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000319 return mixed_low_pass_channels_->channels()[0];
niklase@google.com470e71d2011-07-07 08:21:25 +0000320}
321
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000322const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 if (!reference_copied_) {
324 return NULL;
325 }
326
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000327 return low_pass_reference_channels_->channels()[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000328}
329
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000330const float* AudioBuffer::keyboard_data() const {
331 return keyboard_data_;
332}
333
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000334void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
335 activity_ = activity;
336}
337
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000338AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000339 return activity_;
340}
341
Peter Kasting69558702016-01-12 16:26:35 -0800342size_t AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000343 return num_channels_;
344}
345
Peter Kasting69558702016-01-12 16:26:35 -0800346void AudioBuffer::set_num_channels(size_t num_channels) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000347 num_channels_ = num_channels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000348}
349
Peter Kastingdce40cf2015-08-24 14:52:23 -0700350size_t AudioBuffer::num_frames() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000351 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352}
353
Peter Kastingdce40cf2015-08-24 14:52:23 -0700354size_t AudioBuffer::num_frames_per_band() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000355 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000356}
357
Peter Kastingdce40cf2015-08-24 14:52:23 -0700358size_t AudioBuffer::num_keyboard_frames() const {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000359 // We don't resample the keyboard channel.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000360 return input_num_frames_;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000361}
362
Peter Kastingdce40cf2015-08-24 14:52:23 -0700363size_t AudioBuffer::num_bands() const {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000364 return num_bands_;
365}
366
Alejandro Luebs05c76052015-05-20 14:39:39 -0700367// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000368void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000369 assert(frame->num_channels_ == num_input_channels_);
Peter Kasting728d9032015-06-11 14:31:38 -0700370 assert(frame->samples_per_channel_ == input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000371 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700372 // Initialized lazily because there's a different condition in CopyFrom.
373 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
374 input_buffer_.reset(
375 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
376 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000377 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000378
Alejandro Luebs05c76052015-05-20 14:39:39 -0700379 int16_t* const* deinterleaved;
380 if (input_num_frames_ == proc_num_frames_) {
381 deinterleaved = data_->ibuf()->channels();
382 } else {
383 deinterleaved = input_buffer_->ibuf()->channels();
384 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 if (num_proc_channels_ == 1) {
386 // Downmix and deinterleave simultaneously.
387 DownmixInterleavedToMono(frame->data_, input_num_frames_,
388 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000389 } else {
390 assert(num_proc_channels_ == num_input_channels_);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700391 Deinterleave(frame->data_,
392 input_num_frames_,
393 num_proc_channels_,
394 deinterleaved);
395 }
396
397 // Resample.
398 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800399 for (size_t i = 0; i < num_proc_channels_; ++i) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700400 input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i],
401 input_num_frames_,
402 data_->fbuf()->channels()[i],
403 proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000404 }
405 }
406}
407
ekmeyerson60d9b332015-08-14 10:35:55 -0700408void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000409 frame->vad_activity_ = activity_;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000410 if (!data_changed) {
411 return;
412 }
413
ekmeyerson60d9b332015-08-14 10:35:55 -0700414 assert(frame->num_channels_ == num_channels_ || num_channels_ == 1);
415 assert(frame->samples_per_channel_ == output_num_frames_);
416
417 // Resample if necessary.
418 IFChannelBuffer* data_ptr = data_.get();
419 if (proc_num_frames_ != output_num_frames_) {
420 if (!output_buffer_) {
421 output_buffer_.reset(
422 new IFChannelBuffer(output_num_frames_, num_channels_));
423 }
Peter Kasting69558702016-01-12 16:26:35 -0800424 for (size_t i = 0; i < num_channels_; ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -0700425 output_resamplers_[i]->Resample(
426 data_->fbuf()->channels()[i], proc_num_frames_,
427 output_buffer_->fbuf()->channels()[i], output_num_frames_);
428 }
429 data_ptr = output_buffer_.get();
430 }
431
432 if (frame->num_channels_ == num_channels_) {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700433 Interleave(data_ptr->ibuf()->channels(), output_num_frames_, num_channels_,
ekmeyerson60d9b332015-08-14 10:35:55 -0700434 frame->data_);
435 } else {
Alejandro Luebs40cbec52016-04-05 17:29:19 -0700436 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], output_num_frames_,
ekmeyerson60d9b332015-08-14 10:35:55 -0700437 frame->num_channels_, frame->data_);
438 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
niklase@google.com470e71d2011-07-07 08:21:25 +0000441void AudioBuffer::CopyLowPassToReference() {
442 reference_copied_ = true;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000443 if (!low_pass_reference_channels_.get() ||
444 low_pass_reference_channels_->num_channels() != num_channels_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000445 low_pass_reference_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000446 new ChannelBuffer<int16_t>(num_split_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000447 num_proc_channels_));
448 }
Peter Kasting69558702016-01-12 16:26:35 -0800449 for (size_t i = 0; i < num_proc_channels_; i++) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000450 memcpy(low_pass_reference_channels_->channels()[i],
451 split_bands_const(i)[kBand0To8kHz],
452 low_pass_reference_channels_->num_frames_per_band() *
453 sizeof(split_bands_const(i)[kBand0To8kHz][0]));
niklase@google.com470e71d2011-07-07 08:21:25 +0000454 }
455}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000456
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000457void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000458 splitting_filter_->Analysis(data_.get(), split_data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000459}
460
461void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000462 splitting_filter_->Synthesis(split_data_.get(), data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000463}
464
niklase@google.com470e71d2011-07-07 08:21:25 +0000465} // namespace webrtc