blob: f4ef6cdccb5890ef16c07b4c821fdc11abf6d822 [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
14#include "api/array_view.h"
15#include "modules/audio_coding/neteq/audio_multi_vector.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
17namespace webrtc {
18
Henrik Lundincf808d22015-05-27 14:33:29 +020019Accelerate::ReturnCodes Accelerate::Process(const int16_t* input,
20 size_t input_length,
21 bool fast_accelerate,
22 AudioMultiVector* output,
Peter Kastingdce40cf2015-08-24 14:52:23 -070023 size_t* length_change_samples) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024 // Input length must be (almost) 30 ms.
Peter Kastingdce40cf2015-08-24 14:52:23 -070025 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.orgd94659d2013-01-29 12:09:21 +000028 // Length of input data too short to do accelerate. Simply move all data
29 // from input to output.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020030 output->PushBackInterleaved(
31 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 return kError;
33 }
Henrik Lundincf808d22015-05-27 14:33:29 +020034 return TimeStretch::Process(input, input_length, fast_accelerate, output,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035 length_change_samples);
36}
37
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000038void Accelerate::SetParametersForPassiveSpeech(size_t /*len*/,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 int16_t* best_correlation,
Peter Kastingdce40cf2015-08-24 14:52:23 -070040 size_t* /*peak_index*/) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041 // 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
46Accelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch(
Henrik Lundincf808d22015-05-27 14:33:29 +020047 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.orgfd11bbf2013-09-30 20:38:44 +000053 AudioMultiVector* output) const {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054 // Check for strong correlation or passive speech.
Henrik Lundincf808d22015-05-27 14:33:29 +020055 // 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.orgd94659d2013-01-29 12:09:21 +000058 // Do accelerate operation by overlap add.
59
Artem Titovd00ce742021-07-28 20:00:17 +020060 // Pre-calculate common multiplication with `fs_mult_`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 // 120 corresponds to 15 ms.
62 size_t fs_mult_120 = fs_mult_ * 120;
63
Henrik Lundincf808d22015-05-27 14:33:29 +020064 if (fast_mode) {
Artem Titovd00ce742021-07-28 20:00:17 +020065 // Fit as many multiples of `peak_index` as possible in fs_mult_120.
Henrik Lundincf808d22015-05-27 14:33:29 +020066 // 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 Bonadei25ab3222021-07-08 20:08:20 +020071 RTC_DCHECK_GE(fs_mult_120, peak_index); // Should be handled in Process().
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072 // Copy first part; 0 to 15 ms.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020073 output->PushBackInterleaved(
74 rtc::ArrayView<const int16_t>(input, fs_mult_120 * num_channels_));
Artem Titovd00ce742021-07-28 20:00:17 +020075 // Copy the `peak_index` starting at 15 ms to `temp_vector`.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000076 AudioMultiVector temp_vector(num_channels_);
Henrik Lundin00eb12a2018-09-05 18:14:52 +020077 temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
78 &input[fs_mult_120 * num_channels_], peak_index * num_channels_));
Artem Titovd00ce742021-07-28 20:00:17 +020079 // Cross-fade `temp_vector` onto the end of `output`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 output->CrossFade(temp_vector, peak_index);
81 // Copy the last unmodified part, 15 ms + pitch period until the end.
Henrik Lundin00eb12a2018-09-05 18:14:52 +020082 output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083 &input[(fs_mult_120 + peak_index) * num_channels_],
Henrik Lundin00eb12a2018-09-05 18:14:52 +020084 input_length - (fs_mult_120 + peak_index) * num_channels_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000085
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 Lundin00eb12a2018-09-05 18:14:52 +020093 output->PushBackInterleaved(
94 rtc::ArrayView<const int16_t>(input, input_length));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095 return kNoStretch;
96 }
97}
98
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000099Accelerate* 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.orgd94659d2013-01-29 12:09:21 +0000106} // namespace webrtc