| 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; | 
|  | | 19 |  | 'j' declared without an initial value |  | 
 | 
| 159 |  | 
| 160 | if (rotation >= 0) | 
|  |  | 
| 161 | { | 
| 162 |  | 
| 163 |  | 
| 164 |  | 
| 165 | if (steps < 0) | 
|  | | 21 |  | Assuming 'steps' is >= 0 |  | 
 | 
|  |  | 
| 166 | { | 
| 167 |  | 
| 168 |  | 
| 169 |  | 
| 170 | steps = -steps; | 
| 171 | j = (rotation + steps - 2)%steps; | 
| 172 | if (pattern[j].f1 != test[8].f1  ||  pattern[j].f2 != test[8].f2) | 
| 173 | return 0; | 
| 174 |  | 
| 175 | if (pattern[j].min_duration > test[8].min_duration*SUPER_TONE_BINS128 | 
| 176 | || | 
| 177 | pattern[j].max_duration < test[8].min_duration*SUPER_TONE_BINS128) | 
| 178 | { | 
| 179 | return 0; | 
| 180 | } | 
| 181 |  | 
| 182 | } | 
| 183 |  | 
| 184 | if (steps) | 
|  |  | 
| 185 | j = (rotation + steps - 1)%steps; | 
| 186 |  | 
| 187 | if (pattern[j].f1 != test[9].f1  ||  pattern[j].f2 != test[9].f2) | 
|  | | 24 |  | Array subscript is undefined | 
 | 
| 188 | return 0; | 
| 189 |  | 
| 190 | if (pattern[j].max_duration < test[9].min_duration*SUPER_TONE_BINS128) | 
| 191 | return 0; | 
| 192 |  | 
| 193 | } | 
| 194 | else | 
| 195 | { | 
| 196 |  | 
| 197 | for (i = 0;  i < steps;  i++) | 
| 198 | { | 
| 199 | j = i + 10 - steps; | 
| 200 | if (pattern[i].f1 != test[j].f1  ||  pattern[i].f2 != test[j].f2) | 
| 201 | return 0; | 
| 202 |  | 
| 203 | if (pattern[i].min_duration > test[j].min_duration*SUPER_TONE_BINS128 | 
| 204 | || | 
| 205 | pattern[i].max_duration < test[j].min_duration*SUPER_TONE_BINS128) | 
| 206 | { | 
| 207 | return 0; | 
| 208 | } | 
| 209 |  | 
| 210 | } | 
| 211 |  | 
| 212 | } | 
| 213 |  | 
| 214 | return 1; | 
| 215 | } | 
| 216 |  | 
| 217 |  | 
| 218 | 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)
 | 
| 219 | { | 
| 220 | if (desc == NULL((void*)0)) | 
| 221 | { | 
| 222 | if ((desc = (super_tone_rx_descriptor_t *) span_alloc(sizeof(*desc))) == NULL((void*)0)) | 
| 223 | return NULL((void*)0); | 
| 224 |  | 
| 225 | } | 
| 226 |  | 
| 227 | desc->tone_list = NULL((void*)0); | 
| 228 | desc->tone_segs = NULL((void*)0); | 
| 229 |  | 
| 230 | desc->used_frequencies = 0; | 
| 231 | desc->monitored_frequencies = 0; | 
| 232 | desc->desc = NULL((void*)0); | 
| 233 | desc->tones = 0; | 
| 234 | return desc; | 
| 235 | } | 
| 236 |  | 
| 237 |  | 
| 238 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free_descriptor(super_tone_rx_descriptor_t *desc) | 
| 239 | { | 
| 240 | int i; | 
| 241 |  | 
| 242 | if (desc) | 
| 243 | { | 
| 244 | for (i = 0; i < desc->tones; i++) | 
| 245 | { | 
| 246 | if (desc->tone_list[i]) | 
| 247 | span_free(desc->tone_list[i]); | 
| 248 |  | 
| 249 | } | 
| 250 |  | 
| 251 | if (desc->tone_list) | 
| 252 | span_free(desc->tone_list); | 
| 253 |  | 
| 254 | if (desc->tone_segs) | 
| 255 | span_free(desc->tone_segs); | 
| 256 |  | 
| 257 | if (desc->desc) | 
| 258 | span_free(desc->desc); | 
| 259 |  | 
| 260 | span_free(desc); | 
| 261 | } | 
| 262 |  | 
| 263 | return 0; | 
| 264 | } | 
| 265 |  | 
| 266 |  | 
| 267 | SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_tone_callback(super_tone_rx_state_t *s, | 
| 268 | span_tone_report_func_t callback, | 
| 269 | void *user_data) | 
| 270 | { | 
| 271 | s->tone_callback = callback; | 
| 272 | s->callback_data = user_data; | 
| 273 | } | 
| 274 |  | 
| 275 |  | 
| 276 | static void super_tone_chunk(super_tone_rx_state_t *s) | 
| 277 | { | 
| 278 | int i; | 
| 279 | int j; | 
| 280 | int k1; | 
| 281 | int k2; | 
| 282 | #if defined(SPANDSP_USE_FIXED_POINT) | 
| 283 | int32_t res[SUPER_TONE_BINS128/2]; | 
| 284 | #else | 
| 285 | float res[SUPER_TONE_BINS128/2]; | 
| 286 | #endif | 
| 287 |  | 
| 288 | for (i = 0;  i < s->desc->monitored_frequencies;  i++) | 
|  | | 13 |  | Loop condition is false. Execution continues on line 292 |  | 
 | 
| 289 | res[i] = goertzel_result(&s->state[i]); | 
| 290 |  | 
| 291 |  | 
| 292 | if (s->energy < DETECTION_THRESHOLD2104205.6f) | 
|  |  | 
| 293 | { | 
| 294 | k1 = -1; | 
| 295 | k2 = -1; | 
| 296 | } | 
| 297 | else | 
| 298 | { | 
| 299 | if (res[0] > res[1]) | 
| 300 | { | 
| 301 | k1 = 0; | 
| 302 | k2 = 1; | 
| 303 | } | 
| 304 | else | 
| 305 | { | 
| 306 | k1 = 1; | 
| 307 | k2 = 0; | 
| 308 | } | 
| 309 |  | 
| 310 | for (j = 2;  j < s->desc->monitored_frequencies;  j++) | 
| 311 | { | 
| 312 | if (res[j] >= res[k1]) | 
| 313 | { | 
| 314 | k2 = k1; | 
| 315 | k1 = j; | 
| 316 | } | 
| 317 | else if (res[j] >= res[k2]) | 
| 318 | { | 
| 319 | k2 = j; | 
| 320 | } | 
| 321 |  | 
| 322 | } | 
| 323 |  | 
| 324 | if ((res[k1] + res[k2]) < TONE_TO_TOTAL_ENERGY1.995f*s->energy) | 
| 325 | { | 
| 326 | k1 = -1; | 
| 327 | k2 = -1; | 
| 328 | } | 
| 329 | else if (res[k1] > TONE_TWIST3.981f*res[k2]) | 
| 330 | { | 
| 331 | k2 = -1; | 
| 332 | } | 
| 333 | else if (k2 < k1) | 
| 334 | { | 
| 335 | j = k1; | 
| 336 | k1 = k2; | 
| 337 | k2 = j; | 
| 338 | } | 
| 339 |  | 
| 340 | } | 
| 341 |  | 
| 342 |  | 
| 343 | if (k1 != s->segments[10].f1  ||  k2 != s->segments[10].f2) | 
|  |  | 
| 344 | { | 
| 345 |  | 
| 346 |  | 
| 347 |  | 
| 348 | s->segments[10].f1 = k1; | 
| 349 | s->segments[10].f2 = k2; | 
| 350 |  | 
| 351 |  | 
| 352 | s->segments[9].min_duration++; | 
| 353 | } | 
| 354 | else | 
| 355 | { | 
| 356 | if (k1 != s->segments[9].f1  ||  k2 != s->segments[9].f2) | 
|  |  | 
| 357 | { | 
| 358 | if (s->detected_tone >= 0) | 
| 359 | { | 
| 360 |  | 
| 361 |  | 
| 362 | if (!test_cadence(s->desc->tone_list[s->detected_tone], -s->desc->tone_segs[s->detected_tone], s->segments, s->rotation++)) | 
| 363 | { | 
| 364 | s->detected_tone = -1; | 
| 365 | s->tone_callback(s->callback_data, s->detected_tone, -10, 0); | 
| 366 | } | 
| 367 |  | 
| 368 | } | 
| 369 |  | 
| 370 | if (s->segment_callback) | 
| 371 | { | 
| 372 | s->segment_callback(s->callback_data, | 
| 373 | s->segments[9].f1, | 
| 374 | s->segments[9].f2, | 
| 375 | s->segments[9].min_duration*SUPER_TONE_BINS128/8); | 
| 376 | } | 
| 377 |  | 
| 378 | memmove(&s->segments[0], &s->segments[1], 9*sizeof(s->segments[0])); | 
| 379 | s->segments[9].f1 = k1; | 
| 380 | s->segments[9].f2 = k2; | 
| 381 | s->segments[9].min_duration = 1; | 
| 382 | } | 
| 383 | else | 
| 384 | { | 
| 385 |  | 
| 386 | if (s->detected_tone >= 0) | 
|  |  | 
| 387 | { | 
| 388 |  | 
| 389 |  | 
| 390 | if (!test_cadence(s->desc->tone_list[s->detected_tone], s->desc->tone_segs[s->detected_tone], s->segments, s->rotation)) | 
|  |  | 
| 391 | { | 
| 392 | s->detected_tone = -1; | 
| 393 | s->tone_callback(s->callback_data, s->detected_tone, -10, 0); | 
| 394 | } | 
| 395 |  | 
| 396 | } | 
| 397 |  | 
| 398 | s->segments[9].min_duration++; | 
| 399 | } | 
| 400 |  | 
| 401 | } | 
| 402 |  | 
| 403 | if (s->detected_tone < 0) | 
| 404 | { | 
| 405 |  | 
| 406 | for (j = 0;  j < s->desc->tones;  j++) | 
| 407 | { | 
| 408 | if (test_cadence(s->desc->tone_list[j], s->desc->tone_segs[j], s->segments, -1)) | 
| 409 | { | 
| 410 | s->detected_tone = j; | 
| 411 | s->rotation = 0; | 
| 412 | s->tone_callback(s->callback_data, s->detected_tone, -10, 0); | 
| 413 | break; | 
| 414 | } | 
| 415 |  | 
| 416 | } | 
| 417 |  | 
| 418 | } | 
| 419 |  | 
| 420 | #if defined(SPANDSP_USE_FIXED_POINT) | 
| 421 | s->energy = 0; | 
| 422 | #else | 
| 423 | s->energy = 0.0f; | 
| 424 | #endif | 
| 425 | } | 
| 426 |  | 
| 427 |  | 
| 428 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx(super_tone_rx_state_t *s, const int16_t amp[], int samples) | 
| 429 | { | 
| 430 | int i; | 
| 431 | int x; | 
| 432 | int sample; | 
| 433 | #if defined(SPANDSP_USE_FIXED_POINT) | 
| 434 | int16_t xamp; | 
| 435 | #else | 
| 436 | float xamp; | 
| 437 | #endif | 
| 438 |  | 
| 439 | x = 0; | 
| 440 | for (sample = 0;  sample < samples;  sample += x) | 
|  | | 1 | Assuming 'sample' is < 'samples' |  | 
 | 
|  | | 2 |  | Loop condition is true.  Entering loop body |  | 
 | 
|  | | 8 |  | Loop condition is true.  Entering loop body |  | 
 | 
| 441 | { | 
| 442 | for (i = 0;  i < s->desc->monitored_frequencies;  i++) | 
|  | | 3 |  | Assuming the condition is false |  | 
 | 
|  | | 4 |  | Loop condition is false. Execution continues on line 445 |  | 
 | 
|  | | 9 |  | Loop condition is false. Execution continues on line 445 |  | 
 | 
| 443 | x = goertzel_update(&s->state[i], amp + sample, samples - sample); | 
| 444 |  | 
| 445 | for (i = 0;  i < x;  i++) | 
|  | | 5 |  | Loop condition is false. Execution continues on line 455 |  | 
 | 
|  | | 10 |  | Loop condition is false. Execution continues on line 455 |  | 
 | 
| 446 | { | 
| 447 | xamp = goertzel_preadjust_amp(amp[sample + i])((float) amp[sample + i]); | 
| 448 | #if defined(SPANDSP_USE_FIXED_POINT) | 
| 449 | s->energy += ((int32_t) xamp*xamp); | 
| 450 | #else | 
| 451 | s->energy += xamp*xamp; | 
| 452 | #endif | 
| 453 | } | 
| 454 |  | 
| 455 | if (s->state[0].current_sample >= SUPER_TONE_BINS128) | 
|  | | 6 |  | Assuming the condition is true |  | 
 | 
|  |  | 
|  |  | 
| 456 | { | 
| 457 |  | 
| 458 | super_tone_chunk(s); | 
|  | | 12 |  | Calling 'super_tone_chunk' |  | 
 | 
| 459 | #if defined(SPANDSP_USE_FIXED_POINT) | 
| 460 | s->energy = 0; | 
| 461 | #else | 
| 462 | s->energy = 0.0f; | 
| 463 | #endif | 
| 464 | } | 
| 465 |  | 
| 466 | } | 
| 467 |  | 
| 468 | return samples; | 
| 469 | } | 
| 470 |  | 
| 471 |  | 
| 472 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_fillin(super_tone_rx_state_t *s, int samples) | 
| 473 | { | 
| 474 |  | 
| 475 | return 0; | 
| 476 | } | 
| 477 |  | 
| 478 |  | 
| 479 | SPAN_DECLARE(void)__attribute__((visibility("default"))) void super_tone_rx_segment_callback(super_tone_rx_state_t *s, | 
| 480 | tone_segment_func_t callback) | 
| 481 | { | 
| 482 | s->segment_callback = callback; | 
| 483 | } | 
| 484 |  | 
| 485 |  | 
| 486 | 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, | 
| 487 | super_tone_rx_descriptor_t *desc, | 
| 488 | span_tone_report_func_t callback, | 
| 489 | void *user_data) | 
| 490 | { | 
| 491 | int i; | 
| 492 |  | 
| 493 | if (desc == NULL((void*)0)) | 
| 494 | return NULL((void*)0); | 
| 495 |  | 
| 496 | if (callback == NULL((void*)0)) | 
| 497 | return NULL((void*)0); | 
| 498 |  | 
| 499 | if (s == NULL((void*)0)) | 
| 500 | { | 
| 501 | if ((s = (super_tone_rx_state_t *) span_alloc(sizeof(*s) + desc->monitored_frequencies*sizeof(goertzel_state_t))) == NULL((void*)0)) | 
| 502 | return NULL((void*)0); | 
| 503 |  | 
| 504 | } | 
| 505 |  | 
| 506 |  | 
| 507 | for (i = 0;  i < 11;  i++) | 
| 508 | { | 
| 509 | s->segments[i].f1 = -1; | 
| 510 | s->segments[i].f2 = -1; | 
| 511 | s->segments[i].min_duration = 0; | 
| 512 | } | 
| 513 |  | 
| 514 | s->segment_callback = NULL((void*)0); | 
| 515 | s->tone_callback = callback; | 
| 516 | s->callback_data = user_data; | 
| 517 | if (desc) | 
| 518 | s->desc = desc; | 
| 519 |  | 
| 520 | s->detected_tone = -1; | 
| 521 | #if defined(SPANDSP_USE_FIXED_POINT) | 
| 522 | s->energy = 0; | 
| 523 | #else | 
| 524 | s->energy = 0.0f; | 
| 525 | #endif | 
| 526 | for (i = 0;  i < desc->monitored_frequencies;  i++) | 
| 527 | goertzel_init(&s->state[i], &s->desc->desc[i]); | 
| 528 |  | 
| 529 | return s; | 
| 530 | } | 
| 531 |  | 
| 532 |  | 
| 533 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_release(super_tone_rx_state_t *s) | 
| 534 | { | 
| 535 | return 0; | 
| 536 | } | 
| 537 |  | 
| 538 |  | 
| 539 | SPAN_DECLARE(int)__attribute__((visibility("default"))) int super_tone_rx_free(super_tone_rx_state_t *s) | 
| 540 | { | 
| 541 | if (s) | 
| 542 | span_free(s); | 
| 543 |  | 
| 544 | return 0; | 
| 545 | } | 
| 546 |  | 
| 547 |  |