blob: 6159a9cb15d70e67086dff1f7ef0c025c751f393 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/preemptive_expand.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
13#include <algorithm> // min, max
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "common_audio/signal_processing/include/signal_processing_library.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
17namespace webrtc {
18
19PreemptiveExpand::ReturnCodes PreemptiveExpand::Process(
20 const int16_t* input,
Peter Kastingdce40cf2015-08-24 14:52:23 -070021 size_t input_length,
22 size_t old_data_length,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000023 AudioMultiVector* output,
Peter Kastingdce40cf2015-08-24 14:52:23 -070024 size_t* length_change_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025 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 Kastingdce40cf2015-08-24 14:52:23 -070028 static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029 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.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020034 output->PushBackInterleaved(
35 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036 return kError;
37 }
Henrik Lundincf808d22015-05-27 14:33:29 +020038 const bool kFastMode = false; // Fast mode is not available for PE Expand.
39 return TimeStretch::Process(input, input_length, kFastMode, output,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040 length_change_samples);
41}
42
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000043void PreemptiveExpand::SetParametersForPassiveSpeech(size_t len,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044 int16_t* best_correlation,
Peter Kastingdce40cf2015-08-24 14:52:23 -070045 size_t* peak_index) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046 // When the signal does not contain any active speech, the correlation does
47 // not matter. Simply set it to zero.
48 *best_correlation = 0;
49
50 // For low energy expansion, the new data can be less than 15 ms,
51 // but we must ensure that best_correlation is not larger than the length of
52 // the new data.
53 // but we must ensure that best_correlation is not larger than the new data.
Yves Gerey665174f2018-06-19 15:03:05 +020054 *peak_index = std::min(*peak_index, len - old_data_length_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055}
56
57PreemptiveExpand::ReturnCodes PreemptiveExpand::CheckCriteriaAndStretch(
Henrik Lundincf808d22015-05-27 14:33:29 +020058 const int16_t* input,
59 size_t input_length,
60 size_t peak_index,
61 int16_t best_correlation,
62 bool active_speech,
63 bool /*fast_mode*/,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000064 AudioMultiVector* output) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 // Pre-calculate common multiplication with |fs_mult_|.
66 // 120 corresponds to 15 ms.
Peter Kastingdce40cf2015-08-24 14:52:23 -070067 size_t fs_mult_120 = static_cast<size_t>(fs_mult_ * 120);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 // Check for strong correlation (>0.9 in Q14) and at least 15 ms new data,
69 // or passive speech.
70 if (((best_correlation > kCorrelationThreshold) &&
Yves Gerey665174f2018-06-19 15:03:05 +020071 (old_data_length_per_channel_ <= fs_mult_120)) ||
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072 !active_speech) {
73 // Do accelerate operation by overlap add.
74
75 // Set length of the first part, not to be modified.
Yves Gerey665174f2018-06-19 15:03:05 +020076 size_t unmodified_length =
77 std::max(old_data_length_per_channel_, fs_mult_120);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078 // Copy first part, including cross-fade region.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020079 output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
80 input, (unmodified_length + peak_index) * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 // Copy the last |peak_index| samples up to 15 ms to |temp_vector|.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000082 AudioMultiVector temp_vector(num_channels_);
Henrik Lundin00eb12a2018-09-05 18:14:52 +020083 temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 &input[(unmodified_length - peak_index) * num_channels_],
Henrik Lundin00eb12a2018-09-05 18:14:52 +020085 peak_index * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086 // Cross-fade |temp_vector| onto the end of |output|.
87 output->CrossFade(temp_vector, peak_index);
88 // Copy the last unmodified part, 15 ms + pitch period until the end.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020089 output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090 &input[unmodified_length * num_channels_],
Henrik Lundin00eb12a2018-09-05 18:14:52 +020091 input_length - unmodified_length * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092
93 if (active_speech) {
94 return kSuccess;
95 } else {
96 return kSuccessLowEnergy;
97 }
98 } else {
99 // Accelerate not allowed. Simply move all data from decoded to outData.
Henrik Lundin00eb12a2018-09-05 18:14:52 +0200100 output->PushBackInterleaved(
101 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102 return kNoStretch;
103 }
104}
105
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000106PreemptiveExpand* PreemptiveExpandFactory::Create(
107 int sample_rate_hz,
108 size_t num_channels,
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000109 const BackgroundNoise& background_noise,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700110 size_t overlap_samples) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200111 return new PreemptiveExpand(sample_rate_hz, num_channels, background_noise,
112 overlap_samples);
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000113}
114
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115} // namespace webrtc