| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#include "asterisk.h" |
| 57 |
|
| 58 |
#include <math.h> |
| 59 |
|
| 60 |
#include "asterisk/module.h" |
| 61 |
#include "asterisk/frame.h" |
| 62 |
#include "asterisk/format_cache.h" |
| 63 |
#include "asterisk/channel.h" |
| 64 |
#include "asterisk/dsp.h" |
| 65 |
#include "asterisk/ulaw.h" |
| 66 |
#include "asterisk/alaw.h" |
| 67 |
#include "asterisk/utils.h" |
| 68 |
#include "asterisk/options.h" |
| 69 |
#include "asterisk/config.h" |
| 70 |
#include "asterisk/test.h" |
| 71 |
|
| 72 |
|
| 73 |
enum gsamp_size { |
| 74 |
GSAMP_SIZE_NA = 183, |
| 75 |
GSAMP_SIZE_CR = 188, |
| 76 |
GSAMP_SIZE_UK = 160 |
| 77 |
}; |
| 78 |
|
| 79 |
enum prog_mode { |
| 80 |
PROG_MODE_NA = 0, |
| 81 |
PROG_MODE_CR, |
| 82 |
PROG_MODE_UK |
| 83 |
}; |
| 84 |
|
| 85 |
enum freq_index { |
| 86 |
|
| 87 |
HZ_350 = 0, |
| 88 |
HZ_440, |
| 89 |
HZ_480, |
| 90 |
HZ_620, |
| 91 |
HZ_950, |
| 92 |
HZ_1400, |
| 93 |
HZ_1800, |
| 94 |
|
| 95 |
|
| 96 |
HZ_425 = 0, |
| 97 |
|
| 98 |
|
| 99 |
HZ_350UK = 0, |
| 100 |
HZ_400UK, |
| 101 |
HZ_440UK |
| 102 |
}; |
| 103 |
|
| 104 |
static struct progalias { |
| 105 |
char *name; |
| 106 |
enum prog_mode mode; |
| 107 |
} aliases[] = { |
| 108 |
{ "us", PROG_MODE_NA }, |
| 109 |
{ "ca", PROG_MODE_NA }, |
| 110 |
{ "cr", PROG_MODE_CR }, |
| 111 |
{ "br", PROG_MODE_CR }, |
| 112 |
{ "uk", PROG_MODE_UK }, |
| 113 |
}; |
| 114 |
|
| 115 |
#define FREQ_ARRAY_SIZE 7 |
| 116 |
|
| 117 |
static struct progress { |
| 118 |
enum gsamp_size size; |
| 119 |
int freqs[FREQ_ARRAY_SIZE]; |
| 120 |
} modes[] = { |
| 121 |
{ GSAMP_SIZE_NA, { 350, 440, 480, 620, 950, 1400, 1800 } }, |
| 122 |
{ GSAMP_SIZE_CR, { 425 } }, |
| 123 |
{ GSAMP_SIZE_UK, { 350, 400, 440 } }, |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#define DEFAULT_THRESHOLD 512 |
| 141 |
|
| 142 |
enum busy_detect { |
| 143 |
BUSY_PERCENT = 10, |
| 144 |
BUSY_PAT_PERCENT = 7, |
| 145 |
BUSY_THRESHOLD = 100, |
| 146 |
BUSY_MIN = 75, |
| 147 |
BUSY_MAX = 3100 |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
#define DSP_HISTORY 15 |
| 152 |
|
| 153 |
#define TONE_THRESH 10.0 |
| 154 |
#define TONE_MIN_THRESH 1e8 |
| 155 |
|
| 156 |
|
| 157 |
enum gsamp_thresh { |
| 158 |
THRESH_RING = 8, |
| 159 |
THRESH_TALK = 2, |
| 160 |
THRESH_BUSY = 4, |
| 161 |
THRESH_CONGESTION = 4, |
| 162 |
THRESH_HANGUP = 60, |
| 163 |
THRESH_RING2ANSWER = 300 |
| 164 |
}; |
| 165 |
|
| 166 |
#define MAX_DTMF_DIGITS 128 |
| 167 |
|
| 168 |
#define DTMF_MATRIX_SIZE 4 |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
#define DTMF_THRESHOLD 8.0e7 |
| 183 |
#define TONE_THRESHOLD 7.8e7 |
| 184 |
|
| 185 |
#define DEF_DTMF_NORMAL_TWIST 6.31 |
| 186 |
#define DEF_RELAX_DTMF_NORMAL_TWIST 6.31 |
| 187 |
|
| 188 |
#ifdef RADIO_RELAX |
| 189 |
#define DEF_DTMF_REVERSE_TWIST 2.51 |
| 190 |
#define DEF_RELAX_DTMF_REVERSE_TWIST 6.61 |
| 191 |
#else |
| 192 |
#define DEF_DTMF_REVERSE_TWIST 2.51 |
| 193 |
#define DEF_RELAX_DTMF_REVERSE_TWIST 3.98 |
| 194 |
#endif |
| 195 |
|
| 196 |
#define DTMF_RELATIVE_PEAK_ROW 6.3 |
| 197 |
#define DTMF_RELATIVE_PEAK_COL 6.3 |
| 198 |
#define DTMF_TO_TOTAL_ENERGY 42.0 |
| 199 |
|
| 200 |
#define BELL_MF_THRESHOLD 1.6e9 |
| 201 |
#define BELL_MF_TWIST 4.0 |
| 202 |
#define BELL_MF_RELATIVE_PEAK 12.6 |
| 203 |
|
| 204 |
#define R2_MF_THRESHOLD 5.0e8f |
| 205 |
#define R2_MF_TWIST 5.0f |
| 206 |
#define R2_MF_RELATIVE_PEAK 12.6f |
| 207 |
|
| 208 |
#if defined(BUSYDETECT_TONEONLY) && defined(BUSYDETECT_COMPARE_TONE_AND_SILENCE) |
| 209 |
#error You cant use BUSYDETECT_TONEONLY together with BUSYDETECT_COMPARE_TONE_AND_SILENCE |
| 210 |
#endif |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
#define FAX_TONE_CNG_FREQ 1100 |
| 216 |
#define FAX_TONE_CNG_DURATION 500 |
| 217 |
#define FAX_TONE_CNG_DB 16 |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
#define FAX_TONE_CED_FREQ 2100 |
| 224 |
#define FAX_TONE_CED_DURATION 2600 |
| 225 |
#define FAX_TONE_CED_DB 16 |
| 226 |
|
| 227 |
#define DEFAULT_SAMPLE_RATE 8000 |
| 228 |
|
| 229 |
|
| 230 |
#define MF_GSIZE 120 |
| 231 |
|
| 232 |
|
| 233 |
#define DTMF_GSIZE 102 |
| 234 |
|
| 235 |
|
| 236 |
#define R2_GSIZE 133 |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
#define DEF_DTMF_HITS_TO_BEGIN 2 |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
#define DEF_DTMF_MISSES_TO_END 3 |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
static const int DEFAULT_SILENCE_THRESHOLD = 256; |
| 253 |
|
| 254 |
#define CONFIG_FILE_NAME "dsp.conf" |
| 255 |
|
| 256 |
typedef struct { |
| 257 |
|
| 258 |
int v2; |
| 259 |
|
| 260 |
int v3; |
| 261 |
|
| 262 |
int chunky; |
| 263 |
|
| 264 |
int fac; |
| 265 |
} goertzel_state_t; |
| 266 |
|
| 267 |
typedef struct { |
| 268 |
int value; |
| 269 |
int power; |
| 270 |
} goertzel_result_t; |
| 271 |
|
| 272 |
typedef struct |
| 273 |
{ |
| 274 |
int freq; |
| 275 |
int block_size; |
| 276 |
int squelch; |
| 277 |
goertzel_state_t tone; |
| 278 |
float energy; |
| 279 |
int samples_pending; |
| 280 |
int mute_samples; |
| 281 |
|
| 282 |
int hits_required; |
| 283 |
float threshold; |
| 284 |
|
| 285 |
int hit_count; |
| 286 |
int last_hit; |
| 287 |
|
| 288 |
} tone_detect_state_t; |
| 289 |
|
| 290 |
typedef struct |
| 291 |
{ |
| 292 |
goertzel_state_t row_out[DTMF_MATRIX_SIZE]; |
| 293 |
goertzel_state_t col_out[DTMF_MATRIX_SIZE]; |
| 294 |
int hits; |
| 295 |
int misses; |
| 296 |
int lasthit; |
| 297 |
int current_hit; |
| 298 |
float energy; |
| 299 |
int current_sample; |
| 300 |
int mute_samples; |
| 301 |
} dtmf_detect_state_t; |
| 302 |
|
| 303 |
typedef struct |
| 304 |
{ |
| 305 |
goertzel_state_t tone_out[6]; |
| 306 |
int current_hit; |
| 307 |
int hits[5]; |
| 308 |
int current_sample; |
| 309 |
int mute_samples; |
| 310 |
} mf_detect_state_t; |
| 311 |
|
| 312 |
typedef struct |
| 313 |
{ |
| 314 |
char digits[MAX_DTMF_DIGITS + 1]; |
| 315 |
int digitlen[MAX_DTMF_DIGITS + 1]; |
| 316 |
int current_digits; |
| 317 |
int detected_digits; |
| 318 |
int lost_digits; |
| 319 |
|
| 320 |
union { |
| 321 |
dtmf_detect_state_t dtmf; |
| 322 |
mf_detect_state_t mf; |
| 323 |
} td; |
| 324 |
} digit_detect_state_t; |
| 325 |
|
| 326 |
static const float dtmf_row[] = { |
| 327 |
697.0, 770.0, 852.0, 941.0 |
| 328 |
}; |
| 329 |
static const float dtmf_col[] = { |
| 330 |
1209.0, 1336.0, 1477.0, 1633.0 |
| 331 |
}; |
| 332 |
static const float mf_tones[] = { |
| 333 |
700.0, 900.0, 1100.0, 1300.0, 1500.0, 1700.0 |
| 334 |
}; |
| 335 |
static const float r2_forward_tones[] = { |
| 336 |
1380.0, 1500.0, 1620.0, 1740.0, 1860.0, 1980.0 |
| 337 |
}; |
| 338 |
static const float r2_backward_tones[] = { |
| 339 |
1140.0, 1020.0, 900.0, 780.0, 660.0, 540.0 |
| 340 |
}; |
| 341 |
static const char dtmf_positions[] = "123A" "456B" "789C" "*0#D"; |
| 342 |
static const char bell_mf_positions[] = "1247C-358A--69*---0B----#"; |
| 343 |
static const char r2_mf_positions[] = "1247B-358C--69D---0E----F"; |
| 344 |
static int thresholds[THRESHOLD_MAX]; |
| 345 |
static float dtmf_normal_twist; |
| 346 |
static float dtmf_reverse_twist; |
| 347 |
static float relax_dtmf_normal_twist; |
| 348 |
static float relax_dtmf_reverse_twist; |
| 349 |
static int dtmf_hits_to_begin; |
| 350 |
static int dtmf_misses_to_end; |
| 351 |
|
| 352 |
static inline void goertzel_sample(goertzel_state_t *s, short sample) |
| 353 |
{ |
| 354 |
int v1; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
v1 = s->v2; |
| 363 |
s->v2 = s->v3; |
| 364 |
|
| 365 |
|
| 366 |
s->v3 = (s->fac * s->v2) >> 15; |
| 367 |
|
| 368 |
s->v3 = s->v3 - v1 + (sample >> s->chunky); |
| 369 |
|
| 370 |
if (abs(s->v3) > (1 << 15)) { |
| 371 |
|
| 372 |
s->chunky++; |
| 373 |
s->v3 = s->v3 >> 1; |
| 374 |
s->v2 = s->v2 >> 1; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
static inline float goertzel_result(goertzel_state_t *s) |
| 379 |
{ |
| 380 |
goertzel_result_t r; |
| 381 |
|
| 382 |
r.value = (s->v3 * s->v3) + (s->v2 * s->v2); |
| 383 |
r.value -= ((s->v2 * s->v3) >> 15) * s->fac; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
r.power = s->chunky * 2; |
| 389 |
return (float)r.value * (float)(1 << r.power); |
| 390 |
} |
| 391 |
|
| 392 |
static inline void goertzel_init(goertzel_state_t *s, float freq, unsigned int sample_rate) |
| 393 |
{ |
| 394 |
s->v2 = s->v3 = s->chunky = 0; |
| 395 |
s->fac = (int)(32768.0 * 2.0 * cos(2.0 * M_PI * freq / sample_rate)); |
| 396 |
} |
| 397 |
|
| 398 |
static inline void goertzel_reset(goertzel_state_t *s) |
| 399 |
{ |
| 400 |
s->v2 = s->v3 = s->chunky = 0; |
| 401 |
} |
| 402 |
|
| 403 |
typedef struct { |
| 404 |
int start; |
| 405 |
int end; |
| 406 |
} fragment_t; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
struct ast_dsp { |
| 422 |
struct ast_frame f; |
| 423 |
int threshold; |
| 424 |
|
| 425 |
int totalsilence; |
| 426 |
|
| 427 |
int totalnoise; |
| 428 |
int features; |
| 429 |
int ringtimeout; |
| 430 |
int busymaybe; |
| 431 |
int busycount; |
| 432 |
struct ast_dsp_busy_pattern busy_cadence; |
| 433 |
int historicnoise[DSP_HISTORY]; |
| 434 |
int historicsilence[DSP_HISTORY]; |
| 435 |
goertzel_state_t freqs[FREQ_ARRAY_SIZE]; |
| 436 |
int freqcount; |
| 437 |
int gsamps; |
| 438 |
enum gsamp_size gsamp_size; |
| 439 |
enum prog_mode progmode; |
| 440 |
int tstate; |
| 441 |
int tcount; |
| 442 |
int digitmode; |
| 443 |
int faxmode; |
| 444 |
int freqmode; |
| 445 |
int dtmf_began; |
| 446 |
int display_inband_dtmf_warning; |
| 447 |
float genergy; |
| 448 |
int mute_fragments; |
| 449 |
unsigned int sample_rate; |
| 450 |
fragment_t mute_data[5]; |
| 451 |
digit_detect_state_t digit_state; |
| 452 |
tone_detect_state_t cng_tone_state; |
| 453 |
tone_detect_state_t ced_tone_state; |
| 454 |
}; |
| 455 |
|
| 456 |
static void mute_fragment(struct ast_dsp *dsp, fragment_t *fragment) |
| 457 |
{ |
| 458 |
if (dsp->mute_fragments >= ARRAY_LEN(dsp->mute_data)) { |
| 459 |
ast_log(LOG_ERROR, "Too many fragments to mute. Ignoring\n"); |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
dsp->mute_data[dsp->mute_fragments++] = *fragment; |
| 464 |
} |
| 465 |
|
| 466 |
static void ast_tone_detect_init(tone_detect_state_t *s, int freq, int duration, int amp, unsigned int sample_rate) |
| 467 |
{ |
| 468 |
int duration_samples; |
| 469 |
float x; |
| 470 |
int periods_in_block; |
| 471 |
|
| 472 |
s->freq = freq; |
| 473 |
|
| 474 |
|
| 475 |
duration_samples = duration * sample_rate / 1000; |
| 476 |
|
| 477 |
duration_samples = duration_samples * 9 / 10; |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
s->block_size = (20 * sample_rate) / 1000; |
| 483 |
|
| 484 |
periods_in_block = s->block_size * freq / sample_rate; |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
if (periods_in_block < 5) { |
| 490 |
periods_in_block = 5; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
s->block_size = periods_in_block * sample_rate / freq; |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
s->squelch = 0; |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
s->hits_required = (duration_samples - (s->block_size - 1)) / s->block_size; |
| 503 |
|
| 504 |
goertzel_init(&s->tone, freq, sample_rate); |
| 505 |
|
| 506 |
s->samples_pending = s->block_size; |
| 507 |
s->hit_count = 0; |
| 508 |
s->last_hit = 0; |
| 509 |
s->energy = 0.0; |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
x = pow(10.0, amp / 10.0); |
| 522 |
s->threshold = x / (x + 1); |
| 523 |
|
| 524 |
ast_debug(1, "Setup tone %d Hz, %d ms, block_size=%d, hits_required=%d\n", freq, duration, s->block_size, s->hits_required); |
| 525 |
} |
| 526 |
|
| 527 |
static void ast_fax_detect_init(struct ast_dsp *s) |
| 528 |
{ |
| 529 |
ast_tone_detect_init(&s->cng_tone_state, FAX_TONE_CNG_FREQ, FAX_TONE_CNG_DURATION, FAX_TONE_CNG_DB, s->sample_rate); |
| 530 |
ast_tone_detect_init(&s->ced_tone_state, FAX_TONE_CED_FREQ, FAX_TONE_CED_DURATION, FAX_TONE_CED_DB, s->sample_rate); |
| 531 |
if (s->faxmode & DSP_FAXMODE_DETECT_SQUELCH) { |
| 532 |
s->cng_tone_state.squelch = 1; |
| 533 |
s->ced_tone_state.squelch = 1; |
| 534 |
} |
| 535 |
|
| 536 |
} |
| 537 |
|
| 538 |
static void ast_freq_detect_init(struct ast_dsp *s, int freq, int dur, int db, int squelch) |
| 539 |
{ |
| 540 |
|
| 541 |
ast_tone_detect_init(&s->cng_tone_state, freq, dur, db, s->sample_rate); |
| 542 |
if (s->freqmode & squelch) { |
| 543 |
s->cng_tone_state.squelch = 1; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
static void ast_dtmf_detect_init(dtmf_detect_state_t *s, unsigned int sample_rate) |
| 548 |
{ |
| 549 |
int i; |
| 550 |
|
| 551 |
for (i = 0; i < DTMF_MATRIX_SIZE; i++) { |
| 552 |
goertzel_init(&s->row_out[i], dtmf_row[i], sample_rate); |
| 553 |
goertzel_init(&s->col_out[i], dtmf_col[i], sample_rate); |
| 554 |
} |
| 555 |
s->lasthit = 0; |
| 556 |
s->current_hit = 0; |
| 557 |
s->energy = 0.0; |
| 558 |
s->current_sample = 0; |
| 559 |
s->hits = 0; |
| 560 |
s->misses = 0; |
| 561 |
} |
| 562 |
|
| 563 |
static void ast_mf_detect_init(mf_detect_state_t *s, unsigned int sample_rate) |
| 564 |
{ |
| 565 |
int i; |
| 566 |
|
| 567 |
for (i = 0; i < 6; i++) { |
| 568 |
goertzel_init(&s->tone_out[i], mf_tones[i], sample_rate); |
| 569 |
} |
| 570 |
s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0; |
| 571 |
s->current_sample = 0; |
| 572 |
s->current_hit = 0; |
| 573 |
} |
| 574 |
|
| 575 |
static void ast_r2_detect_init(mf_detect_state_t *s, unsigned int sample_rate, int backward) |
| 576 |
{ |
| 577 |
int i; |
| 578 |
|
| 579 |
for (i = 0; i < 6; i++) { |
| 580 |
goertzel_init(&s->tone_out[i], backward ? r2_backward_tones[i] : r2_forward_tones[i], sample_rate); |
| 581 |
} |
| 582 |
s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0; |
| 583 |
s->current_sample = 0; |
| 584 |
s->current_hit = 0; |
| 585 |
} |
| 586 |
|
| 587 |
static void ast_digit_detect_init(digit_detect_state_t *s, int digitmode, unsigned int sample_rate) |
| 588 |
{ |
| 589 |
s->current_digits = 0; |
| 590 |
s->detected_digits = 0; |
| 591 |
s->lost_digits = 0; |
| 592 |
s->digits[0] = '\0'; |
| 593 |
|
| 594 |
if (digitmode & DSP_DIGITMODE_R2_FORWARD) { |
| 595 |
ast_r2_detect_init(&s->td.mf, sample_rate, 0); |
| 596 |
} else if (digitmode & DSP_DIGITMODE_R2_BACKWARD) { |
| 597 |
ast_r2_detect_init(&s->td.mf, sample_rate, 1); |
| 598 |
} else if (digitmode & DSP_DIGITMODE_MF) { |
| 599 |
ast_mf_detect_init(&s->td.mf, sample_rate); |
| 600 |
} else { |
| 601 |
ast_dtmf_detect_init(&s->td.dtmf, sample_rate); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
static int tone_detect(struct ast_dsp *dsp, tone_detect_state_t *s, int16_t *amp, int samples) |
| 606 |
{ |
| 607 |
float tone_energy; |
| 608 |
int i; |
| 609 |
int hit = 0; |
| 610 |
int limit; |
| 611 |
int res = 0; |
| 612 |
int16_t *ptr; |
| 613 |
short samp; |
| 614 |
int start, end; |
| 615 |
fragment_t mute = {0, 0}; |
| 616 |
|
| 617 |
if (s->squelch && s->mute_samples > 0) { |
| 618 |
mute.end = (s->mute_samples < samples) ? s->mute_samples : samples; |
| 619 |
s->mute_samples -= mute.end; |
| 620 |
} |
| 621 |
|
| 622 |
for (start = 0; start < samples; start = end) { |
| 623 |
|
| 624 |
limit = samples - start; |
| 625 |
if (limit > s->samples_pending) { |
| 626 |
limit = s->samples_pending; |
| 627 |
} |
| 628 |
end = start + limit; |
| 629 |
|
| 630 |
for (i = limit, ptr = amp ; i > 0; i--, ptr++) { |
| 631 |
samp = *ptr; |
| 632 |
|
| 633 |
s->energy += (int32_t) samp * (int32_t) samp; |
| 634 |
|
| 635 |
goertzel_sample(&s->tone, samp); |
| 636 |
} |
| 637 |
|
| 638 |
s->samples_pending -= limit; |
| 639 |
|
| 640 |
if (s->samples_pending) { |
| 641 |
|
| 642 |
break; |
| 643 |
} |
| 644 |
|
| 645 |
tone_energy = goertzel_result(&s->tone); |
| 646 |
|
| 647 |
|
| 648 |
tone_energy *= 2.0; |
| 649 |
s->energy *= s->block_size; |
| 650 |
|
| 651 |
|
| 652 |
if (TONE_THRESHOLD <= tone_energy |
| 653 |
&& tone_energy > s->energy * s->threshold) { |
| 654 |
ast_debug(9, "%d Hz tone Hit! [%d/%d] Ew=%.4E, Et=%.4E, s/n=%10.2f\n", s->freq, s->hit_count + 1, s->hits_required, tone_energy, s->energy, tone_energy / (s->energy - tone_energy)); |
| 655 |
hit = 1; |
| 656 |
} else { |
| 657 |
ast_debug(10, "%d Hz tone [%d/%d] Ew=%.4E, Et=%.4E, s/n=%10.2f\n", s->freq, s->hit_count, s->hits_required, tone_energy, s->energy, tone_energy / (s->energy - tone_energy)); |
| 658 |
hit = 0; |
| 659 |
} |
| 660 |
|
| 661 |
if (s->hit_count) { |
| 662 |
s->hit_count++; |
| 663 |
} |
| 664 |
|
| 665 |
if (hit == s->last_hit) { |
| 666 |
if (!hit) { |
| 667 |
|
| 668 |
if (s->hit_count) { |
| 669 |
ast_debug(9, "Partial detect expired after %d/%d hits\n", s->hit_count, s->hits_required); |
| 670 |
} |
| 671 |
s->hit_count = 0; |
| 672 |
} else if (!s->hit_count) { |
| 673 |
s->hit_count++; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
if (s->hit_count == s->hits_required) { |
| 678 |
ast_debug(1, "%d Hz tone detected\n", s->freq); |
| 679 |
res = 1; |
| 680 |
} |
| 681 |
|
| 682 |
s->last_hit = hit; |
| 683 |
|
| 684 |
|
| 685 |
if (s->squelch && hit) { |
| 686 |
if (mute.end < start - s->block_size) { |
| 687 |
|
| 688 |
mute_fragment(dsp, &mute); |
| 689 |
mute.start = (start > s->block_size) ? (start - s->block_size) : 0; |
| 690 |
} |
| 691 |
mute.end = end + s->block_size; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
goertzel_reset(&s->tone); |
| 697 |
|
| 698 |
|
| 699 |
s->energy = 0.0; |
| 700 |
s->samples_pending = s->block_size; |
| 701 |
|
| 702 |
amp += limit; |
| 703 |
} |
| 704 |
|
| 705 |
if (s->squelch && mute.end) { |
| 706 |
if (mute.end > samples) { |
| 707 |
s->mute_samples = mute.end - samples; |
| 708 |
mute.end = samples; |
| 709 |
} |
| 710 |
mute_fragment(dsp, &mute); |
| 711 |
} |
| 712 |
|
| 713 |
return res; |
| 714 |
} |
| 715 |
|
| 716 |
static void store_digit(digit_detect_state_t *s, char digit) |
| 717 |
{ |
| 718 |
s->detected_digits++; |
| 719 |
if (s->current_digits < MAX_DTMF_DIGITS) { |
| 720 |
s->digitlen[s->current_digits] = 0; |
| 721 |
s->digits[s->current_digits++] = digit; |
| 722 |
s->digits[s->current_digits] = '\0'; |
| 723 |
} else { |
| 724 |
ast_log(LOG_WARNING, "Digit lost due to full buffer\n"); |
| 725 |
s->lost_digits++; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
static int dtmf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax) |
| 730 |
{ |
| 731 |
float row_energy[DTMF_MATRIX_SIZE]; |
| 732 |
float col_energy[DTMF_MATRIX_SIZE]; |
| 733 |
int i; |
| 734 |
int j; |
| 735 |
int sample; |
| 736 |
short samp; |
| 737 |
int best_row; |
| 738 |
int best_col; |
| 739 |
int hit; |
| 740 |
int limit; |
| 741 |
fragment_t mute = {0, 0}; |
| 742 |
|
| 743 |
if (squelch && s->td.dtmf.mute_samples > 0) { |
| 744 |
mute.end = (s->td.dtmf.mute_samples < samples) ? s->td.dtmf.mute_samples : samples; |
| 745 |
s->td.dtmf.mute_samples -= mute.end; |
| 746 |
} |
| 747 |
|
| 748 |
hit = 0; |
| 749 |
for (sample = 0; sample < samples; sample = limit) { |
| 750 |
|
| 751 |
if ((samples - sample) >= (DTMF_GSIZE - s->td.dtmf.current_sample)) { |
| 752 |
limit = sample + (DTMF_GSIZE - s->td.dtmf.current_sample); |
| 753 |
} else { |
| 754 |
limit = samples; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
for (j = sample; j < limit; j++) { |
| 759 |
samp = amp[j]; |
| 760 |
s->td.dtmf.energy += (int32_t) samp * (int32_t) samp; |
| 761 |
|
| 762 |
|
| 763 |
goertzel_sample(s->td.dtmf.row_out, samp); |
| 764 |
goertzel_sample(s->td.dtmf.col_out, samp); |
| 765 |
goertzel_sample(s->td.dtmf.row_out + 1, samp); |
| 766 |
goertzel_sample(s->td.dtmf.col_out + 1, samp); |
| 767 |
goertzel_sample(s->td.dtmf.row_out + 2, samp); |
| 768 |
goertzel_sample(s->td.dtmf.col_out + 2, samp); |
| 769 |
goertzel_sample(s->td.dtmf.row_out + 3, samp); |
| 770 |
goertzel_sample(s->td.dtmf.col_out + 3, samp); |
| 771 |
|
| 772 |
} |
| 773 |
s->td.dtmf.current_sample += (limit - sample); |
| 774 |
if (s->td.dtmf.current_sample < DTMF_GSIZE) { |
| 775 |
continue; |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
row_energy[0] = goertzel_result(&s->td.dtmf.row_out[0]); |
| 780 |
col_energy[0] = goertzel_result(&s->td.dtmf.col_out[0]); |
| 781 |
|
| 782 |
for (best_row = best_col = 0, i = 1; i < DTMF_MATRIX_SIZE; i++) { |
| 783 |
row_energy[i] = goertzel_result(&s->td.dtmf.row_out[i]); |
| 784 |
if (row_energy[i] > row_energy[best_row]) { |
| 785 |
best_row = i; |
| 786 |
} |
| 787 |
col_energy[i] = goertzel_result(&s->td.dtmf.col_out[i]); |
| 788 |
if (col_energy[i] > col_energy[best_col]) { |
| 789 |
best_col = i; |
| 790 |
} |
| 791 |
} |
| 792 |
ast_debug(10, "DTMF best '%c' Erow=%.4E Ecol=%.4E Erc=%.4E Et=%.4E\n", |
| 793 |
dtmf_positions[(best_row << 2) + best_col], |
| 794 |
row_energy[best_row], col_energy[best_col], |
| 795 |
row_energy[best_row] + col_energy[best_col], s->td.dtmf.energy); |
| 796 |
hit = 0; |
| 797 |
|
| 798 |
if (row_energy[best_row] >= DTMF_THRESHOLD && |
| 799 |
col_energy[best_col] >= DTMF_THRESHOLD && |
| 800 |
col_energy[best_col] < row_energy[best_row] * (relax ? relax_dtmf_reverse_twist : dtmf_reverse_twist) && |
| 801 |
row_energy[best_row] < col_energy[best_col] * (relax ? relax_dtmf_normal_twist : dtmf_normal_twist)) { |
| 802 |
|
| 803 |
for (i = 0; i < DTMF_MATRIX_SIZE; i++) { |
| 804 |
if ((i != best_col && |
| 805 |
col_energy[i] * DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) || |
| 806 |
(i != best_row |
| 807 |
&& row_energy[i] * DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) { |
| 808 |
break; |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
if (i >= DTMF_MATRIX_SIZE && |
| 813 |
(row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY * s->td.dtmf.energy) { |
| 814 |
|
| 815 |
hit = dtmf_positions[(best_row << 2) + best_col]; |
| 816 |
ast_debug(10, "DTMF hit '%c'\n", hit); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
if (s->td.dtmf.current_hit) { |
| 875 |
|
| 876 |
if (hit != s->td.dtmf.current_hit) { |
| 877 |
s->td.dtmf.misses++; |
| 878 |
if (s->td.dtmf.misses == dtmf_misses_to_end) { |
| 879 |
|
| 880 |
s->td.dtmf.current_hit = 0; |
| 881 |
} |
| 882 |
} else { |
| 883 |
s->td.dtmf.misses = 0; |
| 884 |
|
| 885 |
s->digitlen[s->current_digits - 1] += DTMF_GSIZE; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
if (hit != s->td.dtmf.lasthit) { |
| 894 |
s->td.dtmf.lasthit = hit; |
| 895 |
s->td.dtmf.hits = 0; |
| 896 |
} |
| 897 |
if (hit && hit != s->td.dtmf.current_hit) { |
| 898 |
s->td.dtmf.hits++; |
| 899 |
if (s->td.dtmf.hits == dtmf_hits_to_begin) { |
| 900 |
store_digit(s, hit); |
| 901 |
s->digitlen[s->current_digits - 1] = dtmf_hits_to_begin * DTMF_GSIZE; |
| 902 |
s->td.dtmf.current_hit = hit; |
| 903 |
s->td.dtmf.misses = 0; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
if (squelch && hit) { |
| 909 |
if (mute.end < sample - DTMF_GSIZE) { |
| 910 |
|
| 911 |
mute_fragment(dsp, &mute); |
| 912 |
mute.start = (sample > DTMF_GSIZE) ? (sample - DTMF_GSIZE) : 0; |
| 913 |
} |
| 914 |
mute.end = limit + DTMF_GSIZE; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
for (i = 0; i < DTMF_MATRIX_SIZE; i++) { |
| 919 |
goertzel_reset(&s->td.dtmf.row_out[i]); |
| 920 |
goertzel_reset(&s->td.dtmf.col_out[i]); |
| 921 |
} |
| 922 |
s->td.dtmf.energy = 0.0; |
| 923 |
s->td.dtmf.current_sample = 0; |
| 924 |
} |
| 925 |
|
| 926 |
if (squelch && mute.end) { |
| 927 |
if (mute.end > samples) { |
| 928 |
s->td.dtmf.mute_samples = mute.end - samples; |
| 929 |
mute.end = samples; |
| 930 |
} |
| 931 |
mute_fragment(dsp, &mute); |
| 932 |
} |
| 933 |
|
| 934 |
return (s->td.dtmf.current_hit); |
| 935 |
} |
| 936 |
|
| 937 |
static int mf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], |
| 938 |
int samples, int squelch, int relax) |
| 939 |
{ |
| 940 |
float energy[6]; |
| 941 |
int best; |
| 942 |
int second_best; |
| 943 |
int i; |
| 944 |
int j; |
| 945 |
int sample; |
| 946 |
short samp; |
| 947 |
int hit; |
| 948 |
int limit; |
| 949 |
fragment_t mute = {0, 0}; |
| 950 |
|
| 951 |
if (squelch && s->td.mf.mute_samples > 0) { |
| 952 |
mute.end = (s->td.mf.mute_samples < samples) ? s->td.mf.mute_samples : samples; |
| 953 |
s->td.mf.mute_samples -= mute.end; |
| 954 |
} |
| 955 |
|
| 956 |
hit = 0; |
| 957 |
for (sample = 0; sample < samples; sample = limit) { |
| 958 |
|
| 959 |
|
| 960 |
if ((samples - sample) >= (MF_GSIZE - s->td.mf.current_sample)) { |
| 961 |
limit = sample + (MF_GSIZE - s->td.mf.current_sample); |
| 962 |
} else { |
| 963 |
limit = samples; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
for (j = sample; j < limit; j++) { |
| 968 |
|
| 969 |
|
| 970 |
samp = amp[j]; |
| 971 |
goertzel_sample(s->td.mf.tone_out, samp); |
| 972 |
goertzel_sample(s->td.mf.tone_out + 1, samp); |
| 973 |
goertzel_sample(s->td.mf.tone_out + 2, samp); |
| 974 |
goertzel_sample(s->td.mf.tone_out + 3, samp); |
| 975 |
goertzel_sample(s->td.mf.tone_out + 4, samp); |
| 976 |
goertzel_sample(s->td.mf.tone_out + 5, samp); |
| 977 |
} |
| 978 |
s->td.mf.current_sample += (limit - sample); |
| 979 |
if (s->td.mf.current_sample < MF_GSIZE) { |
| 980 |
continue; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
energy[0] = goertzel_result(&s->td.mf.tone_out[0]); |
| 990 |
energy[1] = goertzel_result(&s->td.mf.tone_out[1]); |
| 991 |
if (energy[0] > energy[1]) { |
| 992 |
best = 0; |
| 993 |
second_best = 1; |
| 994 |
} else { |
| 995 |
best = 1; |
| 996 |
second_best = 0; |
| 997 |
} |
| 998 |
|
| 999 |
for (i = 2; i < 6; i++) { |
| 1000 |
energy[i] = goertzel_result(&s->td.mf.tone_out[i]); |
| 1001 |
if (energy[i] >= energy[best]) { |
| 1002 |
second_best = best; |
| 1003 |
best = i; |
| 1004 |
} else if (energy[i] >= energy[second_best]) { |
| 1005 |
second_best = i; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
hit = 0; |
| 1010 |
if (energy[best] >= BELL_MF_THRESHOLD && energy[second_best] >= BELL_MF_THRESHOLD |
| 1011 |
&& energy[best] < energy[second_best]*BELL_MF_TWIST |
| 1012 |
&& energy[best] * BELL_MF_TWIST > energy[second_best]) { |
| 1013 |
|
| 1014 |
hit = -1; |
| 1015 |
for (i = 0; i < 6; i++) { |
| 1016 |
if (i != best && i != second_best) { |
| 1017 |
if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best]) { |
| 1018 |
|
| 1019 |
hit = 0; |
| 1020 |
break; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
if (hit) { |
| 1026 |
|
| 1027 |
if (second_best < best) { |
| 1028 |
i = best; |
| 1029 |
best = second_best; |
| 1030 |
second_best = i; |
| 1031 |
} |
| 1032 |
best = best * 5 + second_best - 1; |
| 1033 |
hit = bell_mf_positions[best]; |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
if (hit == s->td.mf.hits[4] && hit == s->td.mf.hits[3] && |
| 1041 |
((hit != '*' && hit != s->td.mf.hits[2] && hit != s->td.mf.hits[1])|| |
| 1042 |
(hit == '*' && hit == s->td.mf.hits[2] && hit != s->td.mf.hits[1] && |
| 1043 |
hit != s->td.mf.hits[0]))) { |
| 1044 |
store_digit(s, hit); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
if (hit != s->td.mf.hits[4] && hit != s->td.mf.hits[3]) { |
| 1050 |
|
| 1051 |
s->td.mf.current_hit = 0; |
| 1052 |
} |
| 1053 |
|
| 1054 |
s->td.mf.hits[0] = s->td.mf.hits[1]; |
| 1055 |
s->td.mf.hits[1] = s->td.mf.hits[2]; |
| 1056 |
s->td.mf.hits[2] = s->td.mf.hits[3]; |
| 1057 |
s->td.mf.hits[3] = s->td.mf.hits[4]; |
| 1058 |
s->td.mf.hits[4] = hit; |
| 1059 |
|
| 1060 |
|
| 1061 |
if (squelch && hit) { |
| 1062 |
if (mute.end < sample - MF_GSIZE) { |
| 1063 |
|
| 1064 |
mute_fragment(dsp, &mute); |
| 1065 |
mute.start = (sample > MF_GSIZE) ? (sample - MF_GSIZE) : 0; |
| 1066 |
} |
| 1067 |
mute.end = limit + MF_GSIZE; |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
for (i = 0; i < 6; i++) { |
| 1072 |
goertzel_reset(&s->td.mf.tone_out[i]); |
| 1073 |
} |
| 1074 |
s->td.mf.current_sample = 0; |
| 1075 |
} |
| 1076 |
|
| 1077 |
if (squelch && mute.end) { |
| 1078 |
if (mute.end > samples) { |
| 1079 |
s->td.mf.mute_samples = mute.end - samples; |
| 1080 |
mute.end = samples; |
| 1081 |
} |
| 1082 |
mute_fragment(dsp, &mute); |
| 1083 |
} |
| 1084 |
|
| 1085 |
return (s->td.mf.current_hit); |
| 1086 |
} |
| 1087 |
|
| 1088 |
static int r2_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], |
| 1089 |
int samples, int squelch, int relax, const char *r2_positions) |
| 1090 |
{ |
| 1091 |
float energy[6]; |
| 1092 |
int best; |
| 1093 |
int second_best; |
| 1094 |
int i; |
| 1095 |
int j; |
| 1096 |
int sample; |
| 1097 |
short samp; |
| 1098 |
int hit; |
| 1099 |
int limit; |
| 1100 |
fragment_t mute = {0, 0}; |
| 1101 |
|
| 1102 |
if (squelch && s->td.mf.mute_samples > 0) { |
| 1103 |
mute.end = (s->td.mf.mute_samples < samples) ? s->td.mf.mute_samples : samples; |
| 1104 |
s->td.mf.mute_samples -= mute.end; |
| 1105 |
} |
| 1106 |
|
| 1107 |
hit = 0; |
| 1108 |
for (sample = 0; sample < samples; sample = limit) { |
| 1109 |
if ((samples - sample) >= (R2_GSIZE - s->td.mf.current_sample)) { |
| 1110 |
limit = sample + (R2_GSIZE - s->td.mf.current_sample); |
| 1111 |
} else { |
| 1112 |
limit = samples; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
for (j = sample; j < limit; j++) { |
| 1117 |
|
| 1118 |
|
| 1119 |
samp = amp[j]; |
| 1120 |
goertzel_sample(s->td.mf.tone_out, samp); |
| 1121 |
goertzel_sample(s->td.mf.tone_out + 1, samp); |
| 1122 |
goertzel_sample(s->td.mf.tone_out + 2, samp); |
| 1123 |
goertzel_sample(s->td.mf.tone_out + 3, samp); |
| 1124 |
goertzel_sample(s->td.mf.tone_out + 4, samp); |
| 1125 |
goertzel_sample(s->td.mf.tone_out + 5, samp); |
| 1126 |
} |
| 1127 |
s->td.mf.current_sample += (limit - sample); |
| 1128 |
if (s->td.mf.current_sample < R2_GSIZE) { |
| 1129 |
continue; |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
energy[0] = goertzel_result(&s->td.mf.tone_out[0]); |
| 1139 |
energy[1] = goertzel_result(&s->td.mf.tone_out[1]); |
| 1140 |
if (energy[0] > energy[1]) { |
| 1141 |
best = 0; |
| 1142 |
second_best = 1; |
| 1143 |
} else { |
| 1144 |
best = 1; |
| 1145 |
second_best = 0; |
| 1146 |
} |
| 1147 |
|
| 1148 |
for (i = 2; i < 6; i++) { |
| 1149 |
energy[i] = goertzel_result(&s->td.mf.tone_out[i]); |
| 1150 |
if (energy[i] >= energy[best]) { |
| 1151 |
second_best = best; |
| 1152 |
best = i; |
| 1153 |
} else if (energy[i] >= energy[second_best]) { |
| 1154 |
second_best = i; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
hit = 0; |
| 1159 |
if (energy[best] >= R2_MF_THRESHOLD && energy[second_best] >= R2_MF_THRESHOLD |
| 1160 |
&& energy[best] < energy[second_best]*R2_MF_TWIST |
| 1161 |
&& energy[best] * R2_MF_TWIST > energy[second_best]) { |
| 1162 |
|
| 1163 |
hit = -1; |
| 1164 |
for (i = 0; i < 6; i++) { |
| 1165 |
if (i != best && i != second_best) { |
| 1166 |
if (energy[i]*R2_MF_RELATIVE_PEAK >= energy[second_best]) { |
| 1167 |
|
| 1168 |
hit = 0; |
| 1169 |
break; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
} |
| 1173 |
} |
| 1174 |
if (hit) { |
| 1175 |
|
| 1176 |
if (second_best < best) { |
| 1177 |
i = best; |
| 1178 |
best = second_best; |
| 1179 |
second_best = i; |
| 1180 |
} |
| 1181 |
best = best * 5 + second_best - 1; |
| 1182 |
hit = r2_positions[best]; |
| 1183 |
|
| 1184 |
if (relax) { |
| 1185 |
|
| 1186 |
|
| 1187 |
store_digit(s, hit); |
| 1188 |
} else { |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
if (hit == s->td.mf.hits[4] && hit == s->td.mf.hits[3] && hit != s->td.mf.hits[2]) { |
| 1193 |
store_digit(s, hit); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} else { |
| 1197 |
hit = 0; |
| 1198 |
} |
| 1199 |
|
| 1200 |
s->td.mf.current_hit = hit; |
| 1201 |
|
| 1202 |
if (!relax) { |
| 1203 |
s->td.mf.hits[0] = s->td.mf.hits[1]; |
| 1204 |
s->td.mf.hits[1] = s->td.mf.hits[2]; |
| 1205 |
s->td.mf.hits[2] = s->td.mf.hits[3]; |
| 1206 |
s->td.mf.hits[3] = s->td.mf.hits[4]; |
| 1207 |
s->td.mf.hits[4] = hit; |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
if (squelch && hit) { |
| 1212 |
if (mute.end < sample - R2_GSIZE) { |
| 1213 |
|
| 1214 |
mute_fragment(dsp, &mute); |
| 1215 |
mute.start = (sample > R2_GSIZE) ? (sample - R2_GSIZE) : 0; |
| 1216 |
} |
| 1217 |
mute.end = limit + R2_GSIZE; |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
for (i = 0; i < 6; i++) { |
| 1222 |
goertzel_reset(&s->td.mf.tone_out[i]); |
| 1223 |
} |
| 1224 |
s->td.mf.current_sample = 0; |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (squelch && mute.end) { |
| 1228 |
if (mute.end > samples) { |
| 1229 |
s->td.mf.mute_samples = mute.end - samples; |
| 1230 |
mute.end = samples; |
| 1231 |
} |
| 1232 |
mute_fragment(dsp, &mute); |
| 1233 |
} |
| 1234 |
|
| 1235 |
return (s->td.mf.current_hit); |
| 1236 |
} |
| 1237 |
|
| 1238 |
static inline int pair_there(float p1, float p2, float i1, float i2, float e) |
| 1239 |
{ |
| 1240 |
|
| 1241 |
|
| 1242 |
if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH)) { |
| 1243 |
return 0; |
| 1244 |
} |
| 1245 |
|
| 1246 |
i2 *= TONE_THRESH; |
| 1247 |
i1 *= TONE_THRESH; |
| 1248 |
e *= TONE_THRESH; |
| 1249 |
|
| 1250 |
if ((p1 < i1) || (p1 < i2) || (p1 < e)) { |
| 1251 |
return 0; |
| 1252 |
} |
| 1253 |
|
| 1254 |
if ((p2 < i1) || (p2 < i2) || (p2 < e)) { |
| 1255 |
return 0; |
| 1256 |
} |
| 1257 |
|
| 1258 |
return 1; |
| 1259 |
} |
| 1260 |
|
| 1261 |
static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len) |
| 1262 |
{ |
| 1263 |
short samp; |
| 1264 |
int x; |
| 1265 |
int y; |
| 1266 |
int pass; |
| 1267 |
int newstate = DSP_TONE_STATE_SILENCE; |
| 1268 |
int res = 0; |
| 1269 |
int freqcount = dsp->freqcount > FREQ_ARRAY_SIZE ? FREQ_ARRAY_SIZE : dsp->freqcount; |
| 1270 |
|
| 1271 |
while (len) { |
| 1272 |
|
| 1273 |
pass = len; |
| 1274 |
if (pass > dsp->gsamp_size - dsp->gsamps) { |
| 1275 |
pass = dsp->gsamp_size - dsp->gsamps; |
| 1276 |
} |
| 1277 |
for (x = 0; x < pass; x++) { |
| 1278 |
samp = s[x]; |
| 1279 |
dsp->genergy += (int32_t) samp * (int32_t) samp; |
| 1280 |
for (y = 0; y < freqcount; y++) { |
| 1281 |
goertzel_sample(&dsp->freqs[y], samp); |
| 1282 |
} |
| 1283 |
} |
| 1284 |
s += pass; |
| 1285 |
dsp->gsamps += pass; |
| 1286 |
len -= pass; |
| 1287 |
if (dsp->gsamps == dsp->gsamp_size) { |
| 1288 |
float hz[FREQ_ARRAY_SIZE]; |
| 1289 |
for (y = 0; y < FREQ_ARRAY_SIZE; y++) { |
| 1290 |
hz[y] = goertzel_result(&dsp->freqs[y]); |
| 1291 |
} |
| 1292 |
switch (dsp->progmode) { |
| 1293 |
case PROG_MODE_NA: |
| 1294 |
if (pair_there(hz[HZ_480], hz[HZ_620], hz[HZ_350], hz[HZ_440], dsp->genergy)) { |
| 1295 |
newstate = DSP_TONE_STATE_BUSY; |
| 1296 |
} else if (pair_there(hz[HZ_440], hz[HZ_480], hz[HZ_350], hz[HZ_620], dsp->genergy)) { |
| 1297 |
newstate = DSP_TONE_STATE_RINGING; |
| 1298 |
} else if (pair_there(hz[HZ_350], hz[HZ_440], hz[HZ_480], hz[HZ_620], dsp->genergy)) { |
| 1299 |
newstate = DSP_TONE_STATE_DIALTONE; |
| 1300 |
} else if (hz[HZ_950] > TONE_MIN_THRESH * TONE_THRESH) { |
| 1301 |
newstate = DSP_TONE_STATE_SPECIAL1; |
| 1302 |
} else if (hz[HZ_1400] > TONE_MIN_THRESH * TONE_THRESH) { |
| 1303 |
|
| 1304 |
if (dsp->tstate == DSP_TONE_STATE_SPECIAL1 || dsp->tstate == DSP_TONE_STATE_SPECIAL2) { |
| 1305 |
newstate = DSP_TONE_STATE_SPECIAL2; |
| 1306 |
} |
| 1307 |
} else if (hz[HZ_1800] > TONE_MIN_THRESH * TONE_THRESH) { |
| 1308 |
|
| 1309 |
if (dsp->tstate == DSP_TONE_STATE_SPECIAL2 || dsp->tstate == DSP_TONE_STATE_SPECIAL3) { |
| 1310 |
newstate = DSP_TONE_STATE_SPECIAL3; |
| 1311 |
} |
| 1312 |
} else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) { |
| 1313 |
newstate = DSP_TONE_STATE_TALKING; |
| 1314 |
} else { |
| 1315 |
newstate = DSP_TONE_STATE_SILENCE; |
| 1316 |
} |
| 1317 |
break; |
| 1318 |
case PROG_MODE_CR: |
| 1319 |
if (hz[HZ_425] > TONE_MIN_THRESH * TONE_THRESH) { |
| 1320 |
newstate = DSP_TONE_STATE_RINGING; |
| 1321 |
} else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) { |
| 1322 |
newstate = DSP_TONE_STATE_TALKING; |
| 1323 |
} else { |
| 1324 |
newstate = DSP_TONE_STATE_SILENCE; |
| 1325 |
} |
| 1326 |
break; |
| 1327 |
case PROG_MODE_UK: |
| 1328 |
if (hz[HZ_400UK] > TONE_MIN_THRESH * TONE_THRESH) { |
| 1329 |
newstate = DSP_TONE_STATE_HUNGUP; |
| 1330 |
} else if (pair_there(hz[HZ_350UK], hz[HZ_440UK], hz[HZ_400UK], hz[HZ_400UK], dsp->genergy)) { |
| 1331 |
newstate = DSP_TONE_STATE_DIALTONE; |
| 1332 |
} |
| 1333 |
break; |
| 1334 |
default: |
| 1335 |
ast_log(LOG_WARNING, "Can't process in unknown prog mode '%u'\n", dsp->progmode); |
| 1336 |
} |
| 1337 |
if (newstate == dsp->tstate) { |
| 1338 |
dsp->tcount++; |
| 1339 |
if (dsp->ringtimeout) { |
| 1340 |
dsp->ringtimeout++; |
| 1341 |
} |
| 1342 |
switch (dsp->tstate) { |
| 1343 |
case DSP_TONE_STATE_RINGING: |
| 1344 |
if ((dsp->features & DSP_PROGRESS_RINGING) && |
| 1345 |
(dsp->tcount == THRESH_RING)) { |
| 1346 |
res = AST_CONTROL_RINGING; |
| 1347 |
dsp->ringtimeout = 1; |
| 1348 |
} |
| 1349 |
break; |
| 1350 |
case DSP_TONE_STATE_BUSY: |
| 1351 |
if ((dsp->features & DSP_PROGRESS_BUSY) && |
| 1352 |
(dsp->tcount == THRESH_BUSY)) { |
| 1353 |
res = AST_CONTROL_BUSY; |
| 1354 |
dsp->features &= ~DSP_FEATURE_CALL_PROGRESS; |
| 1355 |
} |
| 1356 |
break; |
| 1357 |
case DSP_TONE_STATE_TALKING: |
| 1358 |
if ((dsp->features & DSP_PROGRESS_TALK) && |
| 1359 |
(dsp->tcount == THRESH_TALK)) { |
| 1360 |
res = AST_CONTROL_ANSWER; |
| 1361 |
dsp->features &= ~DSP_FEATURE_CALL_PROGRESS; |
| 1362 |
} |
| 1363 |
break; |
| 1364 |
case DSP_TONE_STATE_SPECIAL3: |
| 1365 |
if ((dsp->features & DSP_PROGRESS_CONGESTION) && |
| 1366 |
(dsp->tcount == THRESH_CONGESTION)) { |
| 1367 |
res = AST_CONTROL_CONGESTION; |
| 1368 |
dsp->features &= ~DSP_FEATURE_CALL_PROGRESS; |
| 1369 |
} |
| 1370 |
break; |
| 1371 |
case DSP_TONE_STATE_HUNGUP: |
| 1372 |
if ((dsp->features & DSP_FEATURE_CALL_PROGRESS) && |
| 1373 |
(dsp->tcount == THRESH_HANGUP)) { |
| 1374 |
res = AST_CONTROL_HANGUP; |
| 1375 |
dsp->features &= ~DSP_FEATURE_CALL_PROGRESS; |
| 1376 |
} |
| 1377 |
break; |
| 1378 |
} |
| 1379 |
if (dsp->ringtimeout == THRESH_RING2ANSWER) { |
| 1380 |
ast_debug(1, "Consider call as answered because of timeout after last ring\n"); |
| 1381 |
res = AST_CONTROL_ANSWER; |
| 1382 |
dsp->features &= ~DSP_FEATURE_CALL_PROGRESS; |
| 1383 |
} |
| 1384 |
} else { |
| 1385 |
ast_debug(5, "Stop state %d with duration %d\n", dsp->tstate, dsp->tcount); |
| 1386 |
ast_debug(5, "Start state %d\n", newstate); |
| 1387 |
dsp->tstate = newstate; |
| 1388 |
dsp->tcount = 1; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
for (x = 0; x < 7; x++) { |
| 1393 |
dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0; |
| 1394 |
} |
| 1395 |
dsp->gsamps = 0; |
| 1396 |
dsp->genergy = 0.0; |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
return res; |
| 1401 |
} |
| 1402 |
|
| 1403 |
int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf) |
| 1404 |
{ |
| 1405 |
if (inf->frametype != AST_FRAME_VOICE) { |
| 1406 |
ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n"); |
| 1407 |
return 0; |
| 1408 |
} |
| 1409 |
if (!ast_format_cache_is_slinear(inf->subclass.format)) { |
| 1410 |
ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames, %s not supported\n", |
| 1411 |
ast_format_get_name(inf->subclass.format)); |
| 1412 |
return 0; |
| 1413 |
} |
| 1414 |
return __ast_dsp_call_progress(dsp, inf->data.ptr, inf->datalen / 2); |
| 1415 |
} |
| 1416 |
|
| 1417 |
static int __ast_dsp_silence_noise(struct ast_dsp *dsp, short *s, int len, int *totalsilence, int *totalnoise, int *frames_energy) |
| 1418 |
{ |
| 1419 |
int accum; |
| 1420 |
int x; |
| 1421 |
int res = 0; |
| 1422 |
|
| 1423 |
if (!len) { |
| 1424 |
return 0; |
| 1425 |
} |
| 1426 |
accum = 0; |
| 1427 |
for (x = 0; x < len; x++) { |
| 1428 |
accum += abs(s[x]); |
| 1429 |
} |
| 1430 |
accum /= len; |
| 1431 |
if (accum < dsp->threshold) { |
| 1432 |
|
| 1433 |
dsp->totalsilence += len / (dsp->sample_rate / 1000); |
| 1434 |
if (dsp->totalnoise) { |
| 1435 |
|
| 1436 |
memmove(dsp->historicnoise + DSP_HISTORY - dsp->busycount, dsp->historicnoise + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicnoise[0])); |
| 1437 |
dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise; |
| 1438 |
|
| 1439 |
#if 0 |
| 1440 |
dsp->busymaybe = 1; |
| 1441 |
#endif |
| 1442 |
} |
| 1443 |
dsp->totalnoise = 0; |
| 1444 |
res = 1; |
| 1445 |
} else { |
| 1446 |
|
| 1447 |
dsp->totalnoise += len / (dsp->sample_rate / 1000); |
| 1448 |
if (dsp->totalsilence) { |
| 1449 |
int silence1 = dsp->historicsilence[DSP_HISTORY - 1]; |
| 1450 |
int silence2 = dsp->historicsilence[DSP_HISTORY - 2]; |
| 1451 |
|
| 1452 |
memmove(dsp->historicsilence + DSP_HISTORY - dsp->busycount, dsp->historicsilence + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicsilence[0])); |
| 1453 |
dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence; |
| 1454 |
|
| 1455 |
if (silence1 < silence2) { |
| 1456 |
if (silence1 + silence1 * BUSY_PERCENT / 100 >= silence2) { |
| 1457 |
dsp->busymaybe = 1; |
| 1458 |
} else { |
| 1459 |
dsp->busymaybe = 0; |
| 1460 |
} |
| 1461 |
} else { |
| 1462 |
if (silence1 - silence1 * BUSY_PERCENT / 100 <= silence2) { |
| 1463 |
dsp->busymaybe = 1; |
| 1464 |
} else { |
| 1465 |
dsp->busymaybe = 0; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
} |
| 1469 |
dsp->totalsilence = 0; |
| 1470 |
} |
| 1471 |
if (totalsilence) { |
| 1472 |
*totalsilence = dsp->totalsilence; |
| 1473 |
} |
| 1474 |
if (totalnoise) { |
| 1475 |
*totalnoise = dsp->totalnoise; |
| 1476 |
} |
| 1477 |
if (frames_energy) { |
| 1478 |
*frames_energy = accum; |
| 1479 |
} |
| 1480 |
return res; |
| 1481 |
} |
| 1482 |
|
| 1483 |
int ast_dsp_busydetect(struct ast_dsp *dsp) |
| 1484 |
{ |
| 1485 |
int res = 0, x; |
| 1486 |
#ifndef BUSYDETECT_TONEONLY |
| 1487 |
int avgsilence = 0, hitsilence = 0; |
| 1488 |
#endif |
| 1489 |
int avgtone = 0, hittone = 0; |
| 1490 |
|
| 1491 |
|
| 1492 |
if (dsp->busy_cadence.length != 4) { |
| 1493 |
if (!dsp->busymaybe) { |
| 1494 |
return res; |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) { |
| 1499 |
#ifndef BUSYDETECT_TONEONLY |
| 1500 |
avgsilence += dsp->historicsilence[x]; |
| 1501 |
#endif |
| 1502 |
avgtone += dsp->historicnoise[x]; |
| 1503 |
} |
| 1504 |
#ifndef BUSYDETECT_TONEONLY |
| 1505 |
avgsilence /= dsp->busycount; |
| 1506 |
#endif |
| 1507 |
avgtone /= dsp->busycount; |
| 1508 |
for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) { |
| 1509 |
#ifndef BUSYDETECT_TONEONLY |
| 1510 |
if (avgsilence > dsp->historicsilence[x]) { |
| 1511 |
if (avgsilence - (avgsilence * BUSY_PERCENT / 100) <= dsp->historicsilence[x]) { |
| 1512 |
hitsilence++; |
| 1513 |
} |
| 1514 |
} else { |
| 1515 |
if (avgsilence + (avgsilence * BUSY_PERCENT / 100) >= dsp->historicsilence[x]) { |
| 1516 |
hitsilence++; |
| 1517 |
} |
| 1518 |
} |
| 1519 |
#endif |
| 1520 |
if (avgtone > dsp->historicnoise[x]) { |
| 1521 |
if (avgtone - (avgtone * BUSY_PERCENT / 100) <= dsp->historicnoise[x]) { |
| 1522 |
hittone++; |
| 1523 |
} |
| 1524 |
} else { |
| 1525 |
if (avgtone + (avgtone * BUSY_PERCENT / 100) >= dsp->historicnoise[x]) { |
| 1526 |
hittone++; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
} |
| 1530 |
#ifndef BUSYDETECT_TONEONLY |
| 1531 |
if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) && |
| 1532 |
(avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) && |
| 1533 |
(avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) |
| 1534 |
#else |
| 1535 |
if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) |
| 1536 |
#endif |
| 1537 |
{ |
| 1538 |
#ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE |
| 1539 |
if (avgtone > avgsilence) { |
| 1540 |
if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence) { |
| 1541 |
res = 1; |
| 1542 |
} |
| 1543 |
} else { |
| 1544 |
if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence) { |
| 1545 |
res = 1; |
| 1546 |
} |
| 1547 |
} |
| 1548 |
#else |
| 1549 |
res = 1; |
| 1550 |
#endif |
| 1551 |
} |
| 1552 |
|
| 1553 |
|
| 1554 |
if (dsp->busy_cadence.length == 4) { |
| 1555 |
int x; |
| 1556 |
int errors = 0; |
| 1557 |
int errors_max = ((4 * dsp->busycount) / 100.0) * BUSY_PAT_PERCENT; |
| 1558 |
|
| 1559 |
for (x = DSP_HISTORY - (dsp->busycount); x < DSP_HISTORY; x += 2) { |
| 1560 |
int temp_error; |
| 1561 |
temp_error = abs(dsp->historicnoise[x] - dsp->busy_cadence.pattern[0]); |
| 1562 |
if ((temp_error * 100) / dsp->busy_cadence.pattern[0] > BUSY_PERCENT) { |
| 1563 |
errors++; |
| 1564 |
} |
| 1565 |
|
| 1566 |
temp_error = abs(dsp->historicnoise[x + 1] - dsp->busy_cadence.pattern[2]); |
| 1567 |
if ((temp_error * 100) / dsp->busy_cadence.pattern[2] > BUSY_PERCENT) { |
| 1568 |
errors++; |
| 1569 |
} |
| 1570 |
|
| 1571 |
temp_error = abs(dsp->historicsilence[x] - dsp->busy_cadence.pattern[1]); |
| 1572 |
if ((temp_error * 100) / dsp->busy_cadence.pattern[1] > BUSY_PERCENT) { |
| 1573 |
errors++; |
| 1574 |
} |
| 1575 |
|
| 1576 |
temp_error = abs(dsp->historicsilence[x + 1] - dsp->busy_cadence.pattern[3]); |
| 1577 |
if ((temp_error * 100) / dsp->busy_cadence.pattern[3] > BUSY_PERCENT) { |
| 1578 |
errors++; |
| 1579 |
} |
| 1580 |
} |
| 1581 |
|
| 1582 |
ast_debug(5, "errors = %d max = %d\n", errors, errors_max); |
| 1583 |
|
| 1584 |
if (errors <= errors_max) { |
| 1585 |
return 1; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
|
| 1590 |
if (res && (dsp->busy_cadence.pattern[0] > 0)) { |
| 1591 |
if (abs(avgtone - dsp->busy_cadence.pattern[0]) > MAX(dsp->busy_cadence.pattern[0]*BUSY_PAT_PERCENT/100, 20)) { |
| 1592 |
#ifdef BUSYDETECT_DEBUG |
| 1593 |
ast_debug(5, "busy detector: avgtone of %d not close enough to desired %d\n", |
| 1594 |
avgtone, dsp->busy_cadence.pattern[0]); |
| 1595 |
#endif |
| 1596 |
res = 0; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
#ifndef BUSYDETECT_TONEONLY |
| 1600 |
|
| 1601 |
if (res && (dsp->busy_cadence.pattern[1] > 0)) { |
| 1602 |
if (abs(avgsilence - dsp->busy_cadence.pattern[1]) > MAX(dsp->busy_cadence.pattern[1]*BUSY_PAT_PERCENT/100, 20)) { |
| 1603 |
#ifdef BUSYDETECT_DEBUG |
| 1604 |
ast_debug(5, "busy detector: avgsilence of %d not close enough to desired %d\n", |
| 1605 |
avgsilence, dsp->busy_cadence.pattern[1]); |
| 1606 |
#endif |
| 1607 |
res = 0; |
| 1608 |
} |
| 1609 |
} |
| 1610 |
#endif |
| 1611 |
#if !defined(BUSYDETECT_TONEONLY) && defined(BUSYDETECT_DEBUG) |
| 1612 |
if (res) { |
| 1613 |
ast_debug(5, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence); |
| 1614 |
} else { |
| 1615 |
ast_debug(5, "busy detector: FAILED with avgtone: %d, avgsilence %d\n", avgtone, avgsilence); |
| 1616 |
} |
| 1617 |
#endif |
| 1618 |
return res; |
| 1619 |
} |
| 1620 |
|
| 1621 |
static int ast_dsp_silence_noise_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *total, int *frames_energy, int noise) |
| 1622 |
{ |
| 1623 |
short *s; |
| 1624 |
int len; |
| 1625 |
int x; |
| 1626 |
unsigned char *odata; |
| 1627 |
|
| 1628 |
if (!f) { |
| 1629 |
return 0; |
| 1630 |
} |
| 1631 |
|
| 1632 |
if (f->frametype != AST_FRAME_VOICE) { |
| 1633 |
ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n"); |
| 1634 |
return 0; |
| 1635 |
} |
| 1636 |
|
| 1637 |
if (ast_format_cache_is_slinear(f->subclass.format)) { |
| 1638 |
s = f->data.ptr; |
| 1639 |
len = f->datalen/2; |
| 1640 |
} else { |
| 1641 |
odata = f->data.ptr; |
| 1642 |
len = f->datalen; |
| 1643 |
if (ast_format_cmp(f->subclass.format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) { |
| 1644 |
s = ast_alloca(len * 2); |
| 1645 |
for (x = 0; x < len; x++) { |
| 1646 |
s[x] = AST_MULAW(odata[x]); |
| 1647 |
} |
| 1648 |
} else if (ast_format_cmp(f->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) { |
| 1649 |
s = ast_alloca(len * 2); |
| 1650 |
for (x = 0; x < len; x++) { |
| 1651 |
s[x] = AST_ALAW(odata[x]); |
| 1652 |
} |
| 1653 |
} else { |
| 1654 |
ast_log(LOG_WARNING, "Can only calculate silence on signed-linear, alaw or ulaw frames, %s not supported\n", |
| 1655 |
ast_format_get_name(f->subclass.format)); |
| 1656 |
return 0; |
| 1657 |
} |
| 1658 |
} |
| 1659 |
|
| 1660 |
if (noise) { |
| 1661 |
return __ast_dsp_silence_noise(dsp, s, len, NULL, total, frames_energy); |
| 1662 |
} else { |
| 1663 |
return __ast_dsp_silence_noise(dsp, s, len, total, NULL, frames_energy); |
| 1664 |
} |
| 1665 |
} |
| 1666 |
|
| 1667 |
int ast_dsp_silence_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence, int *frames_energy) |
| 1668 |
{ |
| 1669 |
return ast_dsp_silence_noise_with_energy(dsp, f, totalsilence, frames_energy, 0); |
| 1670 |
} |
| 1671 |
|
| 1672 |
int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence) |
| 1673 |
{ |
| 1674 |
return ast_dsp_silence_noise_with_energy(dsp, f, totalsilence, NULL, 0); |
| 1675 |
} |
| 1676 |
|
| 1677 |
int ast_dsp_noise(struct ast_dsp *dsp, struct ast_frame *f, int *totalnoise) |
| 1678 |
{ |
| 1679 |
return ast_dsp_silence_noise_with_energy(dsp, f, totalnoise, NULL, 1); |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af) |
| 1684 |
{ |
| 1685 |
int silence; |
| 1686 |
int res; |
| 1687 |
int digit = 0, fax_digit = 0, custom_freq_digit = 0; |
| 1688 |
int x; |
| 1689 |
short *shortdata; |
| 1690 |
unsigned char *odata; |
| 1691 |
int len; |
| 1692 |
struct ast_frame *outf = NULL; |
| 1693 |
|
| 1694 |
if (!af) { |
| 1695 |
return NULL; |
| 1696 |
} |
| 1697 |
if (af->frametype != AST_FRAME_VOICE) { |
| 1698 |
return af; |
| 1699 |
} |
| 1700 |
|
| 1701 |
odata = af->data.ptr; |
| 1702 |
len = af->datalen; |
| 1703 |
|
| 1704 |
if (ast_format_cache_is_slinear(af->subclass.format)) { |
| 1705 |
shortdata = af->data.ptr; |
| 1706 |
len = af->datalen / 2; |
| 1707 |
} else if (ast_format_cmp(af->subclass.format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) { |
| 1708 |
shortdata = ast_alloca(af->datalen * 2); |
| 1709 |
for (x = 0; x < len; x++) { |
| 1710 |
shortdata[x] = AST_MULAW(odata[x]); |
| 1711 |
} |
| 1712 |
} else if (ast_format_cmp(af->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) { |
| 1713 |
shortdata = ast_alloca(af->datalen * 2); |
| 1714 |
for (x = 0; x < len; x++) { |
| 1715 |
shortdata[x] = AST_ALAW(odata[x]); |
| 1716 |
} |
| 1717 |
} else { |
| 1718 |
|
| 1719 |
if (dsp->display_inband_dtmf_warning) { |
| 1720 |
|
| 1721 |
|
| 1722 |
if ((dsp->features & DSP_FEATURE_DIGIT_DETECT) && (dsp->digitmode & DSP_DIGITMODE_DTMF)) { |
| 1723 |
ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n", |
| 1724 |
ast_format_get_name(af->subclass.format)); |
| 1725 |
} else { |
| 1726 |
ast_log(LOG_WARNING, "Can only do DSP on signed-linear, alaw or ulaw frames (%s not supported)\n", |
| 1727 |
ast_format_get_name(af->subclass.format)); |
| 1728 |
} |
| 1729 |
} |
| 1730 |
dsp->display_inband_dtmf_warning = 0; |
| 1731 |
return af; |
| 1732 |
} |
| 1733 |
|
| 1734 |
|
| 1735 |
dsp->mute_fragments = 0; |
| 1736 |
|
| 1737 |
|
| 1738 |
if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) || (dsp->features & DSP_FEATURE_BUSY_DETECT)) { |
| 1739 |
res = __ast_dsp_silence_noise(dsp, shortdata, len, &silence, NULL, NULL); |
| 1740 |
} |
| 1741 |
|
| 1742 |
if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) { |
| 1743 |
memset(&dsp->f, 0, sizeof(dsp->f)); |
| 1744 |
dsp->f.frametype = AST_FRAME_NULL; |
| 1745 |
ast_frfree(af); |
| 1746 |
return ast_frisolate(&dsp->f); |
| 1747 |
} |
| 1748 |
if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) { |
| 1749 |
ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV); |
| 1750 |
memset(&dsp->f, 0, sizeof(dsp->f)); |
| 1751 |
dsp->f.frametype = AST_FRAME_CONTROL; |
| 1752 |
dsp->f.subclass.integer = AST_CONTROL_BUSY; |
| 1753 |
ast_frfree(af); |
| 1754 |
ast_debug(1, "Requesting Hangup because the busy tone was detected on channel %s\n", ast_channel_name(chan)); |
| 1755 |
return ast_frisolate(&dsp->f); |
| 1756 |
} |
| 1757 |
|
| 1758 |
if ((dsp->features & DSP_FEATURE_FAX_DETECT)) { |
| 1759 |
if ((dsp->faxmode & DSP_FAXMODE_DETECT_CNG) && tone_detect(dsp, &dsp->cng_tone_state, shortdata, len)) { |
| 1760 |
fax_digit = 'f'; |
| 1761 |
} |
| 1762 |
|
| 1763 |
if ((dsp->faxmode & DSP_FAXMODE_DETECT_CED) && tone_detect(dsp, &dsp->ced_tone_state, shortdata, len)) { |
| 1764 |
fax_digit = 'e'; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
|
| 1768 |
if ((dsp->features & DSP_FEATURE_FREQ_DETECT)) { |
| 1769 |
if ((dsp->freqmode) && tone_detect(dsp, &dsp->cng_tone_state, shortdata, len)) { |
| 1770 |
custom_freq_digit = 'q'; |
| 1771 |
} |
| 1772 |
} |
| 1773 |
|
| 1774 |
if (dsp->features & (DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_BUSY_DETECT)) { |
| 1775 |
if (dsp->digitmode & (DSP_DIGITMODE_R2_FORWARD | DSP_DIGITMODE_R2_BACKWARD)) { |
| 1776 |
digit = r2_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF), r2_mf_positions); |
| 1777 |
} else if (dsp->digitmode & DSP_DIGITMODE_MF) { |
| 1778 |
digit = mf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF)); |
| 1779 |
} else { |
| 1780 |
digit = dtmf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF)); |
| 1781 |
} |
| 1782 |
|
| 1783 |
if (dsp->digit_state.current_digits) { |
| 1784 |
int event = 0, event_len = 0; |
| 1785 |
char event_digit = 0; |
| 1786 |
|
| 1787 |
if (!dsp->dtmf_began) { |
| 1788 |
|
| 1789 |
|
| 1790 |
if (dsp->features & DSP_FEATURE_DIGIT_DETECT) { |
| 1791 |
event = AST_FRAME_DTMF_BEGIN; |
| 1792 |
event_digit = dsp->digit_state.digits[0]; |
| 1793 |
} |
| 1794 |
dsp->dtmf_began = 1; |
| 1795 |
} else if (dsp->digit_state.current_digits > 1 || digit != dsp->digit_state.digits[0]) { |
| 1796 |
|
| 1797 |
if (dsp->features & DSP_FEATURE_DIGIT_DETECT) { |
| 1798 |
event = AST_FRAME_DTMF_END; |
| 1799 |
event_digit = dsp->digit_state.digits[0]; |
| 1800 |
event_len = dsp->digit_state.digitlen[0] * 1000 / dsp->sample_rate; |
| 1801 |
} |
| 1802 |
memmove(&dsp->digit_state.digits[0], &dsp->digit_state.digits[1], dsp->digit_state.current_digits); |
| 1803 |
memmove(&dsp->digit_state.digitlen[0], &dsp->digit_state.digitlen[1], dsp->digit_state.current_digits * sizeof(dsp->digit_state.digitlen[0])); |
| 1804 |
dsp->digit_state.current_digits--; |
| 1805 |
dsp->dtmf_began = 0; |
| 1806 |
|
| 1807 |
if (dsp->features & DSP_FEATURE_BUSY_DETECT) { |
| 1808 |
|
| 1809 |
memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence)); |
| 1810 |
memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise)); |
| 1811 |
ast_debug(1, "DTMF Detected - Reset busydetector\n"); |
| 1812 |
} |
| 1813 |
} |
| 1814 |
|
| 1815 |
if (event) { |
| 1816 |
memset(&dsp->f, 0, sizeof(dsp->f)); |
| 1817 |
dsp->f.frametype = event; |
| 1818 |
dsp->f.subclass.integer = event_digit; |
| 1819 |
dsp->f.len = event_len; |
| 1820 |
outf = &dsp->f; |
| 1821 |
goto done; |
| 1822 |
} |
| 1823 |
} |
| 1824 |
} |
| 1825 |
|
| 1826 |
if (fax_digit) { |
| 1827 |
|
| 1828 |
|
| 1829 |
memset(&dsp->f, 0, sizeof(dsp->f)); |
| 1830 |
dsp->f.frametype = AST_FRAME_DTMF; |
| 1831 |
dsp->f.subclass.integer = fax_digit; |
| 1832 |
outf = &dsp->f; |
| 1833 |
goto done; |
| 1834 |
} |
| 1835 |
|
| 1836 |
if (custom_freq_digit) { |
| 1837 |
|
| 1838 |
|
| 1839 |
memset(&dsp->f, 0, sizeof(dsp->f)); |
| 1840 |
dsp->f.frametype = AST_FRAME_DTMF; |
| 1841 |
dsp->f.subclass.integer = custom_freq_digit; |
| 1842 |
outf = &dsp->f; |
| 1843 |
goto done; |
| 1844 |
} |
| 1845 |
|
| 1846 |
if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) { |
| 1847 |
res = __ast_dsp_call_progress(dsp, shortdata, len); |
| 1848 |
if (res) { |
| 1849 |
switch (res) { |
| 1850 |
case AST_CONTROL_ANSWER: |
| 1851 |
case AST_CONTROL_BUSY: |
| 1852 |
case AST_CONTROL_RINGING: |
| 1853 |
case AST_CONTROL_CONGESTION: |
| 1854 |
case AST_CONTROL_HANGUP: |
| 1855 |
memset(&dsp->f, 0, sizeof(dsp->f)); |
| 1856 |
dsp->f.frametype = AST_FRAME_CONTROL; |
| 1857 |
dsp->f.subclass.integer = res; |
| 1858 |
dsp->f.src = "dsp_progress"; |
| 1859 |
if (chan) { |
| 1860 |
ast_queue_frame(chan, &dsp->f); |
| 1861 |
} |
| 1862 |
break; |
| 1863 |
default: |
| 1864 |
ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res); |
| 1865 |
} |
| 1866 |
} |
| 1867 |
} else if ((dsp->features & DSP_FEATURE_WAITDIALTONE)) { |
| 1868 |
res = __ast_dsp_call_progress(dsp, shortdata, len); |
| 1869 |
} |
| 1870 |
|
| 1871 |
done: |
| 1872 |
|
| 1873 |
for (x = 0; x < dsp->mute_fragments; x++) { |
| 1874 |
memset(shortdata + dsp->mute_data[x].start, 0, sizeof(int16_t) * (dsp->mute_data[x].end - dsp->mute_data[x].start)); |
| 1875 |
} |
| 1876 |
|
| 1877 |
if (ast_format_cmp(af->subclass.format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) { |
| 1878 |
for (x = 0; x < len; x++) { |
| 1879 |
odata[x] = AST_LIN2MU((unsigned short) shortdata[x]); |
| 1880 |
} |
| 1881 |
} else if (ast_format_cmp(af->subclass.format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) { |
| 1882 |
for (x = 0; x < len; x++) { |
| 1883 |
odata[x] = AST_LIN2A((unsigned short) shortdata[x]); |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
if (outf) { |
| 1888 |
if (chan) { |
| 1889 |
ast_queue_frame(chan, af); |
| 1890 |
} |
| 1891 |
ast_frfree(af); |
| 1892 |
return ast_frisolate(outf); |
| 1893 |
} else { |
| 1894 |
return af; |
| 1895 |
} |
| 1896 |
} |
| 1897 |
|
| 1898 |
static void ast_dsp_prog_reset(struct ast_dsp *dsp) |
| 1899 |
{ |
| 1900 |
int max = 0; |
| 1901 |
int x; |
| 1902 |
|
| 1903 |
dsp->gsamp_size = modes[dsp->progmode].size; |
| 1904 |
dsp->gsamps = 0; |
| 1905 |
for (x = 0; x < FREQ_ARRAY_SIZE; x++) { |
| 1906 |
if (modes[dsp->progmode].freqs[x]) { |
| 1907 |
goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->sample_rate); |
| 1908 |
max = x + 1; |
| 1909 |
} |
| 1910 |
} |
| 1911 |
dsp->freqcount = max; |
| 1912 |
dsp->ringtimeout = 0; |
| 1913 |
} |
| 1914 |
|
| 1915 |
unsigned int ast_dsp_get_sample_rate(const struct ast_dsp *dsp) |
| 1916 |
{ |
| 1917 |
return dsp->sample_rate; |
| 1918 |
} |
| 1919 |
|
| 1920 |
static struct ast_dsp *__ast_dsp_new(unsigned int sample_rate) |
| 1921 |
{ |
| 1922 |
struct ast_dsp *dsp; |
| 1923 |
|
| 1924 |
if ((dsp = ast_calloc(1, sizeof(*dsp)))) { |
| 1925 |
dsp->threshold = DEFAULT_THRESHOLD; |
| 1926 |
dsp->features = DSP_FEATURE_SILENCE_SUPPRESS; |
| 1927 |
dsp->busycount = DSP_HISTORY; |
| 1928 |
dsp->digitmode = DSP_DIGITMODE_DTMF; |
| 1929 |
dsp->faxmode = DSP_FAXMODE_DETECT_CNG; |
| 1930 |
dsp->sample_rate = sample_rate; |
| 1931 |
dsp->freqcount = 0; |
| 1932 |
|
| 1933 |
ast_digit_detect_init(&dsp->digit_state, dsp->digitmode, dsp->sample_rate); |
| 1934 |
dsp->display_inband_dtmf_warning = 1; |
| 1935 |
|
| 1936 |
ast_dsp_prog_reset(dsp); |
| 1937 |
|
| 1938 |
ast_fax_detect_init(dsp); |
| 1939 |
} |
| 1940 |
return dsp; |
| 1941 |
} |
| 1942 |
|
| 1943 |
struct ast_dsp *ast_dsp_new(void) |
| 1944 |
{ |
| 1945 |
return __ast_dsp_new(DEFAULT_SAMPLE_RATE); |
| 1946 |
} |
| 1947 |
|
| 1948 |
struct ast_dsp *ast_dsp_new_with_rate(unsigned int sample_rate) |
| 1949 |
{ |
| 1950 |
return __ast_dsp_new(sample_rate); |
| 1951 |
} |
| 1952 |
|
| 1953 |
void ast_dsp_set_features(struct ast_dsp *dsp, int features) |
| 1954 |
{ |
| 1955 |
dsp->features = features; |
| 1956 |
if (!(features & DSP_FEATURE_DIGIT_DETECT)) { |
| 1957 |
dsp->display_inband_dtmf_warning = 0; |
| 1958 |
} |
| 1959 |
} |
| 1960 |
|
| 1961 |
|
| 1962 |
int ast_dsp_get_features(struct ast_dsp *dsp) |
| 1963 |
{ |
| 1964 |
return (dsp->features); |
| 1965 |
} |
| 1966 |
|
| 1967 |
|
| 1968 |
void ast_dsp_free(struct ast_dsp *dsp) |
| 1969 |
{ |
| 1970 |
ast_free(dsp); |
| 1971 |
} |
| 1972 |
|
| 1973 |
void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold) |
| 1974 |
{ |
| 1975 |
dsp->threshold = threshold; |
| 1976 |
} |
| 1977 |
|
| 1978 |
void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences) |
| 1979 |
{ |
| 1980 |
if (cadences < 4) { |
| 1981 |
cadences = 4; |
| 1982 |
} |
| 1983 |
if (cadences > DSP_HISTORY) { |
| 1984 |
cadences = DSP_HISTORY; |
| 1985 |
} |
| 1986 |
dsp->busycount = cadences; |
| 1987 |
} |
| 1988 |
|
| 1989 |
void ast_dsp_set_busy_pattern(struct ast_dsp *dsp, const struct ast_dsp_busy_pattern *cadence) |
| 1990 |
{ |
| 1991 |
dsp->busy_cadence = *cadence; |
| 1992 |
ast_debug(1, "dsp busy pattern set to %d,%d,%d,%d\n", cadence->pattern[0], cadence->pattern[1], (cadence->length == 4) ? cadence->pattern[2] : 0, (cadence->length == 4) ? cadence->pattern[3] : 0); |
| 1993 |
} |
| 1994 |
|
| 1995 |
void ast_dsp_digitreset(struct ast_dsp *dsp) |
| 1996 |
{ |
| 1997 |
int i; |
| 1998 |
|
| 1999 |
dsp->dtmf_began = 0; |
| 2000 |
if (dsp->digitmode & (DSP_DIGITMODE_MF | DSP_DIGITMODE_R2_FORWARD | DSP_DIGITMODE_R2_BACKWARD)) { |
| 2001 |
mf_detect_state_t *s = &dsp->digit_state.td.mf; |
| 2002 |
|
| 2003 |
for (i = 0; i < 6; i++) { |
| 2004 |
goertzel_reset(&s->tone_out[i]); |
| 2005 |
} |
| 2006 |
s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = 0; |
| 2007 |
s->current_hit = 0; |
| 2008 |
s->current_sample = 0; |
| 2009 |
} else { |
| 2010 |
dtmf_detect_state_t *s = &dsp->digit_state.td.dtmf; |
| 2011 |
|
| 2012 |
for (i = 0; i < 4; i++) { |
| 2013 |
goertzel_reset(&s->row_out[i]); |
| 2014 |
goertzel_reset(&s->col_out[i]); |
| 2015 |
} |
| 2016 |
s->lasthit = 0; |
| 2017 |
s->current_hit = 0; |
| 2018 |
s->energy = 0.0; |
| 2019 |
s->current_sample = 0; |
| 2020 |
s->hits = 0; |
| 2021 |
s->misses = 0; |
| 2022 |
} |
| 2023 |
|
| 2024 |
dsp->digit_state.digits[0] = '\0'; |
| 2025 |
dsp->digit_state.current_digits = 0; |
| 2026 |
} |
| 2027 |
|
| 2028 |
void ast_dsp_reset(struct ast_dsp *dsp) |
| 2029 |
{ |
| 2030 |
int x; |
| 2031 |
|
| 2032 |
dsp->totalsilence = 0; |
| 2033 |
dsp->gsamps = 0; |
| 2034 |
for (x = 0; x < 4; x++) { |
| 2035 |
dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0; |
| 2036 |
} |
| 2037 |
memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence)); |
| 2038 |
memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise)); |
| 2039 |
dsp->ringtimeout = 0; |
| 2040 |
} |
| 2041 |
|
| 2042 |
#define DSP_DIGITMODES (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_R2_FORWARD | DSP_DIGITMODE_R2_BACKWARD) |
| 2043 |
|
| 2044 |
int ast_dsp_set_digitmode(struct ast_dsp *dsp, int digitmode) |
| 2045 |
{ |
| 2046 |
int new; |
| 2047 |
int old; |
| 2048 |
|
| 2049 |
old = dsp->digitmode & (DSP_DIGITMODES | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX); |
| 2050 |
new = digitmode & (DSP_DIGITMODES | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX); |
| 2051 |
if (old != new) { |
| 2052 |
|
| 2053 |
ast_digit_detect_init(&dsp->digit_state, new, dsp->sample_rate); |
| 2054 |
} |
| 2055 |
dsp->digitmode = digitmode; |
| 2056 |
return 0; |
| 2057 |
} |
| 2058 |
|
| 2059 |
int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq, int dur, int db, int squelch) |
| 2060 |
{ |
| 2061 |
if (freq > 0) { |
| 2062 |
dsp->freqmode = 1; |
| 2063 |
ast_freq_detect_init(dsp, freq, dur, db, squelch); |
| 2064 |
} else { |
| 2065 |
dsp->freqmode = 0; |
| 2066 |
} |
| 2067 |
return 0; |
| 2068 |
} |
| 2069 |
|
| 2070 |
int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode) |
| 2071 |
{ |
| 2072 |
if (dsp->faxmode != faxmode) { |
| 2073 |
dsp->faxmode = faxmode; |
| 2074 |
ast_fax_detect_init(dsp); |
| 2075 |
} |
| 2076 |
return 0; |
| 2077 |
} |
| 2078 |
|
| 2079 |
int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone) |
| 2080 |
{ |
| 2081 |
int x; |
| 2082 |
|
| 2083 |
for (x = 0; x < ARRAY_LEN(aliases); x++) { |
| 2084 |
if (!strcasecmp(aliases[x].name, zone)) { |
| 2085 |
dsp->progmode = aliases[x].mode; |
| 2086 |
ast_dsp_prog_reset(dsp); |
| 2087 |
return 0; |
| 2088 |
} |
| 2089 |
} |
| 2090 |
return -1; |
| 2091 |
} |
| 2092 |
|
| 2093 |
int ast_dsp_was_muted(struct ast_dsp *dsp) |
| 2094 |
{ |
| 2095 |
return (dsp->mute_fragments > 0); |
| 2096 |
} |
| 2097 |
|
| 2098 |
int ast_dsp_get_tstate(struct ast_dsp *dsp) |
| 2099 |
{ |
| 2100 |
return dsp->tstate; |
| 2101 |
} |
| 2102 |
|
| 2103 |
int ast_dsp_get_tcount(struct ast_dsp *dsp) |
| 2104 |
{ |
| 2105 |
return dsp->tcount; |
| 2106 |
} |
| 2107 |
|
| 2108 |
static int _dsp_init(int reload) |
| 2109 |
{ |
| 2110 |
struct ast_config *cfg; |
| 2111 |
struct ast_variable *v; |
| 2112 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 2113 |
int cfg_threshold; |
| 2114 |
float cfg_twist; |
| 2115 |
|
| 2116 |
if ((cfg = ast_config_load2(CONFIG_FILE_NAME, "dsp", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) { |
| 2117 |
return 0; |
| 2118 |
} |
| 2119 |
|
| 2120 |
thresholds[THRESHOLD_SILENCE] = DEFAULT_SILENCE_THRESHOLD; |
| 2121 |
dtmf_normal_twist = DEF_DTMF_NORMAL_TWIST; |
| 2122 |
dtmf_reverse_twist = DEF_DTMF_REVERSE_TWIST; |
| 2123 |
relax_dtmf_normal_twist = DEF_RELAX_DTMF_NORMAL_TWIST; |
| 2124 |
relax_dtmf_reverse_twist = DEF_RELAX_DTMF_REVERSE_TWIST; |
| 2125 |
dtmf_hits_to_begin = DEF_DTMF_HITS_TO_BEGIN; |
| 2126 |
dtmf_misses_to_end = DEF_DTMF_MISSES_TO_END; |
| 2127 |
|
| 2128 |
if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) { |
| 2129 |
return 0; |
| 2130 |
} |
| 2131 |
|
| 2132 |
for (v = ast_variable_browse(cfg, "default"); v; v = v->next) { |
| 2133 |
if (!strcasecmp(v->name, "silencethreshold")) { |
| 2134 |
if (sscanf(v->value, "%30d", &cfg_threshold) < 1) { |
| 2135 |
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value); |
| 2136 |
} else if (cfg_threshold < 0) { |
| 2137 |
ast_log(LOG_WARNING, "Invalid silence threshold '%d' specified, using default\n", cfg_threshold); |
| 2138 |
} else { |
| 2139 |
thresholds[THRESHOLD_SILENCE] = cfg_threshold; |
| 2140 |
} |
| 2141 |
} else if (!strcasecmp(v->name, "dtmf_normal_twist")) { |
| 2142 |
if (sscanf(v->value, "%30f", &cfg_twist) < 1) { |
| 2143 |
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value); |
| 2144 |
} else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { |
| 2145 |
ast_log(LOG_WARNING, "Invalid dtmf_normal_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, dtmf_normal_twist); |
| 2146 |
} else { |
| 2147 |
dtmf_normal_twist = cfg_twist; |
| 2148 |
} |
| 2149 |
} else if (!strcasecmp(v->name, "dtmf_reverse_twist")) { |
| 2150 |
if (sscanf(v->value, "%30f", &cfg_twist) < 1) { |
| 2151 |
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value); |
| 2152 |
} else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { |
| 2153 |
ast_log(LOG_WARNING, "Invalid dtmf_reverse_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, dtmf_reverse_twist); |
| 2154 |
} else { |
| 2155 |
dtmf_reverse_twist = cfg_twist; |
| 2156 |
} |
| 2157 |
} else if (!strcasecmp(v->name, "relax_dtmf_normal_twist")) { |
| 2158 |
if (sscanf(v->value, "%30f", &cfg_twist) < 1) { |
| 2159 |
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value); |
| 2160 |
} else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { |
| 2161 |
ast_log(LOG_WARNING, "Invalid relax_dtmf_normal_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, relax_dtmf_normal_twist); |
| 2162 |
} else { |
| 2163 |
relax_dtmf_normal_twist = cfg_twist; |
| 2164 |
} |
| 2165 |
} else if (!strcasecmp(v->name, "relax_dtmf_reverse_twist")) { |
| 2166 |
if (sscanf(v->value, "%30f", &cfg_twist) < 1) { |
| 2167 |
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value); |
| 2168 |
} else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { |
| 2169 |
ast_log(LOG_WARNING, "Invalid relax_dtmf_reverse_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, relax_dtmf_reverse_twist); |
| 2170 |
} else { |
| 2171 |
relax_dtmf_reverse_twist = cfg_twist; |
| 2172 |
} |
| 2173 |
} else if (!strcasecmp(v->name, "dtmf_hits_to_begin")) { |
| 2174 |
if (sscanf(v->value, "%30d", &cfg_threshold) < 1) { |
| 2175 |
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value); |
| 2176 |
} else if (cfg_threshold < 1) { |
| 2177 |
ast_log(LOG_WARNING, "Invalid dtmf_hits_to_begin value '%d' specified, using default of %d\n", cfg_threshold, dtmf_hits_to_begin); |
| 2178 |
} else { |
| 2179 |
dtmf_hits_to_begin = cfg_threshold; |
| 2180 |
} |
| 2181 |
} else if (!strcasecmp(v->name, "dtmf_misses_to_end")) { |
| 2182 |
if (sscanf(v->value, "%30d", &cfg_threshold) < 1) { |
| 2183 |
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value); |
| 2184 |
} else if (cfg_threshold < 1) { |
| 2185 |
ast_log(LOG_WARNING, "Invalid dtmf_misses_to_end value '%d' specified, using default of %d\n", cfg_threshold, dtmf_misses_to_end); |
| 2186 |
} else { |
| 2187 |
dtmf_misses_to_end = cfg_threshold; |
| 2188 |
} |
| 2189 |
} |
| 2190 |
} |
| 2191 |
ast_config_destroy(cfg); |
| 2192 |
|
| 2193 |
return 0; |
| 2194 |
} |
| 2195 |
|
| 2196 |
int ast_dsp_get_threshold_from_settings(enum threshold which) |
| 2197 |
{ |
| 2198 |
return thresholds[which]; |
| 2199 |
} |
| 2200 |
|
| 2201 |
#ifdef TEST_FRAMEWORK |
| 2202 |
static void test_tone_sample_gen(short *slin_buf, int samples, int rate, int freq, short amplitude) |
| 2203 |
{ |
| 2204 |
int idx; |
| 2205 |
double sample_step = 2.0 * M_PI * freq / rate; |
| 2206 |
|
| 2207 |
for (idx = 0; idx < samples; ++idx) { |
| 2208 |
slin_buf[idx] = amplitude * sin(sample_step * idx); |
| 2209 |
} |
| 2210 |
} |
| 2211 |
#endif |
| 2212 |
|
| 2213 |
#ifdef TEST_FRAMEWORK |
| 2214 |
static void test_tone_sample_gen_add(short *slin_buf, int samples, int rate, int freq, short amplitude) |
| 2215 |
{ |
| 2216 |
int idx; |
| 2217 |
double sample_step = 2.0 * M_PI * freq / rate; |
| 2218 |
|
| 2219 |
for (idx = 0; idx < samples; ++idx) { |
| 2220 |
slin_buf[idx] += amplitude * sin(sample_step * idx); |
| 2221 |
} |
| 2222 |
} |
| 2223 |
#endif |
| 2224 |
|
| 2225 |
#ifdef TEST_FRAMEWORK |
| 2226 |
static void test_dual_sample_gen(short *slin_buf, int samples, int rate, int f1, short a1, int f2, short a2) |
| 2227 |
{ |
| 2228 |
test_tone_sample_gen(slin_buf, samples, rate, f1, a1); |
| 2229 |
test_tone_sample_gen_add(slin_buf, samples, rate, f2, a2); |
| 2230 |
} |
| 2231 |
#endif |
| 2232 |
|
| 2233 |
#ifdef TEST_FRAMEWORK |
| 2234 |
#define TONE_AMPLITUDE_MAX 0x7fff |
| 2235 |
#define TONE_AMPLITUDE_MIN 80 |
| 2236 |
|
| 2237 |
static int test_tone_amplitude_sweep(struct ast_test *test, struct ast_dsp *dsp, tone_detect_state_t *tone_state) |
| 2238 |
{ |
| 2239 |
short slin_buf[tone_state->block_size]; |
| 2240 |
int result; |
| 2241 |
int idx; |
| 2242 |
struct { |
| 2243 |
short amp_val; |
| 2244 |
int detect; |
| 2245 |
} amp_tests[] = { |
| 2246 |
{ .amp_val = TONE_AMPLITUDE_MAX, .detect = 1, }, |
| 2247 |
{ .amp_val = 10000, .detect = 1, }, |
| 2248 |
{ .amp_val = 1000, .detect = 1, }, |
| 2249 |
{ .amp_val = 100, .detect = 1, }, |
| 2250 |
{ .amp_val = TONE_AMPLITUDE_MIN, .detect = 1, }, |
| 2251 |
{ .amp_val = 75, .detect = 0, }, |
| 2252 |
{ .amp_val = 10, .detect = 0, }, |
| 2253 |
{ .amp_val = 1, .detect = 0, }, |
| 2254 |
}; |
| 2255 |
|
| 2256 |
result = 0; |
| 2257 |
|
| 2258 |
for (idx = 0; idx < ARRAY_LEN(amp_tests); ++idx) { |
| 2259 |
int detected; |
| 2260 |
int duration; |
| 2261 |
|
| 2262 |
ast_debug(1, "Test %d Hz at amplitude %d\n", |
| 2263 |
tone_state->freq, amp_tests[idx].amp_val); |
| 2264 |
test_tone_sample_gen(slin_buf, tone_state->block_size, DEFAULT_SAMPLE_RATE, |
| 2265 |
tone_state->freq, amp_tests[idx].amp_val); |
| 2266 |
|
| 2267 |
detected = 0; |
| 2268 |
for (duration = 0; !detected && duration < tone_state->hits_required + 3; ++duration) { |
| 2269 |
detected = tone_detect(dsp, tone_state, slin_buf, tone_state->block_size) ? 1 : 0; |
| 2270 |
} |
| 2271 |
if (amp_tests[idx].detect != detected) { |
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
ast_debug(1, |
| 2279 |
"Test %d Hz at amplitude %d failed. Detected: %s\n", |
| 2280 |
tone_state->freq, amp_tests[idx].amp_val, |
| 2281 |
detected ? "yes" : "no"); |
| 2282 |
ast_test_status_update(test, |
| 2283 |
"Test %d Hz at amplitude %d failed. Detected: %s\n", |
| 2284 |
tone_state->freq, amp_tests[idx].amp_val, |
| 2285 |
detected ? "yes" : "no"); |
| 2286 |
result = -1; |
| 2287 |
} |
| 2288 |
tone_state->hit_count = 0; |
| 2289 |
} |
| 2290 |
|
| 2291 |
return result; |
| 2292 |
} |
| 2293 |
#endif |
| 2294 |
|
| 2295 |
#ifdef TEST_FRAMEWORK |
| 2296 |
static int test_dtmf_amplitude_sweep(struct ast_test *test, struct ast_dsp *dsp, int digit_index) |
| 2297 |
{ |
| 2298 |
short slin_buf[DTMF_GSIZE]; |
| 2299 |
int result; |
| 2300 |
int row; |
| 2301 |
int column; |
| 2302 |
int idx; |
| 2303 |
struct { |
| 2304 |
short amp_val; |
| 2305 |
int digit; |
| 2306 |
} amp_tests[] = { |
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
{ .amp_val = TONE_AMPLITUDE_MAX/2, .digit = dtmf_positions[digit_index], }, |
| 2312 |
{ .amp_val = 10000, .digit = dtmf_positions[digit_index], }, |
| 2313 |
{ .amp_val = 1000, .digit = dtmf_positions[digit_index], }, |
| 2314 |
{ .amp_val = 500, .digit = dtmf_positions[digit_index], }, |
| 2315 |
{ .amp_val = 250, .digit = dtmf_positions[digit_index], }, |
| 2316 |
{ .amp_val = 200, .digit = dtmf_positions[digit_index], }, |
| 2317 |
{ .amp_val = 180, .digit = dtmf_positions[digit_index], }, |
| 2318 |
|
| 2319 |
{ .amp_val = 170, .digit = 0, }, |
| 2320 |
{ .amp_val = 100, .digit = 0, }, |
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
{ .amp_val = TONE_AMPLITUDE_MIN, .digit = 0, }, |
| 2326 |
{ .amp_val = 75, .digit = 0, }, |
| 2327 |
{ .amp_val = 10, .digit = 0, }, |
| 2328 |
{ .amp_val = 1, .digit = 0, }, |
| 2329 |
}; |
| 2330 |
|
| 2331 |
row = (digit_index >> 2) & 0x03; |
| 2332 |
column = digit_index & 0x03; |
| 2333 |
|
| 2334 |
result = 0; |
| 2335 |
|
| 2336 |
for (idx = 0; idx < ARRAY_LEN(amp_tests); ++idx) { |
| 2337 |
int digit; |
| 2338 |
int duration; |
| 2339 |
|
| 2340 |
ast_debug(1, "Test '%c' at amplitude %d\n", |
| 2341 |
dtmf_positions[digit_index], amp_tests[idx].amp_val); |
| 2342 |
test_dual_sample_gen(slin_buf, ARRAY_LEN(slin_buf), DEFAULT_SAMPLE_RATE, |
| 2343 |
(int) dtmf_row[row], amp_tests[idx].amp_val, |
| 2344 |
(int) dtmf_col[column], amp_tests[idx].amp_val); |
| 2345 |
|
| 2346 |
digit = 0; |
| 2347 |
for (duration = 0; !digit && duration < 3; ++duration) { |
| 2348 |
digit = dtmf_detect(dsp, &dsp->digit_state, slin_buf, ARRAY_LEN(slin_buf), |
| 2349 |
0, 0); |
| 2350 |
} |
| 2351 |
if (amp_tests[idx].digit != digit) { |
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
|
| 2358 |
ast_debug(1, |
| 2359 |
"Test '%c' at amplitude %d failed. Detected Digit: '%c'\n", |
| 2360 |
dtmf_positions[digit_index], amp_tests[idx].amp_val, |
| 2361 |
digit ?: ' '); |
| 2362 |
ast_test_status_update(test, |
| 2363 |
"Test '%c' at amplitude %d failed. Detected Digit: '%c'\n", |
| 2364 |
dtmf_positions[digit_index], amp_tests[idx].amp_val, |
| 2365 |
digit ?: ' '); |
| 2366 |
result = -1; |
| 2367 |
} |
| 2368 |
ast_dsp_digitreset(dsp); |
| 2369 |
} |
| 2370 |
|
| 2371 |
return result; |
| 2372 |
} |
| 2373 |
#endif |
| 2374 |
|
| 2375 |
#ifdef TEST_FRAMEWORK |
| 2376 |
static int test_dtmf_twist_sweep(struct ast_test *test, struct ast_dsp *dsp, int digit_index) |
| 2377 |
{ |
| 2378 |
short slin_buf[DTMF_GSIZE]; |
| 2379 |
int result; |
| 2380 |
int row; |
| 2381 |
int column; |
| 2382 |
int idx; |
| 2383 |
struct { |
| 2384 |
short amp_row; |
| 2385 |
short amp_col; |
| 2386 |
int digit; |
| 2387 |
} twist_tests[] = { |
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
{ .amp_row = 1000 + 1800, .amp_col = 1000 + 0, .digit = 0, }, |
| 2396 |
{ .amp_row = 1000 + 1700, .amp_col = 1000 + 0, .digit = 0, }, |
| 2397 |
|
| 2398 |
{ .amp_row = 1000 + 1400, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], }, |
| 2399 |
{ .amp_row = 1000 + 1300, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], }, |
| 2400 |
{ .amp_row = 1000 + 1200, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], }, |
| 2401 |
{ .amp_row = 1000 + 1100, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], }, |
| 2402 |
{ .amp_row = 1000 + 1000, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], }, |
| 2403 |
{ .amp_row = 1000 + 100, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], }, |
| 2404 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 100, .digit = dtmf_positions[digit_index], }, |
| 2405 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 200, .digit = dtmf_positions[digit_index], }, |
| 2406 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 300, .digit = dtmf_positions[digit_index], }, |
| 2407 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 400, .digit = dtmf_positions[digit_index], }, |
| 2408 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 500, .digit = dtmf_positions[digit_index], }, |
| 2409 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 550, .digit = dtmf_positions[digit_index], }, |
| 2410 |
|
| 2411 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 650, .digit = 0, }, |
| 2412 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 700, .digit = 0, }, |
| 2413 |
{ .amp_row = 1000 + 0, .amp_col = 1000 + 800, .digit = 0, }, |
| 2414 |
}; |
| 2415 |
float save_normal_twist; |
| 2416 |
float save_reverse_twist; |
| 2417 |
|
| 2418 |
save_normal_twist = dtmf_normal_twist; |
| 2419 |
save_reverse_twist = dtmf_reverse_twist; |
| 2420 |
dtmf_normal_twist = DEF_DTMF_NORMAL_TWIST; |
| 2421 |
dtmf_reverse_twist = DEF_DTMF_REVERSE_TWIST; |
| 2422 |
|
| 2423 |
row = (digit_index >> 2) & 0x03; |
| 2424 |
column = digit_index & 0x03; |
| 2425 |
|
| 2426 |
result = 0; |
| 2427 |
|
| 2428 |
for (idx = 0; idx < ARRAY_LEN(twist_tests); ++idx) { |
| 2429 |
int digit; |
| 2430 |
int duration; |
| 2431 |
|
| 2432 |
ast_debug(1, "Test '%c' twist row %d col %d amplitudes\n", |
| 2433 |
dtmf_positions[digit_index], |
| 2434 |
twist_tests[idx].amp_row, twist_tests[idx].amp_col); |
| 2435 |
test_dual_sample_gen(slin_buf, ARRAY_LEN(slin_buf), DEFAULT_SAMPLE_RATE, |
| 2436 |
(int) dtmf_row[row], twist_tests[idx].amp_row, |
| 2437 |
(int) dtmf_col[column], twist_tests[idx].amp_col); |
| 2438 |
|
| 2439 |
digit = 0; |
| 2440 |
for (duration = 0; !digit && duration < 3; ++duration) { |
| 2441 |
digit = dtmf_detect(dsp, &dsp->digit_state, slin_buf, ARRAY_LEN(slin_buf), |
| 2442 |
0, 0); |
| 2443 |
} |
| 2444 |
if (twist_tests[idx].digit != digit) { |
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
ast_debug(1, |
| 2452 |
"Test '%c' twist row %d col %d amplitudes failed. Detected Digit: '%c'\n", |
| 2453 |
dtmf_positions[digit_index], |
| 2454 |
twist_tests[idx].amp_row, twist_tests[idx].amp_col, |
| 2455 |
digit ?: ' '); |
| 2456 |
ast_test_status_update(test, |
| 2457 |
"Test '%c' twist row %d col %d amplitudes failed. Detected Digit: '%c'\n", |
| 2458 |
dtmf_positions[digit_index], |
| 2459 |
twist_tests[idx].amp_row, twist_tests[idx].amp_col, |
| 2460 |
digit ?: ' '); |
| 2461 |
result = -1; |
| 2462 |
} |
| 2463 |
ast_dsp_digitreset(dsp); |
| 2464 |
} |
| 2465 |
|
| 2466 |
dtmf_normal_twist = save_normal_twist; |
| 2467 |
dtmf_reverse_twist = save_reverse_twist; |
| 2468 |
|
| 2469 |
return result; |
| 2470 |
} |
| 2471 |
#endif |
| 2472 |
|
| 2473 |
#ifdef TEST_FRAMEWORK |
| 2474 |
static int test_tone_freq_sweep(struct ast_test *test, struct ast_dsp *dsp, tone_detect_state_t *tone_state, short amplitude) |
| 2475 |
{ |
| 2476 |
short slin_buf[tone_state->block_size]; |
| 2477 |
int result; |
| 2478 |
int freq; |
| 2479 |
int lower_freq; |
| 2480 |
int upper_freq; |
| 2481 |
|
| 2482 |
|
| 2483 |
lower_freq = tone_state->freq - 4; |
| 2484 |
upper_freq = tone_state->freq + 4; |
| 2485 |
|
| 2486 |
result = 0; |
| 2487 |
|
| 2488 |
|
| 2489 |
for (freq = 100; freq <= 3500; freq += 1) { |
| 2490 |
int detected; |
| 2491 |
int duration; |
| 2492 |
int expect_detection; |
| 2493 |
|
| 2494 |
if (freq == tone_state->freq) { |
| 2495 |
|
| 2496 |
continue; |
| 2497 |
} |
| 2498 |
|
| 2499 |
expect_detection = (lower_freq <= freq && freq <= upper_freq) ? 1 : 0; |
| 2500 |
|
| 2501 |
ast_debug(1, "Test %d Hz detection given %d Hz tone at amplitude %d. Range:%d-%d Expect detect: %s\n", |
| 2502 |
tone_state->freq, freq, amplitude, lower_freq, upper_freq, |
| 2503 |
expect_detection ? "yes" : "no"); |
| 2504 |
test_tone_sample_gen(slin_buf, tone_state->block_size, DEFAULT_SAMPLE_RATE, freq, |
| 2505 |
amplitude); |
| 2506 |
|
| 2507 |
detected = 0; |
| 2508 |
for (duration = 0; !detected && duration < tone_state->hits_required + 3; ++duration) { |
| 2509 |
detected = tone_detect(dsp, tone_state, slin_buf, tone_state->block_size) ? 1 : 0; |
| 2510 |
} |
| 2511 |
if (expect_detection != detected) { |
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
ast_debug(1, |
| 2519 |
"Test %d Hz detection given %d Hz tone at amplitude %d failed. Range:%d-%d Detected: %s\n", |
| 2520 |
tone_state->freq, freq, amplitude, lower_freq, upper_freq, |
| 2521 |
detected ? "yes" : "no"); |
| 2522 |
ast_test_status_update(test, |
| 2523 |
"Test %d Hz detection given %d Hz tone at amplitude %d failed. Range:%d-%d Detected: %s\n", |
| 2524 |
tone_state->freq, freq, amplitude, lower_freq, upper_freq, |
| 2525 |
detected ? "yes" : "no"); |
| 2526 |
result = -1; |
| 2527 |
} |
| 2528 |
tone_state->hit_count = 0; |
| 2529 |
} |
| 2530 |
|
| 2531 |
return result; |
| 2532 |
} |
| 2533 |
#endif |
| 2534 |
|
| 2535 |
#ifdef TEST_FRAMEWORK |
| 2536 |
AST_TEST_DEFINE(test_dsp_fax_detect) |
| 2537 |
{ |
| 2538 |
struct ast_dsp *dsp; |
| 2539 |
enum ast_test_result_state result; |
| 2540 |
|
| 2541 |
switch (cmd) { |
| 2542 |
case TEST_INIT: |
| 2543 |
info->name = "fax"; |
| 2544 |
info->category = "/main/dsp/"; |
| 2545 |
info->summary = "DSP fax tone detect unit test"; |
| 2546 |
info->description = |
| 2547 |
"Tests fax tone detection code."; |
| 2548 |
return AST_TEST_NOT_RUN; |
| 2549 |
case TEST_EXECUTE: |
| 2550 |
break; |
| 2551 |
} |
| 2552 |
|
| 2553 |
dsp = ast_dsp_new(); |
| 2554 |
if (!dsp) { |
| 2555 |
return AST_TEST_FAIL; |
| 2556 |
} |
| 2557 |
|
| 2558 |
result = AST_TEST_PASS; |
| 2559 |
|
| 2560 |
|
| 2561 |
if (test_tone_amplitude_sweep(test, dsp, &dsp->cng_tone_state)) { |
| 2562 |
result = AST_TEST_FAIL; |
| 2563 |
} |
| 2564 |
|
| 2565 |
|
| 2566 |
if (test_tone_amplitude_sweep(test, dsp, &dsp->ced_tone_state)) { |
| 2567 |
result = AST_TEST_FAIL; |
| 2568 |
} |
| 2569 |
|
| 2570 |
|
| 2571 |
if (test_tone_freq_sweep(test, dsp, &dsp->cng_tone_state, TONE_AMPLITUDE_MAX)) { |
| 2572 |
result = AST_TEST_FAIL; |
| 2573 |
} |
| 2574 |
if (test_tone_freq_sweep(test, dsp, &dsp->cng_tone_state, TONE_AMPLITUDE_MIN)) { |
| 2575 |
result = AST_TEST_FAIL; |
| 2576 |
} |
| 2577 |
|
| 2578 |
|
| 2579 |
if (test_tone_freq_sweep(test, dsp, &dsp->ced_tone_state, TONE_AMPLITUDE_MAX)) { |
| 2580 |
result = AST_TEST_FAIL; |
| 2581 |
} |
| 2582 |
if (test_tone_freq_sweep(test, dsp, &dsp->ced_tone_state, TONE_AMPLITUDE_MIN)) { |
| 2583 |
result = AST_TEST_FAIL; |
| 2584 |
} |
| 2585 |
|
| 2586 |
ast_dsp_free(dsp); |
| 2587 |
return result; |
| 2588 |
} |
| 2589 |
#endif |
| 2590 |
|
| 2591 |
#ifdef TEST_FRAMEWORK |
| 2592 |
AST_TEST_DEFINE(test_dsp_dtmf_detect) |
| 2593 |
{ |
| 2594 |
int idx; |
| 2595 |
struct ast_dsp *dsp; |
| 2596 |
enum ast_test_result_state result; |
| 2597 |
|
| 2598 |
switch (cmd) { |
| 2599 |
case TEST_INIT: |
| 2600 |
info->name = "dtmf"; |
| 2601 |
info->category = "/main/dsp/"; |
| 2602 |
info->summary = "DSP DTMF detect unit test"; |
| 2603 |
info->description = |
| 2604 |
"Tests DTMF detection code."; |
| 2605 |
return AST_TEST_NOT_RUN; |
| 2606 |
case TEST_EXECUTE: |
| 2607 |
break; |
| 2608 |
} |
| 2609 |
|
| 2610 |
dsp = ast_dsp_new(); |
| 2611 |
if (!dsp) { |
| 2612 |
return AST_TEST_FAIL; |
| 2613 |
} |
| 2614 |
|
| 2615 |
result = AST_TEST_PASS; |
| 2616 |
|
| 2617 |
for (idx = 0; dtmf_positions[idx]; ++idx) { |
| 2618 |
if (test_dtmf_amplitude_sweep(test, dsp, idx)) { |
| 2619 |
result = AST_TEST_FAIL; |
| 2620 |
} |
| 2621 |
} |
| 2622 |
|
| 2623 |
for (idx = 0; dtmf_positions[idx]; ++idx) { |
| 2624 |
if (test_dtmf_twist_sweep(test, dsp, idx)) { |
| 2625 |
result = AST_TEST_FAIL; |
| 2626 |
} |
| 2627 |
} |
| 2628 |
|
| 2629 |
ast_dsp_free(dsp); |
| 2630 |
return result; |
| 2631 |
} |
| 2632 |
#endif |
| 2633 |
|
| 2634 |
static int unload_module(void) |
| 2635 |
{ |
| 2636 |
AST_TEST_UNREGISTER(test_dsp_fax_detect); |
| 2637 |
AST_TEST_UNREGISTER(test_dsp_dtmf_detect); |
| 2638 |
|
| 2639 |
return 0; |
| 2640 |
} |
| 2641 |
|
| 2642 |
static int load_module(void) |
| 2643 |
{ |
| 2644 |
if (_dsp_init(0)) { |
| 2645 |
return AST_MODULE_LOAD_FAILURE; |
| 2646 |
} |
| 2647 |
|
| 2648 |
AST_TEST_REGISTER(test_dsp_fax_detect); |
| 2649 |
AST_TEST_REGISTER(test_dsp_dtmf_detect); |
| 2650 |
|
| 2651 |
return AST_MODULE_LOAD_SUCCESS; |
| 2652 |
} |
| 2653 |
|
| 2654 |
static int reload_module(void) |
| 2655 |
{ |
| 2656 |
return _dsp_init(1); |
| 2657 |
} |
| 2658 |
|
| 2659 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "DSP", |
| 2660 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 2661 |
.load = load_module, |
| 2662 |
.unload = unload_module, |
| 2663 |
.reload = reload_module, |
| 2664 |
.load_pri = AST_MODPRI_CORE, |
| 2665 |
.requires = "extconfig", |
| 2666 |
); |