Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: webrtc/modules/audio_processing/audio_buffer.cc

Issue 1226093007: Allow more than 2 input channels in AudioProcessing. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Address Comments Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_processing/audio_buffer.h" 11 #include "webrtc/modules/audio_processing/audio_buffer.h"
12 12
13 #include <vector>
14
13 #include "webrtc/common_audio/include/audio_util.h" 15 #include "webrtc/common_audio/include/audio_util.h"
14 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" 16 #include "webrtc/common_audio/resampler/push_sinc_resampler.h"
15 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" 17 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h"
16 #include "webrtc/common_audio/channel_buffer.h" 18 #include "webrtc/common_audio/channel_buffer.h"
17 #include "webrtc/modules/audio_processing/common.h" 19 #include "webrtc/modules/audio_processing/common.h"
18 20
19 namespace webrtc { 21 namespace webrtc {
20 namespace { 22 namespace {
21 23
22 const int kSamplesPer16kHzChannel = 160; 24 const int kSamplesPer16kHzChannel = 160;
23 const int kSamplesPer32kHzChannel = 320; 25 const int kSamplesPer32kHzChannel = 320;
24 const int kSamplesPer48kHzChannel = 480; 26 const int kSamplesPer48kHzChannel = 480;
25 27
26 bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) { 28 int KeyboardChannelIndex(const StreamConfig& stream_config) {
27 switch (layout) { 29 if (!stream_config.has_keyboard()) {
28 case AudioProcessing::kMono: 30 assert(false);
29 case AudioProcessing::kStereo: 31 return -1;
30 return false;
31 case AudioProcessing::kMonoAndKeyboard:
32 case AudioProcessing::kStereoAndKeyboard:
33 return true;
34 } 32 }
35 assert(false);
36 return false;
37 }
38 33
39 int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) { 34 switch (stream_config.num_channels()) {
40 switch (layout) { 35 case 1:
41 case AudioProcessing::kMono:
42 case AudioProcessing::kStereo:
43 assert(false);
44 return -1;
45 case AudioProcessing::kMonoAndKeyboard:
46 return 1; 36 return 1;
47 case AudioProcessing::kStereoAndKeyboard: 37 case 2:
48 return 2; 38 return 2;
49 } 39 }
50 assert(false); 40 assert(false);
51 return -1; 41 return -1;
52 } 42 }
53 43
54 template <typename T>
55 void StereoToMono(const T* left, const T* right, T* out,
56 int num_frames) {
57 for (int i = 0; i < num_frames; ++i)
58 out[i] = (left[i] + right[i]) / 2;
59 }
60
61 int NumBandsFromSamplesPerChannel(int num_frames) { 44 int NumBandsFromSamplesPerChannel(int num_frames) {
62 int num_bands = 1; 45 int num_bands = 1;
63 if (num_frames == kSamplesPer32kHzChannel || 46 if (num_frames == kSamplesPer32kHzChannel ||
64 num_frames == kSamplesPer48kHzChannel) { 47 num_frames == kSamplesPer48kHzChannel) {
65 num_bands = rtc::CheckedDivExact(num_frames, 48 num_bands = rtc::CheckedDivExact(num_frames,
66 static_cast<int>(kSamplesPer16kHzChannel)); 49 static_cast<int>(kSamplesPer16kHzChannel));
67 } 50 }
68 return num_bands; 51 return num_bands;
69 } 52 }
70 53
(...skipping 13 matching lines...) Expand all
84 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), 67 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)),
85 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), 68 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)),
86 mixed_low_pass_valid_(false), 69 mixed_low_pass_valid_(false),
87 reference_copied_(false), 70 reference_copied_(false),
88 activity_(AudioFrame::kVadUnknown), 71 activity_(AudioFrame::kVadUnknown),
89 keyboard_data_(NULL), 72 keyboard_data_(NULL),
90 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { 73 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) {
91 assert(input_num_frames_ > 0); 74 assert(input_num_frames_ > 0);
92 assert(proc_num_frames_ > 0); 75 assert(proc_num_frames_ > 0);
93 assert(output_num_frames_ > 0); 76 assert(output_num_frames_ > 0);
94 assert(num_input_channels_ > 0 && num_input_channels_ <= 2); 77 assert(num_input_channels_ > 0);
95 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_); 78 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_);
96 79
97 if (input_num_frames_ != proc_num_frames_ || 80 if (input_num_frames_ != proc_num_frames_ ||
98 output_num_frames_ != proc_num_frames_) { 81 output_num_frames_ != proc_num_frames_) {
99 // Create an intermediate buffer for resampling. 82 // Create an intermediate buffer for resampling.
100 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, 83 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_,
101 num_proc_channels_)); 84 num_proc_channels_));
102 85
103 if (input_num_frames_ != proc_num_frames_) { 86 if (input_num_frames_ != proc_num_frames_) {
104 for (int i = 0; i < num_proc_channels_; ++i) { 87 for (int i = 0; i < num_proc_channels_; ++i) {
(...skipping 18 matching lines...) Expand all
123 num_bands_)); 106 num_bands_));
124 splitting_filter_.reset(new SplittingFilter(num_proc_channels_, 107 splitting_filter_.reset(new SplittingFilter(num_proc_channels_,
125 num_bands_, 108 num_bands_,
126 proc_num_frames_)); 109 proc_num_frames_));
127 } 110 }
128 } 111 }
129 112
130 AudioBuffer::~AudioBuffer() {} 113 AudioBuffer::~AudioBuffer() {}
131 114
132 void AudioBuffer::CopyFrom(const float* const* data, 115 void AudioBuffer::CopyFrom(const float* const* data,
133 int num_frames, 116 const StreamConfig& stream_config) {
134 AudioProcessing::ChannelLayout layout) { 117 assert(stream_config.samples_per_channel() == input_num_frames_);
135 assert(num_frames == input_num_frames_); 118 assert(stream_config.num_channels() == num_input_channels_);
136 assert(ChannelsFromLayout(layout) == num_input_channels_);
137 InitForNewData(); 119 InitForNewData();
138 // Initialized lazily because there's a different condition in 120 // Initialized lazily because there's a different condition in
139 // DeinterleaveFrom. 121 // DeinterleaveFrom.
140 if ((num_input_channels_ == 2 && num_proc_channels_ == 1) && !input_buffer_) { 122 const bool need_to_downmix =
123 num_input_channels_ > 1 && num_proc_channels_ == 1;
124 if (need_to_downmix && !input_buffer_) {
141 input_buffer_.reset( 125 input_buffer_.reset(
142 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); 126 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
143 } 127 }
144 128
145 if (HasKeyboardChannel(layout)) { 129 if (stream_config.has_keyboard()) {
146 keyboard_data_ = data[KeyboardChannelIndex(layout)]; 130 keyboard_data_ = data[KeyboardChannelIndex(stream_config)];
147 } 131 }
148 132
149 // Downmix. 133 // Downmix.
150 const float* const* data_ptr = data; 134 const float* const* data_ptr = data;
151 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { 135 if (need_to_downmix) {
152 StereoToMono(data[0], 136 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_,
153 data[1], 137 input_buffer_->fbuf()->channels()[0]);
154 input_buffer_->fbuf()->channels()[0],
155 input_num_frames_);
156 data_ptr = input_buffer_->fbuf_const()->channels(); 138 data_ptr = input_buffer_->fbuf_const()->channels();
157 } 139 }
158 140
159 // Resample. 141 // Resample.
160 if (input_num_frames_ != proc_num_frames_) { 142 if (input_num_frames_ != proc_num_frames_) {
161 for (int i = 0; i < num_proc_channels_; ++i) { 143 for (int i = 0; i < num_proc_channels_; ++i) {
162 input_resamplers_[i]->Resample(data_ptr[i], 144 input_resamplers_[i]->Resample(data_ptr[i],
163 input_num_frames_, 145 input_num_frames_,
164 process_buffer_->channels()[i], 146 process_buffer_->channels()[i],
165 proc_num_frames_); 147 proc_num_frames_);
166 } 148 }
167 data_ptr = process_buffer_->channels(); 149 data_ptr = process_buffer_->channels();
168 } 150 }
169 151
170 // Convert to the S16 range. 152 // Convert to the S16 range.
171 for (int i = 0; i < num_proc_channels_; ++i) { 153 for (int i = 0; i < num_proc_channels_; ++i) {
172 FloatToFloatS16(data_ptr[i], 154 FloatToFloatS16(data_ptr[i],
173 proc_num_frames_, 155 proc_num_frames_,
174 data_->fbuf()->channels()[i]); 156 data_->fbuf()->channels()[i]);
175 } 157 }
176 } 158 }
177 159
178 void AudioBuffer::CopyTo(int num_frames, 160 void AudioBuffer::CopyTo(const StreamConfig& stream_config,
179 AudioProcessing::ChannelLayout layout,
180 float* const* data) { 161 float* const* data) {
181 assert(num_frames == output_num_frames_); 162 assert(stream_config.samples_per_channel() == output_num_frames_);
182 assert(ChannelsFromLayout(layout) == num_channels_); 163 assert(stream_config.num_channels() == num_channels_);
183 164
184 // Convert to the float range. 165 // Convert to the float range.
185 float* const* data_ptr = data; 166 float* const* data_ptr = data;
186 if (output_num_frames_ != proc_num_frames_) { 167 if (output_num_frames_ != proc_num_frames_) {
187 // Convert to an intermediate buffer for subsequent resampling. 168 // Convert to an intermediate buffer for subsequent resampling.
188 data_ptr = process_buffer_->channels(); 169 data_ptr = process_buffer_->channels();
189 } 170 }
190 for (int i = 0; i < num_channels_; ++i) { 171 for (int i = 0; i < num_channels_; ++i) {
191 FloatS16ToFloat(data_->fbuf()->channels()[i], 172 FloatS16ToFloat(data_->fbuf()->channels()[i],
192 proc_num_frames_, 173 proc_num_frames_,
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 ChannelBuffer<float>* AudioBuffer::split_data_f() { 301 ChannelBuffer<float>* AudioBuffer::split_data_f() {
321 mixed_low_pass_valid_ = false; 302 mixed_low_pass_valid_ = false;
322 return split_data_.get() ? split_data_->fbuf() : data_->fbuf(); 303 return split_data_.get() ? split_data_->fbuf() : data_->fbuf();
323 } 304 }
324 305
325 const ChannelBuffer<float>* AudioBuffer::split_data_f() const { 306 const ChannelBuffer<float>* AudioBuffer::split_data_f() const {
326 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const(); 307 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const();
327 } 308 }
328 309
329 const int16_t* AudioBuffer::mixed_low_pass_data() { 310 const int16_t* AudioBuffer::mixed_low_pass_data() {
330 // Currently only mixing stereo to mono is supported.
331 assert(num_proc_channels_ == 1 || num_proc_channels_ == 2);
332
333 if (num_proc_channels_ == 1) { 311 if (num_proc_channels_ == 1) {
334 return split_bands_const(0)[kBand0To8kHz]; 312 return split_bands_const(0)[kBand0To8kHz];
335 } 313 }
336 314
337 if (!mixed_low_pass_valid_) { 315 if (!mixed_low_pass_valid_) {
338 if (!mixed_low_pass_channels_.get()) { 316 if (!mixed_low_pass_channels_.get()) {
339 mixed_low_pass_channels_.reset( 317 mixed_low_pass_channels_.reset(
340 new ChannelBuffer<int16_t>(num_split_frames_, 1)); 318 new ChannelBuffer<int16_t>(num_split_frames_, 1));
341 } 319 }
342 StereoToMono(split_bands_const(0)[kBand0To8kHz], 320
343 split_bands_const(1)[kBand0To8kHz], 321 std::vector<const int16_t*> low_pass_data_channels(num_proc_channels_);
344 mixed_low_pass_channels_->channels()[0], 322 for (int i = 0; i < num_proc_channels_; ++i) {
345 num_split_frames_); 323 low_pass_data_channels[i] = split_bands_const(i)[kBand0To8kHz];
324 }
325 DownmixToMono<int16_t, int32_t>(low_pass_data_channels.data(),
326 num_split_frames_, num_channels_,
327 mixed_low_pass_channels_->channels()[0]);
346 mixed_low_pass_valid_ = true; 328 mixed_low_pass_valid_ = true;
347 } 329 }
348 return mixed_low_pass_channels_->channels()[0]; 330 return mixed_low_pass_channels_->channels()[0];
349 } 331 }
350 332
351 const int16_t* AudioBuffer::low_pass_reference(int channel) const { 333 const int16_t* AudioBuffer::low_pass_reference(int channel) const {
352 if (!reference_copied_) { 334 if (!reference_copied_) {
353 return NULL; 335 return NULL;
354 } 336 }
355 337
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); 386 new IFChannelBuffer(input_num_frames_, num_proc_channels_));
405 } 387 }
406 activity_ = frame->vad_activity_; 388 activity_ = frame->vad_activity_;
407 389
408 int16_t* const* deinterleaved; 390 int16_t* const* deinterleaved;
409 if (input_num_frames_ == proc_num_frames_) { 391 if (input_num_frames_ == proc_num_frames_) {
410 deinterleaved = data_->ibuf()->channels(); 392 deinterleaved = data_->ibuf()->channels();
411 } else { 393 } else {
412 deinterleaved = input_buffer_->ibuf()->channels(); 394 deinterleaved = input_buffer_->ibuf()->channels();
413 } 395 }
414 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { 396 if (num_proc_channels_ == 1) {
415 // Downmix directly; no explicit deinterleaving needed. 397 // Downmix and deinterleave simultaneously.
416 for (int i = 0; i < input_num_frames_; ++i) { 398 DownmixInterleavedToMono(frame->data_, input_num_frames_,
417 deinterleaved[0][i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2; 399 num_input_channels_, deinterleaved[0]);
418 }
419 } else { 400 } else {
420 assert(num_proc_channels_ == num_input_channels_); 401 assert(num_proc_channels_ == num_input_channels_);
421 Deinterleave(frame->data_, 402 Deinterleave(frame->data_,
422 input_num_frames_, 403 input_num_frames_,
423 num_proc_channels_, 404 num_proc_channels_,
424 deinterleaved); 405 deinterleaved);
425 } 406 }
426 407
427 // Resample. 408 // Resample.
428 if (input_num_frames_ != proc_num_frames_) { 409 if (input_num_frames_ != proc_num_frames_) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 451
471 void AudioBuffer::SplitIntoFrequencyBands() { 452 void AudioBuffer::SplitIntoFrequencyBands() {
472 splitting_filter_->Analysis(data_.get(), split_data_.get()); 453 splitting_filter_->Analysis(data_.get(), split_data_.get());
473 } 454 }
474 455
475 void AudioBuffer::MergeFrequencyBands() { 456 void AudioBuffer::MergeFrequencyBands() {
476 splitting_filter_->Synthesis(split_data_.get(), data_.get()); 457 splitting_filter_->Synthesis(split_data_.get(), data_.get());
477 } 458 }
478 459
479 } // namespace webrtc 460 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698