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/preemptive_expand.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> // min, max |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | PreemptiveExpand::ReturnCodes PreemptiveExpand::Process( |
| 20 | const int16_t* input, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 21 | size_t input_length, |
| 22 | size_t old_data_length, |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 23 | AudioMultiVector* output, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 24 | size_t* length_change_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 28 | static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | } |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 37 | const bool kFastMode = false; // Fast mode is not available for PE Expand. |
| 38 | return TimeStretch::Process(input, input_length, kFastMode, output, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 39 | length_change_samples); |
| 40 | } |
| 41 | |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 42 | void PreemptiveExpand::SetParametersForPassiveSpeech(size_t len, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 43 | int16_t* best_correlation, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 44 | size_t* peak_index) const { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 45 | // When the signal does not contain any active speech, the correlation does |
| 46 | // not matter. Simply set it to zero. |
| 47 | *best_correlation = 0; |
| 48 | |
| 49 | // For low energy expansion, the new data can be less than 15 ms, |
| 50 | // but we must ensure that best_correlation is not larger than the length of |
| 51 | // the new data. |
| 52 | // but we must ensure that best_correlation is not larger than the new data. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 53 | *peak_index = std::min(*peak_index, len - old_data_length_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | PreemptiveExpand::ReturnCodes PreemptiveExpand::CheckCriteriaAndStretch( |
Henrik Lundin | cf808d2 | 2015-05-27 14:33:29 +0200 | [diff] [blame] | 57 | const int16_t* input, |
| 58 | size_t input_length, |
| 59 | size_t peak_index, |
| 60 | int16_t best_correlation, |
| 61 | bool active_speech, |
| 62 | bool /*fast_mode*/, |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 63 | AudioMultiVector* output) const { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | // Pre-calculate common multiplication with |fs_mult_|. |
| 65 | // 120 corresponds to 15 ms. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 66 | size_t fs_mult_120 = static_cast<size_t>(fs_mult_ * 120); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 67 | // Check for strong correlation (>0.9 in Q14) and at least 15 ms new data, |
| 68 | // or passive speech. |
| 69 | if (((best_correlation > kCorrelationThreshold) && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 70 | (old_data_length_per_channel_ <= fs_mult_120)) || |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 71 | !active_speech) { |
| 72 | // Do accelerate operation by overlap add. |
| 73 | |
| 74 | // Set length of the first part, not to be modified. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 75 | size_t unmodified_length = |
| 76 | std::max(old_data_length_per_channel_, fs_mult_120); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | // Copy first part, including cross-fade region. |
| 78 | output->PushBackInterleaved( |
| 79 | input, (unmodified_length + peak_index) * num_channels_); |
| 80 | // Copy the last |peak_index| samples up to 15 ms to |temp_vector|. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 81 | AudioMultiVector temp_vector(num_channels_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 82 | temp_vector.PushBackInterleaved( |
| 83 | &input[(unmodified_length - peak_index) * num_channels_], |
| 84 | peak_index * num_channels_); |
| 85 | // Cross-fade |temp_vector| onto the end of |output|. |
| 86 | output->CrossFade(temp_vector, peak_index); |
| 87 | // Copy the last unmodified part, 15 ms + pitch period until the end. |
| 88 | output->PushBackInterleaved( |
| 89 | &input[unmodified_length * num_channels_], |
| 90 | input_length - unmodified_length * num_channels_); |
| 91 | |
| 92 | if (active_speech) { |
| 93 | return kSuccess; |
| 94 | } else { |
| 95 | return kSuccessLowEnergy; |
| 96 | } |
| 97 | } else { |
| 98 | // Accelerate not allowed. Simply move all data from decoded to outData. |
| 99 | output->PushBackInterleaved(input, input_length); |
| 100 | return kNoStretch; |
| 101 | } |
| 102 | } |
| 103 | |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 104 | PreemptiveExpand* PreemptiveExpandFactory::Create( |
| 105 | int sample_rate_hz, |
| 106 | size_t num_channels, |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 107 | const BackgroundNoise& background_noise, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 108 | size_t overlap_samples) const { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 109 | return new PreemptiveExpand(sample_rate_hz, num_channels, background_noise, |
| 110 | overlap_samples); |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 111 | } |
| 112 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 113 | } // namespace webrtc |