blob: 18350b0a78971064654497bfda761ea1d30ffaf4 [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/accelerate.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "common_audio/signal_processing/include/signal_processing_library.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014
15namespace webrtc {
16
Henrik Lundincf808d22015-05-27 14:33:29 +020017Accelerate::ReturnCodes Accelerate::Process(const int16_t* input,
18 size_t input_length,
19 bool fast_accelerate,
20 AudioMultiVector* output,
Peter Kastingdce40cf2015-08-24 14:52:23 -070021 size_t* length_change_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022 // Input length must be (almost) 30 ms.
Peter Kastingdce40cf2015-08-24 14:52:23 -070023 static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate.
24 if (num_channels_ == 0 ||
25 input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026 // Length of input data too short to do accelerate. Simply move all data
27 // from input to output.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020028 output->PushBackInterleaved(
29 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030 return kError;
31 }
Henrik Lundincf808d22015-05-27 14:33:29 +020032 return TimeStretch::Process(input, input_length, fast_accelerate, output,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033 length_change_samples);
34}
35
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000036void Accelerate::SetParametersForPassiveSpeech(size_t /*len*/,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037 int16_t* best_correlation,
Peter Kastingdce40cf2015-08-24 14:52:23 -070038 size_t* /*peak_index*/) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 // When the signal does not contain any active speech, the correlation does
40 // not matter. Simply set it to zero.
41 *best_correlation = 0;
42}
43
44Accelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch(
Henrik Lundincf808d22015-05-27 14:33:29 +020045 const int16_t* input,
46 size_t input_length,
47 size_t peak_index,
48 int16_t best_correlation,
49 bool active_speech,
50 bool fast_mode,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000051 AudioMultiVector* output) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052 // Check for strong correlation or passive speech.
Henrik Lundincf808d22015-05-27 14:33:29 +020053 // Use 8192 (0.5 in Q14) in fast mode.
54 const int correlation_threshold = fast_mode ? 8192 : kCorrelationThreshold;
55 if ((best_correlation > correlation_threshold) || !active_speech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056 // Do accelerate operation by overlap add.
57
58 // Pre-calculate common multiplication with |fs_mult_|.
59 // 120 corresponds to 15 ms.
60 size_t fs_mult_120 = fs_mult_ * 120;
61
Henrik Lundincf808d22015-05-27 14:33:29 +020062 if (fast_mode) {
63 // Fit as many multiples of |peak_index| as possible in fs_mult_120.
64 // TODO(henrik.lundin) Consider finding multiple correlation peaks and
65 // pick the one with the longest correlation lag in this case.
66 peak_index = (fs_mult_120 / peak_index) * peak_index;
67 }
68
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 assert(fs_mult_120 >= peak_index); // Should be handled in Process().
70 // Copy first part; 0 to 15 ms.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020071 output->PushBackInterleaved(
72 rtc::ArrayView<const int16_t>(input, fs_mult_120 * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073 // Copy the |peak_index| starting at 15 ms to |temp_vector|.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000074 AudioMultiVector temp_vector(num_channels_);
Henrik Lundin00eb12a2018-09-05 18:14:52 +020075 temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
76 &input[fs_mult_120 * num_channels_], peak_index * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077 // Cross-fade |temp_vector| onto the end of |output|.
78 output->CrossFade(temp_vector, peak_index);
79 // Copy the last unmodified part, 15 ms + pitch period until the end.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020080 output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 &input[(fs_mult_120 + peak_index) * num_channels_],
Henrik Lundin00eb12a2018-09-05 18:14:52 +020082 input_length - (fs_mult_120 + peak_index) * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083
84 if (active_speech) {
85 return kSuccess;
86 } else {
87 return kSuccessLowEnergy;
88 }
89 } else {
90 // Accelerate not allowed. Simply move all data from decoded to outData.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020091 output->PushBackInterleaved(
92 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093 return kNoStretch;
94 }
95}
96
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000097Accelerate* AccelerateFactory::Create(
98 int sample_rate_hz,
99 size_t num_channels,
100 const BackgroundNoise& background_noise) const {
101 return new Accelerate(sample_rate_hz, num_channels, background_noise);
102}
103
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104} // namespace webrtc