blob: c1c4061f4884c727866fd185ec394974a4ed7bb8 [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,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000047 int num_input_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070048 size_t process_num_frames,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000049 int 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_) {
77 for (int i = 0; i < num_proc_channels_; ++i) {
78 input_resamplers_.push_back(
79 new PushSincResampler(input_num_frames_,
80 proc_num_frames_));
81 }
82 }
83
84 if (output_num_frames_ != proc_num_frames_) {
85 for (int i = 0; i < num_proc_channels_; ++i) {
86 output_resamplers_.push_back(
87 new PushSincResampler(proc_num_frames_,
88 output_num_frames_));
89 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000090 }
91 }
92
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000093 if (num_bands_ > 1) {
94 split_data_.reset(new IFChannelBuffer(proc_num_frames_,
95 num_proc_channels_,
96 num_bands_));
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070097 splitting_filter_.reset(new SplittingFilter(num_proc_channels_,
98 num_bands_,
99 proc_num_frames_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000100 }
101}
102
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000103AudioBuffer::~AudioBuffer() {}
104
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000105void AudioBuffer::CopyFrom(const float* const* data,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700106 const StreamConfig& stream_config) {
107 assert(stream_config.num_frames() == input_num_frames_);
108 assert(stream_config.num_channels() == num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000109 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700110 // Initialized lazily because there's a different condition in
111 // DeinterleaveFrom.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700112 const bool need_to_downmix =
113 num_input_channels_ > 1 && num_proc_channels_ == 1;
114 if (need_to_downmix && !input_buffer_) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700115 input_buffer_.reset(
116 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
117 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000118
Michael Graczyk86c6d332015-07-23 11:41:39 -0700119 if (stream_config.has_keyboard()) {
120 keyboard_data_ = data[KeyboardChannelIndex(stream_config)];
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000121 }
122
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123 // Downmix.
124 const float* const* data_ptr = data;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700125 if (need_to_downmix) {
126 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
127 input_buffer_->fbuf()->channels()[0]);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700128 data_ptr = input_buffer_->fbuf_const()->channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000129 }
130
131 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000132 if (input_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000133 for (int i = 0; i < num_proc_channels_; ++i) {
134 input_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000135 input_num_frames_,
136 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.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000143 for (int i = 0; i < num_proc_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000144 FloatToFloatS16(data_ptr[i],
145 proc_num_frames_,
146 data_->fbuf()->channels()[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000147 }
148}
149
Michael Graczyk86c6d332015-07-23 11:41:39 -0700150void AudioBuffer::CopyTo(const StreamConfig& stream_config,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000151 float* const* data) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700152 assert(stream_config.num_frames() == output_num_frames_);
153 assert(stream_config.num_channels() == num_channels_);
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 }
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000161 for (int i = 0; i < num_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000162 FloatS16ToFloat(data_->fbuf()->channels()[i],
163 proc_num_frames_,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000164 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000165 }
166
167 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000168 if (output_num_frames_ != proc_num_frames_) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000169 for (int i = 0; i < num_channels_; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000170 output_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000171 proc_num_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000172 data[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000173 output_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000174 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000175 }
176}
177
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000178void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000179 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000180 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000181 reference_copied_ = false;
182 activity_ = AudioFrame::kVadUnknown;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000183 num_channels_ = num_proc_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184}
185
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000186const int16_t* const* AudioBuffer::channels_const() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000187 return data_->ibuf_const()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000188}
189
190int16_t* const* AudioBuffer::channels() {
191 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000192 return data_->ibuf()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000193}
194
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000195const int16_t* const* AudioBuffer::split_bands_const(int channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000196 return split_data_.get() ?
197 split_data_->ibuf_const()->bands(channel) :
198 data_->ibuf_const()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000199}
200
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000201int16_t* const* AudioBuffer::split_bands(int channel) {
202 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000203 return split_data_.get() ?
204 split_data_->ibuf()->bands(channel) :
205 data_->ibuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000206}
207
208const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000209 if (split_data_.get()) {
210 return split_data_->ibuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000211 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000212 return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000213 }
214}
215
216int16_t* const* AudioBuffer::split_channels(Band band) {
217 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000218 if (split_data_.get()) {
219 return split_data_->ibuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000220 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000221 return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000222 }
223}
224
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000225ChannelBuffer<int16_t>* AudioBuffer::data() {
226 mixed_low_pass_valid_ = false;
227 return data_->ibuf();
228}
229
230const ChannelBuffer<int16_t>* AudioBuffer::data() const {
231 return data_->ibuf_const();
232}
233
234ChannelBuffer<int16_t>* AudioBuffer::split_data() {
235 mixed_low_pass_valid_ = false;
236 return split_data_.get() ? split_data_->ibuf() : data_->ibuf();
237}
238
239const ChannelBuffer<int16_t>* AudioBuffer::split_data() const {
240 return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const();
241}
242
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000243const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000244 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000245}
246
247float* const* AudioBuffer::channels_f() {
248 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000249 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000250}
251
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000252const float* const* AudioBuffer::split_bands_const_f(int channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000253 return split_data_.get() ?
254 split_data_->fbuf_const()->bands(channel) :
255 data_->fbuf_const()->bands(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000256}
257
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000258float* const* AudioBuffer::split_bands_f(int channel) {
259 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000260 return split_data_.get() ?
261 split_data_->fbuf()->bands(channel) :
262 data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000263}
264
265const float* const* AudioBuffer::split_channels_const_f(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000266 if (split_data_.get()) {
267 return split_data_->fbuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000268 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000269 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000270 }
271}
272
273float* const* AudioBuffer::split_channels_f(Band band) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000274 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000275 if (split_data_.get()) {
276 return split_data_->fbuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000277 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000278 return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000279 }
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000280}
281
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000282ChannelBuffer<float>* AudioBuffer::data_f() {
283 mixed_low_pass_valid_ = false;
284 return data_->fbuf();
285}
286
287const ChannelBuffer<float>* AudioBuffer::data_f() const {
288 return data_->fbuf_const();
289}
290
291ChannelBuffer<float>* AudioBuffer::split_data_f() {
292 mixed_low_pass_valid_ = false;
293 return split_data_.get() ? split_data_->fbuf() : data_->fbuf();
294}
295
296const ChannelBuffer<float>* AudioBuffer::split_data_f() const {
297 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const();
298}
299
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000300const int16_t* AudioBuffer::mixed_low_pass_data() {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000301 if (num_proc_channels_ == 1) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000302 return split_bands_const(0)[kBand0To8kHz];
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000303 }
304
305 if (!mixed_low_pass_valid_) {
306 if (!mixed_low_pass_channels_.get()) {
307 mixed_low_pass_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000308 new ChannelBuffer<int16_t>(num_split_frames_, 1));
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000309 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700310
311 DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz),
312 num_split_frames_, num_channels_,
313 mixed_low_pass_channels_->channels()[0]);
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000314 mixed_low_pass_valid_ = true;
315 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000316 return mixed_low_pass_channels_->channels()[0];
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000319const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000320 if (!reference_copied_) {
321 return NULL;
322 }
323
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000324 return low_pass_reference_channels_->channels()[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000327const float* AudioBuffer::keyboard_data() const {
328 return keyboard_data_;
329}
330
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000331void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
332 activity_ = activity;
333}
334
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000335AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000336 return activity_;
337}
338
339int AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000340 return num_channels_;
341}
342
343void AudioBuffer::set_num_channels(int num_channels) {
344 num_channels_ = num_channels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000345}
346
Peter Kastingdce40cf2015-08-24 14:52:23 -0700347size_t AudioBuffer::num_frames() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000348 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000349}
350
Peter Kastingdce40cf2015-08-24 14:52:23 -0700351size_t AudioBuffer::num_frames_per_band() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000352 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000353}
354
Peter Kastingdce40cf2015-08-24 14:52:23 -0700355size_t AudioBuffer::num_keyboard_frames() const {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000356 // We don't resample the keyboard channel.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000357 return input_num_frames_;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000358}
359
Peter Kastingdce40cf2015-08-24 14:52:23 -0700360size_t AudioBuffer::num_bands() const {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000361 return num_bands_;
362}
363
Alejandro Luebs05c76052015-05-20 14:39:39 -0700364// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000365void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000366 assert(frame->num_channels_ == num_input_channels_);
Peter Kasting728d9032015-06-11 14:31:38 -0700367 assert(frame->samples_per_channel_ == input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000368 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700369 // Initialized lazily because there's a different condition in CopyFrom.
370 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
371 input_buffer_.reset(
372 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
373 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000374 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
Alejandro Luebs05c76052015-05-20 14:39:39 -0700376 int16_t* const* deinterleaved;
377 if (input_num_frames_ == proc_num_frames_) {
378 deinterleaved = data_->ibuf()->channels();
379 } else {
380 deinterleaved = input_buffer_->ibuf()->channels();
381 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700382 if (num_proc_channels_ == 1) {
383 // Downmix and deinterleave simultaneously.
384 DownmixInterleavedToMono(frame->data_, input_num_frames_,
385 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000386 } else {
387 assert(num_proc_channels_ == num_input_channels_);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700388 Deinterleave(frame->data_,
389 input_num_frames_,
390 num_proc_channels_,
391 deinterleaved);
392 }
393
394 // Resample.
395 if (input_num_frames_ != proc_num_frames_) {
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000396 for (int i = 0; i < num_proc_channels_; ++i) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700397 input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i],
398 input_num_frames_,
399 data_->fbuf()->channels()[i],
400 proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000401 }
402 }
403}
404
ekmeyerson60d9b332015-08-14 10:35:55 -0700405void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000406 frame->vad_activity_ = activity_;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000407 if (!data_changed) {
408 return;
409 }
410
ekmeyerson60d9b332015-08-14 10:35:55 -0700411 assert(frame->num_channels_ == num_channels_ || num_channels_ == 1);
412 assert(frame->samples_per_channel_ == output_num_frames_);
413
414 // Resample if necessary.
415 IFChannelBuffer* data_ptr = data_.get();
416 if (proc_num_frames_ != output_num_frames_) {
417 if (!output_buffer_) {
418 output_buffer_.reset(
419 new IFChannelBuffer(output_num_frames_, num_channels_));
420 }
421 for (int i = 0; i < num_channels_; ++i) {
422 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
429 if (frame->num_channels_ == num_channels_) {
430 Interleave(data_ptr->ibuf()->channels(), proc_num_frames_, num_channels_,
431 frame->data_);
432 } else {
433 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], proc_num_frames_,
434 frame->num_channels_, frame->data_);
435 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000436}
437
niklase@google.com470e71d2011-07-07 08:21:25 +0000438void AudioBuffer::CopyLowPassToReference() {
439 reference_copied_ = true;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000440 if (!low_pass_reference_channels_.get() ||
441 low_pass_reference_channels_->num_channels() != num_channels_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000442 low_pass_reference_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000443 new ChannelBuffer<int16_t>(num_split_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000444 num_proc_channels_));
445 }
446 for (int 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