blob: cd8d93aa705fb478513c8bba39d222ac355c1826 [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.org17e40642014-03-04 20:58:13 +000013#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"
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000016
niklase@google.com470e71d2011-07-07 08:21:25 +000017namespace webrtc {
18namespace {
19
20enum {
21 kSamplesPer8kHzChannel = 80,
22 kSamplesPer16kHzChannel = 160,
23 kSamplesPer32kHzChannel = 320
24};
25
andrew@webrtc.org103657b2014-04-24 18:28:56 +000026bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) {
27 switch (layout) {
28 case AudioProcessing::kMono:
29 case AudioProcessing::kStereo:
30 return false;
31 case AudioProcessing::kMonoAndKeyboard:
32 case AudioProcessing::kStereoAndKeyboard:
33 return true;
34 }
35 assert(false);
36 return false;
37}
38
39int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) {
40 switch (layout) {
41 case AudioProcessing::kMono:
42 case AudioProcessing::kStereo:
43 assert(false);
44 return -1;
45 case AudioProcessing::kMonoAndKeyboard:
46 return 1;
47 case AudioProcessing::kStereoAndKeyboard:
48 return 2;
49 }
50 assert(false);
51 return -1;
52}
53
kwiberg@webrtc.orgbcfb4d02014-10-30 11:16:06 +000054void StereoToMono(const float* left, const float* right, float* out,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000055 int samples_per_channel) {
kwiberg@webrtc.orgbcfb4d02014-10-30 11:16:06 +000056 for (int i = 0; i < samples_per_channel; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000057 out[i] = (left[i] + right[i]) / 2;
kwiberg@webrtc.orgbcfb4d02014-10-30 11:16:06 +000058 }
59}
60
61void StereoToMono(const int16_t* left, const int16_t* right, int16_t* out,
62 int samples_per_channel) {
63 for (int i = 0; i < samples_per_channel; ++i) {
64 out[i] = (left[i] + right[i]) >> 1;
65 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000066}
67
niklase@google.com470e71d2011-07-07 08:21:25 +000068} // namespace
69
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000070// One int16_t and one float ChannelBuffer that are kept in sync. The sync is
71// broken when someone requests write access to either ChannelBuffer, and
72// reestablished when someone requests the outdated ChannelBuffer. It is
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000073// therefore safe to use the return value of ibuf_const() and fbuf_const()
74// until the next call to ibuf() or fbuf(), and the return value of ibuf() and
75// fbuf() until the next call to any of the other functions.
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000076class IFChannelBuffer {
77 public:
78 IFChannelBuffer(int samples_per_channel, int num_channels)
79 : ivalid_(true),
80 ibuf_(samples_per_channel, num_channels),
81 fvalid_(true),
82 fbuf_(samples_per_channel, num_channels) {}
83
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000084 ChannelBuffer<int16_t>* ibuf() { return ibuf(false); }
85 ChannelBuffer<float>* fbuf() { return fbuf(false); }
86 const ChannelBuffer<int16_t>* ibuf_const() { return ibuf(true); }
87 const ChannelBuffer<float>* fbuf_const() { return fbuf(true); }
88
89 private:
90 ChannelBuffer<int16_t>* ibuf(bool readonly) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000091 RefreshI();
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000092 fvalid_ = readonly;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000093 return &ibuf_;
94 }
95
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000096 ChannelBuffer<float>* fbuf(bool readonly) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000097 RefreshF();
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000098 ivalid_ = readonly;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000099 return &fbuf_;
100 }
101
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000102 void RefreshF() {
103 if (!fvalid_) {
104 assert(ivalid_);
105 const int16_t* const int_data = ibuf_.data();
106 float* const float_data = fbuf_.data();
107 const int length = fbuf_.length();
108 for (int i = 0; i < length; ++i)
109 float_data[i] = int_data[i];
110 fvalid_ = true;
111 }
112 }
113
114 void RefreshI() {
115 if (!ivalid_) {
116 assert(fvalid_);
kwiberg@webrtc.orgbcfb4d02014-10-30 11:16:06 +0000117 const float* const float_data = fbuf_.data();
118 int16_t* const int_data = ibuf_.data();
119 const int length = ibuf_.length();
120 for (int i = 0; i < length; ++i)
121 int_data[i] = WEBRTC_SPL_SAT(std::numeric_limits<int16_t>::max(),
122 float_data[i],
123 std::numeric_limits<int16_t>::min());
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000124 ivalid_ = true;
125 }
126 }
127
128 bool ivalid_;
129 ChannelBuffer<int16_t> ibuf_;
130 bool fvalid_;
131 ChannelBuffer<float> fbuf_;
132};
133
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000134AudioBuffer::AudioBuffer(int input_samples_per_channel,
135 int num_input_channels,
136 int process_samples_per_channel,
137 int num_process_channels,
138 int output_samples_per_channel)
139 : input_samples_per_channel_(input_samples_per_channel),
140 num_input_channels_(num_input_channels),
141 proc_samples_per_channel_(process_samples_per_channel),
142 num_proc_channels_(num_process_channels),
143 output_samples_per_channel_(output_samples_per_channel),
144 samples_per_split_channel_(proc_samples_per_channel_),
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000145 mixed_low_pass_valid_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000146 reference_copied_(false),
147 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000148 keyboard_data_(NULL),
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000149 channels_(new IFChannelBuffer(proc_samples_per_channel_,
150 num_proc_channels_)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000151 assert(input_samples_per_channel_ > 0);
152 assert(proc_samples_per_channel_ > 0);
153 assert(output_samples_per_channel_ > 0);
154 assert(num_input_channels_ > 0 && num_input_channels_ <= 2);
155 assert(num_proc_channels_ <= num_input_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000157 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
158 input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_,
159 num_proc_channels_));
160 }
161
162 if (input_samples_per_channel_ != proc_samples_per_channel_ ||
163 output_samples_per_channel_ != proc_samples_per_channel_) {
164 // Create an intermediate buffer for resampling.
165 process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_,
166 num_proc_channels_));
167 }
168
169 if (input_samples_per_channel_ != proc_samples_per_channel_) {
170 input_resamplers_.reserve(num_proc_channels_);
171 for (int i = 0; i < num_proc_channels_; ++i) {
172 input_resamplers_.push_back(
173 new PushSincResampler(input_samples_per_channel_,
174 proc_samples_per_channel_));
175 }
176 }
177
178 if (output_samples_per_channel_ != proc_samples_per_channel_) {
179 output_resamplers_.reserve(num_proc_channels_);
180 for (int i = 0; i < num_proc_channels_; ++i) {
181 output_resamplers_.push_back(
182 new PushSincResampler(proc_samples_per_channel_,
183 output_samples_per_channel_));
184 }
185 }
186
187 if (proc_samples_per_channel_ == kSamplesPer32kHzChannel) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 samples_per_split_channel_ = kSamplesPer16kHzChannel;
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000189 split_channels_low_.reset(new IFChannelBuffer(samples_per_split_channel_,
190 num_proc_channels_));
191 split_channels_high_.reset(new IFChannelBuffer(samples_per_split_channel_,
192 num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000193 filter_states_.reset(new SplitFilterStates[num_proc_channels_]);
194 }
195}
196
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000197AudioBuffer::~AudioBuffer() {}
198
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000199void AudioBuffer::CopyFrom(const float* const* data,
200 int samples_per_channel,
201 AudioProcessing::ChannelLayout layout) {
202 assert(samples_per_channel == input_samples_per_channel_);
203 assert(ChannelsFromLayout(layout) == num_input_channels_);
204 InitForNewData();
205
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000206 if (HasKeyboardChannel(layout)) {
207 keyboard_data_ = data[KeyboardChannelIndex(layout)];
208 }
209
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000210 // Downmix.
211 const float* const* data_ptr = data;
212 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
213 StereoToMono(data[0],
214 data[1],
215 input_buffer_->channel(0),
216 input_samples_per_channel_);
217 data_ptr = input_buffer_->channels();
218 }
219
220 // Resample.
221 if (input_samples_per_channel_ != proc_samples_per_channel_) {
222 for (int i = 0; i < num_proc_channels_; ++i) {
223 input_resamplers_[i]->Resample(data_ptr[i],
224 input_samples_per_channel_,
225 process_buffer_->channel(i),
226 proc_samples_per_channel_);
227 }
228 data_ptr = process_buffer_->channels();
229 }
230
231 // Convert to int16.
232 for (int i = 0; i < num_proc_channels_; ++i) {
kwiberg@webrtc.orgbcfb4d02014-10-30 11:16:06 +0000233 FloatToS16(data_ptr[i], proc_samples_per_channel_,
234 channels_->ibuf()->channel(i));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000235 }
236}
237
238void AudioBuffer::CopyTo(int samples_per_channel,
239 AudioProcessing::ChannelLayout layout,
240 float* const* data) {
241 assert(samples_per_channel == output_samples_per_channel_);
242 assert(ChannelsFromLayout(layout) == num_proc_channels_);
243
244 // Convert to float.
245 float* const* data_ptr = data;
246 if (output_samples_per_channel_ != proc_samples_per_channel_) {
247 // Convert to an intermediate buffer for subsequent resampling.
248 data_ptr = process_buffer_->channels();
249 }
250 for (int i = 0; i < num_proc_channels_; ++i) {
kwiberg@webrtc.orgbcfb4d02014-10-30 11:16:06 +0000251 S16ToFloat(channels_->ibuf()->channel(i),
252 proc_samples_per_channel_,
253 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000254 }
255
256 // Resample.
257 if (output_samples_per_channel_ != proc_samples_per_channel_) {
258 for (int i = 0; i < num_proc_channels_; ++i) {
259 output_resamplers_[i]->Resample(data_ptr[i],
260 proc_samples_per_channel_,
261 data[i],
262 output_samples_per_channel_);
263 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 }
265}
266
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000267void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000268 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000269 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000270 reference_copied_ = false;
271 activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000272}
273
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000274const int16_t* AudioBuffer::data(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000275 return channels_->ibuf_const()->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276}
277
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000278int16_t* AudioBuffer::data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000279 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000280 return channels_->ibuf()->channel(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000281}
282
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000283const float* AudioBuffer::data_f(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000284 return channels_->fbuf_const()->channel(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000285}
286
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000287float* AudioBuffer::data_f(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000288 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000289 return channels_->fbuf()->channel(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000290}
291
claguna@google.combfacaab2014-09-25 20:52:08 +0000292const float* const* AudioBuffer::channels_f() const {
293 return channels_->fbuf_const()->channels();
294}
295
296float* const* AudioBuffer::channels_f() {
297 mixed_low_pass_valid_ = false;
298 return channels_->fbuf()->channels();
299}
300
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000301const int16_t* AudioBuffer::low_pass_split_data(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000302 return split_channels_low_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000303 ? split_channels_low_->ibuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000304 : data(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000307int16_t* AudioBuffer::low_pass_split_data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000308 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000309 return split_channels_low_.get()
310 ? split_channels_low_->ibuf()->channel(channel)
311 : data(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000312}
313
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000314const float* AudioBuffer::low_pass_split_data_f(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000315 return split_channels_low_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000316 ? split_channels_low_->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000317 : data_f(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000318}
319
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000320float* AudioBuffer::low_pass_split_data_f(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000321 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000322 return split_channels_low_.get()
323 ? split_channels_low_->fbuf()->channel(channel)
324 : data_f(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000325}
326
claguna@google.combfacaab2014-09-25 20:52:08 +0000327const float* const* AudioBuffer::low_pass_split_channels_f() const {
328 return split_channels_low_.get()
329 ? split_channels_low_->fbuf_const()->channels()
330 : channels_f();
331}
332
333float* const* AudioBuffer::low_pass_split_channels_f() {
334 mixed_low_pass_valid_ = false;
335 return split_channels_low_.get()
336 ? split_channels_low_->fbuf()->channels()
337 : channels_f();
338}
339
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000340const int16_t* AudioBuffer::high_pass_split_data(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000341 return split_channels_high_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000342 ? split_channels_high_->ibuf_const()->channel(channel)
343 : NULL;
344}
345
346int16_t* AudioBuffer::high_pass_split_data(int channel) {
347 return split_channels_high_.get()
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000348 ? split_channels_high_->ibuf()->channel(channel)
349 : NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000352const float* AudioBuffer::high_pass_split_data_f(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000353 return split_channels_high_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000354 ? split_channels_high_->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000355 : NULL;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000356}
357
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000358float* AudioBuffer::high_pass_split_data_f(int channel) {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000359 return split_channels_high_.get()
360 ? split_channels_high_->fbuf()->channel(channel)
361 : NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000362}
363
claguna@google.combfacaab2014-09-25 20:52:08 +0000364const float* const* AudioBuffer::high_pass_split_channels_f() const {
365 return split_channels_high_.get()
366 ? split_channels_high_->fbuf_const()->channels()
367 : NULL;
368}
369
370float* const* AudioBuffer::high_pass_split_channels_f() {
371 return split_channels_high_.get()
372 ? split_channels_high_->fbuf()->channels()
373 : NULL;
374}
375
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000376const int16_t* AudioBuffer::mixed_low_pass_data() {
377 // Currently only mixing stereo to mono is supported.
378 assert(num_proc_channels_ == 1 || num_proc_channels_ == 2);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000379
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000380 if (num_proc_channels_ == 1) {
381 return low_pass_split_data(0);
382 }
383
384 if (!mixed_low_pass_valid_) {
385 if (!mixed_low_pass_channels_.get()) {
386 mixed_low_pass_channels_.reset(
387 new ChannelBuffer<int16_t>(samples_per_split_channel_, 1));
388 }
389 StereoToMono(low_pass_split_data(0),
390 low_pass_split_data(1),
391 mixed_low_pass_channels_->data(),
392 samples_per_split_channel_);
393 mixed_low_pass_valid_ = true;
394 }
395 return mixed_low_pass_channels_->data();
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000398const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000399 if (!reference_copied_) {
400 return NULL;
401 }
402
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000403 return low_pass_reference_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000404}
405
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000406const float* AudioBuffer::keyboard_data() const {
407 return keyboard_data_;
408}
409
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000410SplitFilterStates* AudioBuffer::filter_states(int channel) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 assert(channel >= 0 && channel < num_proc_channels_);
412 return &filter_states_[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000413}
414
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000415void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
416 activity_ = activity;
417}
418
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000419AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000420 return activity_;
421}
422
423int AudioBuffer::num_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000424 return num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000425}
426
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000427int AudioBuffer::samples_per_channel() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000428 return proc_samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000429}
430
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000431int AudioBuffer::samples_per_split_channel() const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 return samples_per_split_channel_;
433}
434
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000435int AudioBuffer::samples_per_keyboard_channel() const {
436 // We don't resample the keyboard channel.
437 return input_samples_per_channel_;
438}
439
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000440// TODO(andrew): Do deinterleaving and mixing in one step?
441void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000442 assert(proc_samples_per_channel_ == input_samples_per_channel_);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000443 assert(frame->num_channels_ == num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000444 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
445 InitForNewData();
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000446 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000448 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
449 // Downmix directly; no explicit deinterleaving needed.
450 int16_t* downmixed = channels_->ibuf()->channel(0);
451 for (int i = 0; i < input_samples_per_channel_; ++i) {
kwiberg@webrtc.orgbcfb4d02014-10-30 11:16:06 +0000452 // HACK(ajm): The downmixing in the int16_t path is in practice never
453 // called from production code. We do this weird scaling to and from float
454 // to satisfy tests checking for bit-exactness with the float path.
455 float downmix_float = (S16ToFloat(frame->data_[i * 2]) +
456 S16ToFloat(frame->data_[i * 2 + 1])) / 2;
457 downmixed[i] = FloatToS16(downmix_float);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000458 }
459 } else {
460 assert(num_proc_channels_ == num_input_channels_);
461 int16_t* interleaved = frame->data_;
462 for (int i = 0; i < num_proc_channels_; ++i) {
463 int16_t* deinterleaved = channels_->ibuf()->channel(i);
464 int interleaved_idx = i;
465 for (int j = 0; j < proc_samples_per_channel_; ++j) {
466 deinterleaved[j] = interleaved[interleaved_idx];
467 interleaved_idx += num_proc_channels_;
468 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000469 }
470 }
471}
472
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000473void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000474 assert(proc_samples_per_channel_ == output_samples_per_channel_);
475 assert(num_proc_channels_ == num_input_channels_);
476 assert(frame->num_channels_ == num_proc_channels_);
477 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000478 frame->vad_activity_ = activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000480 if (!data_changed) {
481 return;
482 }
483
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000484 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485 for (int i = 0; i < num_proc_channels_; i++) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000486 int16_t* deinterleaved = channels_->ibuf()->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000487 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000488 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000489 interleaved[interleaved_idx] = deinterleaved[j];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000490 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491 }
492 }
493}
494
niklase@google.com470e71d2011-07-07 08:21:25 +0000495void AudioBuffer::CopyLowPassToReference() {
496 reference_copied_ = true;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000497 if (!low_pass_reference_channels_.get()) {
498 low_pass_reference_channels_.reset(
499 new ChannelBuffer<int16_t>(samples_per_split_channel_,
500 num_proc_channels_));
501 }
502 for (int i = 0; i < num_proc_channels_; i++) {
503 low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i);
niklase@google.com470e71d2011-07-07 08:21:25 +0000504 }
505}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000506
niklase@google.com470e71d2011-07-07 08:21:25 +0000507} // namespace webrtc