henrik.lundin@webrtc.org | 9a40081 | 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 | |
| 11 | #include "webrtc/modules/audio_coding/neteq4/preemptive_expand.h" |
| 12 | |
| 13 | #include <algorithm> // min, max |
| 14 | |
| 15 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | PreemptiveExpand::ReturnCodes PreemptiveExpand::Process( |
| 20 | const int16_t* input, |
| 21 | int input_length, |
| 22 | int old_data_length, |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 23 | AudioMultiVector* output, |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | int16_t* length_change_samples) { |
| 25 | old_data_length_per_channel_ = old_data_length; |
| 26 | // Input length must be (almost) 30 ms. |
| 27 | // Also, the new part must be at least |overlap_samples_| elements. |
| 28 | static const int k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate. |
| 29 | if (num_channels_ == 0 || |
| 30 | input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_ || |
| 31 | old_data_length >= input_length / num_channels_ - overlap_samples_) { |
| 32 | // Length of input data too short to do preemptive expand. Simply move all |
| 33 | // data from input to output. |
| 34 | output->PushBackInterleaved(input, input_length); |
| 35 | return kError; |
| 36 | } |
| 37 | return TimeStretch::Process(input, input_length, output, |
| 38 | length_change_samples); |
| 39 | } |
| 40 | |
turaj@webrtc.org | 045e45e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 41 | void PreemptiveExpand::SetParametersForPassiveSpeech(size_t len, |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 42 | int16_t* best_correlation, |
| 43 | int* 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 | // For low energy expansion, the new data can be less than 15 ms, |
| 49 | // but we must ensure that best_correlation is not larger than the length of |
| 50 | // the new data. |
| 51 | // but we must ensure that best_correlation is not larger than the new data. |
turaj@webrtc.org | 045e45e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 52 | *peak_index = std::min(*peak_index, |
| 53 | static_cast<int>(len - old_data_length_per_channel_)); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | PreemptiveExpand::ReturnCodes PreemptiveExpand::CheckCriteriaAndStretch( |
turaj@webrtc.org | 045e45e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 57 | const int16_t *input, size_t input_length, size_t peak_index, |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 58 | int16_t best_correlation, bool active_speech, |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 59 | AudioMultiVector* output) const { |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | // Pre-calculate common multiplication with |fs_mult_|. |
| 61 | // 120 corresponds to 15 ms. |
| 62 | int fs_mult_120 = fs_mult_ * 120; |
| 63 | assert(old_data_length_per_channel_ >= 0); // Make sure it's been set. |
| 64 | // Check for strong correlation (>0.9 in Q14) and at least 15 ms new data, |
| 65 | // or passive speech. |
| 66 | if (((best_correlation > kCorrelationThreshold) && |
| 67 | (old_data_length_per_channel_ <= fs_mult_120)) || |
| 68 | !active_speech) { |
| 69 | // Do accelerate operation by overlap add. |
| 70 | |
| 71 | // Set length of the first part, not to be modified. |
| 72 | size_t unmodified_length = std::max(old_data_length_per_channel_, |
| 73 | fs_mult_120); |
| 74 | // Copy first part, including cross-fade region. |
| 75 | output->PushBackInterleaved( |
| 76 | input, (unmodified_length + peak_index) * num_channels_); |
| 77 | // Copy the last |peak_index| samples up to 15 ms to |temp_vector|. |
henrik.lundin@webrtc.org | 0e9c399 | 2013-09-30 20:38:44 +0000 | [diff] [blame^] | 78 | AudioMultiVector temp_vector(num_channels_); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 79 | temp_vector.PushBackInterleaved( |
| 80 | &input[(unmodified_length - peak_index) * num_channels_], |
| 81 | 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( |
| 86 | &input[unmodified_length * num_channels_], |
| 87 | input_length - unmodified_length * 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(input, input_length); |
| 97 | return kNoStretch; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } // namespace webrtc |