blob: 33af43e6b4a70a6205f48b4bc3f8c94f64d83d2a [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
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000013#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000014#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +000015#include "webrtc/modules/audio_processing/channel_buffer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000016#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000017
niklase@google.com470e71d2011-07-07 08:21:25 +000018namespace webrtc {
19namespace {
20
andrew@webrtc.org103657b2014-04-24 18:28:56 +000021bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) {
22 switch (layout) {
23 case AudioProcessing::kMono:
24 case AudioProcessing::kStereo:
25 return false;
26 case AudioProcessing::kMonoAndKeyboard:
27 case AudioProcessing::kStereoAndKeyboard:
28 return true;
29 }
30 assert(false);
31 return false;
32}
33
34int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) {
35 switch (layout) {
36 case AudioProcessing::kMono:
37 case AudioProcessing::kStereo:
38 assert(false);
39 return -1;
40 case AudioProcessing::kMonoAndKeyboard:
41 return 1;
42 case AudioProcessing::kStereoAndKeyboard:
43 return 2;
44 }
45 assert(false);
46 return -1;
47}
48
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000049template <typename T>
50void StereoToMono(const T* left, const T* right, T* out,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000051 int samples_per_channel) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000052 for (int i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000053 out[i] = (left[i] + right[i]) / 2;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000054}
55
niklase@google.com470e71d2011-07-07 08:21:25 +000056} // namespace
57
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000058AudioBuffer::AudioBuffer(int input_samples_per_channel,
59 int num_input_channels,
60 int process_samples_per_channel,
61 int num_process_channels,
62 int output_samples_per_channel)
63 : input_samples_per_channel_(input_samples_per_channel),
64 num_input_channels_(num_input_channels),
65 proc_samples_per_channel_(process_samples_per_channel),
66 num_proc_channels_(num_process_channels),
67 output_samples_per_channel_(output_samples_per_channel),
68 samples_per_split_channel_(proc_samples_per_channel_),
aluebs@webrtc.org2561d522014-07-17 08:27:39 +000069 mixed_low_pass_valid_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +000070 reference_copied_(false),
71 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:56 +000072 keyboard_data_(NULL),
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000073 channels_(new IFChannelBuffer(proc_samples_per_channel_,
74 num_proc_channels_)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075 assert(input_samples_per_channel_ > 0);
76 assert(proc_samples_per_channel_ > 0);
77 assert(output_samples_per_channel_ > 0);
78 assert(num_input_channels_ > 0 && num_input_channels_ <= 2);
79 assert(num_proc_channels_ <= num_input_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
82 input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_,
83 num_proc_channels_));
84 }
85
86 if (input_samples_per_channel_ != proc_samples_per_channel_ ||
87 output_samples_per_channel_ != proc_samples_per_channel_) {
88 // Create an intermediate buffer for resampling.
89 process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_,
90 num_proc_channels_));
91 }
92
93 if (input_samples_per_channel_ != proc_samples_per_channel_) {
94 input_resamplers_.reserve(num_proc_channels_);
95 for (int i = 0; i < num_proc_channels_; ++i) {
96 input_resamplers_.push_back(
97 new PushSincResampler(input_samples_per_channel_,
98 proc_samples_per_channel_));
99 }
100 }
101
102 if (output_samples_per_channel_ != proc_samples_per_channel_) {
103 output_resamplers_.reserve(num_proc_channels_);
104 for (int i = 0; i < num_proc_channels_; ++i) {
105 output_resamplers_.push_back(
106 new PushSincResampler(proc_samples_per_channel_,
107 output_samples_per_channel_));
108 }
109 }
110
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000111 if (proc_samples_per_channel_ == kSamplesPer32kHzChannel ||
112 proc_samples_per_channel_ == kSamplesPer48kHzChannel) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 samples_per_split_channel_ = kSamplesPer16kHzChannel;
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000114 split_channels_.push_back(new IFChannelBuffer(samples_per_split_channel_,
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000115 num_proc_channels_));
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000116 split_channels_.push_back(new IFChannelBuffer(samples_per_split_channel_,
117 num_proc_channels_));
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000118 splitting_filter_.reset(new SplittingFilter(num_proc_channels_));
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000119 if (proc_samples_per_channel_ == kSamplesPer48kHzChannel) {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000120 split_channels_.push_back(new IFChannelBuffer(samples_per_split_channel_,
121 num_proc_channels_));
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000122 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123 }
124}
125
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000126AudioBuffer::~AudioBuffer() {}
127
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000128void AudioBuffer::CopyFrom(const float* const* data,
129 int samples_per_channel,
130 AudioProcessing::ChannelLayout layout) {
131 assert(samples_per_channel == input_samples_per_channel_);
132 assert(ChannelsFromLayout(layout) == num_input_channels_);
133 InitForNewData();
134
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000135 if (HasKeyboardChannel(layout)) {
136 keyboard_data_ = data[KeyboardChannelIndex(layout)];
137 }
138
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000139 // Downmix.
140 const float* const* data_ptr = data;
141 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
142 StereoToMono(data[0],
143 data[1],
144 input_buffer_->channel(0),
145 input_samples_per_channel_);
146 data_ptr = input_buffer_->channels();
147 }
148
149 // Resample.
150 if (input_samples_per_channel_ != proc_samples_per_channel_) {
151 for (int i = 0; i < num_proc_channels_; ++i) {
152 input_resamplers_[i]->Resample(data_ptr[i],
153 input_samples_per_channel_,
154 process_buffer_->channel(i),
155 proc_samples_per_channel_);
156 }
157 data_ptr = process_buffer_->channels();
158 }
159
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000160 // Convert to the S16 range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000161 for (int i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000162 FloatToFloatS16(data_ptr[i], proc_samples_per_channel_,
163 channels_->fbuf()->channel(i));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000164 }
165}
166
167void AudioBuffer::CopyTo(int samples_per_channel,
168 AudioProcessing::ChannelLayout layout,
169 float* const* data) {
170 assert(samples_per_channel == output_samples_per_channel_);
171 assert(ChannelsFromLayout(layout) == num_proc_channels_);
172
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000173 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000174 float* const* data_ptr = data;
175 if (output_samples_per_channel_ != proc_samples_per_channel_) {
176 // Convert to an intermediate buffer for subsequent resampling.
177 data_ptr = process_buffer_->channels();
178 }
179 for (int i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000180 FloatS16ToFloat(channels_->fbuf()->channel(i), proc_samples_per_channel_,
181 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000182 }
183
184 // Resample.
185 if (output_samples_per_channel_ != proc_samples_per_channel_) {
186 for (int i = 0; i < num_proc_channels_; ++i) {
187 output_resamplers_[i]->Resample(data_ptr[i],
188 proc_samples_per_channel_,
189 data[i],
190 output_samples_per_channel_);
191 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000192 }
193}
194
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000195void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000196 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000197 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000198 reference_copied_ = false;
199 activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000200}
201
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000202const int16_t* AudioBuffer::data(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000203 return channels_->ibuf_const()->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204}
205
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000206int16_t* AudioBuffer::data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000207 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000208 return channels_->ibuf()->channel(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000209}
210
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000211const int16_t* const* AudioBuffer::channels() const {
212 return channels_->ibuf_const()->channels();
213}
214
215int16_t* const* AudioBuffer::channels() {
216 mixed_low_pass_valid_ = false;
217 return channels_->ibuf()->channels();
218}
219
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000220const float* AudioBuffer::data_f(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000221 return channels_->fbuf_const()->channel(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000222}
223
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000224float* AudioBuffer::data_f(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000225 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000226 return channels_->fbuf()->channel(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000227}
228
claguna@google.combfacaab2014-09-25 20:52:08 +0000229const float* const* AudioBuffer::channels_f() const {
230 return channels_->fbuf_const()->channels();
231}
232
233float* const* AudioBuffer::channels_f() {
234 mixed_low_pass_valid_ = false;
235 return channels_->fbuf()->channels();
236}
237
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000238const int16_t* AudioBuffer::low_pass_split_data(int channel) const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000239 return split_channels_.size() > 0
240 ? split_channels_[0]->ibuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000241 : data(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242}
243
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000244int16_t* AudioBuffer::low_pass_split_data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000245 mixed_low_pass_valid_ = false;
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000246 return split_channels_.size() > 0
247 ? split_channels_[0]->ibuf()->channel(channel)
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000248 : data(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000249}
250
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000251const int16_t* const* AudioBuffer::low_pass_split_channels() const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000252 return split_channels_.size() > 0
253 ? split_channels_[0]->ibuf_const()->channels()
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000254 : channels();
255}
256
257int16_t* const* AudioBuffer::low_pass_split_channels() {
258 mixed_low_pass_valid_ = false;
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000259 return split_channels_.size() > 0 ? split_channels_[0]->ibuf()->channels()
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000260 : channels();
261}
262
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000263const float* AudioBuffer::low_pass_split_data_f(int channel) const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000264 return split_channels_.size() > 0
265 ? split_channels_[0]->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000266 : data_f(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000267}
268
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000269float* AudioBuffer::low_pass_split_data_f(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000270 mixed_low_pass_valid_ = false;
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000271 return split_channels_.size() > 0
272 ? split_channels_[0]->fbuf()->channel(channel)
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000273 : data_f(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000274}
275
claguna@google.combfacaab2014-09-25 20:52:08 +0000276const float* const* AudioBuffer::low_pass_split_channels_f() const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000277 return split_channels_.size() > 0
278 ? split_channels_[0]->fbuf_const()->channels()
claguna@google.combfacaab2014-09-25 20:52:08 +0000279 : channels_f();
280}
281
282float* const* AudioBuffer::low_pass_split_channels_f() {
283 mixed_low_pass_valid_ = false;
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000284 return split_channels_.size() > 0
285 ? split_channels_[0]->fbuf()->channels()
claguna@google.combfacaab2014-09-25 20:52:08 +0000286 : channels_f();
287}
288
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000289const int16_t* AudioBuffer::high_pass_split_data(int channel) const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000290 return split_channels_.size() > 1
291 ? split_channels_[1]->ibuf_const()->channel(channel)
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000292 : NULL;
293}
294
295int16_t* AudioBuffer::high_pass_split_data(int channel) {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000296 return split_channels_.size() > 1
297 ? split_channels_[1]->ibuf()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000298 : NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299}
300
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000301const int16_t* const* AudioBuffer::high_pass_split_channels() const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000302 return split_channels_.size() > 1
303 ? split_channels_[1]->ibuf_const()->channels()
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000304 : NULL;
305}
306
307int16_t* const* AudioBuffer::high_pass_split_channels() {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000308 return split_channels_.size() > 1 ? split_channels_[1]->ibuf()->channels()
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000309 : NULL;
310}
311
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000312const float* AudioBuffer::high_pass_split_data_f(int channel) const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000313 return split_channels_.size() > 1
314 ? split_channels_[1]->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000315 : NULL;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000316}
317
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000318float* AudioBuffer::high_pass_split_data_f(int channel) {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000319 return split_channels_.size() > 1
320 ? split_channels_[1]->fbuf()->channel(channel)
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000321 : NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000322}
323
claguna@google.combfacaab2014-09-25 20:52:08 +0000324const float* const* AudioBuffer::high_pass_split_channels_f() const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000325 return split_channels_.size() > 1
326 ? split_channels_[1]->fbuf_const()->channels()
claguna@google.combfacaab2014-09-25 20:52:08 +0000327 : NULL;
328}
329
330float* const* AudioBuffer::high_pass_split_channels_f() {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000331 return split_channels_.size() > 1
332 ? split_channels_[1]->fbuf()->channels()
claguna@google.combfacaab2014-09-25 20:52:08 +0000333 : NULL;
334}
335
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000336const float* const* AudioBuffer::super_high_pass_split_channels_f() const {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000337 return split_channels_.size() > 2
338 ? split_channels_[2]->fbuf_const()->channels()
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000339 : NULL;
340}
341
342float* const* AudioBuffer::super_high_pass_split_channels_f() {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000343 return split_channels_.size() > 2
344 ? split_channels_[2]->fbuf()->channels()
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000345 : NULL;
346}
347
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000348const int16_t* AudioBuffer::mixed_low_pass_data() {
349 // Currently only mixing stereo to mono is supported.
350 assert(num_proc_channels_ == 1 || num_proc_channels_ == 2);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000351
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000352 if (num_proc_channels_ == 1) {
353 return low_pass_split_data(0);
354 }
355
356 if (!mixed_low_pass_valid_) {
357 if (!mixed_low_pass_channels_.get()) {
358 mixed_low_pass_channels_.reset(
359 new ChannelBuffer<int16_t>(samples_per_split_channel_, 1));
360 }
361 StereoToMono(low_pass_split_data(0),
362 low_pass_split_data(1),
363 mixed_low_pass_channels_->data(),
364 samples_per_split_channel_);
365 mixed_low_pass_valid_ = true;
366 }
367 return mixed_low_pass_channels_->data();
niklase@google.com470e71d2011-07-07 08:21:25 +0000368}
369
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000370const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000371 if (!reference_copied_) {
372 return NULL;
373 }
374
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000375 return low_pass_reference_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000376}
377
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000378const float* AudioBuffer::keyboard_data() const {
379 return keyboard_data_;
380}
381
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000382void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
383 activity_ = activity;
384}
385
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000386AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000387 return activity_;
388}
389
390int AudioBuffer::num_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000391 return num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000392}
393
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000394int AudioBuffer::samples_per_channel() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000395 return proc_samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000398int AudioBuffer::samples_per_split_channel() const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000399 return samples_per_split_channel_;
400}
401
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000402int AudioBuffer::samples_per_keyboard_channel() const {
403 // We don't resample the keyboard channel.
404 return input_samples_per_channel_;
405}
406
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000407// TODO(andrew): Do deinterleaving and mixing in one step?
408void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409 assert(proc_samples_per_channel_ == input_samples_per_channel_);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000410 assert(frame->num_channels_ == num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
412 InitForNewData();
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000413 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000415 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
416 // Downmix directly; no explicit deinterleaving needed.
417 int16_t* downmixed = channels_->ibuf()->channel(0);
418 for (int i = 0; i < input_samples_per_channel_; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000419 downmixed[i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2;
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000420 }
421 } else {
422 assert(num_proc_channels_ == num_input_channels_);
423 int16_t* interleaved = frame->data_;
424 for (int i = 0; i < num_proc_channels_; ++i) {
425 int16_t* deinterleaved = channels_->ibuf()->channel(i);
426 int interleaved_idx = i;
427 for (int j = 0; j < proc_samples_per_channel_; ++j) {
428 deinterleaved[j] = interleaved[interleaved_idx];
429 interleaved_idx += num_proc_channels_;
430 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000431 }
432 }
433}
434
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000435void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000436 assert(proc_samples_per_channel_ == output_samples_per_channel_);
437 assert(num_proc_channels_ == num_input_channels_);
438 assert(frame->num_channels_ == num_proc_channels_);
439 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000440 frame->vad_activity_ = activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000442 if (!data_changed) {
443 return;
444 }
445
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000446 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000447 for (int i = 0; i < num_proc_channels_; i++) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000448 int16_t* deinterleaved = channels_->ibuf()->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000449 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000450 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000451 interleaved[interleaved_idx] = deinterleaved[j];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000452 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000453 }
454 }
455}
456
niklase@google.com470e71d2011-07-07 08:21:25 +0000457void AudioBuffer::CopyLowPassToReference() {
458 reference_copied_ = true;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000459 if (!low_pass_reference_channels_.get()) {
460 low_pass_reference_channels_.reset(
461 new ChannelBuffer<int16_t>(samples_per_split_channel_,
462 num_proc_channels_));
463 }
464 for (int i = 0; i < num_proc_channels_; i++) {
465 low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i);
niklase@google.com470e71d2011-07-07 08:21:25 +0000466 }
467}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000468
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000469void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000470 splitting_filter_->Analysis(channels_.get(),
471 split_channels_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000472}
473
474void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.org79b9eba2014-11-26 20:21:38 +0000475 splitting_filter_->Synthesis(split_channels_.get(),
476 channels_.get());
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000477}
478
niklase@google.com470e71d2011-07-07 08:21:25 +0000479} // namespace webrtc