blob: 6f732624930fb5d04e8ed860dd442d71a9fc91a3 [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 Kasting728d9032015-06-11 14:31:38 -070022const int kSamplesPer16kHzChannel = 160;
23const int kSamplesPer32kHzChannel = 320;
24const int 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);
29 return -1;
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
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000035int NumBandsFromSamplesPerChannel(int num_frames) {
36 int num_bands = 1;
37 if (num_frames == kSamplesPer32kHzChannel ||
38 num_frames == kSamplesPer48kHzChannel) {
39 num_bands = rtc::CheckedDivExact(num_frames,
40 static_cast<int>(kSamplesPer16kHzChannel));
41 }
42 return num_bands;
43}
44
niklase@google.com470e71d2011-07-07 08:21:25 +000045} // namespace
46
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000047AudioBuffer::AudioBuffer(int input_num_frames,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000048 int num_input_channels,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000049 int process_num_frames,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000050 int num_process_channels,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000051 int output_num_frames)
52 : input_num_frames_(input_num_frames),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000053 num_input_channels_(num_input_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000054 proc_num_frames_(process_num_frames),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000055 num_proc_channels_(num_process_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000056 output_num_frames_(output_num_frames),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +000057 num_channels_(num_process_channels),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000058 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
Peter Kasting728d9032015-06-11 14:31:38 -070059 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
aluebs@webrtc.org2561d522014-07-17 08:27:39 +000060 mixed_low_pass_valid_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +000061 reference_copied_(false),
62 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:56 +000063 keyboard_data_(NULL),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000064 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) {
65 assert(input_num_frames_ > 0);
66 assert(proc_num_frames_ > 0);
67 assert(output_num_frames_ > 0);
Michael Graczyk86c6d332015-07-23 11:41:39 -070068 assert(num_input_channels_ > 0);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000069 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +000070
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000071 if (input_num_frames_ != proc_num_frames_ ||
72 output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000073 // Create an intermediate buffer for resampling.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000074 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075 num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000076
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000077 if (input_num_frames_ != proc_num_frames_) {
78 for (int i = 0; i < num_proc_channels_; ++i) {
79 input_resamplers_.push_back(
80 new PushSincResampler(input_num_frames_,
81 proc_num_frames_));
82 }
83 }
84
85 if (output_num_frames_ != proc_num_frames_) {
86 for (int i = 0; i < num_proc_channels_; ++i) {
87 output_resamplers_.push_back(
88 new PushSincResampler(proc_num_frames_,
89 output_num_frames_));
90 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000091 }
92 }
93
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000094 if (num_bands_ > 1) {
95 split_data_.reset(new IFChannelBuffer(proc_num_frames_,
96 num_proc_channels_,
97 num_bands_));
Alejandro Luebs5a92aa82015-04-27 11:34:45 -070098 splitting_filter_.reset(new SplittingFilter(num_proc_channels_,
99 num_bands_,
100 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) {
108 assert(stream_config.num_frames() == input_num_frames_);
109 assert(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_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000134 for (int i = 0; i < num_proc_channels_; ++i) {
135 input_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000136 input_num_frames_,
137 process_buffer_->channels()[i],
138 proc_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000139 }
140 data_ptr = process_buffer_->channels();
141 }
142
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000143 // Convert to the S16 range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000144 for (int i = 0; i < num_proc_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000145 FloatToFloatS16(data_ptr[i],
146 proc_num_frames_,
147 data_->fbuf()->channels()[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000148 }
149}
150
Michael Graczyk86c6d332015-07-23 11:41:39 -0700151void AudioBuffer::CopyTo(const StreamConfig& stream_config,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000152 float* const* data) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700153 assert(stream_config.num_frames() == output_num_frames_);
154 assert(stream_config.num_channels() == num_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000155
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000156 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000157 float* const* data_ptr = data;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000158 if (output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000159 // Convert to an intermediate buffer for subsequent resampling.
160 data_ptr = process_buffer_->channels();
161 }
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000162 for (int i = 0; i < num_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000163 FloatS16ToFloat(data_->fbuf()->channels()[i],
164 proc_num_frames_,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000165 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000166 }
167
168 // Resample.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000169 if (output_num_frames_ != proc_num_frames_) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000170 for (int i = 0; i < num_channels_; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000171 output_resamplers_[i]->Resample(data_ptr[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000172 proc_num_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000173 data[i],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000174 output_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000175 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 }
177}
178
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000179void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000180 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000181 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000182 reference_copied_ = false;
183 activity_ = AudioFrame::kVadUnknown;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000184 num_channels_ = num_proc_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000185}
186
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000187const int16_t* const* AudioBuffer::channels_const() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000188 return data_->ibuf_const()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000189}
190
191int16_t* const* AudioBuffer::channels() {
192 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000193 return data_->ibuf()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000194}
195
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000196const int16_t* const* AudioBuffer::split_bands_const(int channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000197 return split_data_.get() ?
198 split_data_->ibuf_const()->bands(channel) :
199 data_->ibuf_const()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000200}
201
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000202int16_t* const* AudioBuffer::split_bands(int channel) {
203 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000204 return split_data_.get() ?
205 split_data_->ibuf()->bands(channel) :
206 data_->ibuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000207}
208
209const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000210 if (split_data_.get()) {
211 return split_data_->ibuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000212 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000213 return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000214 }
215}
216
217int16_t* const* AudioBuffer::split_channels(Band band) {
218 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000219 if (split_data_.get()) {
220 return split_data_->ibuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000221 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000222 return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000223 }
224}
225
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000226ChannelBuffer<int16_t>* AudioBuffer::data() {
227 mixed_low_pass_valid_ = false;
228 return data_->ibuf();
229}
230
231const ChannelBuffer<int16_t>* AudioBuffer::data() const {
232 return data_->ibuf_const();
233}
234
235ChannelBuffer<int16_t>* AudioBuffer::split_data() {
236 mixed_low_pass_valid_ = false;
237 return split_data_.get() ? split_data_->ibuf() : data_->ibuf();
238}
239
240const ChannelBuffer<int16_t>* AudioBuffer::split_data() const {
241 return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const();
242}
243
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000244const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000245 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000246}
247
248float* const* AudioBuffer::channels_f() {
249 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000250 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000251}
252
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000253const float* const* AudioBuffer::split_bands_const_f(int channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000254 return split_data_.get() ?
255 split_data_->fbuf_const()->bands(channel) :
256 data_->fbuf_const()->bands(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000257}
258
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000259float* const* AudioBuffer::split_bands_f(int channel) {
260 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000261 return split_data_.get() ?
262 split_data_->fbuf()->bands(channel) :
263 data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000264}
265
266const float* const* AudioBuffer::split_channels_const_f(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000267 if (split_data_.get()) {
268 return split_data_->fbuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000269 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000270 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000271 }
272}
273
274float* const* AudioBuffer::split_channels_f(Band band) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000275 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000276 if (split_data_.get()) {
277 return split_data_->fbuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000278 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000279 return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000280 }
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000281}
282
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000283ChannelBuffer<float>* AudioBuffer::data_f() {
284 mixed_low_pass_valid_ = false;
285 return data_->fbuf();
286}
287
288const ChannelBuffer<float>* AudioBuffer::data_f() const {
289 return data_->fbuf_const();
290}
291
292ChannelBuffer<float>* AudioBuffer::split_data_f() {
293 mixed_low_pass_valid_ = false;
294 return split_data_.get() ? split_data_->fbuf() : data_->fbuf();
295}
296
297const ChannelBuffer<float>* AudioBuffer::split_data_f() const {
298 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const();
299}
300
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000301const int16_t* AudioBuffer::mixed_low_pass_data() {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000302 if (num_proc_channels_ == 1) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000303 return split_bands_const(0)[kBand0To8kHz];
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000304 }
305
306 if (!mixed_low_pass_valid_) {
307 if (!mixed_low_pass_channels_.get()) {
308 mixed_low_pass_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000309 new ChannelBuffer<int16_t>(num_split_frames_, 1));
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000310 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700311
312 DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz),
313 num_split_frames_, num_channels_,
314 mixed_low_pass_channels_->channels()[0]);
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000315 mixed_low_pass_valid_ = true;
316 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000317 return mixed_low_pass_channels_->channels()[0];
niklase@google.com470e71d2011-07-07 08:21:25 +0000318}
319
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000320const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000321 if (!reference_copied_) {
322 return NULL;
323 }
324
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000325 return low_pass_reference_channels_->channels()[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000326}
327
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000328const float* AudioBuffer::keyboard_data() const {
329 return keyboard_data_;
330}
331
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000332void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
333 activity_ = activity;
334}
335
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000336AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000337 return activity_;
338}
339
340int AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000341 return num_channels_;
342}
343
344void AudioBuffer::set_num_channels(int num_channels) {
345 num_channels_ = num_channels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000346}
347
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000348int AudioBuffer::num_frames() const {
349 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000352int AudioBuffer::num_frames_per_band() const {
353 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000356int AudioBuffer::num_keyboard_frames() const {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000357 // We don't resample the keyboard channel.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000358 return input_num_frames_;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000359}
360
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000361int AudioBuffer::num_bands() const {
362 return num_bands_;
363}
364
Alejandro Luebs05c76052015-05-20 14:39:39 -0700365// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000366void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000367 assert(frame->num_channels_ == num_input_channels_);
Peter Kasting728d9032015-06-11 14:31:38 -0700368 assert(frame->samples_per_channel_ == input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000369 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700370 // Initialized lazily because there's a different condition in CopyFrom.
371 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
372 input_buffer_.reset(
373 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
374 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000375 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
Alejandro Luebs05c76052015-05-20 14:39:39 -0700377 int16_t* const* deinterleaved;
378 if (input_num_frames_ == proc_num_frames_) {
379 deinterleaved = data_->ibuf()->channels();
380 } else {
381 deinterleaved = input_buffer_->ibuf()->channels();
382 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700383 if (num_proc_channels_ == 1) {
384 // Downmix and deinterleave simultaneously.
385 DownmixInterleavedToMono(frame->data_, input_num_frames_,
386 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000387 } else {
388 assert(num_proc_channels_ == num_input_channels_);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700389 Deinterleave(frame->data_,
390 input_num_frames_,
391 num_proc_channels_,
392 deinterleaved);
393 }
394
395 // Resample.
396 if (input_num_frames_ != proc_num_frames_) {
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000397 for (int i = 0; i < num_proc_channels_; ++i) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700398 input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i],
399 input_num_frames_,
400 data_->fbuf()->channels()[i],
401 proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000402 }
403 }
404}
405
ekmeyerson60d9b332015-08-14 10:35:55 -0700406void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000407 frame->vad_activity_ = activity_;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000408 if (!data_changed) {
409 return;
410 }
411
ekmeyerson60d9b332015-08-14 10:35:55 -0700412 assert(frame->num_channels_ == num_channels_ || num_channels_ == 1);
413 assert(frame->samples_per_channel_ == output_num_frames_);
414
415 // Resample if necessary.
416 IFChannelBuffer* data_ptr = data_.get();
417 if (proc_num_frames_ != output_num_frames_) {
418 if (!output_buffer_) {
419 output_buffer_.reset(
420 new IFChannelBuffer(output_num_frames_, num_channels_));
421 }
422 for (int i = 0; i < num_channels_; ++i) {
423 output_resamplers_[i]->Resample(
424 data_->fbuf()->channels()[i], proc_num_frames_,
425 output_buffer_->fbuf()->channels()[i], output_num_frames_);
426 }
427 data_ptr = output_buffer_.get();
428 }
429
430 if (frame->num_channels_ == num_channels_) {
431 Interleave(data_ptr->ibuf()->channels(), proc_num_frames_, num_channels_,
432 frame->data_);
433 } else {
434 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], proc_num_frames_,
435 frame->num_channels_, frame->data_);
436 }
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(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000444 new ChannelBuffer<int16_t>(num_split_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000445 num_proc_channels_));
446 }
447 for (int i = 0; i < num_proc_channels_; i++) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000448 memcpy(low_pass_reference_channels_->channels()[i],
449 split_bands_const(i)[kBand0To8kHz],
450 low_pass_reference_channels_->num_frames_per_band() *
451 sizeof(split_bands_const(i)[kBand0To8kHz][0]));
niklase@google.com470e71d2011-07-07 08:21:25 +0000452 }
453}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000454
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000455void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000456 splitting_filter_->Analysis(data_.get(), split_data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000457}
458
459void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000460 splitting_filter_->Synthesis(split_data_.get(), data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000461}
462
niklase@google.com470e71d2011-07-07 08:21:25 +0000463} // namespace webrtc