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