blob: 2746e61580f6b94720341f1fec190879123b4cc2 [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,
aluebs@webrtc.org087da132014-11-17 23:01:23 +000023 kSamplesPer32kHzChannel = 320,
24 kSamplesPer48kHzChannel = 480
niklase@google.com470e71d2011-07-07 08:21:25 +000025};
26
andrew@webrtc.org103657b2014-04-24 18:28:56 +000027bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) {
28 switch (layout) {
29 case AudioProcessing::kMono:
30 case AudioProcessing::kStereo:
31 return false;
32 case AudioProcessing::kMonoAndKeyboard:
33 case AudioProcessing::kStereoAndKeyboard:
34 return true;
35 }
36 assert(false);
37 return false;
38}
39
40int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) {
41 switch (layout) {
42 case AudioProcessing::kMono:
43 case AudioProcessing::kStereo:
44 assert(false);
45 return -1;
46 case AudioProcessing::kMonoAndKeyboard:
47 return 1;
48 case AudioProcessing::kStereoAndKeyboard:
49 return 2;
50 }
51 assert(false);
52 return -1;
53}
54
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000055template <typename T>
56void StereoToMono(const T* left, const T* right, T* out,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000057 int samples_per_channel) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000058 for (int i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000059 out[i] = (left[i] + right[i]) / 2;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000060}
61
niklase@google.com470e71d2011-07-07 08:21:25 +000062} // namespace
63
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000064// One int16_t and one float ChannelBuffer that are kept in sync. The sync is
65// broken when someone requests write access to either ChannelBuffer, and
66// reestablished when someone requests the outdated ChannelBuffer. It is
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000067// therefore safe to use the return value of ibuf_const() and fbuf_const()
68// until the next call to ibuf() or fbuf(), and the return value of ibuf() and
69// fbuf() until the next call to any of the other functions.
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000070class IFChannelBuffer {
71 public:
72 IFChannelBuffer(int samples_per_channel, int num_channels)
73 : ivalid_(true),
74 ibuf_(samples_per_channel, num_channels),
75 fvalid_(true),
76 fbuf_(samples_per_channel, num_channels) {}
77
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000078 ChannelBuffer<int16_t>* ibuf() { return ibuf(false); }
79 ChannelBuffer<float>* fbuf() { return fbuf(false); }
80 const ChannelBuffer<int16_t>* ibuf_const() { return ibuf(true); }
81 const ChannelBuffer<float>* fbuf_const() { return fbuf(true); }
82
83 private:
84 ChannelBuffer<int16_t>* ibuf(bool readonly) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000085 RefreshI();
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000086 fvalid_ = readonly;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000087 return &ibuf_;
88 }
89
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000090 ChannelBuffer<float>* fbuf(bool readonly) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000091 RefreshF();
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +000092 ivalid_ = readonly;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000093 return &fbuf_;
94 }
95
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000096 void RefreshF() {
97 if (!fvalid_) {
98 assert(ivalid_);
99 const int16_t* const int_data = ibuf_.data();
100 float* const float_data = fbuf_.data();
101 const int length = fbuf_.length();
102 for (int i = 0; i < length; ++i)
103 float_data[i] = int_data[i];
104 fvalid_ = true;
105 }
106 }
107
108 void RefreshI() {
109 if (!ivalid_) {
110 assert(fvalid_);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000111 FloatS16ToS16(fbuf_.data(), ibuf_.length(), ibuf_.data());
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000112 ivalid_ = true;
113 }
114 }
115
116 bool ivalid_;
117 ChannelBuffer<int16_t> ibuf_;
118 bool fvalid_;
119 ChannelBuffer<float> fbuf_;
120};
121
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122AudioBuffer::AudioBuffer(int input_samples_per_channel,
123 int num_input_channels,
124 int process_samples_per_channel,
125 int num_process_channels,
126 int output_samples_per_channel)
127 : input_samples_per_channel_(input_samples_per_channel),
128 num_input_channels_(num_input_channels),
129 proc_samples_per_channel_(process_samples_per_channel),
130 num_proc_channels_(num_process_channels),
131 output_samples_per_channel_(output_samples_per_channel),
132 samples_per_split_channel_(proc_samples_per_channel_),
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000133 mixed_low_pass_valid_(false),
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000134 reference_copied_(false),
135 activity_(AudioFrame::kVadUnknown),
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000136 keyboard_data_(NULL),
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000137 channels_(new IFChannelBuffer(proc_samples_per_channel_,
138 num_proc_channels_)) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000139 assert(input_samples_per_channel_ > 0);
140 assert(proc_samples_per_channel_ > 0);
141 assert(output_samples_per_channel_ > 0);
142 assert(num_input_channels_ > 0 && num_input_channels_ <= 2);
143 assert(num_proc_channels_ <= num_input_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000145 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
146 input_buffer_.reset(new ChannelBuffer<float>(input_samples_per_channel_,
147 num_proc_channels_));
148 }
149
150 if (input_samples_per_channel_ != proc_samples_per_channel_ ||
151 output_samples_per_channel_ != proc_samples_per_channel_) {
152 // Create an intermediate buffer for resampling.
153 process_buffer_.reset(new ChannelBuffer<float>(proc_samples_per_channel_,
154 num_proc_channels_));
155 }
156
157 if (input_samples_per_channel_ != proc_samples_per_channel_) {
158 input_resamplers_.reserve(num_proc_channels_);
159 for (int i = 0; i < num_proc_channels_; ++i) {
160 input_resamplers_.push_back(
161 new PushSincResampler(input_samples_per_channel_,
162 proc_samples_per_channel_));
163 }
164 }
165
166 if (output_samples_per_channel_ != proc_samples_per_channel_) {
167 output_resamplers_.reserve(num_proc_channels_);
168 for (int i = 0; i < num_proc_channels_; ++i) {
169 output_resamplers_.push_back(
170 new PushSincResampler(proc_samples_per_channel_,
171 output_samples_per_channel_));
172 }
173 }
174
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000175 if (proc_samples_per_channel_ == kSamplesPer32kHzChannel ||
176 proc_samples_per_channel_ == kSamplesPer48kHzChannel) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000177 samples_per_split_channel_ = kSamplesPer16kHzChannel;
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000178 split_channels_low_.reset(new IFChannelBuffer(samples_per_split_channel_,
179 num_proc_channels_));
180 split_channels_high_.reset(new IFChannelBuffer(samples_per_split_channel_,
181 num_proc_channels_));
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000182 splitting_filter_.reset(new SplittingFilter(num_proc_channels_));
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000183 if (proc_samples_per_channel_ == kSamplesPer48kHzChannel) {
184 split_channels_super_high_.reset(
185 new IFChannelBuffer(samples_per_split_channel_, num_proc_channels_));
186 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000187 }
188}
189
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000190AudioBuffer::~AudioBuffer() {}
191
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000192void AudioBuffer::CopyFrom(const float* const* data,
193 int samples_per_channel,
194 AudioProcessing::ChannelLayout layout) {
195 assert(samples_per_channel == input_samples_per_channel_);
196 assert(ChannelsFromLayout(layout) == num_input_channels_);
197 InitForNewData();
198
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000199 if (HasKeyboardChannel(layout)) {
200 keyboard_data_ = data[KeyboardChannelIndex(layout)];
201 }
202
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000203 // Downmix.
204 const float* const* data_ptr = data;
205 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
206 StereoToMono(data[0],
207 data[1],
208 input_buffer_->channel(0),
209 input_samples_per_channel_);
210 data_ptr = input_buffer_->channels();
211 }
212
213 // Resample.
214 if (input_samples_per_channel_ != proc_samples_per_channel_) {
215 for (int i = 0; i < num_proc_channels_; ++i) {
216 input_resamplers_[i]->Resample(data_ptr[i],
217 input_samples_per_channel_,
218 process_buffer_->channel(i),
219 proc_samples_per_channel_);
220 }
221 data_ptr = process_buffer_->channels();
222 }
223
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000224 // Convert to the S16 range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000225 for (int i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000226 FloatToFloatS16(data_ptr[i], proc_samples_per_channel_,
227 channels_->fbuf()->channel(i));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000228 }
229}
230
231void AudioBuffer::CopyTo(int samples_per_channel,
232 AudioProcessing::ChannelLayout layout,
233 float* const* data) {
234 assert(samples_per_channel == output_samples_per_channel_);
235 assert(ChannelsFromLayout(layout) == num_proc_channels_);
236
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000237 // Convert to the float range.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000238 float* const* data_ptr = data;
239 if (output_samples_per_channel_ != proc_samples_per_channel_) {
240 // Convert to an intermediate buffer for subsequent resampling.
241 data_ptr = process_buffer_->channels();
242 }
243 for (int i = 0; i < num_proc_channels_; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000244 FloatS16ToFloat(channels_->fbuf()->channel(i), proc_samples_per_channel_,
245 data_ptr[i]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000246 }
247
248 // Resample.
249 if (output_samples_per_channel_ != proc_samples_per_channel_) {
250 for (int i = 0; i < num_proc_channels_; ++i) {
251 output_resamplers_[i]->Resample(data_ptr[i],
252 proc_samples_per_channel_,
253 data[i],
254 output_samples_per_channel_);
255 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000256 }
257}
258
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000259void AudioBuffer::InitForNewData() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000260 keyboard_data_ = NULL;
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000261 mixed_low_pass_valid_ = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000262 reference_copied_ = false;
263 activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000264}
265
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000266const int16_t* AudioBuffer::data(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000267 return channels_->ibuf_const()->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000268}
269
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000270int16_t* AudioBuffer::data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000271 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000272 return channels_->ibuf()->channel(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000273}
274
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000275const int16_t* const* AudioBuffer::channels() const {
276 return channels_->ibuf_const()->channels();
277}
278
279int16_t* const* AudioBuffer::channels() {
280 mixed_low_pass_valid_ = false;
281 return channels_->ibuf()->channels();
282}
283
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000284const float* AudioBuffer::data_f(int channel) const {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000285 return channels_->fbuf_const()->channel(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000286}
287
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000288float* AudioBuffer::data_f(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000289 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000290 return channels_->fbuf()->channel(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000291}
292
claguna@google.combfacaab2014-09-25 20:52:08 +0000293const float* const* AudioBuffer::channels_f() const {
294 return channels_->fbuf_const()->channels();
295}
296
297float* const* AudioBuffer::channels_f() {
298 mixed_low_pass_valid_ = false;
299 return channels_->fbuf()->channels();
300}
301
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000302const int16_t* AudioBuffer::low_pass_split_data(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000303 return split_channels_low_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000304 ? split_channels_low_->ibuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000305 : data(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306}
307
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000308int16_t* AudioBuffer::low_pass_split_data(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000309 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000310 return split_channels_low_.get()
311 ? split_channels_low_->ibuf()->channel(channel)
312 : data(channel);
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000313}
314
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000315const int16_t* const* AudioBuffer::low_pass_split_channels() const {
316 return split_channels_low_.get()
317 ? split_channels_low_->ibuf_const()->channels()
318 : channels();
319}
320
321int16_t* const* AudioBuffer::low_pass_split_channels() {
322 mixed_low_pass_valid_ = false;
323 return split_channels_low_.get() ? split_channels_low_->ibuf()->channels()
324 : channels();
325}
326
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000327const float* AudioBuffer::low_pass_split_data_f(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000328 return split_channels_low_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000329 ? split_channels_low_->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000330 : data_f(channel);
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000331}
332
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000333float* AudioBuffer::low_pass_split_data_f(int channel) {
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000334 mixed_low_pass_valid_ = false;
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000335 return split_channels_low_.get()
336 ? split_channels_low_->fbuf()->channel(channel)
337 : data_f(channel);
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000338}
339
claguna@google.combfacaab2014-09-25 20:52:08 +0000340const float* const* AudioBuffer::low_pass_split_channels_f() const {
341 return split_channels_low_.get()
342 ? split_channels_low_->fbuf_const()->channels()
343 : channels_f();
344}
345
346float* const* AudioBuffer::low_pass_split_channels_f() {
347 mixed_low_pass_valid_ = false;
348 return split_channels_low_.get()
349 ? split_channels_low_->fbuf()->channels()
350 : channels_f();
351}
352
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000353const int16_t* AudioBuffer::high_pass_split_data(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000354 return split_channels_high_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000355 ? split_channels_high_->ibuf_const()->channel(channel)
356 : NULL;
357}
358
359int16_t* AudioBuffer::high_pass_split_data(int channel) {
360 return split_channels_high_.get()
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000361 ? split_channels_high_->ibuf()->channel(channel)
362 : NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000363}
364
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000365const int16_t* const* AudioBuffer::high_pass_split_channels() const {
366 return split_channels_high_.get()
367 ? split_channels_high_->ibuf_const()->channels()
368 : NULL;
369}
370
371int16_t* const* AudioBuffer::high_pass_split_channels() {
372 return split_channels_high_.get() ? split_channels_high_->ibuf()->channels()
373 : NULL;
374}
375
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000376const float* AudioBuffer::high_pass_split_data_f(int channel) const {
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000377 return split_channels_high_.get()
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000378 ? split_channels_high_->fbuf_const()->channel(channel)
kwiberg@webrtc.org2b6bc8d2014-07-17 09:46:37 +0000379 : NULL;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000380}
381
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000382float* AudioBuffer::high_pass_split_data_f(int channel) {
kwiberg@webrtc.orge364ac92014-07-18 07:50:29 +0000383 return split_channels_high_.get()
384 ? split_channels_high_->fbuf()->channel(channel)
385 : NULL;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +0000386}
387
claguna@google.combfacaab2014-09-25 20:52:08 +0000388const float* const* AudioBuffer::high_pass_split_channels_f() const {
389 return split_channels_high_.get()
390 ? split_channels_high_->fbuf_const()->channels()
391 : NULL;
392}
393
394float* const* AudioBuffer::high_pass_split_channels_f() {
395 return split_channels_high_.get()
396 ? split_channels_high_->fbuf()->channels()
397 : NULL;
398}
399
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000400const float* const* AudioBuffer::super_high_pass_split_channels_f() const {
401 return split_channels_super_high_.get()
402 ? split_channels_super_high_->fbuf_const()->channels()
403 : NULL;
404}
405
406float* const* AudioBuffer::super_high_pass_split_channels_f() {
407 return split_channels_super_high_.get()
408 ? split_channels_super_high_->fbuf()->channels()
409 : NULL;
410}
411
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000412const int16_t* AudioBuffer::mixed_low_pass_data() {
413 // Currently only mixing stereo to mono is supported.
414 assert(num_proc_channels_ == 1 || num_proc_channels_ == 2);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000415
aluebs@webrtc.org2561d522014-07-17 08:27:39 +0000416 if (num_proc_channels_ == 1) {
417 return low_pass_split_data(0);
418 }
419
420 if (!mixed_low_pass_valid_) {
421 if (!mixed_low_pass_channels_.get()) {
422 mixed_low_pass_channels_.reset(
423 new ChannelBuffer<int16_t>(samples_per_split_channel_, 1));
424 }
425 StereoToMono(low_pass_split_data(0),
426 low_pass_split_data(1),
427 mixed_low_pass_channels_->data(),
428 samples_per_split_channel_);
429 mixed_low_pass_valid_ = true;
430 }
431 return mixed_low_pass_channels_->data();
niklase@google.com470e71d2011-07-07 08:21:25 +0000432}
433
andrew@webrtc.org65f93382014-04-30 16:44:13 +0000434const int16_t* AudioBuffer::low_pass_reference(int channel) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000435 if (!reference_copied_) {
436 return NULL;
437 }
438
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000439 return low_pass_reference_channels_->channel(channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000442const float* AudioBuffer::keyboard_data() const {
443 return keyboard_data_;
444}
445
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000446void AudioBuffer::set_activity(AudioFrame::VADActivity activity) {
447 activity_ = activity;
448}
449
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000450AudioFrame::VADActivity AudioBuffer::activity() const {
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000451 return activity_;
452}
453
454int AudioBuffer::num_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000455 return num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000458int AudioBuffer::samples_per_channel() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000459 return proc_samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000462int AudioBuffer::samples_per_split_channel() const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000463 return samples_per_split_channel_;
464}
465
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000466int AudioBuffer::samples_per_keyboard_channel() const {
467 // We don't resample the keyboard channel.
468 return input_samples_per_channel_;
469}
470
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000471// TODO(andrew): Do deinterleaving and mixing in one step?
472void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000473 assert(proc_samples_per_channel_ == input_samples_per_channel_);
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000474 assert(frame->num_channels_ == num_input_channels_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000475 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
476 InitForNewData();
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000477 activity_ = frame->vad_activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000479 if (num_input_channels_ == 2 && num_proc_channels_ == 1) {
480 // Downmix directly; no explicit deinterleaving needed.
481 int16_t* downmixed = channels_->ibuf()->channel(0);
482 for (int i = 0; i < input_samples_per_channel_; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000483 downmixed[i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2;
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000484 }
485 } else {
486 assert(num_proc_channels_ == num_input_channels_);
487 int16_t* interleaved = frame->data_;
488 for (int i = 0; i < num_proc_channels_; ++i) {
489 int16_t* deinterleaved = channels_->ibuf()->channel(i);
490 int interleaved_idx = i;
491 for (int j = 0; j < proc_samples_per_channel_; ++j) {
492 deinterleaved[j] = interleaved[interleaved_idx];
493 interleaved_idx += num_proc_channels_;
494 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 }
496 }
497}
498
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000499void AudioBuffer::InterleaveTo(AudioFrame* frame, bool data_changed) const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000500 assert(proc_samples_per_channel_ == output_samples_per_channel_);
501 assert(num_proc_channels_ == num_input_channels_);
502 assert(frame->num_channels_ == num_proc_channels_);
503 assert(frame->samples_per_channel_ == proc_samples_per_channel_);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000504 frame->vad_activity_ = activity_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000506 if (!data_changed) {
507 return;
508 }
509
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000510 int16_t* interleaved = frame->data_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000511 for (int i = 0; i < num_proc_channels_; i++) {
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +0000512 int16_t* deinterleaved = channels_->ibuf()->channel(i);
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000513 int interleaved_idx = i;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000514 for (int j = 0; j < proc_samples_per_channel_; j++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000515 interleaved[interleaved_idx] = deinterleaved[j];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000516 interleaved_idx += num_proc_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000517 }
518 }
519}
520
niklase@google.com470e71d2011-07-07 08:21:25 +0000521void AudioBuffer::CopyLowPassToReference() {
522 reference_copied_ = true;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000523 if (!low_pass_reference_channels_.get()) {
524 low_pass_reference_channels_.reset(
525 new ChannelBuffer<int16_t>(samples_per_split_channel_,
526 num_proc_channels_));
527 }
528 for (int i = 0; i < num_proc_channels_; i++) {
529 low_pass_reference_channels_->CopyFrom(low_pass_split_data(i), i);
niklase@google.com470e71d2011-07-07 08:21:25 +0000530 }
531}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000532
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000533void AudioBuffer::SplitIntoFrequencyBands() {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000534 if (samples_per_channel() == kSamplesPer32kHzChannel) {
535 splitting_filter_->TwoBandsAnalysis(
536 channels(), samples_per_channel(), num_proc_channels_,
537 low_pass_split_channels(), high_pass_split_channels());
538 } else if (samples_per_channel() == kSamplesPer48kHzChannel) {
539 splitting_filter_->ThreeBandsAnalysis(
540 channels_f(), samples_per_channel(), num_proc_channels_,
541 low_pass_split_channels_f(), high_pass_split_channels_f(),
542 super_high_pass_split_channels_f());
543 }
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000544}
545
546void AudioBuffer::MergeFrequencyBands() {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000547 if (samples_per_channel() == kSamplesPer32kHzChannel) {
548 splitting_filter_->TwoBandsSynthesis(
549 low_pass_split_channels(), high_pass_split_channels(),
550 samples_per_split_channel(), num_proc_channels_, channels());
551 } else if (samples_per_channel() == kSamplesPer48kHzChannel) {
552 splitting_filter_->ThreeBandsSynthesis(
553 low_pass_split_channels_f(), high_pass_split_channels_f(),
554 super_high_pass_split_channels_f(), samples_per_split_channel(),
555 num_proc_channels_, channels_f());
556 }
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000557}
558
niklase@google.com470e71d2011-07-07 08:21:25 +0000559} // namespace webrtc