Changeset 2212 for pjproject/trunk/pjmedia/src/pjmedia/echo_suppress.c
- Timestamp:
- Aug 13, 2008 6:21:03 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia/echo_suppress.c
r2208 r2212 244 244 float best_corr; /* Best correlation so far. */ 245 245 246 unsigned sum_rec_level; /* Running sum of level in rec_hist */ 247 float rec_corr; /* Running corr in rec_hist. */ 248 249 unsigned sum_play_level0; /* Running sum of level for first pos */ 250 float play_corr0; /* Running corr for first pos . */ 251 246 252 float *min_factor; /* Array of minimum scaling factor */ 247 253 float *avg_factor; /* Array of average scaling factor */ … … 354 360 ec->residue = 0; 355 361 ec->running_cnt = 0; 362 ec->sum_rec_level = ec->sum_play_level0 = 0; 363 ec->rec_corr = ec->play_corr0 = 0; 356 364 } 357 365 … … 376 384 ec->residue = 0; 377 385 ec->running_cnt = 0; 386 ec->sum_rec_level = ec->sum_play_level0 = 0; 387 ec->rec_corr = ec->play_corr0 = 0; 378 388 379 389 PJ_LOG(4,(THIS_FILE, "Echo suppressor soft reset. Re-learning..")); … … 404 414 { 405 415 int prev_index; 406 unsigned i, frm_level, sum_rec_level; 407 float rec_corr; 416 unsigned i, j, frm_level, sum_play_level, ulaw; 417 pj_uint16_t old_rec_frm_level, old_play_frm_level; 418 float play_corr; 408 419 409 420 ++ec->update_cnt; … … 415 426 ++frm_level; /* to avoid division by zero */ 416 427 428 /* Save the oldest frame level for later */ 429 old_play_frm_level = ec->play_hist[0]; 430 417 431 /* Push current frame level to the back of the play history */ 418 432 pj_array_erase(ec->play_hist, sizeof(pj_uint16_t), ec->play_hist_cnt, 0); … … 423 437 ++frm_level; /* to avoid division by zero */ 424 438 439 /* Save the oldest frame level for later */ 440 old_rec_frm_level = ec->rec_hist[0]; 441 425 442 /* Push to the back of the rec history */ 426 443 pj_array_erase(ec->rec_hist, sizeof(pj_uint16_t), ec->templ_cnt, 0); … … 438 455 439 456 /* Calculate rec signal pattern */ 440 rec_corr = 0; 441 sum_rec_level = 0; 442 for (i=0; i < ec->templ_cnt-1; ++i) { 443 float corr; 444 corr = (float)ec->rec_hist[i+1] / ec->rec_hist[i]; 445 rec_corr += corr; 446 sum_rec_level += ec->rec_hist[i]; 447 } 448 sum_rec_level += ec->rec_hist[i]; 457 if (ec->sum_rec_level == 0) { 458 /* Buffer has just been filled up, do full calculation */ 459 ec->rec_corr = 0; 460 ec->sum_rec_level = 0; 461 for (i=0; i < ec->templ_cnt-1; ++i) { 462 float corr; 463 corr = (float)ec->rec_hist[i+1] / ec->rec_hist[i]; 464 ec->rec_corr += corr; 465 ec->sum_rec_level += ec->rec_hist[i]; 466 } 467 ec->sum_rec_level += ec->rec_hist[i]; 468 } else { 469 /* Update from previous calculation */ 470 ec->sum_rec_level = ec->sum_rec_level - old_rec_frm_level + 471 ec->rec_hist[ec->templ_cnt-1]; 472 ec->rec_corr = ec->rec_corr - ((float)ec->rec_hist[0] / 473 old_rec_frm_level) + 474 ((float)ec->rec_hist[ec->templ_cnt-1] / 475 ec->rec_hist[ec->templ_cnt-2]); 476 } 449 477 450 478 /* Iterate through the play history and calculate the signal correlation … … 453 481 * to detect echo. 454 482 */ 455 for (i=0; i < ec->tail_cnt; ++i) { 456 unsigned j, end, sum_play_level, ulaw; 457 float play_corr = 0, corr_diff; 458 483 /* 484 * First phase: do full calculation for the first position 485 */ 486 if (ec->sum_play_level0 == 0) { 487 /* Buffer has just been filled up, do full calculation */ 459 488 sum_play_level = 0; 460 for (j=i, end=i+ec->templ_cnt-1; j<end; ++j) { 489 play_corr = 0; 490 for (j=0; j<ec->templ_cnt-1; ++j) { 461 491 float corr; 462 492 corr = (float)ec->play_hist[j+1] / ec->play_hist[j]; … … 465 495 } 466 496 sum_play_level += ec->play_hist[j]; 497 ec->sum_play_level0 = sum_play_level; 498 ec->play_corr0 = play_corr; 499 } else { 500 /* Update from previous calculation */ 501 ec->sum_play_level0 = ec->sum_play_level0 - old_play_frm_level + 502 ec->play_hist[ec->templ_cnt-1]; 503 ec->play_corr0 = ec->play_corr0 - ((float)ec->play_hist[0] / 504 old_play_frm_level) + 505 ((float)ec->play_hist[ec->templ_cnt-1] / 506 ec->play_hist[ec->templ_cnt-2]); 507 sum_play_level = ec->sum_play_level0; 508 play_corr = ec->play_corr0; 509 } 510 ec->tmp_corr[0] = FABS(play_corr - ec->rec_corr); 511 ec->tmp_factor[0] = (float)ec->sum_rec_level / sum_play_level; 512 513 /* Bail out if remote isn't talking */ 514 ulaw = pjmedia_linear2ulaw(sum_play_level/ec->templ_cnt) ^ 0xFF; 515 if (ulaw < MIN_SIGNAL_ULAW) { 516 echo_supp_set_state(ec, ST_REM_SILENT, ulaw); 517 return; 518 } 519 /* Bail out if local user is talking */ 520 if (ec->sum_rec_level > sum_play_level) { 521 echo_supp_set_state(ec, ST_LOCAL_TALK, ulaw); 522 return; 523 } 524 525 /* 526 * Second phase: do incremental calculation for the rest of positions 527 */ 528 for (i=1; i < ec->tail_cnt; ++i) { 529 unsigned end; 530 531 end = i + ec->templ_cnt; 532 533 sum_play_level = sum_play_level - ec->play_hist[i-1] + 534 ec->play_hist[end-1]; 535 play_corr = play_corr - ((float)ec->play_hist[i]/ec->play_hist[i-1]) + 536 ((float)ec->play_hist[end-1]/ec->play_hist[end-2]); 467 537 468 538 /* Bail out if remote isn't talking */ … … 474 544 475 545 /* Bail out if local user is talking */ 476 if ( sum_rec_level >=sum_play_level) {546 if (ec->sum_rec_level > sum_play_level) { 477 547 echo_supp_set_state(ec, ST_LOCAL_TALK, ulaw); 478 548 return; … … 482 552 // disabled: not a good idea if mic throws out loud echo 483 553 /* Also bail out if we suspect there's a doubletalk */ 484 ulaw = pjmedia_linear2ulaw( sum_rec_level/ec->templ_cnt) ^ 0xFF;554 ulaw = pjmedia_linear2ulaw(ec->sum_rec_level/ec->templ_cnt) ^ 0xFF; 485 555 if (ulaw > MIN_SIGNAL_ULAW) { 486 556 echo_supp_set_state(ec, ST_DOUBLETALK, ulaw); … … 490 560 491 561 /* Calculate correlation and save to temporary array */ 492 corr_diff = FABS(play_corr - rec_corr); 493 ec->tmp_corr[i] = corr_diff; 562 ec->tmp_corr[i] = FABS(play_corr - ec->rec_corr); 494 563 495 564 /* Also calculate the gain factor between mic and speaker level */ 496 ec->tmp_factor[i] = (float) sum_rec_level / sum_play_level;565 ec->tmp_factor[i] = (float)ec->sum_rec_level / sum_play_level; 497 566 pj_assert(ec->tmp_factor[i] < 1); 498 567 } … … 653 722 factor = 1.0; 654 723 echo_supp_set_state(ec, ST_LOCAL_TALK, rec_level); 655 } else if (rec_level > =play_level) {724 } else if (rec_level > play_level) { 656 725 /* Seems that both are talking. Scale the mic signal 657 726 * down a little bit to reduce echo, while allowing both … … 689 758 factor = (factor + ec->last_factor) / 2; 690 759 else 691 factor = (factor + ec->last_factor* 9) / 10;760 factor = (factor + ec->last_factor*19) / 20; 692 761 693 762 /* Amplify frame */
Note: See TracChangeset
for help on using the changeset viewer.