OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_coding/neteq/time_stretch.h" | 11 #include "webrtc/modules/audio_coding/neteq/time_stretch.h" |
12 | 12 |
13 #include <algorithm> // min, max | 13 #include <algorithm> // min, max |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
16 #include "webrtc/base/safe_conversions.h" | 16 #include "webrtc/base/safe_conversions.h" |
17 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 17 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
18 #include "webrtc/modules/audio_coding/neteq/background_noise.h" | 18 #include "webrtc/modules/audio_coding/neteq/background_noise.h" |
| 19 #include "webrtc/modules/audio_coding/neteq/cross_correlation.h" |
19 #include "webrtc/modules/audio_coding/neteq/dsp_helper.h" | 20 #include "webrtc/modules/audio_coding/neteq/dsp_helper.h" |
20 | 21 |
21 namespace webrtc { | 22 namespace webrtc { |
22 | 23 |
23 TimeStretch::ReturnCodes TimeStretch::Process(const int16_t* input, | 24 TimeStretch::ReturnCodes TimeStretch::Process(const int16_t* input, |
24 size_t input_len, | 25 size_t input_len, |
25 bool fast_mode, | 26 bool fast_mode, |
26 AudioMultiVector* output, | 27 AudioMultiVector* output, |
27 size_t* length_change_samples) { | 28 size_t* length_change_samples) { |
28 // Pre-calculate common multiplication with |fs_mult_|. | 29 // Pre-calculate common multiplication with |fs_mult_|. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 break; | 152 break; |
152 case kNoStretch: | 153 case kNoStretch: |
153 case kError: | 154 case kError: |
154 *length_change_samples = 0; | 155 *length_change_samples = 0; |
155 break; | 156 break; |
156 } | 157 } |
157 return return_value; | 158 return return_value; |
158 } | 159 } |
159 | 160 |
160 void TimeStretch::AutoCorrelation() { | 161 void TimeStretch::AutoCorrelation() { |
161 // Set scaling factor for cross correlation to protect against overflow. | |
162 int scaling = kLogCorrelationLen - WebRtcSpl_NormW32( | |
163 max_input_value_ * max_input_value_); | |
164 scaling = std::max(0, scaling); | |
165 | |
166 // Calculate correlation from lag kMinLag to lag kMaxLag in 4 kHz domain. | 162 // Calculate correlation from lag kMinLag to lag kMaxLag in 4 kHz domain. |
167 int32_t auto_corr[kCorrelationLen]; | 163 int32_t auto_corr[kCorrelationLen]; |
168 WebRtcSpl_CrossCorrelation(auto_corr, &downsampled_input_[kMaxLag], | 164 CrossCorrelationWithAutoShift( |
169 &downsampled_input_[kMaxLag - kMinLag], | 165 auto_corr, &downsampled_input_[kMaxLag], |
170 kCorrelationLen, kMaxLag - kMinLag, scaling, -1); | 166 &downsampled_input_[kMaxLag - kMinLag], kCorrelationLen, |
| 167 kMaxLag - kMinLag, -1); |
171 | 168 |
172 // Normalize correlation to 14 bits and write to |auto_correlation_|. | 169 // Normalize correlation to 14 bits and write to |auto_correlation_|. |
173 int32_t max_corr = WebRtcSpl_MaxAbsValueW32(auto_corr, kCorrelationLen); | 170 int32_t max_corr = WebRtcSpl_MaxAbsValueW32(auto_corr, kCorrelationLen); |
174 scaling = std::max(0, 17 - WebRtcSpl_NormW32(max_corr)); | 171 int scaling = std::max(0, 17 - WebRtcSpl_NormW32(max_corr)); |
175 WebRtcSpl_VectorBitShiftW32ToW16(auto_correlation_, kCorrelationLen, | 172 WebRtcSpl_VectorBitShiftW32ToW16(auto_correlation_, kCorrelationLen, |
176 auto_corr, scaling); | 173 auto_corr, scaling); |
177 } | 174 } |
178 | 175 |
179 bool TimeStretch::SpeechDetection(int32_t vec1_energy, int32_t vec2_energy, | 176 bool TimeStretch::SpeechDetection(int32_t vec1_energy, int32_t vec2_energy, |
180 size_t peak_index, int scaling) const { | 177 size_t peak_index, int scaling) const { |
181 // Check if the signal seems to be active speech or not (simple VAD). | 178 // Check if the signal seems to be active speech or not (simple VAD). |
182 // If (vec1_energy + vec2_energy) / (2 * peak_index) <= | 179 // If (vec1_energy + vec2_energy) / (2 * peak_index) <= |
183 // 8 * background_noise_energy, then we say that the signal contains no | 180 // 8 * background_noise_energy, then we say that the signal contains no |
184 // active speech. | 181 // active speech. |
(...skipping 23 matching lines...) Expand all Loading... |
208 int temp_scale = WebRtcSpl_NormW32(left_side); | 205 int temp_scale = WebRtcSpl_NormW32(left_side); |
209 left_side = left_side << temp_scale; | 206 left_side = left_side << temp_scale; |
210 right_side = right_side >> (2 * scaling - temp_scale); | 207 right_side = right_side >> (2 * scaling - temp_scale); |
211 } else { | 208 } else { |
212 left_side = left_side << 2 * scaling; | 209 left_side = left_side << 2 * scaling; |
213 } | 210 } |
214 return left_side > right_side; | 211 return left_side > right_side; |
215 } | 212 } |
216 | 213 |
217 } // namespace webrtc | 214 } // namespace webrtc |
OLD | NEW |