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