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> | 17 #include <assert.h> |
18 #include <math.h> | 18 #include <math.h> |
19 #include <stdlib.h> | 19 #include <stdlib.h> |
20 #include <string.h> | 20 #include <string.h> |
21 | 21 |
22 #include "webrtc/modules/audio_processing/aec/aec_core.h" | 22 #include "webrtc/modules/audio_processing/aec/aec_core.h" |
23 | 23 |
24 enum { | 24 enum { kEstimateLengthFrames = 400 }; |
25 kEstimateLengthFrames = 400 | |
26 }; | |
27 | 25 |
28 typedef struct { | 26 typedef struct { |
29 float buffer[kResamplerBufferSize]; | 27 float buffer[kResamplerBufferSize]; |
30 float position; | 28 float position; |
31 | 29 |
32 int deviceSampleRateHz; | 30 int deviceSampleRateHz; |
33 int skewData[kEstimateLengthFrames]; | 31 int skewData[kEstimateLengthFrames]; |
34 int skewDataIndex; | 32 int skewDataIndex; |
35 float skewEstimate; | 33 float skewEstimate; |
36 } AecResampler; | 34 } AecResampler; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 float be, tnew; | 72 float be, tnew; |
75 size_t tn, mm; | 73 size_t tn, mm; |
76 | 74 |
77 assert(size <= 2 * FRAME_LEN); | 75 assert(size <= 2 * FRAME_LEN); |
78 assert(resampInst != NULL); | 76 assert(resampInst != NULL); |
79 assert(inspeech != NULL); | 77 assert(inspeech != NULL); |
80 assert(outspeech != NULL); | 78 assert(outspeech != NULL); |
81 assert(size_out != NULL); | 79 assert(size_out != NULL); |
82 | 80 |
83 // Add new frame data in lookahead | 81 // Add new frame data in lookahead |
84 memcpy(&obj->buffer[FRAME_LEN + kResamplingDelay], | 82 memcpy(&obj->buffer[FRAME_LEN + kResamplingDelay], inspeech, |
85 inspeech, | |
86 size * sizeof(inspeech[0])); | 83 size * sizeof(inspeech[0])); |
87 | 84 |
88 // Sample rate ratio | 85 // Sample rate ratio |
89 be = 1 + skew; | 86 be = 1 + skew; |
90 | 87 |
91 // Loop over input frame | 88 // Loop over input frame |
92 mm = 0; | 89 mm = 0; |
93 y = &obj->buffer[FRAME_LEN]; // Point at current frame | 90 y = &obj->buffer[FRAME_LEN]; // Point at current frame |
94 | 91 |
95 tnew = be * mm + obj->position; | 92 tnew = be * mm + obj->position; |
96 tn = (size_t)tnew; | 93 tn = (size_t)tnew; |
97 | 94 |
98 while (tn < size) { | 95 while (tn < size) { |
99 | |
100 // Interpolation | 96 // Interpolation |
101 outspeech[mm] = y[tn] + (tnew - tn) * (y[tn + 1] - y[tn]); | 97 outspeech[mm] = y[tn] + (tnew - tn) * (y[tn + 1] - y[tn]); |
102 mm++; | 98 mm++; |
103 | 99 |
104 tnew = be * mm + obj->position; | 100 tnew = be * mm + obj->position; |
105 tn = (int)tnew; | 101 tn = (int)tnew; |
106 } | 102 } |
107 | 103 |
108 *size_out = mm; | 104 *size_out = mm; |
109 obj->position += (*size_out) * be - size; | 105 obj->position += (*size_out) * be - size; |
110 | 106 |
111 // Shift buffer | 107 // Shift buffer |
112 memmove(obj->buffer, | 108 memmove(obj->buffer, &obj->buffer[size], |
113 &obj->buffer[size], | |
114 (kResamplerBufferSize - size) * sizeof(obj->buffer[0])); | 109 (kResamplerBufferSize - size) * sizeof(obj->buffer[0])); |
115 } | 110 } |
116 | 111 |
117 int WebRtcAec_GetSkew(void* resampInst, int rawSkew, float* skewEst) { | 112 int WebRtcAec_GetSkew(void* resampInst, int rawSkew, float* skewEst) { |
118 AecResampler* obj = (AecResampler*)resampInst; | 113 AecResampler* obj = (AecResampler*)resampInst; |
119 int err = 0; | 114 int err = 0; |
120 | 115 |
121 if (obj->skewDataIndex < kEstimateLengthFrames) { | 116 if (obj->skewDataIndex < kEstimateLengthFrames) { |
122 obj->skewData[obj->skewDataIndex] = rawSkew; | 117 obj->skewData[obj->skewDataIndex] = rawSkew; |
123 obj->skewDataIndex++; | 118 obj->skewDataIndex++; |
124 } else if (obj->skewDataIndex == kEstimateLengthFrames) { | 119 } else if (obj->skewDataIndex == kEstimateLengthFrames) { |
125 err = EstimateSkew( | 120 err = EstimateSkew(obj->skewData, kEstimateLengthFrames, |
126 obj->skewData, kEstimateLengthFrames, obj->deviceSampleRateHz, skewEst); | 121 obj->deviceSampleRateHz, skewEst); |
127 obj->skewEstimate = *skewEst; | 122 obj->skewEstimate = *skewEst; |
128 obj->skewDataIndex++; | 123 obj->skewDataIndex++; |
129 } else { | 124 } else { |
130 *skewEst = obj->skewEstimate; | 125 *skewEst = obj->skewEstimate; |
131 } | 126 } |
132 | 127 |
133 return err; | 128 return err; |
134 } | 129 } |
135 | 130 |
136 int EstimateSkew(const int* rawSkew, | 131 int EstimateSkew(const int* rawSkew, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 xAvg = x / n; | 195 xAvg = x / n; |
201 denom = x2 - xAvg * x; | 196 denom = x2 - xAvg * x; |
202 | 197 |
203 if (denom != 0) { | 198 if (denom != 0) { |
204 skew = (xy - xAvg * y) / denom; | 199 skew = (xy - xAvg * y) / denom; |
205 } | 200 } |
206 | 201 |
207 *skewEst = skew; | 202 *skewEst = skew; |
208 return 0; | 203 return 0; |
209 } | 204 } |
OLD | NEW |