blob: 7f579b0d92a9413b42ff13ebc9a4405c031427d7 [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
54
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000055void StereoToMono(const float* left, const float* right, float* out,
56 int samples_per_channel) {
57 for (int i = 0; i < samples_per_channel; ++i) {
58 out[i] = (left[i] + right[i]) / 2;
59 }
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000061
62void StereoToMono(const int16_t* left, const int16_t* right, int16_t* out,
63 int samples_per_channel) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000064 for (int i = 0; i < samples_per_channel; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000065 out[i] = (left[i] + right[i]) >> 1;
andrew@webrtc.org103657b2014-04-24 18:28:56 +000066 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000067}
68
niklase@google.com470e71d2011-07-07 08:21:25 +000069} // namespace
70
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000071// One int16_t and one float ChannelBuffer that are kept in sync. The sync is
72// broken when someone requests write access to either ChannelBuffer, and
73// reestablished when someone requests the outdated ChannelBuffer. It is
74// therefore safe to use the return value of ibuf() and fbuf() until the next
75// call to the other method.
76class 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
84 ChannelBuffer<int16_t>* ibuf() {
85 RefreshI();
86 fvalid_ = false;
87 return &ibuf_;
88 }
89
90 ChannelBuffer<float>* fbuf() {
91 RefreshF();
92 ivalid_ = false;
93 return &fbuf_;
94 }
95
96 private:
97 void RefreshF() {
98 if (!fvalid_) {
99 assert(ivalid_);
100 const int16_t* const int_data = ibuf_.data();
101 float* const float_data = fbuf_.data();
102 const int length = fbuf_.length();
103 for (int i = 0; i < length; ++i)
104 float_data[i] = int_data[i];
105 fvalid_ = true;
106 }
107 }
108
109 void RefreshI() {
110 if (!ivalid_) {
111 assert(fvalid_);
112 const float* const float_data = fbuf_.data();
113 int16_t* const int_data = ibuf_.data();
114 const int length = ibuf_.length();
115 for (int i = 0; i < length; ++i)
116 int_data[i] = WEBRTC_SPL_SAT(std::numeric_limits<int16_t>::max(),
117 float_data[i],
118 std::numeric_limits<int16_t>::min());
119 ivalid_ = true;
120 }
121 }
122
123 bool ivalid_;
124 ChannelBuffer<int16_t> ibuf_;
125 bool fvalid_;
126 ChannelBuffer<float> fbuf_;
127};
128
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000129class SplitChannelBuffer {
130 public:
131 SplitChannelBuffer(int samples_per_split_channel, int num_channels)
132 : low_(samples_per_split_channel, num_channels),
133 high_(samples_per_split_channel, num_channels) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000135 ~SplitChannelBuffer() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000137 int16_t* low_channel(int i) { return low_.ibuf()->channel(i); }
138 int16_t* high_channel(int i) { return high_.ibuf()->channel(i); }
139 float* low_channel_f(int i) { return low_.fbuf()->channel(i); }
140 float* high_channel_f(int i) { return high_.fbuf()->channel(i); }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000141
142 private:
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000143 IFChannelBuffer low_;
144 IFChannelBuffer high_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145};
146
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000147AudioBuffer::AudioBuffer(int input_samples_per_channel,
148 int num_input_channels,
149 int process_samples_per_channel,
150 int num_process_channels,
151 int output_samples_per_channel)
152 : input_samples_per_channel_(input_samples_per_channel),
153 num_input_channels_(num_input_channels),
154 proc_samples_per_channel_(process_samples_per_channel),
155 num_proc_channels_(num_process_channels),
156 output_samples_per_channel_(output_samples_per_channel),
157 samples_per_split_channel_(proc_samples_per_channel_),
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000158 num_mixed_channels_(0),
159 num_mixed_low_pass_channels_(0),
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000160 reference_copied_(false),
161 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000162 keyboard_data_(NULL),
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000163 channels_(new IFChannelBuffer(proc_samples_per_channel_,
164 num_proc_channels_)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000165 assert(input_samples_per_channel_ > 0);
166 assert(proc_samples_per_channel_ > 0);
167 assert(output_samples_per_channel_ > 0);
168 assert(num_input_channels_ > 0 && num_input_channels_ <= 2);
169 assert(num_proc_channels_ <= num_input_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000171 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
172 input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_,
173 num_proc_channels_));
174 }
175
176 if (input_samples_per_channel_ != proc_samples_per_channel_ ||
177 output_samples_per_channel_ != proc_samples_per_channel_) {
178 // Create an intermediate buffer for resampling.
179 process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_,
180 num_proc_channels_));
181 }
182
183 if (input_samples_per_channel_ != proc_samples_per_channel_) {
184 input_resamplers_.reserve(num_proc_channels_);
185 for (int i = 0; i < num_proc_channels_; ++i) {
186 input_resamplers_.push_back(
187 new PushSincResampler(input_samples_per_channel_,
188 proc_samples_per_channel_));
189 }
190 }
191
192 if (output_samples_per_channel_ != proc_samples_per_channel_) {
193 output_resamplers_.reserve(num_proc_channels_);
194 for (int i = 0; i < num_proc_channels_; ++i) {
195 output_resamplers_.push_back(
196 new PushSincResampler(proc_samples_per_channel_,
197 output_samples_per_channel_));
198 }
199 }
200
201 if (proc_samples_per_channel_ == kSamplesPer32kHzChannel) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 samples_per_split_channel_ = kSamplesPer16kHzChannel;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000203 split_channels_.reset(new SplitChannelBuffer(samples_per_split_channel_,
204 num_proc_channels_));
205 filter_states_.reset(new SplitFilterStates[num_proc_channels_]);
206 }
207}
208
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000209AudioBuffer::~AudioBuffer() {}
210
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000211void AudioBuffer::CopyFrom(const float* const* data,
212 int samples_per_channel,
213 AudioProcessing::ChannelLayout layout) {
214 assert(samples_per_channel == input_samples_per_channel_);
215 assert(ChannelsFromLayout(layout) == num_input_channels_);
216 InitForNewData();
217
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000218 if (HasKeyboardChannel(layout)) {
219 keyboard_data_ = data[KeyboardChannelIndex(layout)];
220 }
221
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000222 // Downmix.
223 const float* const* data_ptr = data;
224 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
225 StereoToMono(data[0],
226 data[1],
227 input_buffer_->channel(0),
228 input_samples_per_channel_);
229 data_ptr = input_buffer_->channels();
230 }
231
232 // Resample.
233 if (input_samples_per_channel_ != proc_samples_per_channel_) {
234 for (int i = 0; i < num_proc_channels_; ++i) {
235 input_resamplers_[i]->Resample(data_ptr[i],
236 input_samples_per_channel_,
237 process_buffer_->channel(i),
238 proc_samples_per_channel_);
239 }
240 data_ptr = process_buffer_->channels();
241 }
242
243 // Convert to int16.
244 for (int i = 0; i < num_proc_channels_; ++i) {
245 ScaleAndRoundToInt16(data_ptr[i], proc_samples_per_channel_,
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000246 channels_->ibuf()->channel(i));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000247 }
248}
249
250void AudioBuffer::CopyTo(int samples_per_channel,
251 AudioProcessing::ChannelLayout layout,
252 float* const* data) {
253 assert(samples_per_channel == output_samples_per_channel_);
254 assert(ChannelsFromLayout(layout) == num_proc_channels_);
255
256 // Convert to float.
257 float* const* data_ptr = data;
258 if (output_samples_per_channel_ != proc_samples_per_channel_) {
259 // Convert to an intermediate buffer for subsequent resampling.
260 data_ptr = process_buffer_->channels();
261 }
262 for (int i = 0; i < num_proc_channels_; ++i) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000263 ScaleToFloat(channels_->ibuf()->channel(i),
264 proc_samples_per_channel_,
265 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000266 }
267
268 // Resample.
269 if (output_samples_per_channel_ != proc_samples_per_channel_) {
270 for (int i = 0; i < num_proc_channels_; ++i) {
271 output_resamplers_[i]->Resample(data_ptr[i],
272 proc_samples_per_channel_,
273 data[i],
274 output_samples_per_channel_);
275 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000276 }
277}
278
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000280 keyboard_data_ = NULL;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000281 num_mixed_channels_ = 0;
282 num_mixed_low_pass_channels_ = 0;
283 reference_copied_ = false;
284 activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000285}
286
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000287const int16_t* AudioBuffer::data(int channel) const {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000288 return channels_->ibuf()->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000289}
290
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000291int16_t* AudioBuffer::data(int channel) {
292 const AudioBuffer* t = this;
293 return const_cast<int16_t*>(t->data(channel));
294}
295
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000296const float* AudioBuffer::data_f(int channel) const {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000297 return channels_->fbuf()->channel(channel);
298}
299
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000300float* AudioBuffer::data_f(int channel) {
301 const AudioBuffer* t = this;
302 return const_cast<float*>(t->data_f(channel));
303}
304
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000305const int16_t* AudioBuffer::low_pass_split_data(int channel) const {
kwiberg@webrtc.org8e4401b2014-06-03 10:04:13 +0000306 return split_channels_.get() ? split_channels_->low_channel(channel)
307 : data(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308}
309
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000310int16_t* AudioBuffer::low_pass_split_data(int channel) {
311 const AudioBuffer* t = this;
312 return const_cast<int16_t*>(t->low_pass_split_data(channel));
313}
314
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000315const float* AudioBuffer::low_pass_split_data_f(int channel) const {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000316 return split_channels_.get() ? split_channels_->low_channel_f(channel)
317 : data_f(channel);
318}
319
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000320float* AudioBuffer::low_pass_split_data_f(int channel) {
321 const AudioBuffer* t = this;
322 return const_cast<float*>(t->low_pass_split_data_f(channel));
323}
324
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000325const int16_t* AudioBuffer::high_pass_split_data(int channel) const {
kwiberg@webrtc.org8e4401b2014-06-03 10:04:13 +0000326 return split_channels_.get() ? split_channels_->high_channel(channel) : NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000327}
328
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000329int16_t* AudioBuffer::high_pass_split_data(int channel) {
330 const AudioBuffer* t = this;
331 return const_cast<int16_t*>(t->high_pass_split_data(channel));
332}
333
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000334const float* AudioBuffer::high_pass_split_data_f(int channel) const {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000335 return split_channels_.get() ? split_channels_->high_channel_f(channel)
336 : NULL;
337}
338
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000339float* AudioBuffer::high_pass_split_data_f(int channel) {
340 const AudioBuffer* t = this;
341 return const_cast<float*>(t->high_pass_split_data_f(channel));
342}
343
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000344const int16_t* AudioBuffer::mixed_data(int channel) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000345 return mixed_channels_->channel(channel);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000346}
347
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000348const int16_t* AudioBuffer::mixed_low_pass_data(int channel) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000349 return mixed_low_pass_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000352const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000353 if (!reference_copied_) {
354 return NULL;
355 }
356
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000357 return low_pass_reference_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000358}
359
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000360const float* AudioBuffer::keyboard_data() const {
361 return keyboard_data_;
362}
363
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000364SplitFilterStates* AudioBuffer::filter_states(int channel) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000365 assert(channel >= 0 && channel < num_proc_channels_);
366 return &filter_states_[channel];
niklase@google.com470e71d2011-07-07 08:21:25 +0000367}
368
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000369void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
370 activity_ = activity;
371}
372
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000373AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000374 return activity_;
375}
376
377int AudioBuffer::num_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000378 return num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379}
380
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000381int AudioBuffer::samples_per_channel() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000382 return proc_samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000383}
384
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000385int AudioBuffer::samples_per_split_channel() const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 return samples_per_split_channel_;
387}
388
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000389int AudioBuffer::samples_per_keyboard_channel() const {
390 // We don't resample the keyboard channel.
391 return input_samples_per_channel_;
392}
393
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000394// TODO(andrew): Do deinterleaving and mixing in one step?
395void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000396 assert(proc_samples_per_channel_ == input_samples_per_channel_);
397 assert(num_proc_channels_ == num_input_channels_);
398 assert(frame->num_channels_ == num_proc_channels_);
399 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
400 InitForNewData();
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000401 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000403 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000404 for (int i = 0; i < num_proc_channels_; i++) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000405 int16_t* deinterleaved = channels_->ibuf()->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000406 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000407 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000408 deinterleaved[j] = interleaved[interleaved_idx];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 }
411 }
412}
413
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000414void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000415 assert(proc_samples_per_channel_ == output_samples_per_channel_);
416 assert(num_proc_channels_ == num_input_channels_);
417 assert(frame->num_channels_ == num_proc_channels_);
418 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000419 frame->vad_activity_ = activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000421 if (!data_changed) {
422 return;
423 }
424
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000425 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000426 for (int i = 0; i < num_proc_channels_; i++) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000427 int16_t* deinterleaved = channels_->ibuf()->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000428 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000429 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000430 interleaved[interleaved_idx] = deinterleaved[j];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000431 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 }
433 }
434}
435
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000436void AudioBuffer::CopyAndMix(int num_mixed_channels) {
437 // We currently only support the stereo to mono case.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000438 assert(num_proc_channels_ == 2);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000439 assert(num_mixed_channels == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440 if (!mixed_channels_.get()) {
441 mixed_channels_.reset(
442 new ChannelBuffer<int16_t>(proc_samples_per_channel_,
443 num_mixed_channels));
444 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000445
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000446 StereoToMono(channels_->ibuf()->channel(0),
447 channels_->ibuf()->channel(1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000448 mixed_channels_->channel(0),
449 proc_samples_per_channel_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000450
niklase@google.com470e71d2011-07-07 08:21:25 +0000451 num_mixed_channels_ = num_mixed_channels;
452}
453
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000454void AudioBuffer::CopyAndMixLowPass(int num_mixed_channels) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000455 // We currently only support the stereo to mono case.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000456 assert(num_proc_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457 assert(num_mixed_channels == 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000458 if (!mixed_low_pass_channels_.get()) {
459 mixed_low_pass_channels_.reset(
460 new ChannelBuffer<int16_t>(samples_per_split_channel_,
461 num_mixed_channels));
462 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000463
464 StereoToMono(low_pass_split_data(0),
465 low_pass_split_data(1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000466 mixed_low_pass_channels_->channel(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000467 samples_per_split_channel_);
468
469 num_mixed_low_pass_channels_ = num_mixed_channels;
470}
471
472void AudioBuffer::CopyLowPassToReference() {
473 reference_copied_ = true;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000474 if (!low_pass_reference_channels_.get()) {
475 low_pass_reference_channels_.reset(
476 new ChannelBuffer<int16_t>(samples_per_split_channel_,
477 num_proc_channels_));
478 }
479 for (int i = 0; i < num_proc_channels_; i++) {
480 low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i);
niklase@google.com470e71d2011-07-07 08:21:25 +0000481 }
482}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000483
niklase@google.com470e71d2011-07-07 08:21:25 +0000484} // namespace webrtc