41 #define FILE_ "CAPIDTMF.C"
52 static short capidtmf_expand_table_alaw[0x0100] =
54 -5504, 5504, -344, 344, -22016, 22016, -1376, 1376,
55 -2752, 2752, -88, 88, -11008, 11008, -688, 688,
56 -7552, 7552, -472, 472, -30208, 30208, -1888, 1888,
57 -3776, 3776, -216, 216, -15104, 15104, -944, 944,
58 -4480, 4480, -280, 280, -17920, 17920, -1120, 1120,
59 -2240, 2240, -24, 24, -8960, 8960, -560, 560,
60 -6528, 6528, -408, 408, -26112, 26112, -1632, 1632,
61 -3264, 3264, -152, 152, -13056, 13056, -816, 816,
62 -6016, 6016, -376, 376, -24064, 24064, -1504, 1504,
63 -3008, 3008, -120, 120, -12032, 12032, -752, 752,
64 -8064, 8064, -504, 504, -32256, 32256, -2016, 2016,
65 -4032, 4032, -248, 248, -16128, 16128, -1008, 1008,
66 -4992, 4992, -312, 312, -19968, 19968, -1248, 1248,
67 -2496, 2496, -56, 56, -9984, 9984, -624, 624,
68 -7040, 7040, -440, 440, -28160, 28160, -1760, 1760,
69 -3520, 3520, -184, 184, -14080, 14080, -880, 880,
70 -5248, 5248, -328, 328, -20992, 20992, -1312, 1312,
71 -2624, 2624, -72, 72, -10496, 10496, -656, 656,
72 -7296, 7296, -456, 456, -29184, 29184, -1824, 1824,
73 -3648, 3648, -200, 200, -14592, 14592, -912, 912,
74 -4224, 4224, -264, 264, -16896, 16896, -1056, 1056,
75 -2112, 2112, -8, 8, -8448, 8448, -528, 528,
76 -6272, 6272, -392, 392, -25088, 25088, -1568, 1568,
77 -3136, 3136, -136, 136, -12544, 12544, -784, 784,
78 -5760, 5760, -360, 360, -23040, 23040, -1440, 1440,
79 -2880, 2880, -104, 104, -11520, 11520, -720, 720,
80 -7808, 7808, -488, 488, -31232, 31232, -1952, 1952,
81 -3904, 3904, -232, 232, -15616, 15616, -976, 976,
82 -4736, 4736, -296, 296, -18944, 18944, -1184, 1184,
83 -2368, 2368, -40, 40, -9472, 9472, -592, 592,
84 -6784, 6784, -424, 424, -27136, 27136, -1696, 1696,
85 -3392, 3392, -168, 168, -13568, 13568, -848, 848
88 static short capidtmf_expand_table_ulaw[0x0100] =
90 -32124, 32124, -1884, 1884, -7932, 7932, -372, 372,
91 -15996, 15996, -876, 876, -3900, 3900, -120, 120,
92 -23932, 23932, -1372, 1372, -5884, 5884, -244, 244,
93 -11900, 11900, -620, 620, -2876, 2876, -56, 56,
94 -28028, 28028, -1628, 1628, -6908, 6908, -308, 308,
95 -13948, 13948, -748, 748, -3388, 3388, -88, 88,
96 -19836, 19836, -1116, 1116, -4860, 4860, -180, 180,
97 -9852, 9852, -492, 492, -2364, 2364, -24, 24,
98 -30076, 30076, -1756, 1756, -7420, 7420, -340, 340,
99 -14972, 14972, -812, 812, -3644, 3644, -104, 104,
100 -21884, 21884, -1244, 1244, -5372, 5372, -212, 212,
101 -10876, 10876, -556, 556, -2620, 2620, -40, 40,
102 -25980, 25980, -1500, 1500, -6396, 6396, -276, 276,
103 -12924, 12924, -684, 684, -3132, 3132, -72, 72,
104 -17788, 17788, -988, 988, -4348, 4348, -148, 148,
105 -8828, 8828, -428, 428, -2108, 2108, -8, 8,
106 -31100, 31100, -1820, 1820, -7676, 7676, -356, 356,
107 -15484, 15484, -844, 844, -3772, 3772, -112, 112,
108 -22908, 22908, -1308, 1308, -5628, 5628, -228, 228,
109 -11388, 11388, -588, 588, -2748, 2748, -48, 48,
110 -27004, 27004, -1564, 1564, -6652, 6652, -292, 292,
111 -13436, 13436, -716, 716, -3260, 3260, -80, 80,
112 -18812, 18812, -1052, 1052, -4604, 4604, -164, 164,
113 -9340, 9340, -460, 460, -2236, 2236, -16, 16,
114 -29052, 29052, -1692, 1692, -7164, 7164, -324, 324,
115 -14460, 14460, -780, 780, -3516, 3516, -96, 96,
116 -20860, 20860, -1180, 1180, -5116, 5116, -196, 196,
117 -10364, 10364, -524, 524, -2492, 2492, -32, 32,
118 -24956, 24956, -1436, 1436, -6140, 6140, -260, 260,
119 -12412, 12412, -652, 652, -3004, 3004, -64, 64,
120 -16764, 16764, -924, 924, -4092, 4092, -132, 132,
121 -8316, 8316, -396, 396, -1980, 1980, 0, 0
129 -500
L, -999
L, -1499L, -1998L, -2496L, -2994L, -3491L, -3988L,
130 -4483L, -4978L, -5471L, -5963L, -6454L, -6943L, -7431L, -7917L,
131 -8401L, -8883L, -9363L, -9840L, -10316L, -10789L, -11259L, -11727L,
132 -12193L, -12655L, -13115L, -13571L, -14024L, -14474L, -14921L, -15364L,
133 -15804L, -16240L, -16672L, -17100L, -17524L, -17944L, -18360L, -18772L,
134 -19180L, -19583L, -19981L, -20375L, -20764L, -21148L, -21527L, -21901L,
135 -22270L, -22634L, -22993L, -23346L, -23694L, -24037L, -24374L, -24705L,
136 -25030L, -25350L, -25664L, -25971L, -26273L, -26568L, -26858L, -27141L,
137 -27418L, -27688L, -27952L, -28210L, -28461L, -28705L, -28943L, -29174L,
138 -29398L, -29615L, -29826L, -30029L, -30226L, -30415L, -30598L, -30773L,
139 -30941L, -31102L, -31256L, -31402L, -31541L, -31673L, -31797L, -31914L,
140 -32024L, -32126L, -32221L, -32308L, -32388L, -32460L, -32524L, -32581L,
141 -32631L, -32673L, -32707L, -32734L, -32753L, -32764L, -32768L, -32764L,
142 -32753L, -32734L, -32707L, -32673L, -32631L, -32581L, -32524L, -32460L,
143 -32388L, -32308L, -32221L, -32126L, -32024L, -31914L, -31797L, -31673L,
144 -31541L, -31402L, -31256L, -31102L, -30941L, -30773L, -30598L, -30415L,
145 -30226L, -30029L, -29826L, -29615L, -29398L, -29174L, -28943L, -28705L,
146 -28461L, -28210L, -27952L, -27688L, -27418L, -27141L, -26858L, -26568L,
147 -26273L, -25971L, -25664L, -25350L, -25030L, -24705L, -24374L, -24037L,
148 -23694L, -23346L, -22993L, -22634L, -22270L, -21901L, -21527L, -21148L,
149 -20764L, -20375L, -19981L, -19583L, -19180L, -18772L, -18360L, -17944L,
150 -17524L, -17100L, -16672L, -16240L, -15804L, -15364L, -14921L, -14474L,
151 -14024L, -13571L, -13115L, -12655L, -12193L, -11727L, -11259L, -10789L,
152 -10316L, -9840L, -9363L, -8883L, -8401L, -7917L, -7431L, -6943L,
153 -6454L, -5963L, -5471L, -4978L, -4483L, -3988L, -3491L, -2994L,
154 -2496L, -1998L, -1499L, -999L, -500L,
157 static byte capidtmf_leading_zeroes_table[0x100] =
159 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
160 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
161 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
162 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
163 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
164 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
165 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
166 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
177 #define capidtmf_byte_leading_zeroes(b) (capidtmf_leading_zeroes_table[(BYTE)(b)])
178 #define capidtmf_word_leading_zeroes(w) (((w) & 0xff00) ? capidtmf_leading_zeroes_table[(w) >> 8] : 8 + capidtmf_leading_zeroes_table[(w)])
179 #define capidtmf_dword_leading_zeroes(d) (((d) & 0xffff0000L) ? (((d) & 0xff000000L) ? capidtmf_leading_zeroes_table[(d) >> 24] : 8 + capidtmf_leading_zeroes_table[(d) >> 16]) : (((d) & 0xff00) ? 16 + capidtmf_leading_zeroes_table[(d) >> 8] : 24 + capidtmf_leading_zeroes_table[(d)]))
185 static void capidtmf_goertzel_loop(
long *
buffer,
long *coeffs,
short *
sample,
long count)
188 long c,
d, q0, q1, q2;
198 for (j = 0; j <
count; j++)
200 q0 = sample[
j] - q2 + (c * (q1 >> 16)) + (((
dword)(((
dword)
d) * ((
dword)(q1 & 0xffff)))) >> 15);
209 for (j = 0; j <
count; j++)
211 q0 = sample[
j] - q2 - ((c * (q1 >> 16)) + (((
dword)(((
dword)
d) * ((
dword)(q1 & 0xffff)))) >> 15));
221 c = (coeffs[
i] >> 1) << 1;
224 for (j = 0; j <
count; j++)
226 q0 = sample[
j] - q2 + (c * (q1 >> 16)) + (((
dword)(((
dword)(c >> 1)) * ((
dword)(q1 & 0xffff)))) >> 15);
235 for (j = 0; j <
count; j++)
237 q0 = sample[
j] - q2 - ((c * (q1 >> 16)) + (((
dword)(((
dword)(c >> 1)) * ((
dword)(q1 & 0xffff)))) >> 15));
249 static void capidtmf_goertzel_result(
long *
buffer,
long *coeffs)
261 d = ((d << 1) * (-q1 >> 16)) + (((
dword)(((
dword) d) * ((
dword)(-q1 & 0xffff)))) >> 15);
263 d = ((-d << 1) * (-q1 >> 16)) + (((
dword)(((
dword) -d) * ((
dword)(-q1 & 0xffff)))) >> 15);
264 e = (q2 >= 0) ? q2 : -q2;
267 k = ((
dword)(d & 0xffff)) * ((
dword)(e & 0xffff));
270 k = ((
dword)(d >> 16)) * ((
dword)(e & 0xffff));
273 k = ((
dword)(d & 0xffff)) * ((
dword)(e >> 16));
276 hi += ((
dword)(d >> 16)) * ((
dword)(e >> 16));
281 k = ((
dword)(d & 0xffff)) * ((
dword)(e & 0xffff));
282 lo = -((
long)(k & 0xffff));
283 mid = -((
long)(k >> 16));
284 k = ((
dword)(d >> 16)) * ((
dword)(e & 0xffff));
286 hi = -((
long)(k >> 16));
287 k = ((
dword)(d & 0xffff)) * ((
dword)(e >> 16));
290 hi -= ((
dword)(d >> 16)) * ((
dword)(e >> 16));
298 d = (q1 >= 0) ? q1 : -q1;
299 k = ((
dword)(d & 0xffff)) * ((
dword)(d & 0xffff));
302 k = ((
dword)(d >> 16)) * ((
dword)(d & 0xffff));
303 mid += (k & 0xffff) << 1;
304 hi += (k >> 16) << 1;
305 hi += ((
dword)(d >> 16)) * ((
dword)(d >> 16));
306 d = (q2 >= 0) ? q2 : -q2;
307 k = ((
dword)(d & 0xffff)) * ((
dword)(d & 0xffff));
310 k = ((
dword)(d >> 16)) * ((
dword)(d & 0xffff));
311 mid += (k & 0xffff) << 1;
312 hi += (k >> 16) << 1;
313 hi += ((
dword)(d >> 16)) * ((
dword)(d >> 16));
316 buffer[
i] = (lo & 0xffff) | (mid << 16);
324 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_697 0
325 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_770 1
326 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_852 2
327 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_941 3
328 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1209 4
329 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1336 5
330 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1477 6
331 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1633 7
332 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_635 8
333 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1010 9
334 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1140 10
335 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1272 11
336 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1405 12
337 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1555 13
338 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1715 14
339 #define CAPIDTMF_RECV_GUARD_SNR_INDEX_1875 15
341 #define CAPIDTMF_RECV_GUARD_SNR_DONTCARE 0xc000
342 #define CAPIDTMF_RECV_NO_DIGIT 0xff
343 #define CAPIDTMF_RECV_TIME_GRANULARITY (CAPIDTMF_RECV_ACCUMULATE_CYCLES + 1)
345 #define CAPIDTMF_RECV_INDICATION_DIGIT 0x0001
383 DSPDTMF_RX_HARMONICS_SEL_DEFAULT - 4,
384 DSPDTMF_RX_HARMONICS_SEL_DEFAULT - 4,
453 capidtmf_recv_init(p_state);
488 #define CAPIDTMF_RECV_WINDOWED_SAMPLES 32
493 word sample_number, cycle_counter,
n,
i;
494 word low_peak, high_peak;
506 while (sample_number < length)
510 if (cycle_counter == 0)
519 if (n > length - sample_number)
520 n = length - sample_number;
523 p = buffer + sample_number;
524 q = capidtmf_recv_window_function + cycle_counter;
527 for (i = 0; i <
n; i++)
529 windowed_sample_buffer[
i] =
530 (
short)((capidtmf_expand_table_ulaw[p[i]] * ((
long)(q[i]))) >> 15);
535 for (i = 0; i <
n; i++)
537 windowed_sample_buffer[
i] =
538 (
short)((capidtmf_expand_table_alaw[p[i]] * ((
long)(q[i]))) >> 15);
543 capidtmf_recv_goertzel_coef_table, windowed_sample_buffer, n);
550 capidtmf_recv_goertzel_coef_table);
558 hi = (hi << n) | (lo >> (32 -
n));
567 if (hi >= 0xcb300000L)
569 else if (hi >= 0xa1450000L)
571 goertzel_result_buffer[
i] = (
byte) n;
577 if (goertzel_result_buffer[i] > low_peak)
579 low_peak = goertzel_result_buffer[
i];
580 result_digit = (
byte) i;
587 if (goertzel_result_buffer[i] > high_peak)
589 high_peak = goertzel_result_buffer[
i];
593 result_digit |= (
byte) n;
601 if ((((
short)(low_peak - goertzel_result_buffer[
i] - capidtmf_recv_guard_snr_low_table[
i])) < 0)
602 || (((
short)(high_peak - goertzel_result_buffer[
i] - capidtmf_recv_guard_snr_high_table[
i])) < 0))
655 (
char *)(
FILE_), __LINE__));
680 p_state->
ulaw = ulaw;
681 capidtmf_recv_init(p_state);