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 size_t kSamplesPer16kHzChannel = 160; |
23 const int kSamplesPer32kHzChannel = 320; | 23 const size_t kSamplesPer32kHzChannel = 320; |
24 const int kSamplesPer48kHzChannel = 480; | 24 const size_t kSamplesPer48kHzChannel = 480; |
25 | 25 |
26 bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) { | 26 bool HasKeyboardChannel(AudioProcessing::ChannelLayout layout) { |
27 switch (layout) { | 27 switch (layout) { |
28 case AudioProcessing::kMono: | 28 case AudioProcessing::kMono: |
29 case AudioProcessing::kStereo: | 29 case AudioProcessing::kStereo: |
30 return false; | 30 return false; |
31 case AudioProcessing::kMonoAndKeyboard: | 31 case AudioProcessing::kMonoAndKeyboard: |
32 case AudioProcessing::kStereoAndKeyboard: | 32 case AudioProcessing::kStereoAndKeyboard: |
33 return true; | 33 return true; |
34 } | 34 } |
(...skipping 11 matching lines...) Expand all Loading... |
46 return 1; | 46 return 1; |
47 case AudioProcessing::kStereoAndKeyboard: | 47 case AudioProcessing::kStereoAndKeyboard: |
48 return 2; | 48 return 2; |
49 } | 49 } |
50 assert(false); | 50 assert(false); |
51 return -1; | 51 return -1; |
52 } | 52 } |
53 | 53 |
54 template <typename T> | 54 template <typename T> |
55 void StereoToMono(const T* left, const T* right, T* out, | 55 void StereoToMono(const T* left, const T* right, T* out, |
56 int num_frames) { | 56 size_t num_frames) { |
57 for (int i = 0; i < num_frames; ++i) | 57 for (size_t i = 0; i < num_frames; ++i) |
58 out[i] = (left[i] + right[i]) / 2; | 58 out[i] = (left[i] + right[i]) / 2; |
59 } | 59 } |
60 | 60 |
61 int NumBandsFromSamplesPerChannel(int num_frames) { | 61 size_t NumBandsFromSamplesPerChannel(size_t num_frames) { |
62 int num_bands = 1; | 62 size_t num_bands = 1; |
63 if (num_frames == kSamplesPer32kHzChannel || | 63 if (num_frames == kSamplesPer32kHzChannel || |
64 num_frames == kSamplesPer48kHzChannel) { | 64 num_frames == kSamplesPer48kHzChannel) { |
65 num_bands = rtc::CheckedDivExact(num_frames, | 65 num_bands = rtc::CheckedDivExact(num_frames, kSamplesPer16kHzChannel); |
66 static_cast<int>(kSamplesPer16kHzChannel)); | |
67 } | 66 } |
68 return num_bands; | 67 return num_bands; |
69 } | 68 } |
70 | 69 |
71 } // namespace | 70 } // namespace |
72 | 71 |
73 AudioBuffer::AudioBuffer(int input_num_frames, | 72 AudioBuffer::AudioBuffer(size_t input_num_frames, |
74 int num_input_channels, | 73 int num_input_channels, |
75 int process_num_frames, | 74 size_t process_num_frames, |
76 int num_process_channels, | 75 int num_process_channels, |
77 int output_num_frames) | 76 size_t output_num_frames) |
78 : input_num_frames_(input_num_frames), | 77 : input_num_frames_(input_num_frames), |
79 num_input_channels_(num_input_channels), | 78 num_input_channels_(num_input_channels), |
80 proc_num_frames_(process_num_frames), | 79 proc_num_frames_(process_num_frames), |
81 num_proc_channels_(num_process_channels), | 80 num_proc_channels_(num_process_channels), |
82 output_num_frames_(output_num_frames), | 81 output_num_frames_(output_num_frames), |
83 num_channels_(num_process_channels), | 82 num_channels_(num_process_channels), |
84 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), | 83 num_bands_(NumBandsFromSamplesPerChannel(proc_num_frames_)), |
85 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), | 84 num_split_frames_(rtc::CheckedDivExact(proc_num_frames_, num_bands_)), |
86 mixed_low_pass_valid_(false), | 85 mixed_low_pass_valid_(false), |
87 reference_copied_(false), | 86 reference_copied_(false), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 num_bands_)); | 122 num_bands_)); |
124 splitting_filter_.reset(new SplittingFilter(num_proc_channels_, | 123 splitting_filter_.reset(new SplittingFilter(num_proc_channels_, |
125 num_bands_, | 124 num_bands_, |
126 proc_num_frames_)); | 125 proc_num_frames_)); |
127 } | 126 } |
128 } | 127 } |
129 | 128 |
130 AudioBuffer::~AudioBuffer() {} | 129 AudioBuffer::~AudioBuffer() {} |
131 | 130 |
132 void AudioBuffer::CopyFrom(const float* const* data, | 131 void AudioBuffer::CopyFrom(const float* const* data, |
133 int num_frames, | 132 size_t num_frames, |
134 AudioProcessing::ChannelLayout layout) { | 133 AudioProcessing::ChannelLayout layout) { |
135 assert(num_frames == input_num_frames_); | 134 assert(num_frames == input_num_frames_); |
136 assert(ChannelsFromLayout(layout) == num_input_channels_); | 135 assert(ChannelsFromLayout(layout) == num_input_channels_); |
137 InitForNewData(); | 136 InitForNewData(); |
138 // Initialized lazily because there's a different condition in | 137 // Initialized lazily because there's a different condition in |
139 // DeinterleaveFrom. | 138 // DeinterleaveFrom. |
140 if ((num_input_channels_ == 2 && num_proc_channels_ == 1) && !input_buffer_) { | 139 if ((num_input_channels_ == 2 && num_proc_channels_ == 1) && !input_buffer_) { |
141 input_buffer_.reset( | 140 input_buffer_.reset( |
142 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); | 141 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
143 } | 142 } |
(...skipping 24 matching lines...) Expand all Loading... |
168 } | 167 } |
169 | 168 |
170 // Convert to the S16 range. | 169 // Convert to the S16 range. |
171 for (int i = 0; i < num_proc_channels_; ++i) { | 170 for (int i = 0; i < num_proc_channels_; ++i) { |
172 FloatToFloatS16(data_ptr[i], | 171 FloatToFloatS16(data_ptr[i], |
173 proc_num_frames_, | 172 proc_num_frames_, |
174 data_->fbuf()->channels()[i]); | 173 data_->fbuf()->channels()[i]); |
175 } | 174 } |
176 } | 175 } |
177 | 176 |
178 void AudioBuffer::CopyTo(int num_frames, | 177 void AudioBuffer::CopyTo(size_t num_frames, |
179 AudioProcessing::ChannelLayout layout, | 178 AudioProcessing::ChannelLayout layout, |
180 float* const* data) { | 179 float* const* data) { |
181 assert(num_frames == output_num_frames_); | 180 assert(num_frames == output_num_frames_); |
182 assert(ChannelsFromLayout(layout) == num_channels_); | 181 assert(ChannelsFromLayout(layout) == num_channels_); |
183 | 182 |
184 // Convert to the float range. | 183 // Convert to the float range. |
185 float* const* data_ptr = data; | 184 float* const* data_ptr = data; |
186 if (output_num_frames_ != proc_num_frames_) { | 185 if (output_num_frames_ != proc_num_frames_) { |
187 // Convert to an intermediate buffer for subsequent resampling. | 186 // Convert to an intermediate buffer for subsequent resampling. |
188 data_ptr = process_buffer_->channels(); | 187 data_ptr = process_buffer_->channels(); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 } | 368 } |
370 | 369 |
371 int AudioBuffer::num_channels() const { | 370 int AudioBuffer::num_channels() const { |
372 return num_channels_; | 371 return num_channels_; |
373 } | 372 } |
374 | 373 |
375 void AudioBuffer::set_num_channels(int num_channels) { | 374 void AudioBuffer::set_num_channels(int num_channels) { |
376 num_channels_ = num_channels; | 375 num_channels_ = num_channels; |
377 } | 376 } |
378 | 377 |
379 int AudioBuffer::num_frames() const { | 378 size_t AudioBuffer::num_frames() const { |
380 return proc_num_frames_; | 379 return proc_num_frames_; |
381 } | 380 } |
382 | 381 |
383 int AudioBuffer::num_frames_per_band() const { | 382 size_t AudioBuffer::num_frames_per_band() const { |
384 return num_split_frames_; | 383 return num_split_frames_; |
385 } | 384 } |
386 | 385 |
387 int AudioBuffer::num_keyboard_frames() const { | 386 size_t AudioBuffer::num_keyboard_frames() const { |
388 // We don't resample the keyboard channel. | 387 // We don't resample the keyboard channel. |
389 return input_num_frames_; | 388 return input_num_frames_; |
390 } | 389 } |
391 | 390 |
392 int AudioBuffer::num_bands() const { | 391 size_t AudioBuffer::num_bands() const { |
393 return num_bands_; | 392 return num_bands_; |
394 } | 393 } |
395 | 394 |
396 // The resampler is only for supporting 48kHz to 16kHz in the reverse stream. | 395 // The resampler is only for supporting 48kHz to 16kHz in the reverse stream. |
397 void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { | 396 void AudioBuffer::DeinterleaveFrom(AudioFrame* frame) { |
398 assert(frame->num_channels_ == num_input_channels_); | 397 assert(frame->num_channels_ == num_input_channels_); |
399 assert(frame->samples_per_channel_ == input_num_frames_); | 398 assert(frame->samples_per_channel_ == input_num_frames_); |
400 InitForNewData(); | 399 InitForNewData(); |
401 // Initialized lazily because there's a different condition in CopyFrom. | 400 // Initialized lazily because there's a different condition in CopyFrom. |
402 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) { | 401 if ((input_num_frames_ != proc_num_frames_) && !input_buffer_) { |
403 input_buffer_.reset( | 402 input_buffer_.reset( |
404 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); | 403 new IFChannelBuffer(input_num_frames_, num_proc_channels_)); |
405 } | 404 } |
406 activity_ = frame->vad_activity_; | 405 activity_ = frame->vad_activity_; |
407 | 406 |
408 int16_t* const* deinterleaved; | 407 int16_t* const* deinterleaved; |
409 if (input_num_frames_ == proc_num_frames_) { | 408 if (input_num_frames_ == proc_num_frames_) { |
410 deinterleaved = data_->ibuf()->channels(); | 409 deinterleaved = data_->ibuf()->channels(); |
411 } else { | 410 } else { |
412 deinterleaved = input_buffer_->ibuf()->channels(); | 411 deinterleaved = input_buffer_->ibuf()->channels(); |
413 } | 412 } |
414 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { | 413 if (num_input_channels_ == 2 && num_proc_channels_ == 1) { |
415 // Downmix directly; no explicit deinterleaving needed. | 414 // Downmix directly; no explicit deinterleaving needed. |
416 for (int i = 0; i < input_num_frames_; ++i) { | 415 for (size_t i = 0; i < input_num_frames_; ++i) { |
417 deinterleaved[0][i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2; | 416 deinterleaved[0][i] = (frame->data_[i * 2] + frame->data_[i * 2 + 1]) / 2; |
418 } | 417 } |
419 } else { | 418 } else { |
420 assert(num_proc_channels_ == num_input_channels_); | 419 assert(num_proc_channels_ == num_input_channels_); |
421 Deinterleave(frame->data_, | 420 Deinterleave(frame->data_, |
422 input_num_frames_, | 421 input_num_frames_, |
423 num_proc_channels_, | 422 num_proc_channels_, |
424 deinterleaved); | 423 deinterleaved); |
425 } | 424 } |
426 | 425 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 | 469 |
471 void AudioBuffer::SplitIntoFrequencyBands() { | 470 void AudioBuffer::SplitIntoFrequencyBands() { |
472 splitting_filter_->Analysis(data_.get(), split_data_.get()); | 471 splitting_filter_->Analysis(data_.get(), split_data_.get()); |
473 } | 472 } |
474 | 473 |
475 void AudioBuffer::MergeFrequencyBands() { | 474 void AudioBuffer::MergeFrequencyBands() { |
476 splitting_filter_->Synthesis(split_data_.get(), data_.get()); | 475 splitting_filter_->Synthesis(split_data_.get(), data_.get()); |
477 } | 476 } |
478 | 477 |
479 } // namespace webrtc | 478 } // namespace webrtc |
OLD | NEW |