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