1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | #if defined(HAVE_CONFIG_H1) |
29 | #include "config.h" |
30 | #endif |
31 | |
32 | #include <stdlib.h> |
33 | #include <string.h> |
34 | #include <stdio.h> |
35 | #include <fcntl.h> |
36 | #include <ctype.h> |
37 | #include <time.h> |
38 | #include <inttypes.h> |
39 | #if defined(HAVE_TGMATH_H1) |
40 | #include <tgmath.h> |
41 | #endif |
42 | #if defined(HAVE_MATH_H1) |
43 | #include <math.h> |
44 | #endif |
45 | #include "floating_fudge.h" |
46 | |
47 | #include "spandsp/telephony.h" |
48 | #include "spandsp/alloc.h" |
49 | #include "spandsp/fast_convert.h" |
50 | #include "spandsp/complex.h" |
51 | #include "spandsp/vector_float.h" |
52 | #include "spandsp/complex_vector_float.h" |
53 | #include "spandsp/tone_detect.h" |
54 | #include "spandsp/tone_generate.h" |
55 | #include "spandsp/super_tone_rx.h" |
56 | |
57 | #include "spandsp/private/super_tone_rx.h" |
58 | |
59 | #if defined(SPANDSP_USE_FIXED_POINT) |
60 | #define DETECTION_THRESHOLD2104205.6f 16439 /* -42dBm0 [((SUPER_TONE_BINS*SUPER_TONE_BINS*32768.0/(1.4142*128.0))*10^((-42 - DBM0_MAX_SINE_POWER)/20.0))^2] */ |
61 | #define TONE_TWIST3.981f 4 /* 6dB */ |
62 | #define TONE_TO_TOTAL_ENERGY1.995f 64 /* -3dB */ |
63 | #else |
64 | #define DETECTION_THRESHOLD2104205.6f 2104205.6f /* -42dBm0 [((SUPER_TONE_BINS*SUPER_TONE_BINS*32768.0/1.4142)*10^((-42 - DBM0_MAX_SINE_POWER)/20.0))^2] */ |
65 | #define TONE_TWIST3.981f 3.981f /* 6dB */ |
66 | #define TONE_TO_TOTAL_ENERGY1.995f 1.995f /* 3dB */ |
67 | #endif |
68 | |
69 | static int add_super_tone_freq(super_tone_rx_descriptor_t *desc, int freq) |
70 | { |
71 | int i; |
72 | |
73 | if (freq == 0) |
74 | return -1; |
75 | |
76 | |
77 | for (i = 0; i < desc->used_frequencies; i++) |
78 | { |
79 | if (desc->pitches[i][0] == freq) |
80 | return desc->pitches[i][1]; |
81 | |
82 | } |
83 | |
84 | |
85 | |
86 | for (i = 0; i < desc->used_frequencies; i++) |
87 | { |
88 | if ((desc->pitches[i][0] - 10) <= freq && freq <= (desc->pitches[i][0] + 10)) |
89 | { |
90 | |
91 | desc->pitches[desc->used_frequencies][0] = freq; |
92 | desc->pitches[desc->used_frequencies][1] = i; |
93 | make_goertzel_descriptor(&desc->desc[desc->pitches[i][1]], (float) (freq + desc->pitches[i][0])/2, SUPER_TONE_BINS128); |
94 | desc->used_frequencies++; |
95 | return desc->pitches[i][1]; |
96 | } |
97 | |
98 | } |
99 | |
100 | desc->pitches[i][0] = freq; |
101 | desc->pitches[i][1] = desc->monitored_frequencies; |
102 | if (desc->monitored_frequencies%5 == 0) |
103 | { |
104 | desc->desc = (goertzel_descriptor_t *) span_realloc(desc->desc, (desc->monitored_frequencies + 5)*sizeof(goertzel_descriptor_t)); |
105 | } |
106 | |
107 | make_goertzel_descriptor(&desc->desc[desc->monitored_frequencies++], (float) freq, SUPER_TONE_BINS128); |
108 | desc->used_frequencies++; |
109 | return desc->pitches[i][1]; |
110 | } |
111 | |
112 | |
113 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_add_tone(super_tone_rx_descriptor_t *desc) |
114 | { |
115 | if (desc->tones%5 == 0) |
116 | { |
117 | desc->tone_list = (super_tone_rx_segment_t **) span_realloc(desc->tone_list, (desc->tones + 5)*sizeof(super_tone_rx_segment_t *)); |
118 | desc->tone_segs = (int *) span_realloc(desc->tone_segs, (desc->tones + 5)*sizeof(int)); |
119 | } |
120 | |
121 | desc->tone_list[desc->tones] = NULL((void*)0); |
122 | desc->tone_segs[desc->tones] = 0; |
123 | desc->tones++; |
124 | return desc->tones - 1; |
125 | } |
126 | |
127 | |
128 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_add_element(super_tone_rx_descriptor_t *desc, |
129 | int tone, |
130 | int f1, |
131 | int f2, |
132 | int min, |
133 | int max) |
134 | { |
135 | int step; |
136 | |
137 | step = desc->tone_segs[tone]; |
138 | if (step%5 == 0) |
139 | { |
140 | desc->tone_list[tone] = (super_tone_rx_segment_t *) span_realloc(desc->tone_list[tone], (step + 5)*sizeof(super_tone_rx_segment_t)); |
141 | } |
142 | |
143 | desc->tone_list[tone][step].f1 = add_super_tone_freq(desc, f1); |
144 | desc->tone_list[tone][step].f2 = add_super_tone_freq(desc, f2); |
145 | desc->tone_list[tone][step].min_duration = min*8; |
146 | desc->tone_list[tone][step].max_duration = (max == 0) ? 0x7FFFFFFF : max*8; |
147 | desc->tone_segs[tone]++; |
148 | return step; |
149 | } |
150 | |
151 | |
152 | static int test_cadence(super_tone_rx_segment_t *pattern, |
153 | int steps, |
154 | super_tone_rx_segment_t *test, |
155 | int rotation) |
156 | { |
157 | int i; |
158 | int j; |
159 | |
160 | if (rotation >= 0) |
161 | { |
162 | |
163 | |
164 | |
165 | j = 0; |
166 | if (steps < 0) |
167 | { |
168 | |
169 | |
170 | |
171 | steps = -steps; |
172 | j = (rotation + steps - 2)%steps; |
173 | if (pattern[j].f1 != test[8].f1 || pattern[j].f2 != test[8].f2) |
174 | return 0; |
175 | |
176 | if (pattern[j].min_duration > test[8].min_duration*SUPER_TONE_BINS128 |
177 | || |
178 | pattern[j].max_duration < test[8].min_duration*SUPER_TONE_BINS128) |
179 | { |
180 | return 0; |
181 | } |
182 | |
183 | } |
184 | |
185 | if (steps) |
186 | j = (rotation + steps - 1)%steps; |
187 | |
188 | if (pattern[j].f1 != test[9].f1 || pattern[j].f2 != test[9].f2) |
189 | return 0; |
190 | |
191 | if (pattern[j].max_duration < test[9].min_duration*SUPER_TONE_BINS128) |
192 | return 0; |
193 | |
194 | } |
195 | else |
196 | { |
197 | |
198 | for (i = 0; i < steps; i++) |
199 | { |
200 | j = i + 10 - steps; |
201 | if (pattern[i].f1 != test[j].f1 || pattern[i].f2 != test[j].f2) |
202 | return 0; |
203 | |
204 | if (pattern[i].min_duration > test[j].min_duration*SUPER_TONE_BINS128 |
205 | || |
206 | pattern[i].max_duration < test[j].min_duration*SUPER_TONE_BINS128) |
207 | { |
208 | return 0; |
209 | } |
210 | |
211 | } |
212 | |
213 | } |
214 | |
215 | return 1; |
216 | } |
217 | |
218 | |
219 | SPAN_DECLARE(super_tone_rx_descriptor_t *)__attribute__((visibility("default"))) super_tone_rx_descriptor_t * super_tone_rx_make_descriptor(super_tone_rx_descriptor_t *desc) |
220 | { |
221 | if (desc == NULL((void*)0)) |
222 | { |
223 | if ((desc = (super_tone_rx_descriptor_t *) span_alloc(sizeof(*desc))) == NULL((void*)0)) |
224 | return NULL((void*)0); |
225 | |
226 | } |
227 | |
228 | desc->tone_list = NULL((void*)0); |
229 | desc->tone_segs = NULL((void*)0); |
230 | |
231 | desc->used_frequencies = 0; |
232 | desc->monitored_frequencies = 0; |
233 | desc->desc = NULL((void*)0); |
234 | desc->tones = 0; |
235 | return desc; |
236 | } |
237 | |
238 | |
239 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free_descriptor(super_tone_rx_descriptor_t *desc) |
240 | { |
241 | int i; |
242 | |
243 | if (desc) |
244 | { |
245 | for (i = 0; i < desc->tones; i++) |
246 | { |
247 | if (desc->tone_list[i]) |
248 | span_free(desc->tone_list[i]); |
249 | |
250 | } |
251 | |
252 | if (desc->tone_list) |
253 | span_free(desc->tone_list); |
254 | |
255 | if (desc->tone_segs) |
256 | span_free(desc->tone_segs); |
257 | |
258 | if (desc->desc) |
259 | span_free(desc->desc); |
260 | |
261 | span_free(desc); |
262 | } |
263 | |
264 | return 0; |
265 | } |
266 | |
267 | |
268 | SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_tone_callback(super_tone_rx_state_t *s, |
269 | span_tone_report_func_t callback, |
270 | void *user_data) |
271 | { |
272 | s->tone_callback = callback; |
273 | s->callback_data = user_data; |
274 | } |
275 | |
276 | |
277 | static void super_tone_chunk(super_tone_rx_state_t *s) |
278 | { |
279 | int i; |
280 | int j; |
281 | int k1; |
282 | int k2; |
283 | #if defined(SPANDSP_USE_FIXED_POINT) |
284 | int32_t res[SUPER_TONE_BINS128/2]; |
285 | #else |
286 | float res[SUPER_TONE_BINS128/2]; |
287 | #endif |
288 | |
289 | if (s->energy < DETECTION_THRESHOLD2104205.6f) |
| |
290 | { |
291 | k1 = -1; |
292 | k2 = -1; |
293 | for (i = 0; i < s->desc->monitored_frequencies; i++) |
294 | goertzel_reset(&s->state[i]); |
295 | |
296 | } |
297 | else |
298 | { |
299 | if (s->desc->monitored_frequencies < 2) |
| 2 | | Assuming the condition is false | |
|
| |
300 | { |
301 | k1 = |
302 | k2 = 0; |
303 | } |
304 | else |
305 | { |
306 | |
307 | for (i = 0; i < s->desc->monitored_frequencies; i++) |
| 4 | | Loop condition is true. Entering loop body | |
|
| 5 | | Assuming the condition is false | |
|
| 6 | | Loop condition is false. Execution continues on line 310 | |
|
308 | res[i] = goertzel_result(&s->state[i]); |
309 | |
310 | if (res[0] > res[1]) |
| 7 | | The right operand of '>' is a garbage value |
|
311 | { |
312 | k1 = 0; |
313 | k2 = 1; |
314 | } |
315 | else |
316 | { |
317 | k1 = 1; |
318 | k2 = 0; |
319 | } |
320 | |
321 | for (j = 2; j < s->desc->monitored_frequencies; j++) |
322 | { |
323 | if (res[j] >= res[k1]) |
324 | { |
325 | k2 = k1; |
326 | k1 = j; |
327 | } |
328 | else if (res[j] >= res[k2]) |
329 | { |
330 | k1 = |
331 | k2 = j; |
332 | } |
333 | |
334 | } |
335 | |
336 | if ((res[k1] + res[k2]) < TONE_TO_TOTAL_ENERGY1.995f*s->energy) |
337 | { |
338 | k1 = -1; |
339 | k2 = -1; |
340 | } |
341 | else if (res[k1] > TONE_TWIST3.981f*res[k2]) |
342 | { |
343 | k2 = -1; |
344 | } |
345 | else if (k2 < k1) |
346 | { |
347 | j = k1; |
348 | k1 = k2; |
349 | k2 = j; |
350 | } |
351 | |
352 | } |
353 | |
354 | } |
355 | |
356 | |
357 | if (k1 != s->segments[10].f1 || k2 != s->segments[10].f2) |
358 | { |
359 | |
360 | |
361 | |
362 | s->segments[10].f1 = k1; |
363 | s->segments[10].f2 = k2; |
364 | |
365 | |
366 | s->segments[9].min_duration++; |
367 | } |
368 | else |
369 | { |
370 | if (k1 != s->segments[9].f1 || k2 != s->segments[9].f2) |
371 | { |
372 | if (s->detected_tone >= 0) |
373 | { |
374 | |
375 | |
376 | if (!test_cadence(s->desc->tone_list[s->detected_tone], -s->desc->tone_segs[s->detected_tone], s->segments, s->rotation++)) |
377 | { |
378 | s->detected_tone = -1; |
379 | s->tone_callback(s->callback_data, s->detected_tone, -10, 0); |
380 | } |
381 | |
382 | } |
383 | |
384 | if (s->segment_callback) |
385 | { |
386 | s->segment_callback(s->callback_data, |
387 | s->segments[9].f1, |
388 | s->segments[9].f2, |
389 | s->segments[9].min_duration*SUPER_TONE_BINS128/8); |
390 | } |
391 | |
392 | memmove(&s->segments[0], &s->segments[1], 9*sizeof(s->segments[0])); |
393 | s->segments[9].f1 = k1; |
394 | s->segments[9].f2 = k2; |
395 | s->segments[9].min_duration = 1; |
396 | } |
397 | else |
398 | { |
399 | |
400 | if (s->detected_tone >= 0) |
401 | { |
402 | |
403 | |
404 | if (!test_cadence(s->desc->tone_list[s->detected_tone], s->desc->tone_segs[s->detected_tone], s->segments, s->rotation)) |
405 | { |
406 | s->detected_tone = -1; |
407 | s->tone_callback(s->callback_data, s->detected_tone, -10, 0); |
408 | } |
409 | |
410 | } |
411 | |
412 | s->segments[9].min_duration++; |
413 | } |
414 | |
415 | } |
416 | |
417 | if (s->detected_tone < 0) |
418 | { |
419 | |
420 | for (j = 0; j < s->desc->tones; j++) |
421 | { |
422 | if (test_cadence(s->desc->tone_list[j], s->desc->tone_segs[j], s->segments, -1)) |
423 | { |
424 | s->detected_tone = j; |
425 | s->rotation = 0; |
426 | s->tone_callback(s->callback_data, s->detected_tone, -10, 0); |
427 | break; |
428 | } |
429 | |
430 | } |
431 | |
432 | } |
433 | |
434 | #if defined(SPANDSP_USE_FIXED_POINT) |
435 | s->energy = 0; |
436 | #else |
437 | s->energy = 0.0f; |
438 | #endif |
439 | } |
440 | |
441 | |
442 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx(super_tone_rx_state_t *s, const int16_t amp[], int samples) |
443 | { |
444 | int i; |
445 | int x; |
446 | int sample; |
447 | #if defined(SPANDSP_USE_FIXED_POINT) |
448 | int16_t xamp; |
449 | #else |
450 | float xamp; |
451 | #endif |
452 | |
453 | x = 0; |
454 | for (sample = 0; sample < samples; sample += x) |
455 | { |
456 | for (i = 0; i < s->desc->monitored_frequencies; i++) |
457 | x = goertzel_update(&s->state[i], amp + sample, samples - sample); |
458 | |
459 | for (i = 0; i < x; i++) |
460 | { |
461 | xamp = goertzel_preadjust_amp(amp[sample + i])((float) amp[sample + i]); |
462 | #if defined(SPANDSP_USE_FIXED_POINT) |
463 | s->energy += ((int32_t) xamp*xamp); |
464 | #else |
465 | s->energy += xamp*xamp; |
466 | #endif |
467 | } |
468 | |
469 | if (s->state[0].current_sample >= SUPER_TONE_BINS128) |
470 | { |
471 | |
472 | super_tone_chunk(s); |
473 | #if defined(SPANDSP_USE_FIXED_POINT) |
474 | s->energy = 0; |
475 | #else |
476 | s->energy = 0.0f; |
477 | #endif |
478 | } |
479 | |
480 | } |
481 | |
482 | return samples; |
483 | } |
484 | |
485 | |
486 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_fillin(super_tone_rx_state_t *s, int samples) |
487 | { |
488 | |
489 | return 0; |
490 | } |
491 | |
492 | |
493 | SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_segment_callback(super_tone_rx_state_t *s, |
494 | tone_segment_func_t callback) |
495 | { |
496 | s->segment_callback = callback; |
497 | } |
498 | |
499 | |
500 | SPAN_DECLARE(super_tone_rx_state_t *)__attribute__((visibility("default"))) super_tone_rx_state_t * super_tone_rx_init(super_tone_rx_state_t *s, |
501 | super_tone_rx_descriptor_t *desc, |
502 | span_tone_report_func_t callback, |
503 | void *user_data) |
504 | { |
505 | int i; |
506 | |
507 | if (desc == NULL((void*)0)) |
508 | return NULL((void*)0); |
509 | |
510 | if (callback == NULL((void*)0)) |
511 | return NULL((void*)0); |
512 | |
513 | if (s == NULL((void*)0)) |
514 | { |
515 | if ((s = (super_tone_rx_state_t *) span_alloc(sizeof(*s) + desc->monitored_frequencies*sizeof(goertzel_state_t))) == NULL((void*)0)) |
516 | return NULL((void*)0); |
517 | |
518 | } |
519 | |
520 | |
521 | for (i = 0; i < 11; i++) |
522 | { |
523 | s->segments[i].f1 = -1; |
524 | s->segments[i].f2 = -1; |
525 | s->segments[i].min_duration = 0; |
526 | } |
527 | |
528 | s->segment_callback = NULL((void*)0); |
529 | s->tone_callback = callback; |
530 | s->callback_data = user_data; |
531 | if (desc) |
532 | s->desc = desc; |
533 | |
534 | s->detected_tone = -1; |
535 | #if defined(SPANDSP_USE_FIXED_POINT) |
536 | s->energy = 0; |
537 | #else |
538 | s->energy = 0.0f; |
539 | #endif |
540 | for (i = 0; i < desc->monitored_frequencies; i++) |
541 | goertzel_init(&s->state[i], &s->desc->desc[i]); |
542 | |
543 | return s; |
544 | } |
545 | |
546 | |
547 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_release(super_tone_rx_state_t *s) |
548 | { |
549 | return 0; |
550 | } |
551 | |
552 | |
553 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free(super_tone_rx_state_t *s) |
554 | { |
555 | if (s) |
556 | span_free(s); |
557 | |
558 | return 0; |
559 | } |
560 | |
561 | |