OLD | NEW |
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 "webrtc/common_audio/include/audio_util.h" | 13 #include "webrtc/common_audio/include/audio_util.h" |
14 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" | 14 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
15 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 15 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
16 #include "webrtc/common_audio/channel_buffer.h" | 16 #include "webrtc/common_audio/channel_buffer.h" |
17 #include "webrtc/modules/audio_processing/common.h" | 17 #include "webrtc/modules/audio_processing/common.h" |
18 | 18 |
19 namespace webrtc { | 19 namespace webrtc { |
20 namespace { | 20 namespace { |
21 | 21 |
22 const int kSamplesPer16kHzChannel = 160; | 22 const int kSamplesPer16kHzChannel = 160; |
23 const int kSamplesPer32kHzChannel = 320; | 23 const int kSamplesPer32kHzChannel = 320; |
24 const int kSamplesPer48kHzChannel = 480; | 24 const int kSamplesPer48kHzChannel = 480; |
25 | 25 |
26 bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) { | 26 int KeyboardChannelIndex(const StreamConfig& stream_config) { |
27 switch (layout) { | 27 if (!stream_config.has_keyboard()) { |
28 case AudioProcessing::kMono: | 28 assert(false); |
29 case AudioProcessing::kStereo: | 29 return -1; |
30 return false; | |
31 case AudioProcessing::kMonoAndKeyboard: | |
32 case AudioProcessing::kStereoAndKeyboard: | |
33 return true; | |
34 } | 30 } |
35 assert(false); | |
36 return false; | |
37 } | |
38 | 31 |
39 int KeyboardChannelIndex(AudioProcessing::ChannelLayout layout) { | 32 return stream_config.num_channels(); |
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 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 } | 33 } |
60 | 34 |
61 int NumBandsFromSamplesPerChannel(int num_frames) { | 35 int NumBandsFromSamplesPerChannel(int num_frames) { |
62 int num_bands = 1; | 36 int num_bands = 1; |
63 if (num_frames == kSamplesPer32kHzChannel || | 37 if (num_frames == kSamplesPer32kHzChannel || |
64 num_frames == kSamplesPer48kHzChannel) { | 38 num_frames == kSamplesPer48kHzChannel) { |
65 num_bands = rtc::CheckedDivExact(num_frames, | 39 num_bands = rtc::CheckedDivExact(num_frames, |
66 static_cast<int>(kSamplesPer16kHzChannel)); | 40 static_cast<int>(kSamplesPer16kHzChannel)); |
67 } | 41 } |
68 return num_bands; | 42 return num_bands; |
(...skipping 15 matching lines...) Expand all Loading... |
84 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), | 58 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), |
85 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), | 59 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), |
86 mixed_low_pass_valid_(false), | 60 mixed_low_pass_valid_(false), |
87 reference_copied_(false), | 61 reference_copied_(false), |
88 activity_(AudioFrame::kVadUnknown), | 62 activity_(AudioFrame::kVadUnknown), |
89 keyboard_data_(NULL), | 63 keyboard_data_(NULL), |
90 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { | 64 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { |
91 assert(input_num_frames_ > 0); | 65 assert(input_num_frames_ > 0); |
92 assert(proc_num_frames_ > 0); | 66 assert(proc_num_frames_ > 0); |
93 assert(output_num_frames_ > 0); | 67 assert(output_num_frames_ > 0); |
94 assert(num_input_channels_ > 0 && num_input_channels_ <= 2); | 68 assert(num_input_channels_ > 0); |
95 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_); | 69 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_); |
96 | 70 |
97 if (input_num_frames_ != proc_num_frames_ || | 71 if (input_num_frames_ != proc_num_frames_ || |
98 output_num_frames_ != proc_num_frames_) { | 72 output_num_frames_ != proc_num_frames_) { |
99 // Create an intermediate buffer for resampling. | 73 // Create an intermediate buffer for resampling. |
100 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, | 74 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, |
101 num_proc_channels_)); | 75 num_proc_channels_)); |
102 | 76 |
103 if (input_num_frames_ != proc_num_frames_) { | 77 if (input_num_frames_ != proc_num_frames_) { |
104 for (int i = 0; i < num_proc_channels_; ++i) { | 78 for (int i = 0; i < num_proc_channels_; ++i) { |
(...skipping 18 matching lines...) Expand all Loading... |
123 num_bands_)); | 97 num_bands_)); |
124 splitting_filter_.reset(new SplittingFilter(num_proc_channels_, | 98 splitting_filter_.reset(new SplittingFilter(num_proc_channels_, |
125 num_bands_, | 99 num_bands_, |
126 proc_num_frames_)); | 100 proc_num_frames_)); |
127 } | 101 } |
128 } | 102 } |
129 | 103 |
130 AudioBuffer::~AudioBuffer() {} | 104 AudioBuffer::~AudioBuffer() {} |
131 | 105 |
132 void AudioBuffer::CopyFrom(const float* const* data, | 106 void AudioBuffer::CopyFrom(const float* const* data, |
133 int num_frames, | 107 const StreamConfig& stream_config) { |
134 AudioProcessing::ChannelLayout layout) { | 108 assert(stream_config.num_frames() == input_num_frames_); |
135 assert(num_frames == input_num_frames_); | 109 assert(stream_config.num_channels() == num_input_channels_); |
136 assert(ChannelsFromLayout(layout) == num_input_channels_); | |
137 InitForNewData(); | 110 InitForNewData(); |
138 // Initialized lazily because there's a different condition in | 111 // Initialized lazily because there's a different condition in |
139 // DeinterleaveFrom. | 112 // DeinterleaveFrom. |
140 if ((num_input_channels_ == 2 && num_proc_channels_ == 1) && !input_buffer_) { | 113 const bool need_to_downmix = |
| 114 num_input_channels_ > 1 && num_proc_channels_ == 1; |
| 115 if (need_to_downmix && !input_buffer_) { |
141 input_buffer_.reset( | 116 input_buffer_.reset( |
142 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); | 117 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
143 } | 118 } |
144 | 119 |
145 if (HasKeyboardChannel(layout)) { | 120 if (stream_config.has_keyboard()) { |
146 keyboard_data_ = data[KeyboardChannelIndex(layout)]; | 121 keyboard_data_ = data[KeyboardChannelIndex(stream_config)]; |
147 } | 122 } |
148 | 123 |
149 // Downmix. | 124 // Downmix. |
150 const float* const* data_ptr = data; | 125 const float* const* data_ptr = data; |
151 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { | 126 if (need_to_downmix) { |
152 StereoToMono(data[0], | 127 DownmixToMono<float, float>(data, input_num_frames_, num_input_channels_, |
153 data[1], | 128 input_buffer_->fbuf()->channels()[0]); |
154 input_buffer_->fbuf()->channels()[0], | |
155 input_num_frames_); | |
156 data_ptr = input_buffer_->fbuf_const()->channels(); | 129 data_ptr = input_buffer_->fbuf_const()->channels(); |
157 } | 130 } |
158 | 131 |
159 // Resample. | 132 // Resample. |
160 if (input_num_frames_ != proc_num_frames_) { | 133 if (input_num_frames_ != proc_num_frames_) { |
161 for (int i = 0; i < num_proc_channels_; ++i) { | 134 for (int i = 0; i < num_proc_channels_; ++i) { |
162 input_resamplers_[i]->Resample(data_ptr[i], | 135 input_resamplers_[i]->Resample(data_ptr[i], |
163 input_num_frames_, | 136 input_num_frames_, |
164 process_buffer_->channels()[i], | 137 process_buffer_->channels()[i], |
165 proc_num_frames_); | 138 proc_num_frames_); |
166 } | 139 } |
167 data_ptr = process_buffer_->channels(); | 140 data_ptr = process_buffer_->channels(); |
168 } | 141 } |
169 | 142 |
170 // Convert to the S16 range. | 143 // Convert to the S16 range. |
171 for (int i = 0; i < num_proc_channels_; ++i) { | 144 for (int i = 0; i < num_proc_channels_; ++i) { |
172 FloatToFloatS16(data_ptr[i], | 145 FloatToFloatS16(data_ptr[i], |
173 proc_num_frames_, | 146 proc_num_frames_, |
174 data_->fbuf()->channels()[i]); | 147 data_->fbuf()->channels()[i]); |
175 } | 148 } |
176 } | 149 } |
177 | 150 |
178 void AudioBuffer::CopyTo(int num_frames, | 151 void AudioBuffer::CopyTo(const StreamConfig& stream_config, |
179 AudioProcessing::ChannelLayout layout, | |
180 float* const* data) { | 152 float* const* data) { |
181 assert(num_frames == output_num_frames_); | 153 assert(stream_config.num_frames() == output_num_frames_); |
182 assert(ChannelsFromLayout(layout) == num_channels_); | 154 assert(stream_config.num_channels() == num_channels_); |
183 | 155 |
184 // Convert to the float range. | 156 // Convert to the float range. |
185 float* const* data_ptr = data; | 157 float* const* data_ptr = data; |
186 if (output_num_frames_ != proc_num_frames_) { | 158 if (output_num_frames_ != proc_num_frames_) { |
187 // Convert to an intermediate buffer for subsequent resampling. | 159 // Convert to an intermediate buffer for subsequent resampling. |
188 data_ptr = process_buffer_->channels(); | 160 data_ptr = process_buffer_->channels(); |
189 } | 161 } |
190 for (int i = 0; i < num_channels_; ++i) { | 162 for (int i = 0; i < num_channels_; ++i) { |
191 FloatS16ToFloat(data_->fbuf()->channels()[i], | 163 FloatS16ToFloat(data_->fbuf()->channels()[i], |
192 proc_num_frames_, | 164 proc_num_frames_, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 ChannelBuffer<float>* AudioBuffer::split_data_f() { | 292 ChannelBuffer<float>* AudioBuffer::split_data_f() { |
321 mixed_low_pass_valid_ = false; | 293 mixed_low_pass_valid_ = false; |
322 return split_data_.get() ? split_data_->fbuf() : data_->fbuf(); | 294 return split_data_.get() ? split_data_->fbuf() : data_->fbuf(); |
323 } | 295 } |
324 | 296 |
325 const ChannelBuffer<float>* AudioBuffer::split_data_f() const { | 297 const ChannelBuffer<float>* AudioBuffer::split_data_f() const { |
326 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const(); | 298 return split_data_.get() ? split_data_->fbuf_const() : data_->fbuf_const(); |
327 } | 299 } |
328 | 300 |
329 const int16_t* AudioBuffer::mixed_low_pass_data() { | 301 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) { | 302 if (num_proc_channels_ == 1) { |
334 return split_bands_const(0)[kBand0To8kHz]; | 303 return split_bands_const(0)[kBand0To8kHz]; |
335 } | 304 } |
336 | 305 |
337 if (!mixed_low_pass_valid_) { | 306 if (!mixed_low_pass_valid_) { |
338 if (!mixed_low_pass_channels_.get()) { | 307 if (!mixed_low_pass_channels_.get()) { |
339 mixed_low_pass_channels_.reset( | 308 mixed_low_pass_channels_.reset( |
340 new ChannelBuffer<int16_t>(num_split_frames_, 1)); | 309 new ChannelBuffer<int16_t>(num_split_frames_, 1)); |
341 } | 310 } |
342 StereoToMono(split_bands_const(0)[kBand0To8kHz], | 311 |
343 split_bands_const(1)[kBand0To8kHz], | 312 DownmixToMono<int16_t, int32_t>(split_channels_const(kBand0To8kHz), |
344 mixed_low_pass_channels_->channels()[0], | 313 num_split_frames_, num_channels_, |
345 num_split_frames_); | 314 mixed_low_pass_channels_->channels()[0]); |
346 mixed_low_pass_valid_ = true; | 315 mixed_low_pass_valid_ = true; |
347 } | 316 } |
348 return mixed_low_pass_channels_->channels()[0]; | 317 return mixed_low_pass_channels_->channels()[0]; |
349 } | 318 } |
350 | 319 |
351 const int16_t* AudioBuffer::low_pass_reference(int channel) const { | 320 const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
352 if (!reference_copied_) { | 321 if (!reference_copied_) { |
353 return NULL; | 322 return NULL; |
354 } | 323 } |
355 | 324 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); | 373 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
405 } | 374 } |
406 activity_ = frame->vad_activity_; | 375 activity_ = frame->vad_activity_; |
407 | 376 |
408 int16_t* const* deinterleaved; | 377 int16_t* const* deinterleaved; |
409 if (input_num_frames_ == proc_num_frames_) { | 378 if (input_num_frames_ == proc_num_frames_) { |
410 deinterleaved = data_->ibuf()->channels(); | 379 deinterleaved = data_->ibuf()->channels(); |
411 } else { | 380 } else { |
412 deinterleaved = input_buffer_->ibuf()->channels(); | 381 deinterleaved = input_buffer_->ibuf()->channels(); |
413 } | 382 } |
414 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { | 383 if (num_proc_channels_ == 1) { |
415 // Downmix directly; no explicit deinterleaving needed. | 384 // Downmix and deinterleave simultaneously. |
416 for (int i = 0; i < input_num_frames_; ++i) { | 385 DownmixInterleavedToMono(frame->data_, input_num_frames_, |
417 deinterleaved[0][i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2; | 386 num_input_channels_, deinterleaved[0]); |
418 } | |
419 } else { | 387 } else { |
420 assert(num_proc_channels_ == num_input_channels_); | 388 assert(num_proc_channels_ == num_input_channels_); |
421 Deinterleave(frame->data_, | 389 Deinterleave(frame->data_, |
422 input_num_frames_, | 390 input_num_frames_, |
423 num_proc_channels_, | 391 num_proc_channels_, |
424 deinterleaved); | 392 deinterleaved); |
425 } | 393 } |
426 | 394 |
427 // Resample. | 395 // Resample. |
428 if (input_num_frames_ != proc_num_frames_) { | 396 if (input_num_frames_ != proc_num_frames_) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 | 438 |
471 void AudioBuffer::SplitIntoFrequencyBands() { | 439 void AudioBuffer::SplitIntoFrequencyBands() { |
472 splitting_filter_->Analysis(data_.get(), split_data_.get()); | 440 splitting_filter_->Analysis(data_.get(), split_data_.get()); |
473 } | 441 } |
474 | 442 |
475 void AudioBuffer::MergeFrequencyBands() { | 443 void AudioBuffer::MergeFrequencyBands() { |
476 splitting_filter_->Synthesis(split_data_.get(), data_.get()); | 444 splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
477 } | 445 } |
478 | 446 |
479 } // namespace webrtc | 447 } // namespace webrtc |
OLD | NEW |