blob: 6161a8f91bf294fc4ccc52df4fc23bfadb3e6c89 [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <assert.h>
14
15#include "api/array_view.h"
16#include "modules/audio_coding/neteq/audio_multi_vector.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017
18namespace webrtc {
19
Henrik Lundincf808d22015-05-27 14:33:29 +020020Accelerate::ReturnCodes Accelerate::Process(const int16_t* input,
21 size_t input_length,
22 bool fast_accelerate,
23 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 // Input length must be (almost) 30 ms.
Peter Kastingdce40cf2015-08-24 14:52:23 -070026 static const size_t k15ms = 120; // 15 ms = 120 samples at 8 kHz sample rate.
27 if (num_channels_ == 0 ||
28 input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029 // Length of input data too short to do accelerate. Simply move all data
30 // from input to output.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020031 output->PushBackInterleaved(
32 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033 return kError;
34 }
Henrik Lundincf808d22015-05-27 14:33:29 +020035 return TimeStretch::Process(input, input_length, fast_accelerate, output,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036 length_change_samples);
37}
38
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000039void Accelerate::SetParametersForPassiveSpeech(size_t /*len*/,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040 int16_t* best_correlation,
Peter Kastingdce40cf2015-08-24 14:52:23 -070041 size_t* /*peak_index*/) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042 // When the signal does not contain any active speech, the correlation does
43 // not matter. Simply set it to zero.
44 *best_correlation = 0;
45}
46
47Accelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch(
Henrik Lundincf808d22015-05-27 14:33:29 +020048 const int16_t* input,
49 size_t input_length,
50 size_t peak_index,
51 int16_t best_correlation,
52 bool active_speech,
53 bool fast_mode,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000054 AudioMultiVector* output) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055 // Check for strong correlation or passive speech.
Henrik Lundincf808d22015-05-27 14:33:29 +020056 // Use 8192 (0.5 in Q14) in fast mode.
57 const int correlation_threshold = fast_mode ? 8192 : kCorrelationThreshold;
58 if ((best_correlation > correlation_threshold) || !active_speech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 // Do accelerate operation by overlap add.
60
61 // Pre-calculate common multiplication with |fs_mult_|.
62 // 120 corresponds to 15 ms.
63 size_t fs_mult_120 = fs_mult_ * 120;
64
Henrik Lundincf808d22015-05-27 14:33:29 +020065 if (fast_mode) {
66 // Fit as many multiples of |peak_index| as possible in fs_mult_120.
67 // TODO(henrik.lundin) Consider finding multiple correlation peaks and
68 // pick the one with the longest correlation lag in this case.
69 peak_index = (fs_mult_120 / peak_index) * peak_index;
70 }
71
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072 assert(fs_mult_120 >= peak_index); // Should be handled in Process().
73 // Copy first part; 0 to 15 ms.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020074 output->PushBackInterleaved(
75 rtc::ArrayView<const int16_t>(input, fs_mult_120 * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076 // Copy the |peak_index| starting at 15 ms to |temp_vector|.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000077 AudioMultiVector temp_vector(num_channels_);
Henrik Lundin00eb12a2018-09-05 18:14:52 +020078 temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
79 &input[fs_mult_120 * num_channels_], peak_index * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 // Cross-fade |temp_vector| onto the end of |output|.
81 output->CrossFade(temp_vector, peak_index);
82 // Copy the last unmodified part, 15 ms + pitch period until the end.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020083 output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 &input[(fs_mult_120 + peak_index) * num_channels_],
Henrik Lundin00eb12a2018-09-05 18:14:52 +020085 input_length - (fs_mult_120 + peak_index) * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086
87 if (active_speech) {
88 return kSuccess;
89 } else {
90 return kSuccessLowEnergy;
91 }
92 } else {
93 // Accelerate not allowed. Simply move all data from decoded to outData.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020094 output->PushBackInterleaved(
95 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000096 return kNoStretch;
97 }
98}
99
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +0000100Accelerate* AccelerateFactory::Create(
101 int sample_rate_hz,
102 size_t num_channels,
103 const BackgroundNoise& background_noise) const {
104 return new Accelerate(sample_rate_hz, num_channels, background_noise);
105}
106
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107} // namespace webrtc