preemptive_expand.cc (4714B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 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 11 #include "modules/audio_coding/neteq/preemptive_expand.h" 12 13 #include <algorithm> 14 #include <cstddef> 15 #include <cstdint> 16 17 #include "api/array_view.h" 18 #include "modules/audio_coding/neteq/audio_multi_vector.h" 19 #include "modules/audio_coding/neteq/time_stretch.h" 20 21 namespace webrtc { 22 23 PreemptiveExpand::ReturnCodes PreemptiveExpand::Process( 24 const int16_t* input, 25 size_t input_length, 26 size_t old_data_length, 27 AudioMultiVector* output, 28 size_t* length_change_samples) { 29 old_data_length_per_channel_ = old_data_length; 30 // Input length must be (almost) 30 ms. 31 // Also, the new part must be at least `overlap_samples_` elements. 32 static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate. 33 if (num_channels_ == 0 || 34 input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_ || 35 old_data_length >= input_length / num_channels_ - overlap_samples_) { 36 // Length of input data too short to do preemptive expand. Simply move all 37 // data from input to output. 38 output->PushBackInterleaved(ArrayView<const int16_t>(input, input_length)); 39 return kError; 40 } 41 const bool kFastMode = false; // Fast mode is not available for PE Expand. 42 return TimeStretch::Process(input, input_length, kFastMode, output, 43 length_change_samples); 44 } 45 46 void PreemptiveExpand::SetParametersForPassiveSpeech(size_t len, 47 int16_t* best_correlation, 48 size_t* peak_index) const { 49 // When the signal does not contain any active speech, the correlation does 50 // not matter. Simply set it to zero. 51 *best_correlation = 0; 52 53 // For low energy expansion, the new data can be less than 15 ms, 54 // but we must ensure that best_correlation is not larger than the length of 55 // the new data. 56 // but we must ensure that best_correlation is not larger than the new data. 57 *peak_index = std::min(*peak_index, len - old_data_length_per_channel_); 58 } 59 60 PreemptiveExpand::ReturnCodes PreemptiveExpand::CheckCriteriaAndStretch( 61 const int16_t* input, 62 size_t input_length, 63 size_t peak_index, 64 int16_t best_correlation, 65 bool active_speech, 66 bool /*fast_mode*/, 67 AudioMultiVector* output) const { 68 // Pre-calculate common multiplication with `fs_mult_`. 69 // 120 corresponds to 15 ms. 70 size_t fs_mult_120 = static_cast<size_t>(fs_mult_ * 120); 71 // Check for strong correlation (>0.9 in Q14) and at least 15 ms new data, 72 // or passive speech. 73 if (((best_correlation > kCorrelationThreshold) && 74 (old_data_length_per_channel_ <= fs_mult_120)) || 75 !active_speech) { 76 // Do accelerate operation by overlap add. 77 78 // Set length of the first part, not to be modified. 79 size_t unmodified_length = 80 std::max(old_data_length_per_channel_, fs_mult_120); 81 // Copy first part, including cross-fade region. 82 output->PushBackInterleaved(ArrayView<const int16_t>( 83 input, (unmodified_length + peak_index) * num_channels_)); 84 // Copy the last `peak_index` samples up to 15 ms to `temp_vector`. 85 AudioMultiVector temp_vector(num_channels_); 86 temp_vector.PushBackInterleaved(ArrayView<const int16_t>( 87 &input[(unmodified_length - peak_index) * num_channels_], 88 peak_index * num_channels_)); 89 // Cross-fade `temp_vector` onto the end of `output`. 90 output->CrossFade(temp_vector, peak_index); 91 // Copy the last unmodified part, 15 ms + pitch period until the end. 92 output->PushBackInterleaved(ArrayView<const int16_t>( 93 &input[unmodified_length * num_channels_], 94 input_length - unmodified_length * num_channels_)); 95 96 if (active_speech) { 97 return kSuccess; 98 } else { 99 return kSuccessLowEnergy; 100 } 101 } else { 102 // Accelerate not allowed. Simply move all data from decoded to outData. 103 output->PushBackInterleaved(ArrayView<const int16_t>(input, input_length)); 104 return kNoStretch; 105 } 106 } 107 108 PreemptiveExpand* PreemptiveExpandFactory::Create( 109 int sample_rate_hz, 110 size_t num_channels, 111 const BackgroundNoise& background_noise, 112 size_t overlap_samples) const { 113 return new PreemptiveExpand(sample_rate_hz, num_channels, background_noise, 114 overlap_samples); 115 } 116 117 } // namespace webrtc