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 <type_traits> | |
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 switch (stream_config.num_channels()) { |
28 case AudioProcessing::kMono: | 30 case 1: |
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 | |
39 int 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; | 31 return 1; |
47 case AudioProcessing::kStereoAndKeyboard: | 32 case 2: |
48 return 2; | 33 return 2; |
49 } | 34 } |
50 assert(false); | 35 assert(false); |
51 return -1; | 36 return -1; |
52 } | 37 } |
53 | 38 |
54 template <typename T> | 39 template <typename T> |
55 void StereoToMono(const T* left, const T* right, T* out, | 40 void DownmixInterleavedToMono(const T* interleaved, |
56 int num_frames) { | 41 T* deinterleaved, |
57 for (int i = 0; i < num_frames; ++i) | 42 int num_multichannel_frames, |
58 out[i] = (left[i] + right[i]) / 2; | 43 int num_channels) { |
44 return DownmixInterleavedToMonoImpl<T, T>( | |
45 interleaved, deinterleaved, num_multichannel_frames, num_channels); | |
46 } | |
47 | |
48 template <> | |
49 void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved, | |
50 int16_t* deinterleaved, | |
51 int num_multichannel_frames, | |
52 int num_channels) { | |
53 return DownmixInterleavedToMonoImpl<int16_t, int32_t>( | |
54 interleaved, deinterleaved, num_multichannel_frames, num_channels); | |
59 } | 55 } |
60 | 56 |
61 int NumBandsFromSamplesPerChannel(int num_frames) { | 57 int NumBandsFromSamplesPerChannel(int num_frames) { |
62 int num_bands = 1; | 58 int num_bands = 1; |
63 if (num_frames == kSamplesPer32kHzChannel || | 59 if (num_frames == kSamplesPer32kHzChannel || |
64 num_frames == kSamplesPer48kHzChannel) { | 60 num_frames == kSamplesPer48kHzChannel) { |
65 num_bands = rtc::CheckedDivExact(num_frames, | 61 num_bands = rtc::CheckedDivExact(num_frames, |
66 static_cast<int>(kSamplesPer16kHzChannel)); | 62 static_cast<int>(kSamplesPer16kHzChannel)); |
67 } | 63 } |
68 return num_bands; | 64 return num_bands; |
(...skipping 15 matching lines...) Expand all Loading... | |
84 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), | 80 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), |
85 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), | 81 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), |
86 mixed_low_pass_valid_(false), | 82 mixed_low_pass_valid_(false), |
87 reference_copied_(false), | 83 reference_copied_(false), |
88 activity_(AudioFrame::kVadUnknown), | 84 activity_(AudioFrame::kVadUnknown), |
89 keyboard_data_(NULL), | 85 keyboard_data_(NULL), |
90 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { | 86 data_(new IFChannelBuffer(proc_num_frames_, num_proc_channels_)) { |
91 assert(input_num_frames_ > 0); | 87 assert(input_num_frames_ > 0); |
92 assert(proc_num_frames_ > 0); | 88 assert(proc_num_frames_ > 0); |
93 assert(output_num_frames_ > 0); | 89 assert(output_num_frames_ > 0); |
94 assert(num_input_channels_ > 0 && num_input_channels_ <= 2); | 90 assert(num_input_channels_ > 0); |
95 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_); | 91 assert(num_proc_channels_ > 0 && num_proc_channels_ <= num_input_channels_); |
96 | 92 |
97 if (input_num_frames_ != proc_num_frames_ || | 93 if (input_num_frames_ != proc_num_frames_ || |
98 output_num_frames_ != proc_num_frames_) { | 94 output_num_frames_ != proc_num_frames_) { |
99 // Create an intermediate buffer for resampling. | 95 // Create an intermediate buffer for resampling. |
100 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, | 96 process_buffer_.reset(new ChannelBuffer<float>(proc_num_frames_, |
101 num_proc_channels_)); | 97 num_proc_channels_)); |
102 | 98 |
103 if (input_num_frames_ != proc_num_frames_) { | 99 if (input_num_frames_ != proc_num_frames_) { |
104 for (int i = 0; i < num_proc_channels_; ++i) { | 100 for (int i = 0; i < num_proc_channels_; ++i) { |
(...skipping 18 matching lines...) Expand all Loading... | |
123 num_bands_)); | 119 num_bands_)); |
124 splitting_filter_.reset(new SplittingFilter(num_proc_channels_, | 120 splitting_filter_.reset(new SplittingFilter(num_proc_channels_, |
125 num_bands_, | 121 num_bands_, |
126 proc_num_frames_)); | 122 proc_num_frames_)); |
127 } | 123 } |
128 } | 124 } |
129 | 125 |
130 AudioBuffer::~AudioBuffer() {} | 126 AudioBuffer::~AudioBuffer() {} |
131 | 127 |
132 void AudioBuffer::CopyFrom(const float* const* data, | 128 void AudioBuffer::CopyFrom(const float* const* data, |
133 int num_frames, | 129 const StreamConfig& stream_config) { |
134 AudioProcessing::ChannelLayout layout) { | 130 assert(stream_config.samples_per_channel() == input_num_frames_); |
135 assert(num_frames == input_num_frames_); | 131 assert(stream_config.num_channels() == num_input_channels_); |
136 assert(ChannelsFromLayout(layout) == num_input_channels_); | |
137 InitForNewData(); | 132 InitForNewData(); |
138 // Initialized lazily because there's a different condition in | 133 // Initialized lazily because there's a different condition in |
139 // DeinterleaveFrom. | 134 // DeinterleaveFrom. |
140 if ((num_input_channels_ == 2 && num_proc_channels_ == 1) && !input_buffer_) { | 135 const bool need_to_downmix = |
136 num_input_channels_ > 1 && num_proc_channels_ == 1; | |
137 if (need_to_downmix && !input_buffer_) { | |
141 input_buffer_.reset( | 138 input_buffer_.reset( |
142 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); | 139 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
143 } | 140 } |
144 | 141 |
145 if (HasKeyboardChannel(layout)) { | 142 if (stream_config.has_keyboard()) { |
146 keyboard_data_ = data[KeyboardChannelIndex(layout)]; | 143 keyboard_data_ = data[KeyboardChannelIndex(stream_config)]; |
147 } | 144 } |
148 | 145 |
149 // Downmix. | 146 // Downmix. |
150 const float* const* data_ptr = data; | 147 const float* const* data_ptr = data; |
151 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { | 148 if (need_to_downmix) { |
152 StereoToMono(data[0], | 149 DownmixToMono<float, float>(input_num_frames_, |
153 data[1], | 150 input_buffer_->fbuf()->channels()[0], data, |
154 input_buffer_->fbuf()->channels()[0], | 151 num_input_channels_); |
155 input_num_frames_); | |
156 data_ptr = input_buffer_->fbuf_const()->channels(); | 152 data_ptr = input_buffer_->fbuf_const()->channels(); |
157 } | 153 } |
158 | 154 |
159 // Resample. | 155 // Resample. |
160 if (input_num_frames_ != proc_num_frames_) { | 156 if (input_num_frames_ != proc_num_frames_) { |
161 for (int i = 0; i < num_proc_channels_; ++i) { | 157 for (int i = 0; i < num_proc_channels_; ++i) { |
162 input_resamplers_[i]->Resample(data_ptr[i], | 158 input_resamplers_[i]->Resample(data_ptr[i], |
163 input_num_frames_, | 159 input_num_frames_, |
164 process_buffer_->channels()[i], | 160 process_buffer_->channels()[i], |
165 proc_num_frames_); | 161 proc_num_frames_); |
166 } | 162 } |
167 data_ptr = process_buffer_->channels(); | 163 data_ptr = process_buffer_->channels(); |
168 } | 164 } |
169 | 165 |
170 // Convert to the S16 range. | 166 // Convert to the S16 range. |
171 for (int i = 0; i < num_proc_channels_; ++i) { | 167 for (int i = 0; i < num_proc_channels_; ++i) { |
172 FloatToFloatS16(data_ptr[i], | 168 FloatToFloatS16(data_ptr[i], |
173 proc_num_frames_, | 169 proc_num_frames_, |
174 data_->fbuf()->channels()[i]); | 170 data_->fbuf()->channels()[i]); |
175 } | 171 } |
176 } | 172 } |
177 | 173 |
178 void AudioBuffer::CopyTo(int num_frames, | 174 void AudioBuffer::CopyTo(const StreamConfig& stream_config, |
179 AudioProcessing::ChannelLayout layout, | |
180 float* const* data) { | 175 float* const* data) { |
181 assert(num_frames == output_num_frames_); | 176 assert(stream_config.samples_per_channel() == output_num_frames_); |
182 assert(ChannelsFromLayout(layout) == num_channels_); | 177 assert(stream_config.num_channels() == num_channels_); |
183 | 178 |
184 // Convert to the float range. | 179 // Convert to the float range. |
185 float* const* data_ptr = data; | 180 float* const* data_ptr = data; |
186 if (output_num_frames_ != proc_num_frames_) { | 181 if (output_num_frames_ != proc_num_frames_) { |
187 // Convert to an intermediate buffer for subsequent resampling. | 182 // Convert to an intermediate buffer for subsequent resampling. |
188 data_ptr = process_buffer_->channels(); | 183 data_ptr = process_buffer_->channels(); |
189 } | 184 } |
190 for (int i = 0; i < num_channels_; ++i) { | 185 for (int i = 0; i < num_channels_; ++i) { |
191 FloatS16ToFloat(data_->fbuf()->channels()[i], | 186 FloatS16ToFloat(data_->fbuf()->channels()[i], |
192 proc_num_frames_, | 187 proc_num_frames_, |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 | 327 |
333 if (num_proc_channels_ == 1) { | 328 if (num_proc_channels_ == 1) { |
334 return split_bands_const(0)[kBand0To8kHz]; | 329 return split_bands_const(0)[kBand0To8kHz]; |
335 } | 330 } |
336 | 331 |
337 if (!mixed_low_pass_valid_) { | 332 if (!mixed_low_pass_valid_) { |
338 if (!mixed_low_pass_channels_.get()) { | 333 if (!mixed_low_pass_channels_.get()) { |
339 mixed_low_pass_channels_.reset( | 334 mixed_low_pass_channels_.reset( |
340 new ChannelBuffer<int16_t>(num_split_frames_, 1)); | 335 new ChannelBuffer<int16_t>(num_split_frames_, 1)); |
341 } | 336 } |
342 StereoToMono(split_bands_const(0)[kBand0To8kHz], | 337 DownmixStereoToMono<int16_t, int32_t>( |
343 split_bands_const(1)[kBand0To8kHz], | 338 num_split_frames_, mixed_low_pass_channels_->channels()[0], |
344 mixed_low_pass_channels_->channels()[0], | 339 split_bands_const(0)[kBand0To8kHz], split_bands_const(1)[kBand0To8kHz]); |
345 num_split_frames_); | |
346 mixed_low_pass_valid_ = true; | 340 mixed_low_pass_valid_ = true; |
347 } | 341 } |
348 return mixed_low_pass_channels_->channels()[0]; | 342 return mixed_low_pass_channels_->channels()[0]; |
349 } | 343 } |
350 | 344 |
351 const int16_t* AudioBuffer::low_pass_reference(int channel) const { | 345 const int16_t* AudioBuffer::low_pass_reference(int channel) const { |
352 if (!reference_copied_) { | 346 if (!reference_copied_) { |
353 return NULL; | 347 return NULL; |
354 } | 348 } |
355 | 349 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); | 398 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
405 } | 399 } |
406 activity_ = frame->vad_activity_; | 400 activity_ = frame->vad_activity_; |
407 | 401 |
408 int16_t* const* deinterleaved; | 402 int16_t* const* deinterleaved; |
409 if (input_num_frames_ == proc_num_frames_) { | 403 if (input_num_frames_ == proc_num_frames_) { |
410 deinterleaved = data_->ibuf()->channels(); | 404 deinterleaved = data_->ibuf()->channels(); |
411 } else { | 405 } else { |
412 deinterleaved = input_buffer_->ibuf()->channels(); | 406 deinterleaved = input_buffer_->ibuf()->channels(); |
413 } | 407 } |
414 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { | 408 if (num_proc_channels_ == 1) { |
415 // Downmix directly; no explicit deinterleaving needed. | 409 // Downmix and deinterleave simultaneously. |
416 for (int i = 0; i < input_num_frames_; ++i) { | 410 DownmixInterleavedToMono(frame->data_, deinterleaved[0], input_num_frames_, |
mgraczyk
2015/07/10 00:33:36
This function works for any number of input channe
| |
417 deinterleaved[0][i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2; | 411 num_input_channels_); |
418 } | |
419 } else { | 412 } else { |
420 assert(num_proc_channels_ == num_input_channels_); | 413 assert(num_proc_channels_ == num_input_channels_); |
421 Deinterleave(frame->data_, | 414 Deinterleave(frame->data_, |
422 input_num_frames_, | 415 input_num_frames_, |
423 num_proc_channels_, | 416 num_proc_channels_, |
424 deinterleaved); | 417 deinterleaved); |
425 } | 418 } |
426 | 419 |
427 // Resample. | 420 // Resample. |
428 if (input_num_frames_ != proc_num_frames_) { | 421 if (input_num_frames_ != proc_num_frames_) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
470 | 463 |
471 void AudioBuffer::SplitIntoFrequencyBands() { | 464 void AudioBuffer::SplitIntoFrequencyBands() { |
472 splitting_filter_->Analysis(data_.get(), split_data_.get()); | 465 splitting_filter_->Analysis(data_.get(), split_data_.get()); |
473 } | 466 } |
474 | 467 |
475 void AudioBuffer::MergeFrequencyBands() { | 468 void AudioBuffer::MergeFrequencyBands() { |
476 splitting_filter_->Synthesis(split_data_.get(), data_.get()); | 469 splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
477 } | 470 } |
478 | 471 |
479 } // namespace webrtc | 472 } // namespace webrtc |
OLD | NEW |