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