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