blob: ff64267e8ca1fb9c140cfd5897ae6915098ea33b [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) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000078 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_) {
Peter Kasting69558702016-01-12 16:26:35 -080085 for (size_t i = 0; i < num_proc_channels_; ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000086 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_) {
Peter Kasting69558702016-01-12 16:26:35 -0800133 for (size_t i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000134 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.
Peter Kasting69558702016-01-12 16:26:35 -0800143 for (size_t 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_);
aluebsb2328d12016-01-11 20:32:29 -0800153 assert(stream_config.num_channels() == num_channels_ || num_channels_ == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000154
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000155 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000156 float* const* data_ptr = data;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000157 if (output_num_frames_ != proc_num_frames_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000158 // Convert to an intermediate buffer for subsequent resampling.
159 data_ptr = process_buffer_->channels();
160 }
Peter Kasting69558702016-01-12 16:26:35 -0800161 for (size_t i = 0; i < num_channels_; ++i) {
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_) {
Peter Kasting69558702016-01-12 16:26:35 -0800169 for (size_t 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 }
aluebsb2328d12016-01-11 20:32:29 -0800176
177 // Upmix.
Peter Kasting69558702016-01-12 16:26:35 -0800178 for (size_t i = num_channels_; i < stream_config.num_channels(); ++i) {
aluebsb2328d12016-01-11 20:32:29 -0800179 memcpy(data[i], data[0], output_num_frames_ * sizeof(**data));
180 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000183void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000184 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000185 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186 reference_copied_ = false;
187 activity_ = AudioFrame::kVadUnknown;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000188 num_channels_ = num_proc_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000189}
190
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000191const int16_t* const* AudioBuffer::channels_const() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000192 return data_->ibuf_const()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000193}
194
195int16_t* const* AudioBuffer::channels() {
196 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000197 return data_->ibuf()->channels();
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000198}
199
Peter Kasting69558702016-01-12 16:26:35 -0800200const int16_t* const* AudioBuffer::split_bands_const(size_t channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000201 return split_data_.get() ?
202 split_data_->ibuf_const()->bands(channel) :
203 data_->ibuf_const()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000204}
205
Peter Kasting69558702016-01-12 16:26:35 -0800206int16_t* const* AudioBuffer::split_bands(size_t channel) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000207 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000208 return split_data_.get() ?
209 split_data_->ibuf()->bands(channel) :
210 data_->ibuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000211}
212
213const int16_t* const* AudioBuffer::split_channels_const(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000214 if (split_data_.get()) {
215 return split_data_->ibuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000216 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000217 return band == kBand0To8kHz ? data_->ibuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000218 }
219}
220
221int16_t* const* AudioBuffer::split_channels(Band band) {
222 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000223 if (split_data_.get()) {
224 return split_data_->ibuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000225 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000226 return band == kBand0To8kHz ? data_->ibuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000227 }
228}
229
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000230ChannelBuffer<int16_t>* AudioBuffer::data() {
231 mixed_low_pass_valid_ = false;
232 return data_->ibuf();
233}
234
235const ChannelBuffer<int16_t>* AudioBuffer::data() const {
236 return data_->ibuf_const();
237}
238
239ChannelBuffer<int16_t>* AudioBuffer::split_data() {
240 mixed_low_pass_valid_ = false;
241 return split_data_.get() ? split_data_->ibuf() : data_->ibuf();
242}
243
244const ChannelBuffer<int16_t>* AudioBuffer::split_data() const {
245 return split_data_.get() ? split_data_->ibuf_const() : data_->ibuf_const();
246}
247
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000248const float* const* AudioBuffer::channels_const_f() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000249 return data_->fbuf_const()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000250}
251
252float* const* AudioBuffer::channels_f() {
253 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000254 return data_->fbuf()->channels();
claguna@google.combfacaab2014-09-25 20:52:08 +0000255}
256
Peter Kasting69558702016-01-12 16:26:35 -0800257const float* const* AudioBuffer::split_bands_const_f(size_t channel) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000258 return split_data_.get() ?
259 split_data_->fbuf_const()->bands(channel) :
260 data_->fbuf_const()->bands(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000261}
262
Peter Kasting69558702016-01-12 16:26:35 -0800263float* const* AudioBuffer::split_bands_f(size_t channel) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000264 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000265 return split_data_.get() ?
266 split_data_->fbuf()->bands(channel) :
267 data_->fbuf()->bands(channel);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000268}
269
270const float* const* AudioBuffer::split_channels_const_f(Band band) const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000271 if (split_data_.get()) {
272 return split_data_->fbuf_const()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000273 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000274 return band == kBand0To8kHz ? data_->fbuf_const()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000275 }
276}
277
278float* const* AudioBuffer::split_channels_f(Band band) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000279 mixed_low_pass_valid_ = false;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000280 if (split_data_.get()) {
281 return split_data_->fbuf()->channels(band);
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000282 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000283 return band == kBand0To8kHz ? data_->fbuf()->channels() : nullptr;
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +0000284 }
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000285}
286
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +0000287ChannelBuffer<float>* AudioBuffer::data_f() {
288 mixed_low_pass_valid_ = false;
289 return data_->fbuf();
290}
291
292const ChannelBuffer<float>* AudioBuffer::data_f() const {
293 return data_->fbuf_const();
294}
295
296ChannelBuffer<float>* AudioBuffer::split_data_f() {
297 mixed_low_pass_valid_ = false;
298 return split_data_.get() ? split_data_->fbuf() : data_->fbuf();
299}
300
301const ChannelBuffer<float>* AudioBuffer::split_data_f() const {
302 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const();
303}
304
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000305const int16_t* AudioBuffer::mixed_low_pass_data() {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000306 if (num_proc_channels_ == 1) {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000307 return split_bands_const(0)[kBand0To8kHz];
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000308 }
309
310 if (!mixed_low_pass_valid_) {
311 if (!mixed_low_pass_channels_.get()) {
312 mixed_low_pass_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000313 new ChannelBuffer<int16_t>(num_split_frames_, 1));
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000314 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700315
316 DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz),
317 num_split_frames_, num_channels_,
318 mixed_low_pass_channels_->channels()[0]);
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000319 mixed_low_pass_valid_ = true;
320 }
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000321 return mixed_low_pass_channels_->channels()[0];
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000324const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000325 if (!reference_copied_) {
326 return NULL;
327 }
328
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000329 return low_pass_reference_channels_->channels()[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000330}
331
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000332const float* AudioBuffer::keyboard_data() const {
333 return keyboard_data_;
334}
335
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000336void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
337 activity_ = activity;
338}
339
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000340AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000341 return activity_;
342}
343
Peter Kasting69558702016-01-12 16:26:35 -0800344size_t AudioBuffer::num_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000345 return num_channels_;
346}
347
Peter Kasting69558702016-01-12 16:26:35 -0800348void AudioBuffer::set_num_channels(size_t num_channels) {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000349 num_channels_ = num_channels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
Peter Kastingdce40cf2015-08-24 14:52:23 -0700352size_t AudioBuffer::num_frames() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000353 return proc_num_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
Peter Kastingdce40cf2015-08-24 14:52:23 -0700356size_t AudioBuffer::num_frames_per_band() const {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000357 return num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000358}
359
Peter Kastingdce40cf2015-08-24 14:52:23 -0700360size_t AudioBuffer::num_keyboard_frames() const {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000361 // We don't resample the keyboard channel.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000362 return input_num_frames_;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000363}
364
Peter Kastingdce40cf2015-08-24 14:52:23 -0700365size_t AudioBuffer::num_bands() const {
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +0000366 return num_bands_;
367}
368
Alejandro Luebs05c76052015-05-20 14:39:39 -0700369// The resampler is only for supporting 48kHz to 16kHz in the reverse stream.
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000370void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000371 assert(frame->num_channels_ == num_input_channels_);
Peter Kasting728d9032015-06-11 14:31:38 -0700372 assert(frame->samples_per_channel_ == input_num_frames_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000373 InitForNewData();
Alejandro Luebs05c76052015-05-20 14:39:39 -0700374 // Initialized lazily because there's a different condition in CopyFrom.
375 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) {
376 input_buffer_.reset(
377 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
378 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000379 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000380
Alejandro Luebs05c76052015-05-20 14:39:39 -0700381 int16_t* const* deinterleaved;
382 if (input_num_frames_ == proc_num_frames_) {
383 deinterleaved = data_->ibuf()->channels();
384 } else {
385 deinterleaved = input_buffer_->ibuf()->channels();
386 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700387 if (num_proc_channels_ == 1) {
388 // Downmix and deinterleave simultaneously.
389 DownmixInterleavedToMono(frame->data_, input_num_frames_,
390 num_input_channels_, deinterleaved[0]);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000391 } else {
392 assert(num_proc_channels_ == num_input_channels_);
Alejandro Luebs05c76052015-05-20 14:39:39 -0700393 Deinterleave(frame->data_,
394 input_num_frames_,
395 num_proc_channels_,
396 deinterleaved);
397 }
398
399 // Resample.
400 if (input_num_frames_ != proc_num_frames_) {
Peter Kasting69558702016-01-12 16:26:35 -0800401 for (size_t i = 0; i < num_proc_channels_; ++i) {
Alejandro Luebs05c76052015-05-20 14:39:39 -0700402 input_resamplers_[i]->Resample(input_buffer_->fbuf_const()->channels()[i],
403 input_num_frames_,
404 data_->fbuf()->channels()[i],
405 proc_num_frames_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000406 }
407 }
408}
409
ekmeyerson60d9b332015-08-14 10:35:55 -0700410void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000411 frame->vad_activity_ = activity_;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000412 if (!data_changed) {
413 return;
414 }
415
ekmeyerson60d9b332015-08-14 10:35:55 -0700416 assert(frame->num_channels_ == num_channels_ || num_channels_ == 1);
417 assert(frame->samples_per_channel_ == output_num_frames_);
418
419 // Resample if necessary.
420 IFChannelBuffer* data_ptr = data_.get();
421 if (proc_num_frames_ != output_num_frames_) {
422 if (!output_buffer_) {
423 output_buffer_.reset(
424 new IFChannelBuffer(output_num_frames_, num_channels_));
425 }
Peter Kasting69558702016-01-12 16:26:35 -0800426 for (size_t i = 0; i < num_channels_; ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -0700427 output_resamplers_[i]->Resample(
428 data_->fbuf()->channels()[i], proc_num_frames_,
429 output_buffer_->fbuf()->channels()[i], output_num_frames_);
430 }
431 data_ptr = output_buffer_.get();
432 }
433
434 if (frame->num_channels_ == num_channels_) {
435 Interleave(data_ptr->ibuf()->channels(), proc_num_frames_, num_channels_,
436 frame->data_);
437 } else {
438 UpmixMonoToInterleaved(data_ptr->ibuf()->channels()[0], proc_num_frames_,
439 frame->num_channels_, frame->data_);
440 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
niklase@google.com470e71d2011-07-07 08:21:25 +0000443void AudioBuffer::CopyLowPassToReference() {
444 reference_copied_ = true;
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000445 if (!low_pass_reference_channels_.get() ||
446 low_pass_reference_channels_->num_channels() != num_channels_) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000447 low_pass_reference_channels_.reset(
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000448 new ChannelBuffer<int16_t>(num_split_frames_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000449 num_proc_channels_));
450 }
Peter Kasting69558702016-01-12 16:26:35 -0800451 for (size_t i = 0; i < num_proc_channels_; i++) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000452 memcpy(low_pass_reference_channels_->channels()[i],
453 split_bands_const(i)[kBand0To8kHz],
454 low_pass_reference_channels_->num_frames_per_band() *
455 sizeof(split_bands_const(i)[kBand0To8kHz][0]));
niklase@google.com470e71d2011-07-07 08:21:25 +0000456 }
457}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000458
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000459void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000460 splitting_filter_->Analysis(data_.get(), split_data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000461}
462
463void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000464 splitting_filter_->Synthesis(split_data_.get(), data_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000465}
466
niklase@google.com470e71d2011-07-07 08:21:25 +0000467} // namespace webrtc