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 /* Resamples a signal to an arbitrary rate. Used by the AEC to compensate for | 11 /* Resamples a signal to an arbitrary rate. Used by the AEC to compensate for |
12 * clock skew by resampling the farend signal. | 12 * clock skew by resampling the farend signal. |
13 */ | 13 */ |
14 | 14 |
15 #include "webrtc/modules/audio_processing/aec/aec_resampler.h" | 15 #include "webrtc/modules/audio_processing/aec/aec_resampler.h" |
16 | 16 |
17 #include <assert.h> | |
18 #include <math.h> | 17 #include <math.h> |
19 #include <stdlib.h> | 18 #include <stdlib.h> |
20 #include <string.h> | 19 #include <string.h> |
21 | 20 |
| 21 #include "webrtc/base/checks.h" |
22 #include "webrtc/modules/audio_processing/aec/aec_core.h" | 22 #include "webrtc/modules/audio_processing/aec/aec_core.h" |
23 | 23 |
24 namespace webrtc { | 24 namespace webrtc { |
25 | 25 |
26 enum { kEstimateLengthFrames = 400 }; | 26 enum { kEstimateLengthFrames = 400 }; |
27 | 27 |
28 typedef struct { | 28 typedef struct { |
29 float buffer[kResamplerBufferSize]; | 29 float buffer[kResamplerBufferSize]; |
30 float position; | 30 float position; |
31 | 31 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 size_t size, | 67 size_t size, |
68 float skew, | 68 float skew, |
69 float* outspeech, | 69 float* outspeech, |
70 size_t* size_out) { | 70 size_t* size_out) { |
71 AecResampler* obj = static_cast<AecResampler*>(resampInst); | 71 AecResampler* obj = static_cast<AecResampler*>(resampInst); |
72 | 72 |
73 float* y; | 73 float* y; |
74 float be, tnew; | 74 float be, tnew; |
75 size_t tn, mm; | 75 size_t tn, mm; |
76 | 76 |
77 assert(size <= 2 * FRAME_LEN); | 77 RTC_DCHECK_LE(size, 2u * FRAME_LEN); |
78 assert(resampInst != NULL); | 78 RTC_DCHECK(resampInst); |
79 assert(inspeech != NULL); | 79 RTC_DCHECK(inspeech); |
80 assert(outspeech != NULL); | 80 RTC_DCHECK(outspeech); |
81 assert(size_out != NULL); | 81 RTC_DCHECK(size_out); |
82 | 82 |
83 // Add new frame data in lookahead | 83 // Add new frame data in lookahead |
84 memcpy(&obj->buffer[FRAME_LEN + kResamplingDelay], inspeech, | 84 memcpy(&obj->buffer[FRAME_LEN + kResamplingDelay], inspeech, |
85 size * sizeof(inspeech[0])); | 85 size * sizeof(inspeech[0])); |
86 | 86 |
87 // Sample rate ratio | 87 // Sample rate ratio |
88 be = 1 + skew; | 88 be = 1 + skew; |
89 | 89 |
90 // Loop over input frame | 90 // Loop over input frame |
91 mm = 0; | 91 mm = 0; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 for (i = 0; i < size; i++) { | 156 for (i = 0; i < size; i++) { |
157 if ((rawSkew[i] < absLimitOuter && rawSkew[i] > -absLimitOuter)) { | 157 if ((rawSkew[i] < absLimitOuter && rawSkew[i] > -absLimitOuter)) { |
158 n++; | 158 n++; |
159 rawAvg += rawSkew[i]; | 159 rawAvg += rawSkew[i]; |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 if (n == 0) { | 163 if (n == 0) { |
164 return -1; | 164 return -1; |
165 } | 165 } |
166 assert(n > 0); | 166 RTC_DCHECK_GT(n, 0); |
167 rawAvg /= n; | 167 rawAvg /= n; |
168 | 168 |
169 for (i = 0; i < size; i++) { | 169 for (i = 0; i < size; i++) { |
170 if ((rawSkew[i] < absLimitOuter && rawSkew[i] > -absLimitOuter)) { | 170 if ((rawSkew[i] < absLimitOuter && rawSkew[i] > -absLimitOuter)) { |
171 err = rawSkew[i] - rawAvg; | 171 err = rawSkew[i] - rawAvg; |
172 rawAbsDev += err >= 0 ? err : -err; | 172 rawAbsDev += err >= 0 ? err : -err; |
173 } | 173 } |
174 } | 174 } |
175 assert(n > 0); | 175 RTC_DCHECK_GT(n, 0); |
176 rawAbsDev /= n; | 176 rawAbsDev /= n; |
177 upperLimit = static_cast<int>(rawAvg + 5 * rawAbsDev + 1); // +1 for ceiling. | 177 upperLimit = static_cast<int>(rawAvg + 5 * rawAbsDev + 1); // +1 for ceiling. |
178 lowerLimit = static_cast<int>(rawAvg - 5 * rawAbsDev - 1); // -1 for floor. | 178 lowerLimit = static_cast<int>(rawAvg - 5 * rawAbsDev - 1); // -1 for floor. |
179 | 179 |
180 n = 0; | 180 n = 0; |
181 for (i = 0; i < size; i++) { | 181 for (i = 0; i < size; i++) { |
182 if ((rawSkew[i] < absLimitInner && rawSkew[i] > -absLimitInner) || | 182 if ((rawSkew[i] < absLimitInner && rawSkew[i] > -absLimitInner) || |
183 (rawSkew[i] < upperLimit && rawSkew[i] > lowerLimit)) { | 183 (rawSkew[i] < upperLimit && rawSkew[i] > lowerLimit)) { |
184 n++; | 184 n++; |
185 cumSum += rawSkew[i]; | 185 cumSum += rawSkew[i]; |
186 x += n; | 186 x += n; |
187 x2 += n * n; | 187 x2 += n * n; |
188 y += cumSum; | 188 y += cumSum; |
189 xy += n * cumSum; | 189 xy += n * cumSum; |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 if (n == 0) { | 193 if (n == 0) { |
194 return -1; | 194 return -1; |
195 } | 195 } |
196 assert(n > 0); | 196 RTC_DCHECK_GT(n, 0); |
197 xAvg = x / n; | 197 xAvg = x / n; |
198 denom = x2 - xAvg * x; | 198 denom = x2 - xAvg * x; |
199 | 199 |
200 if (denom != 0) { | 200 if (denom != 0) { |
201 skew = (xy - xAvg * y) / denom; | 201 skew = (xy - xAvg * y) / denom; |
202 } | 202 } |
203 | 203 |
204 *skewEst = skew; | 204 *skewEst = skew; |
205 return 0; | 205 return 0; |
206 } | 206 } |
207 } // namespace webrtc | 207 } // namespace webrtc |
OLD | NEW |