| 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 |
#include <linux/kernel.h> |
| 35 |
#include <linux/slab.h> |
| 36 |
#include <linux/errno.h> |
| 37 |
#include <linux/module.h> |
| 38 |
#include <linux/init.h> |
| 39 |
#include <linux/ctype.h> |
| 40 |
#include <linux/moduleparam.h> |
| 41 |
|
| 42 |
#include <dahdi/kernel.h> |
| 43 |
|
| 44 |
static int debug; |
| 45 |
static int aggressive; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#include "arith.h" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#define DEFAULT_BETA1_I 2048 |
| 62 |
|
| 63 |
|
| 64 |
#define DEFAULT_SIGMA_LY_I 7 |
| 65 |
#define DEFAULT_SIGMA_LU_I 7 |
| 66 |
#define DEFAULT_ALPHA_ST_I 5 |
| 67 |
#define DEFAULT_ALPHA_YT_I 5 |
| 68 |
|
| 69 |
#define DEFAULT_CUTOFF_I 128 |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#define DEFAULT_HANGT 600 |
| 75 |
|
| 76 |
|
| 77 |
#define DEFAULT_SUPPR_I 16 |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define MIN_UPDATE_THRESH_I 4096 |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#define DEFAULT_M 16 |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
#define AGGRESSIVE_HCNTR 160 |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#define SUPPR_FLOOR -64 |
| 114 |
#define SUPPR_CEIL -24 |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#define RES_SUPR_FACTOR -20 |
| 121 |
|
| 122 |
#ifndef NULL |
| 123 |
#define NULL 0 |
| 124 |
#endif |
| 125 |
#ifndef FALSE |
| 126 |
#define FALSE 0 |
| 127 |
#endif |
| 128 |
#ifndef TRUE |
| 129 |
#define TRUE (!FALSE) |
| 130 |
#endif |
| 131 |
|
| 132 |
|
| 133 |
typedef struct { |
| 134 |
|
| 135 |
int idx_d; |
| 136 |
|
| 137 |
int size_d; |
| 138 |
|
| 139 |
short *buf_d; |
| 140 |
} echo_can_cb_s; |
| 141 |
|
| 142 |
static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp, |
| 143 |
struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec); |
| 144 |
static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec); |
| 145 |
static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size); |
| 146 |
static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val); |
| 147 |
static void echocan_NLP_toggle(struct dahdi_echocan_state *ec, unsigned int enable); |
| 148 |
static const char *name = "KB1"; |
| 149 |
static const char *ec_name(const struct dahdi_chan *chan) { return name; } |
| 150 |
|
| 151 |
static const struct dahdi_echocan_factory my_factory = { |
| 152 |
.get_name = ec_name, |
| 153 |
.owner = THIS_MODULE, |
| 154 |
.echocan_create = echo_can_create, |
| 155 |
}; |
| 156 |
|
| 157 |
static const struct dahdi_echocan_features my_features = { |
| 158 |
.NLP_toggle = 1, |
| 159 |
}; |
| 160 |
|
| 161 |
static const struct dahdi_echocan_ops my_ops = { |
| 162 |
.echocan_free = echo_can_free, |
| 163 |
.echocan_process = echo_can_process, |
| 164 |
.echocan_traintap = echo_can_traintap, |
| 165 |
.echocan_NLP_toggle = echocan_NLP_toggle, |
| 166 |
}; |
| 167 |
|
| 168 |
struct ec_pvt { |
| 169 |
struct dahdi_echocan_state dahdi; |
| 170 |
|
| 171 |
int id; |
| 172 |
|
| 173 |
|
| 174 |
int i_d; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
int N_d; |
| 180 |
|
| 181 |
int beta2_i; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
int Ly_i; |
| 187 |
|
| 188 |
int Lu_i; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
int s_tilde_i; |
| 194 |
|
| 195 |
int y_tilde_i; |
| 196 |
|
| 197 |
|
| 198 |
int HCNTR_d; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
int *a_i; |
| 204 |
|
| 205 |
short *a_s; |
| 206 |
|
| 207 |
echo_can_cb_s y_s; |
| 208 |
|
| 209 |
echo_can_cb_s s_s; |
| 210 |
|
| 211 |
echo_can_cb_s u_s; |
| 212 |
|
| 213 |
echo_can_cb_s y_tilde_s; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
short max_y_tilde; |
| 219 |
|
| 220 |
int max_y_tilde_pos; |
| 221 |
|
| 222 |
#ifdef MEC2_STATS |
| 223 |
|
| 224 |
int cntr_nearend_speech_frames; |
| 225 |
int cntr_residualcorrected_frames; |
| 226 |
int cntr_residualcorrected_framesskipped; |
| 227 |
int cntr_coeff_updates; |
| 228 |
int cntr_coeff_missedupdates; |
| 229 |
|
| 230 |
int avg_Lu_i_toolow; |
| 231 |
int avg_Lu_i_ok; |
| 232 |
#endif |
| 233 |
unsigned int aggressive:1; |
| 234 |
int use_nlp; |
| 235 |
}; |
| 236 |
|
| 237 |
#define dahdi_to_pvt(a) container_of(a, struct ec_pvt, dahdi) |
| 238 |
|
| 239 |
static inline void init_cb_s(echo_can_cb_s *cb, int len, void *where) |
| 240 |
{ |
| 241 |
cb->buf_d = (short *)where; |
| 242 |
cb->idx_d = 0; |
| 243 |
cb->size_d = len; |
| 244 |
} |
| 245 |
|
| 246 |
static inline void add_cc_s(echo_can_cb_s *cb, short newval) |
| 247 |
{ |
| 248 |
|
| 249 |
cb->idx_d--; |
| 250 |
if (cb->idx_d < (int)0) |
| 251 |
|
| 252 |
cb->idx_d += cb->size_d; |
| 253 |
|
| 254 |
|
| 255 |
cb->buf_d[cb->idx_d] = newval; |
| 256 |
cb->buf_d[cb->idx_d + cb->size_d] = newval; |
| 257 |
} |
| 258 |
|
| 259 |
static inline short get_cc_s(echo_can_cb_s *cb, int pos) |
| 260 |
{ |
| 261 |
|
| 262 |
return cb->buf_d[cb->idx_d + pos]; |
| 263 |
} |
| 264 |
|
| 265 |
static inline void init_cc(struct ec_pvt *pvt, int N, int maxy, int maxu) |
| 266 |
{ |
| 267 |
char *ptr = (char *) pvt; |
| 268 |
unsigned long tmp; |
| 269 |
|
| 270 |
|
| 271 |
ptr += sizeof(*pvt); |
| 272 |
tmp = (unsigned long)ptr; |
| 273 |
tmp += 3; |
| 274 |
tmp &= ~3L; |
| 275 |
ptr = (void *)tmp; |
| 276 |
|
| 277 |
|
| 278 |
pvt->N_d = N; |
| 279 |
pvt->beta2_i = DEFAULT_BETA1_I; |
| 280 |
|
| 281 |
|
| 282 |
pvt->a_i = (int *) ptr; |
| 283 |
ptr += (sizeof(int) * pvt->N_d); |
| 284 |
pvt->a_s = (short *) ptr; |
| 285 |
ptr += (sizeof(short) * pvt->N_d); |
| 286 |
|
| 287 |
|
| 288 |
init_cb_s(&pvt->y_s, maxy, ptr); |
| 289 |
ptr += (sizeof(short) * (maxy) * 2); |
| 290 |
|
| 291 |
|
| 292 |
init_cb_s(&pvt->s_s, (1 << DEFAULT_ALPHA_ST_I), ptr); |
| 293 |
ptr += (sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) * 2); |
| 294 |
|
| 295 |
init_cb_s(&pvt->u_s, maxu, ptr); |
| 296 |
ptr += (sizeof(short) * maxu * 2); |
| 297 |
|
| 298 |
|
| 299 |
init_cb_s(&pvt->y_tilde_s, pvt->N_d, ptr); |
| 300 |
|
| 301 |
|
| 302 |
pvt->i_d = (int)0; |
| 303 |
|
| 304 |
|
| 305 |
pvt->Ly_i = DEFAULT_CUTOFF_I; |
| 306 |
pvt->Lu_i = DEFAULT_CUTOFF_I; |
| 307 |
|
| 308 |
#ifdef MEC2_STATS |
| 309 |
|
| 310 |
pvt->id = (int)&ptr; |
| 311 |
|
| 312 |
|
| 313 |
pvt->cntr_nearend_speech_frames = (int)0; |
| 314 |
pvt->cntr_residualcorrected_frames = (int)0; |
| 315 |
pvt->cntr_residualcorrected_framesskipped = (int)0; |
| 316 |
pvt->cntr_coeff_updates = (int)0; |
| 317 |
pvt->cntr_coeff_missedupdates = (int)0; |
| 318 |
|
| 319 |
pvt->avg_Lu_i_toolow = (int)0; |
| 320 |
pvt->avg_Lu_i_ok = (int)0; |
| 321 |
#endif |
| 322 |
|
| 323 |
|
| 324 |
pvt->s_tilde_i = (int)0; |
| 325 |
pvt->y_tilde_i = (int)0; |
| 326 |
pvt->HCNTR_d = (int)0; |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec) |
| 331 |
{ |
| 332 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 333 |
|
| 334 |
kfree(pvt); |
| 335 |
} |
| 336 |
|
| 337 |
static inline short sample_update(struct ec_pvt *pvt, short iref, short isig) |
| 338 |
{ |
| 339 |
|
| 340 |
|
| 341 |
int k; |
| 342 |
|
| 343 |
int rs; |
| 344 |
|
| 345 |
short u; |
| 346 |
|
| 347 |
int Py_i; |
| 348 |
|
| 349 |
int two_beta_i; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
pvt->y_tilde_i -= abs(get_cc_s(&pvt->y_s, (1 << DEFAULT_ALPHA_YT_I) - 1)) >> DEFAULT_ALPHA_YT_I; |
| 367 |
|
| 368 |
pvt->y_tilde_i += abs(iref) >> DEFAULT_ALPHA_ST_I; |
| 369 |
|
| 370 |
add_cc_s(&pvt->y_s, iref); |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
rs = CONVOLVE2(pvt->a_s, |
| 375 |
pvt->y_s.buf_d + pvt->y_s.idx_d, |
| 376 |
pvt->N_d); |
| 377 |
rs >>= 15; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
u = isig - rs; |
| 384 |
|
| 385 |
|
| 386 |
add_cc_s(&pvt->u_s, u); |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
pvt->s_tilde_i -= abs(get_cc_s(&pvt->s_s, (1 << DEFAULT_ALPHA_ST_I) - 1)); |
| 393 |
|
| 394 |
pvt->s_tilde_i += abs(isig); |
| 395 |
|
| 396 |
add_cc_s(&pvt->s_s, isig); |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
add_cc_s(&pvt->y_tilde_s, pvt->y_tilde_i); |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
if (!pvt->HCNTR_d) { |
| 406 |
Py_i = (pvt->Ly_i >> DEFAULT_SIGMA_LY_I) * (pvt->Ly_i >> DEFAULT_SIGMA_LY_I); |
| 407 |
Py_i >>= 15; |
| 408 |
} else { |
| 409 |
Py_i = (1 << 15); |
| 410 |
} |
| 411 |
|
| 412 |
#if 0 |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if (pvt->start_speech_d != 0) { |
| 421 |
if (pvt->i_d > (DEFAULT_T0 + pvt->start_speech_d)*(SAMPLE_FREQ)) { |
| 422 |
pvt->beta2_d = max_cc_float(MIN_BETA, DEFAULT_BETA1 * exp((-1/DEFAULT_TAU)*((pvt->i_d/(float)SAMPLE_FREQ) - DEFAULT_T0 - pvt->start_speech_d))); |
| 423 |
} |
| 424 |
} else { |
| 425 |
pvt->beta2_d = DEFAULT_BETA1; |
| 426 |
} |
| 427 |
#endif |
| 428 |
|
| 429 |
|
| 430 |
pvt->beta2_i = DEFAULT_BETA1_I; |
| 431 |
|
| 432 |
|
| 433 |
two_beta_i = (pvt->beta2_i * Py_i) >> 15; |
| 434 |
if (!two_beta_i) |
| 435 |
two_beta_i++; |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
pvt->Lu_i -= abs(get_cc_s(&pvt->u_s, (1 << DEFAULT_SIGMA_LU_I) - 1)); |
| 441 |
|
| 442 |
pvt->Lu_i += abs(u); |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
pvt->Ly_i -= abs(get_cc_s(&pvt->y_s, (1 << DEFAULT_SIGMA_LY_I) - 1)) ; |
| 449 |
|
| 450 |
pvt->Ly_i += abs(iref); |
| 451 |
|
| 452 |
if (pvt->Ly_i < DEFAULT_CUTOFF_I) |
| 453 |
pvt->Ly_i = DEFAULT_CUTOFF_I; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
if (pvt->y_tilde_i > pvt->max_y_tilde) { |
| 459 |
|
| 460 |
pvt->max_y_tilde = pvt->y_tilde_i; |
| 461 |
pvt->max_y_tilde_pos = pvt->N_d - 1; |
| 462 |
} else if (--pvt->max_y_tilde_pos < 0) { |
| 463 |
|
| 464 |
pvt->max_y_tilde = MAX16(pvt->y_tilde_s.buf_d + pvt->y_tilde_s.idx_d, pvt->N_d, &pvt->max_y_tilde_pos); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
if (((pvt->s_tilde_i >> (DEFAULT_ALPHA_ST_I - 1)) > pvt->max_y_tilde) |
| 470 |
&& (pvt->max_y_tilde > 0)) { |
| 471 |
|
| 472 |
pvt->HCNTR_d = DEFAULT_HANGT; |
| 473 |
#ifdef MEC2_STATS_DETAILED |
| 474 |
printk(KERN_INFO "Reset near end speech timer with: s_tilde_i %d, stmnt %d, max_y_tilde %d\n", pvt->s_tilde_i, (pvt->s_tilde_i >> (DEFAULT_ALPHA_ST_I - 1)), pvt->max_y_tilde); |
| 475 |
#endif |
| 476 |
#ifdef MEC2_STATS |
| 477 |
++pvt->cntr_nearend_speech_frames; |
| 478 |
#endif |
| 479 |
} else if (pvt->HCNTR_d > (int)0) { |
| 480 |
|
| 481 |
#ifdef MEC2_STATS |
| 482 |
++pvt->cntr_nearend_speech_frames; |
| 483 |
#endif |
| 484 |
pvt->HCNTR_d--; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
if (!pvt->HCNTR_d && |
| 492 |
!(pvt->i_d % DEFAULT_M)) { |
| 493 |
if (pvt->Lu_i > MIN_UPDATE_THRESH_I) { |
| 494 |
|
| 495 |
#ifdef MEC2_STATS_DETAILED |
| 496 |
printk(KERN_INFO "updating coefficients with: pvt->Lu_i %9d\n", pvt->Lu_i); |
| 497 |
#endif |
| 498 |
#ifdef MEC2_STATS |
| 499 |
pvt->avg_Lu_i_ok = pvt->avg_Lu_i_ok + pvt->Lu_i; |
| 500 |
++pvt->cntr_coeff_updates; |
| 501 |
#endif |
| 502 |
for (k = 0; k < pvt->N_d; k++) { |
| 503 |
|
| 504 |
int grad2; |
| 505 |
grad2 = CONVOLVE2(pvt->u_s.buf_d + pvt->u_s.idx_d, |
| 506 |
pvt->y_s.buf_d + pvt->y_s.idx_d + k, |
| 507 |
DEFAULT_M); |
| 508 |
|
| 509 |
pvt->a_i[k] += grad2 / two_beta_i; |
| 510 |
pvt->a_s[k] = pvt->a_i[k] >> 16; |
| 511 |
} |
| 512 |
} else { |
| 513 |
#ifdef MEC2_STATS_DETAILED |
| 514 |
printk(KERN_INFO "insufficient signal to update coefficients pvt->Lu_i %5d < %5d\n", pvt->Lu_i, MIN_UPDATE_THRESH_I); |
| 515 |
#endif |
| 516 |
#ifdef MEC2_STATS |
| 517 |
pvt->avg_Lu_i_toolow = pvt->avg_Lu_i_toolow + pvt->Lu_i; |
| 518 |
++pvt->cntr_coeff_missedupdates; |
| 519 |
#endif |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
#ifdef MEC2_STATS_DETAILED |
| 528 |
if (pvt->HCNTR_d == 0) |
| 529 |
printk(KERN_INFO "possibly correcting frame with pvt->Ly_i %9d pvt->Lu_i %9d and expression %d\n", pvt->Ly_i, pvt->Lu_i, (pvt->Ly_i/(pvt->Lu_i + 1))); |
| 530 |
#endif |
| 531 |
|
| 532 |
#ifndef NO_ECHO_SUPPRESSOR |
| 533 |
if (pvt->use_nlp) { |
| 534 |
if (pvt->aggressive) { |
| 535 |
if ((pvt->HCNTR_d < AGGRESSIVE_HCNTR) && (pvt->Ly_i > (pvt->Lu_i << 1))) { |
| 536 |
for (k = 0; k < 2; k++) { |
| 537 |
u = u * (pvt->Lu_i >> DEFAULT_SIGMA_LU_I) / ((pvt->Ly_i >> (DEFAULT_SIGMA_LY_I)) + 1); |
| 538 |
} |
| 539 |
#ifdef MEC2_STATS_DETAILED |
| 540 |
printk(KERN_INFO "aggresively correcting frame with pvt->Ly_i %9d pvt->Lu_i %9d expression %d\n", pvt->Ly_i, pvt->Lu_i, (pvt->Ly_i/(pvt->Lu_i + 1))); |
| 541 |
#endif |
| 542 |
#ifdef MEC2_STATS |
| 543 |
++pvt->cntr_residualcorrected_frames; |
| 544 |
#endif |
| 545 |
} |
| 546 |
} else { |
| 547 |
if (pvt->HCNTR_d == 0) { |
| 548 |
if ((pvt->Ly_i/(pvt->Lu_i + 1)) > DEFAULT_SUPPR_I) { |
| 549 |
for (k = 0; k < 1; k++) { |
| 550 |
u = u * (pvt->Lu_i >> DEFAULT_SIGMA_LU_I) / ((pvt->Ly_i >> (DEFAULT_SIGMA_LY_I + 2)) + 1); |
| 551 |
} |
| 552 |
#ifdef MEC2_STATS_DETAILED |
| 553 |
printk(KERN_INFO "correcting frame with pvt->Ly_i %9d pvt->Lu_i %9d expression %d\n", pvt->Ly_i, pvt->Lu_i, (pvt->Ly_i/(pvt->Lu_i + 1))); |
| 554 |
#endif |
| 555 |
#ifdef MEC2_STATS |
| 556 |
++pvt->cntr_residualcorrected_frames; |
| 557 |
#endif |
| 558 |
} |
| 559 |
#ifdef MEC2_STATS |
| 560 |
else { |
| 561 |
++pvt->cntr_residualcorrected_framesskipped; |
| 562 |
} |
| 563 |
#endif |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
#endif |
| 568 |
|
| 569 |
#if 0 |
| 570 |
|
| 571 |
if ((pvt->HCNTR_d == 0) && |
| 572 |
((pvt->Lu_d/pvt->Ly_d) < DEFAULT_SUPPR) && |
| 573 |
(pvt->Lu_d/pvt->Ly_d > EC_MIN_DB_VALUE)) { |
| 574 |
suppr_factor = (10 / (float)(SUPPR_FLOOR - SUPPR_CEIL)) * log(pvt->Lu_d/pvt->Ly_d) |
| 575 |
- SUPPR_CEIL / (float)(SUPPR_FLOOR - SUPPR_CEIL); |
| 576 |
u_suppr = pow(10.0, (suppr_factor) * RES_SUPR_FACTOR / 10.0) * u_suppr; |
| 577 |
} |
| 578 |
#endif |
| 579 |
|
| 580 |
#ifdef MEC2_STATS |
| 581 |
|
| 582 |
if ((pvt->i_d % MEC2_STATS) == 0) { |
| 583 |
|
| 584 |
if (pvt->cntr_coeff_missedupdates > 0) |
| 585 |
pvt->avg_Lu_i_toolow = (int)(pvt->avg_Lu_i_toolow / pvt->cntr_coeff_missedupdates); |
| 586 |
else |
| 587 |
pvt->avg_Lu_i_toolow = -1; |
| 588 |
|
| 589 |
if (pvt->cntr_coeff_updates > 0) |
| 590 |
pvt->avg_Lu_i_ok = (pvt->avg_Lu_i_ok / pvt->cntr_coeff_updates); |
| 591 |
else |
| 592 |
pvt->avg_Lu_i_ok = -1; |
| 593 |
|
| 594 |
printk( KERN_INFO "%d: Near end speech: %5d Residuals corrected/skipped: %5d/%5d Coefficients updated ok/low sig: %3d/%3d Lu_i avg ok/low sig %6d/%5d\n", |
| 595 |
pvt->id, |
| 596 |
pvt->cntr_nearend_speech_frames, |
| 597 |
pvt->cntr_residualcorrected_frames, pvt->cntr_residualcorrected_framesskipped, |
| 598 |
pvt->cntr_coeff_updates, pvt->cntr_coeff_missedupdates, |
| 599 |
pvt->avg_Lu_i_ok, pvt->avg_Lu_i_toolow); |
| 600 |
|
| 601 |
pvt->cntr_nearend_speech_frames = 0; |
| 602 |
pvt->cntr_residualcorrected_frames = 0; |
| 603 |
pvt->cntr_residualcorrected_framesskipped = 0; |
| 604 |
pvt->cntr_coeff_updates = 0; |
| 605 |
pvt->cntr_coeff_missedupdates = 0; |
| 606 |
pvt->avg_Lu_i_ok = 0; |
| 607 |
pvt->avg_Lu_i_toolow = 0; |
| 608 |
} |
| 609 |
#endif |
| 610 |
|
| 611 |
|
| 612 |
pvt->i_d++; |
| 613 |
return u; |
| 614 |
} |
| 615 |
|
| 616 |
static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size) |
| 617 |
{ |
| 618 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 619 |
u32 x; |
| 620 |
short result; |
| 621 |
|
| 622 |
for (x = 0; x < size; x++) { |
| 623 |
result = sample_update(pvt, *iref, *isig); |
| 624 |
*isig++ = result; |
| 625 |
++iref; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp, |
| 630 |
struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec) |
| 631 |
{ |
| 632 |
int maxy; |
| 633 |
int maxu; |
| 634 |
size_t size; |
| 635 |
unsigned int x; |
| 636 |
char *c; |
| 637 |
struct ec_pvt *pvt; |
| 638 |
|
| 639 |
maxy = ecp->tap_length + DEFAULT_M; |
| 640 |
maxu = DEFAULT_M; |
| 641 |
if (maxy < (1 << DEFAULT_ALPHA_YT_I)) |
| 642 |
maxy = (1 << DEFAULT_ALPHA_YT_I); |
| 643 |
if (maxy < (1 << DEFAULT_SIGMA_LY_I)) |
| 644 |
maxy = (1 << DEFAULT_SIGMA_LY_I); |
| 645 |
if (maxu < (1 << DEFAULT_SIGMA_LU_I)) |
| 646 |
maxu = (1 << DEFAULT_SIGMA_LU_I); |
| 647 |
|
| 648 |
size = sizeof(*ec) + |
| 649 |
4 + |
| 650 |
sizeof(int) * ecp->tap_length + |
| 651 |
sizeof(short) * ecp->tap_length + |
| 652 |
2 * sizeof(short) * (maxy) + |
| 653 |
2 * sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) + |
| 654 |
2 * sizeof(short) * (maxu) + |
| 655 |
2 * sizeof(short) * ecp->tap_length; |
| 656 |
|
| 657 |
pvt = kzalloc(size, GFP_KERNEL); |
| 658 |
if (!pvt) |
| 659 |
return -ENOMEM; |
| 660 |
|
| 661 |
pvt->dahdi.ops = &my_ops; |
| 662 |
|
| 663 |
pvt->aggressive = aggressive; |
| 664 |
pvt->dahdi.features = my_features; |
| 665 |
|
| 666 |
for (x = 0; x < ecp->param_count; x++) { |
| 667 |
for (c = p[x].name; *c; c++) |
| 668 |
*c = tolower(*c); |
| 669 |
if (!strcmp(p[x].name, "aggressive")) { |
| 670 |
pvt->aggressive = p[x].value ? 1 : 0; |
| 671 |
} else { |
| 672 |
printk(KERN_WARNING "Unknown parameter supplied to KB1 echo canceler: '%s'\n", p[x].name); |
| 673 |
kfree(pvt); |
| 674 |
|
| 675 |
return -EINVAL; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
init_cc(pvt, ecp->tap_length, maxy, maxu); |
| 680 |
|
| 681 |
|
| 682 |
pvt->use_nlp = TRUE; |
| 683 |
|
| 684 |
*ec = &pvt->dahdi; |
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val) |
| 689 |
{ |
| 690 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
pvt->HCNTR_d = pvt->N_d << 1; |
| 696 |
|
| 697 |
if (pos >= pvt->N_d) |
| 698 |
return 1; |
| 699 |
|
| 700 |
pvt->a_i[pos] = val << 17; |
| 701 |
pvt->a_s[pos] = val << 1; |
| 702 |
|
| 703 |
if (++pos >= pvt->N_d) |
| 704 |
return 1; |
| 705 |
|
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
static void echocan_NLP_toggle(struct dahdi_echocan_state *ec, unsigned int enable) |
| 710 |
{ |
| 711 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 712 |
|
| 713 |
pvt->use_nlp = enable ? 1 : 0; |
| 714 |
} |
| 715 |
|
| 716 |
static int __init mod_init(void) |
| 717 |
{ |
| 718 |
if (dahdi_register_echocan_factory(&my_factory)) { |
| 719 |
module_printk(KERN_ERR, "could not register with DAHDI core\n"); |
| 720 |
|
| 721 |
return -EPERM; |
| 722 |
} |
| 723 |
|
| 724 |
module_printk(KERN_NOTICE, "Registered echo canceler '%s'\n", |
| 725 |
my_factory.get_name(NULL)); |
| 726 |
|
| 727 |
return 0; |
| 728 |
} |
| 729 |
|
| 730 |
static void __exit mod_exit(void) |
| 731 |
{ |
| 732 |
dahdi_unregister_echocan_factory(&my_factory); |
| 733 |
} |
| 734 |
|
| 735 |
module_param(debug, int, S_IRUGO | S_IWUSR); |
| 736 |
module_param(aggressive, int, S_IRUGO | S_IWUSR); |
| 737 |
|
| 738 |
MODULE_DESCRIPTION("DAHDI 'KB1' Echo Canceler"); |
| 739 |
MODULE_AUTHOR("Kris Boutilier"); |
| 740 |
MODULE_LICENSE("GPL v2"); |
| 741 |
|
| 742 |
module_init(mod_init); |
| 743 |
module_exit(mod_exit); |