accelerate.cc (4318B)
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/accelerate.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 16 #include "api/array_view.h" 17 #include "modules/audio_coding/neteq/audio_multi_vector.h" 18 #include "modules/audio_coding/neteq/time_stretch.h" 19 #include "rtc_base/checks.h" 20 21 namespace webrtc { 22 23 Accelerate::ReturnCodes Accelerate::Process(const int16_t* input, 24 size_t input_length, 25 bool fast_accelerate, 26 AudioMultiVector* output, 27 size_t* length_change_samples) { 28 // Input length must be (almost) 30 ms. 29 static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate. 30 if (num_channels_ == 0 || 31 input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_) { 32 // Length of input data too short to do accelerate. Simply move all data 33 // from input to output. 34 output->PushBackInterleaved(ArrayView<const int16_t>(input, input_length)); 35 return kError; 36 } 37 return TimeStretch::Process(input, input_length, fast_accelerate, output, 38 length_change_samples); 39 } 40 41 void Accelerate::SetParametersForPassiveSpeech(size_t /*len*/, 42 int16_t* best_correlation, 43 size_t* /*peak_index*/) const { 44 // When the signal does not contain any active speech, the correlation does 45 // not matter. Simply set it to zero. 46 *best_correlation = 0; 47 } 48 49 Accelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch( 50 const int16_t* input, 51 size_t input_length, 52 size_t peak_index, 53 int16_t best_correlation, 54 bool active_speech, 55 bool fast_mode, 56 AudioMultiVector* output) const { 57 // Check for strong correlation or passive speech. 58 // Use 8192 (0.5 in Q14) in fast mode. 59 const int correlation_threshold = fast_mode ? 8192 : kCorrelationThreshold; 60 if ((best_correlation > correlation_threshold) || !active_speech) { 61 // Do accelerate operation by overlap add. 62 63 // Pre-calculate common multiplication with `fs_mult_`. 64 // 120 corresponds to 15 ms. 65 size_t fs_mult_120 = fs_mult_ * 120; 66 67 if (fast_mode) { 68 // Fit as many multiples of `peak_index` as possible in fs_mult_120. 69 // TODO(henrik.lundin) Consider finding multiple correlation peaks and 70 // pick the one with the longest correlation lag in this case. 71 peak_index = (fs_mult_120 / peak_index) * peak_index; 72 } 73 74 RTC_DCHECK_GE(fs_mult_120, peak_index); // Should be handled in Process(). 75 // Copy first part; 0 to 15 ms. 76 output->PushBackInterleaved( 77 ArrayView<const int16_t>(input, fs_mult_120 * num_channels_)); 78 // Copy the `peak_index` starting at 15 ms to `temp_vector`. 79 AudioMultiVector temp_vector(num_channels_); 80 temp_vector.PushBackInterleaved(ArrayView<const int16_t>( 81 &input[fs_mult_120 * num_channels_], peak_index * num_channels_)); 82 // Cross-fade `temp_vector` onto the end of `output`. 83 output->CrossFade(temp_vector, peak_index); 84 // Copy the last unmodified part, 15 ms + pitch period until the end. 85 output->PushBackInterleaved(ArrayView<const int16_t>( 86 &input[(fs_mult_120 + peak_index) * num_channels_], 87 input_length - (fs_mult_120 + peak_index) * num_channels_)); 88 89 if (active_speech) { 90 return kSuccess; 91 } else { 92 return kSuccessLowEnergy; 93 } 94 } else { 95 // Accelerate not allowed. Simply move all data from decoded to outData. 96 output->PushBackInterleaved(ArrayView<const int16_t>(input, input_length)); 97 return kNoStretch; 98 } 99 } 100 101 Accelerate* AccelerateFactory::Create( 102 int sample_rate_hz, 103 size_t num_channels, 104 const BackgroundNoise& background_noise) const { 105 return new Accelerate(sample_rate_hz, num_channels, background_noise); 106 } 107 108 } // namespace webrtc