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 
11 #include "modules/audio_coding/neteq/accelerate.h"
12 
13 #include <assert.h>
14 
15 #include "api/array_view.h"
16 #include "modules/audio_coding/neteq/audio_multi_vector.h"
17 
18 namespace webrtc {
19 
Process(const int16_t * input,size_t input_length,bool fast_accelerate,AudioMultiVector * output,size_t * length_change_samples)20 Accelerate::ReturnCodes Accelerate::Process(const int16_t* input,
21                                             size_t input_length,
22                                             bool fast_accelerate,
23                                             AudioMultiVector* output,
24                                             size_t* length_change_samples) {
25   // Input length must be (almost) 30 ms.
26   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_) {
29     // Length of input data too short to do accelerate. Simply move all data
30     // from input to output.
31     output->PushBackInterleaved(
32         rtc::ArrayView<const int16_t>(input, input_length));
33     return kError;
34   }
35   return TimeStretch::Process(input, input_length, fast_accelerate, output,
36                               length_change_samples);
37 }
38 
SetParametersForPassiveSpeech(size_t,int16_t * best_correlation,size_t *) const39 void Accelerate::SetParametersForPassiveSpeech(size_t /*len*/,
40                                                int16_t* best_correlation,
41                                                size_t* /*peak_index*/) const {
42   // 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 
CheckCriteriaAndStretch(const int16_t * input,size_t input_length,size_t peak_index,int16_t best_correlation,bool active_speech,bool fast_mode,AudioMultiVector * output) const47 Accelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch(
48     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,
54     AudioMultiVector* output) const {
55   // Check for strong correlation or passive speech.
56   // 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) {
59     // 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 
65     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 
72     assert(fs_mult_120 >= peak_index);  // Should be handled in Process().
73     // Copy first part; 0 to 15 ms.
74     output->PushBackInterleaved(
75         rtc::ArrayView<const int16_t>(input, fs_mult_120 * num_channels_));
76     // Copy the |peak_index| starting at 15 ms to |temp_vector|.
77     AudioMultiVector temp_vector(num_channels_);
78     temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
79         &input[fs_mult_120 * num_channels_], peak_index * num_channels_));
80     // 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.
83     output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
84         &input[(fs_mult_120 + peak_index) * num_channels_],
85         input_length - (fs_mult_120 + peak_index) * num_channels_));
86 
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.
94     output->PushBackInterleaved(
95         rtc::ArrayView<const int16_t>(input, input_length));
96     return kNoStretch;
97   }
98 }
99 
Create(int sample_rate_hz,size_t num_channels,const BackgroundNoise & background_noise) const100 Accelerate* 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 
107 }  // namespace webrtc
108