blob: 99470601412e3aa6d9311d2098b55e388a137528 [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
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000054template <typename T>
55void StereoToMono(const T* left, const T* right, T* out,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000056 int samples_per_channel) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +000057 for (int i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000058 out[i] = (left[i] + right[i]) / 2;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000059}
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061} // namespace
62
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000063// One int16_t and one float ChannelBuffer that are kept in sync. The sync is
64// broken when someone requests write access to either ChannelBuffer, and
65// reestablished when someone requests the outdated ChannelBuffer. It is
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000066// therefore safe to use the return value of ibuf_const() and fbuf_const()
67// until the next call to ibuf() or fbuf(), and the return value of ibuf() and
68// fbuf() until the next call to any of the other functions.
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000069class IFChannelBuffer {
70 public:
71 IFChannelBuffer(int samples_per_channel, int num_channels)
72 : ivalid_(true),
73 ibuf_(samples_per_channel, num_channels),
74 fvalid_(true),
75 fbuf_(samples_per_channel, num_channels) {}
76
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000077 ChannelBuffer<int16_t>* ibuf() { return ibuf(false); }
78 ChannelBuffer<float>* fbuf() { return fbuf(false); }
79 const ChannelBuffer<int16_t>* ibuf_const() { return ibuf(true); }
80 const ChannelBuffer<float>* fbuf_const() { return fbuf(true); }
81
82 private:
83 ChannelBuffer<int16_t>* ibuf(bool readonly) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000084 RefreshI();
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000085 fvalid_ = readonly;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000086 return &ibuf_;
87 }
88
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000089 ChannelBuffer<float>* fbuf(bool readonly) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000090 RefreshF();
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000091 ivalid_ = readonly;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000092 return &fbuf_;
93 }
94
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000095 void RefreshF() {
96 if (!fvalid_) {
97 assert(ivalid_);
98 const int16_t* const int_data = ibuf_.data();
99 float* const float_data = fbuf_.data();
100 const int length = fbuf_.length();
101 for (int i = 0; i < length; ++i)
102 float_data[i] = int_data[i];
103 fvalid_ = true;
104 }
105 }
106
107 void RefreshI() {
108 if (!ivalid_) {
109 assert(fvalid_);
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000110 FloatS16ToS16(fbuf_.data(), ibuf_.length(), ibuf_.data());
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000111 ivalid_ = true;
112 }
113 }
114
115 bool ivalid_;
116 ChannelBuffer<int16_t> ibuf_;
117 bool fvalid_;
118 ChannelBuffer<float> fbuf_;
119};
120
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121AudioBuffer::AudioBuffer(int input_samples_per_channel,
122 int num_input_channels,
123 int process_samples_per_channel,
124 int num_process_channels,
125 int output_samples_per_channel)
126 : input_samples_per_channel_(input_samples_per_channel),
127 num_input_channels_(num_input_channels),
128 proc_samples_per_channel_(process_samples_per_channel),
129 num_proc_channels_(num_process_channels),
130 output_samples_per_channel_(output_samples_per_channel),
131 samples_per_split_channel_(proc_samples_per_channel_),
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000132 mixed_low_pass_valid_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000133 reference_copied_(false),
134 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000135 keyboard_data_(NULL),
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000136 channels_(new IFChannelBuffer(proc_samples_per_channel_,
137 num_proc_channels_)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000138 assert(input_samples_per_channel_ > 0);
139 assert(proc_samples_per_channel_ > 0);
140 assert(output_samples_per_channel_ > 0);
141 assert(num_input_channels_ > 0 && num_input_channels_ <= 2);
142 assert(num_proc_channels_ <= num_input_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000144 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
145 input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_,
146 num_proc_channels_));
147 }
148
149 if (input_samples_per_channel_ != proc_samples_per_channel_ ||
150 output_samples_per_channel_ != proc_samples_per_channel_) {
151 // Create an intermediate buffer for resampling.
152 process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_,
153 num_proc_channels_));
154 }
155
156 if (input_samples_per_channel_ != proc_samples_per_channel_) {
157 input_resamplers_.reserve(num_proc_channels_);
158 for (int i = 0; i < num_proc_channels_; ++i) {
159 input_resamplers_.push_back(
160 new PushSincResampler(input_samples_per_channel_,
161 proc_samples_per_channel_));
162 }
163 }
164
165 if (output_samples_per_channel_ != proc_samples_per_channel_) {
166 output_resamplers_.reserve(num_proc_channels_);
167 for (int i = 0; i < num_proc_channels_; ++i) {
168 output_resamplers_.push_back(
169 new PushSincResampler(proc_samples_per_channel_,
170 output_samples_per_channel_));
171 }
172 }
173
174 if (proc_samples_per_channel_ == kSamplesPer32kHzChannel) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000175 samples_per_split_channel_ = kSamplesPer16kHzChannel;
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000176 split_channels_low_.reset(new IFChannelBuffer(samples_per_split_channel_,
177 num_proc_channels_));
178 split_channels_high_.reset(new IFChannelBuffer(samples_per_split_channel_,
179 num_proc_channels_));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000180 filter_states_.reset(new SplitFilterStates[num_proc_channels_]);
181 }
182}
183
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000184AudioBuffer::~AudioBuffer() {}
185
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000186void AudioBuffer::CopyFrom(const float* const* data,
187 int samples_per_channel,
188 AudioProcessing::ChannelLayout layout) {
189 assert(samples_per_channel == input_samples_per_channel_);
190 assert(ChannelsFromLayout(layout) == num_input_channels_);
191 InitForNewData();
192
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000193 if (HasKeyboardChannel(layout)) {
194 keyboard_data_ = data[KeyboardChannelIndex(layout)];
195 }
196
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000197 // Downmix.
198 const float* const* data_ptr = data;
199 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
200 StereoToMono(data[0],
201 data[1],
202 input_buffer_->channel(0),
203 input_samples_per_channel_);
204 data_ptr = input_buffer_->channels();
205 }
206
207 // Resample.
208 if (input_samples_per_channel_ != proc_samples_per_channel_) {
209 for (int i = 0; i < num_proc_channels_; ++i) {
210 input_resamplers_[i]->Resample(data_ptr[i],
211 input_samples_per_channel_,
212 process_buffer_->channel(i),
213 proc_samples_per_channel_);
214 }
215 data_ptr = process_buffer_->channels();
216 }
217
218 // Convert to int16.
219 for (int i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000220 FloatToFloatS16(data_ptr[i], proc_samples_per_channel_,
221 channels_->fbuf()->channel(i));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000222 }
223}
224
225void AudioBuffer::CopyTo(int samples_per_channel,
226 AudioProcessing::ChannelLayout layout,
227 float* const* data) {
228 assert(samples_per_channel == output_samples_per_channel_);
229 assert(ChannelsFromLayout(layout) == num_proc_channels_);
230
231 // Convert to float.
232 float* const* data_ptr = data;
233 if (output_samples_per_channel_ != proc_samples_per_channel_) {
234 // Convert to an intermediate buffer for subsequent resampling.
235 data_ptr = process_buffer_->channels();
236 }
237 for (int i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000238 FloatS16ToFloat(channels_->fbuf()->channel(i),
239 proc_samples_per_channel_,
240 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000241 }
242
243 // Resample.
244 if (output_samples_per_channel_ != proc_samples_per_channel_) {
245 for (int i = 0; i < num_proc_channels_; ++i) {
246 output_resamplers_[i]->Resample(data_ptr[i],
247 proc_samples_per_channel_,
248 data[i],
249 output_samples_per_channel_);
250 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 }
252}
253
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000254void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000255 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000256 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000257 reference_copied_ = false;
258 activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000259}
260
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000261const int16_t* AudioBuffer::data(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000262 return channels_->ibuf_const()->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000265int16_t* AudioBuffer::data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000266 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000267 return channels_->ibuf()->channel(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000268}
269
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000270const float* AudioBuffer::data_f(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000271 return channels_->fbuf_const()->channel(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000272}
273
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000274float* AudioBuffer::data_f(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000275 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000276 return channels_->fbuf()->channel(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000277}
278
claguna@google.combfacaab2014-09-25 20:52:08 +0000279const float* const* AudioBuffer::channels_f() const {
280 return channels_->fbuf_const()->channels();
281}
282
283float* const* AudioBuffer::channels_f() {
284 mixed_low_pass_valid_ = false;
285 return channels_->fbuf()->channels();
286}
287
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000288const int16_t* AudioBuffer::low_pass_split_data(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000289 return split_channels_low_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000290 ? split_channels_low_->ibuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000291 : data(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000292}
293
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000294int16_t* AudioBuffer::low_pass_split_data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000295 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000296 return split_channels_low_.get()
297 ? split_channels_low_->ibuf()->channel(channel)
298 : data(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000299}
300
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000301const float* AudioBuffer::low_pass_split_data_f(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_->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000304 : data_f(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000305}
306
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000307float* AudioBuffer::low_pass_split_data_f(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_->fbuf()->channel(channel)
311 : data_f(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000312}
313
claguna@google.combfacaab2014-09-25 20:52:08 +0000314const float* const* AudioBuffer::low_pass_split_channels_f() const {
315 return split_channels_low_.get()
316 ? split_channels_low_->fbuf_const()->channels()
317 : channels_f();
318}
319
320float* const* AudioBuffer::low_pass_split_channels_f() {
321 mixed_low_pass_valid_ = false;
322 return split_channels_low_.get()
323 ? split_channels_low_->fbuf()->channels()
324 : channels_f();
325}
326
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000327const int16_t* AudioBuffer::high_pass_split_data(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000328 return split_channels_high_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000329 ? split_channels_high_->ibuf_const()->channel(channel)
330 : NULL;
331}
332
333int16_t* AudioBuffer::high_pass_split_data(int channel) {
334 return split_channels_high_.get()
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000335 ? split_channels_high_->ibuf()->channel(channel)
336 : NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000339const float* AudioBuffer::high_pass_split_data_f(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000340 return split_channels_high_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000341 ? split_channels_high_->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000342 : NULL;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000343}
344
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000345float* AudioBuffer::high_pass_split_data_f(int channel) {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000346 return split_channels_high_.get()
347 ? split_channels_high_->fbuf()->channel(channel)
348 : NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000349}
350
claguna@google.combfacaab2014-09-25 20:52:08 +0000351const float* const* AudioBuffer::high_pass_split_channels_f() const {
352 return split_channels_high_.get()
353 ? split_channels_high_->fbuf_const()->channels()
354 : NULL;
355}
356
357float* const* AudioBuffer::high_pass_split_channels_f() {
358 return split_channels_high_.get()
359 ? split_channels_high_->fbuf()->channels()
360 : NULL;
361}
362
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000363const int16_t* AudioBuffer::mixed_low_pass_data() {
364 // Currently only mixing stereo to mono is supported.
365 assert(num_proc_channels_ == 1 || num_proc_channels_ == 2);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000366
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000367 if (num_proc_channels_ == 1) {
368 return low_pass_split_data(0);
369 }
370
371 if (!mixed_low_pass_valid_) {
372 if (!mixed_low_pass_channels_.get()) {
373 mixed_low_pass_channels_.reset(
374 new ChannelBuffer<int16_t>(samples_per_split_channel_, 1));
375 }
376 StereoToMono(low_pass_split_data(0),
377 low_pass_split_data(1),
378 mixed_low_pass_channels_->data(),
379 samples_per_split_channel_);
380 mixed_low_pass_valid_ = true;
381 }
382 return mixed_low_pass_channels_->data();
niklase@google.com470e71d2011-07-07 08:21:25 +0000383}
384
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000385const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 if (!reference_copied_) {
387 return NULL;
388 }
389
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000390 return low_pass_reference_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000391}
392
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000393const float* AudioBuffer::keyboard_data() const {
394 return keyboard_data_;
395}
396
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000397SplitFilterStates* AudioBuffer::filter_states(int channel) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000398 assert(channel >= 0 && channel < num_proc_channels_);
399 return &filter_states_[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000402void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
403 activity_ = activity;
404}
405
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000406AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000407 return activity_;
408}
409
410int AudioBuffer::num_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 return num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000412}
413
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000414int AudioBuffer::samples_per_channel() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000415 return proc_samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416}
417
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000418int AudioBuffer::samples_per_split_channel() const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000419 return samples_per_split_channel_;
420}
421
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000422int AudioBuffer::samples_per_keyboard_channel() const {
423 // We don't resample the keyboard channel.
424 return input_samples_per_channel_;
425}
426
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000427// TODO(andrew): Do deinterleaving and mixing in one step?
428void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000429 assert(proc_samples_per_channel_ == input_samples_per_channel_);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000430 assert(frame->num_channels_ == num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000431 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
432 InitForNewData();
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000433 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000435 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
436 // Downmix directly; no explicit deinterleaving needed.
437 int16_t* downmixed = channels_->ibuf()->channel(0);
438 for (int i = 0; i < input_samples_per_channel_; ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000439 downmixed[i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2;
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000440 }
441 } else {
442 assert(num_proc_channels_ == num_input_channels_);
443 int16_t* interleaved = frame->data_;
444 for (int i = 0; i < num_proc_channels_; ++i) {
445 int16_t* deinterleaved = channels_->ibuf()->channel(i);
446 int interleaved_idx = i;
447 for (int j = 0; j < proc_samples_per_channel_; ++j) {
448 deinterleaved[j] = interleaved[interleaved_idx];
449 interleaved_idx += num_proc_channels_;
450 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000451 }
452 }
453}
454
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000455void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000456 assert(proc_samples_per_channel_ == output_samples_per_channel_);
457 assert(num_proc_channels_ == num_input_channels_);
458 assert(frame->num_channels_ == num_proc_channels_);
459 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000460 frame->vad_activity_ = activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000461
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000462 if (!data_changed) {
463 return;
464 }
465
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000466 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000467 for (int i = 0; i < num_proc_channels_; i++) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000468 int16_t* deinterleaved = channels_->ibuf()->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000469 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000471 interleaved[interleaved_idx] = deinterleaved[j];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000472 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000473 }
474 }
475}
476
niklase@google.com470e71d2011-07-07 08:21:25 +0000477void AudioBuffer::CopyLowPassToReference() {
478 reference_copied_ = true;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000479 if (!low_pass_reference_channels_.get()) {
480 low_pass_reference_channels_.reset(
481 new ChannelBuffer<int16_t>(samples_per_split_channel_,
482 num_proc_channels_));
483 }
484 for (int i = 0; i < num_proc_channels_; i++) {
485 low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i);
niklase@google.com470e71d2011-07-07 08:21:25 +0000486 }
487}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000488
niklase@google.com470e71d2011-07-07 08:21:25 +0000489} // namespace webrtc