dahdi_echocan_kb1.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * ECHO_CAN_KB1
  3. *
  4. * by Kris Boutilier
  5. *
  6. * Based upon mec2.h
  7. *
  8. * Copyright (C) 2002-2012, Digium, Inc.
  9. *
  10. * Additional background on the techniques used in this code can be found in:
  11. *
  12. * Messerschmitt, David; Hedberg, David; Cole, Christopher; Haoui, Amine;
  13. * Winship, Peter; "Digital Voice Echo Canceller with a TMS32020,"
  14. * in Digital Signal Processing Applications with the TMS320 Family,
  15. * pp. 415-437, Texas Instruments, Inc., 1986.
  16. *
  17. * A pdf of which is available by searching on the document title at http://www.ti.com/
  18. *
  19. */
  20. /*
  21. * See http://www.asterisk.org for more information about
  22. * the Asterisk project. Please do not directly contact
  23. * any of the maintainers of this project for assistance;
  24. * the project provides a web site, mailing lists and IRC
  25. * channels for your use.
  26. *
  27. * This program is free software, distributed under the terms of
  28. * the GNU General Public License Version 2 as published by the
  29. * Free Software Foundation. See the LICENSE file included with
  30. * this program for more details.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include <linux/errno.h>
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/ctype.h>
  38. #include <linux/moduleparam.h>
  39. #include <dahdi/kernel.h>
  40. static int debug;
  41. static int aggressive;
  42. /* Uncomment to provide summary statistics for overall echo can performance every 4000 samples */
  43. /* #define MEC2_STATS 4000 */
  44. /* Uncomment to generate per-sample statistics - this will severely degrade system performance and audio quality */
  45. /* #define MEC2_STATS_DETAILED */
  46. /* Get optimized routines for math */
  47. #include "arith.h"
  48. /*
  49. Important constants for tuning kb1 echo can
  50. */
  51. /* Convergence (aka. adaptation) speed -- higher means slower */
  52. #define DEFAULT_BETA1_I 2048
  53. /* Constants for various power computations */
  54. #define DEFAULT_SIGMA_LY_I 7
  55. #define DEFAULT_SIGMA_LU_I 7
  56. #define DEFAULT_ALPHA_ST_I 5 /* near-end speech detection sensitivity factor */
  57. #define DEFAULT_ALPHA_YT_I 5
  58. #define DEFAULT_CUTOFF_I 128
  59. /* Define the near-end speech hangover counter: if near-end speech
  60. * is declared, hcntr is set equal to hangt (see pg. 432)
  61. */
  62. #define DEFAULT_HANGT 600 /* in samples, so 600 samples = 75ms */
  63. /* define the residual error suppression threshold */
  64. #define DEFAULT_SUPPR_I 16 /* 16 = -24db */
  65. /* This is the minimum reference signal power estimate level
  66. * that will result in filter adaptation.
  67. * If this is too low then background noise will cause the filter
  68. * coefficients to constantly be updated.
  69. */
  70. #define MIN_UPDATE_THRESH_I 4096
  71. /* The number of samples used to update coefficients using the
  72. * the block update method (M). It should be related back to the
  73. * length of the echo can.
  74. * ie. it only updates coefficients when (sample number MOD default_m) = 0
  75. *
  76. * Getting this wrong may cause an oops. Consider yourself warned!
  77. */
  78. #define DEFAULT_M 16 /* every 16th sample */
  79. /* If AGGRESSIVE supression is enabled, then we start cancelling residual
  80. * echos again even while there is potentially the very end of a near-side
  81. * signal present.
  82. * This defines how many samples of DEFAULT_HANGT can remain before we
  83. * kick back in
  84. */
  85. #define AGGRESSIVE_HCNTR 160 /* in samples, so 160 samples = 20ms */
  86. /***************************************************************/
  87. /* The following knobs are not implemented in the current code */
  88. /* we need a dynamic level of suppression varying with the ratio of the
  89. power of the echo to the power of the reference signal this is
  90. done so that we have a smoother background.
  91. we have a higher suppression when the power ratio is closer to
  92. suppr_ceil and reduces logarithmically as we approach suppr_floor.
  93. */
  94. #define SUPPR_FLOOR -64
  95. #define SUPPR_CEIL -24
  96. /* in a second departure, we calculate the residual error suppression
  97. * as a percentage of the reference signal energy level. The threshold
  98. * is defined in terms of dB below the reference signal.
  99. */
  100. #define RES_SUPR_FACTOR -20
  101. #ifndef NULL
  102. #define NULL 0
  103. #endif
  104. #ifndef FALSE
  105. #define FALSE 0
  106. #endif
  107. #ifndef TRUE
  108. #define TRUE (!FALSE)
  109. #endif
  110. /* Generic circular buffer definition */
  111. typedef struct {
  112. /* Pointer to the relative 'start' of the buffer */
  113. int idx_d;
  114. /* The absolute size of the buffer */
  115. int size_d;
  116. /* The actual sample - twice as large as we need, however we do store values at idx_d and idx_d+size_d */
  117. short *buf_d;
  118. } echo_can_cb_s;
  119. static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
  120. struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec);
  121. static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
  122. static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size);
  123. static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val);
  124. static void echocan_NLP_toggle(struct dahdi_echocan_state *ec, unsigned int enable);
  125. static const char *name = "KB1";
  126. static const char *ec_name(const struct dahdi_chan *chan) { return name; }
  127. static const struct dahdi_echocan_factory my_factory = {
  128. .get_name = ec_name,
  129. .owner = THIS_MODULE,
  130. .echocan_create = echo_can_create,
  131. };
  132. static const struct dahdi_echocan_features my_features = {
  133. .NLP_toggle = 1,
  134. };
  135. static const struct dahdi_echocan_ops my_ops = {
  136. .echocan_free = echo_can_free,
  137. .echocan_process = echo_can_process,
  138. .echocan_traintap = echo_can_traintap,
  139. .echocan_NLP_toggle = echocan_NLP_toggle,
  140. };
  141. struct ec_pvt {
  142. struct dahdi_echocan_state dahdi;
  143. /* an arbitrary ID for this echo can - this really should be settable from the calling channel... */
  144. int id;
  145. /* absolute time - aka. sample number index - essentially the number of samples since this can was init'ed */
  146. int i_d;
  147. /* Pre-computed constants */
  148. /* ---------------------- */
  149. /* Number of filter coefficents */
  150. int N_d;
  151. /* Rate of adaptation of filter */
  152. int beta2_i;
  153. /* Accumulators for power computations */
  154. /* ----------------------------------- */
  155. /* reference signal power estimate - aka. Average absolute value of y(k) */
  156. int Ly_i;
  157. /* ... */
  158. int Lu_i;
  159. /* Accumulators for signal detectors */
  160. /* --------------------------------- */
  161. /* Power estimate of the recent past of the near-end hybrid signal - aka. Short-time average of: 2 x |s(i)| */
  162. int s_tilde_i;
  163. /* Power estimate of the recent past of the far-end receive signal - aka. Short-time average of: |y(i)| */
  164. int y_tilde_i;
  165. /* Near end speech detection counter - stores Hangover counter time remaining, in samples */
  166. int HCNTR_d;
  167. /* Circular buffers and coefficients */
  168. /* --------------------------------- */
  169. /* ... */
  170. int *a_i;
  171. /* ... */
  172. short *a_s;
  173. /* Reference samples of far-end receive signal */
  174. echo_can_cb_s y_s;
  175. /* Reference samples of near-end signal */
  176. echo_can_cb_s s_s;
  177. /* Reference samples of near-end signal minus echo estimate */
  178. echo_can_cb_s u_s;
  179. /* Reference samples of far-end receive signal used to calculate short-time average */
  180. echo_can_cb_s y_tilde_s;
  181. /* Peak far-end receive signal */
  182. /* --------------------------- */
  183. /* Highest y_tilde value in the sample buffer */
  184. short max_y_tilde;
  185. /* Index of the sample containing the max_y_tilde value */
  186. int max_y_tilde_pos;
  187. #ifdef MEC2_STATS
  188. /* Storage for performance statistics */
  189. int cntr_nearend_speech_frames;
  190. int cntr_residualcorrected_frames;
  191. int cntr_residualcorrected_framesskipped;
  192. int cntr_coeff_updates;
  193. int cntr_coeff_missedupdates;
  194. int avg_Lu_i_toolow;
  195. int avg_Lu_i_ok;
  196. #endif
  197. unsigned int aggressive:1;
  198. int use_nlp;
  199. };
  200. #define dahdi_to_pvt(a) container_of(a, struct ec_pvt, dahdi)
  201. static inline void init_cb_s(echo_can_cb_s *cb, int len, void *where)
  202. {
  203. cb->buf_d = (short *)where;
  204. cb->idx_d = 0;
  205. cb->size_d = len;
  206. }
  207. static inline void add_cc_s(echo_can_cb_s *cb, short newval)
  208. {
  209. /* Can't use modulus because N+M isn't a power of two (generally) */
  210. cb->idx_d--;
  211. if (cb->idx_d < (int)0)
  212. /* Whoops - the pointer to the 'start' wrapped around so reset it to the top of the buffer */
  213. cb->idx_d += cb->size_d;
  214. /* Load two copies into memory */
  215. cb->buf_d[cb->idx_d] = newval;
  216. cb->buf_d[cb->idx_d + cb->size_d] = newval;
  217. }
  218. static inline short get_cc_s(echo_can_cb_s *cb, int pos)
  219. {
  220. /* Load two copies into memory */
  221. return cb->buf_d[cb->idx_d + pos];
  222. }
  223. static inline void init_cc(struct ec_pvt *pvt, int N, int maxy, int maxu)
  224. {
  225. char *ptr = (char *) pvt;
  226. unsigned long tmp;
  227. /* Double-word align past end of state */
  228. ptr += sizeof(*pvt);
  229. tmp = (unsigned long)ptr;
  230. tmp += 3;
  231. tmp &= ~3L;
  232. ptr = (void *)tmp;
  233. /* Reset parameters */
  234. pvt->N_d = N;
  235. pvt->beta2_i = DEFAULT_BETA1_I;
  236. /* Allocate coefficient memory */
  237. pvt->a_i = (int *) ptr;
  238. ptr += (sizeof(int) * pvt->N_d);
  239. pvt->a_s = (short *) ptr;
  240. ptr += (sizeof(short) * pvt->N_d);
  241. /* Reset Y circular buffer (short version) */
  242. init_cb_s(&pvt->y_s, maxy, ptr);
  243. ptr += (sizeof(short) * (maxy) * 2);
  244. /* Reset Sigma circular buffer (short version for FIR filter) */
  245. init_cb_s(&pvt->s_s, (1 << DEFAULT_ALPHA_ST_I), ptr);
  246. ptr += (sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) * 2);
  247. init_cb_s(&pvt->u_s, maxu, ptr);
  248. ptr += (sizeof(short) * maxu * 2);
  249. /* Allocate a buffer for the reference signal power computation */
  250. init_cb_s(&pvt->y_tilde_s, pvt->N_d, ptr);
  251. /* Reset the absolute time index */
  252. pvt->i_d = (int)0;
  253. /* Reset the power computations (for y and u) */
  254. pvt->Ly_i = DEFAULT_CUTOFF_I;
  255. pvt->Lu_i = DEFAULT_CUTOFF_I;
  256. #ifdef MEC2_STATS
  257. /* set the identity */
  258. pvt->id = (int)&ptr;
  259. /* Reset performance stats */
  260. pvt->cntr_nearend_speech_frames = (int)0;
  261. pvt->cntr_residualcorrected_frames = (int)0;
  262. pvt->cntr_residualcorrected_framesskipped = (int)0;
  263. pvt->cntr_coeff_updates = (int)0;
  264. pvt->cntr_coeff_missedupdates = (int)0;
  265. pvt->avg_Lu_i_toolow = (int)0;
  266. pvt->avg_Lu_i_ok = (int)0;
  267. #endif
  268. /* Reset the near-end speech detector */
  269. pvt->s_tilde_i = (int)0;
  270. pvt->y_tilde_i = (int)0;
  271. pvt->HCNTR_d = (int)0;
  272. }
  273. static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
  274. {
  275. struct ec_pvt *pvt = dahdi_to_pvt(ec);
  276. kfree(pvt);
  277. }
  278. static inline short sample_update(struct ec_pvt *pvt, short iref, short isig)
  279. {
  280. /* Declare local variables that are used more than once */
  281. /* ... */
  282. int k;
  283. /* ... */
  284. int rs;
  285. /* ... */
  286. short u;
  287. /* ... */
  288. int Py_i;
  289. /* ... */
  290. int two_beta_i;
  291. /* flow A on pg. 428 */
  292. /* eq. (16): high-pass filter the input to generate the next value;
  293. * push the current value into the circular buffer
  294. *
  295. * sdc_im1_d = sdc_d;
  296. * sdc_d = sig;
  297. * s_i_d = sdc_d;
  298. * s_d = s_i_d;
  299. * s_i_d = (float)(1.0 - gamma_d) * s_i_d
  300. * + (float)(0.5 * (1.0 - gamma_d)) * (sdc_d - sdc_im1_d);
  301. */
  302. /* Update the Far-end receive signal circular buffers and accumulators */
  303. /* ------------------------------------------------------------------- */
  304. /* Delete the oldest sample from the power estimate accumulator */
  305. pvt->y_tilde_i -= abs(get_cc_s(&pvt->y_s, (1 << DEFAULT_ALPHA_YT_I) - 1)) >> DEFAULT_ALPHA_YT_I;
  306. /* Add the new sample to the power estimate accumulator */
  307. pvt->y_tilde_i += abs(iref) >> DEFAULT_ALPHA_ST_I;
  308. /* Push a copy of the new sample into its circular buffer */
  309. add_cc_s(&pvt->y_s, iref);
  310. /* eq. (2): compute r in fixed-point */
  311. rs = CONVOLVE2(pvt->a_s,
  312. pvt->y_s.buf_d + pvt->y_s.idx_d,
  313. pvt->N_d);
  314. rs >>= 15;
  315. /* eq. (3): compute the output value (see figure 3) and the error
  316. * note: the error is the same as the output signal when near-end
  317. * speech is not present
  318. */
  319. u = isig - rs;
  320. /* Push a copy of the output value sample into its circular buffer */
  321. add_cc_s(&pvt->u_s, u);
  322. /* Update the Near-end hybrid signal circular buffers and accumulators */
  323. /* ------------------------------------------------------------------- */
  324. /* Delete the oldest sample from the power estimate accumulator */
  325. pvt->s_tilde_i -= abs(get_cc_s(&pvt->s_s, (1 << DEFAULT_ALPHA_ST_I) - 1));
  326. /* Add the new sample to the power estimate accumulator */
  327. pvt->s_tilde_i += abs(isig);
  328. /* Push a copy of the new sample into it's circular buffer */
  329. add_cc_s(&pvt->s_s, isig);
  330. /* Push a copy of the current short-time average of the far-end receive signal into it's circular buffer */
  331. add_cc_s(&pvt->y_tilde_s, pvt->y_tilde_i);
  332. /* flow B on pg. 428 */
  333. /* If the hangover timer isn't running then compute the new convergence factor, otherwise set Py_i to 32768 */
  334. if (!pvt->HCNTR_d) {
  335. Py_i = (pvt->Ly_i >> DEFAULT_SIGMA_LY_I) * (pvt->Ly_i >> DEFAULT_SIGMA_LY_I);
  336. Py_i >>= 15;
  337. } else {
  338. Py_i = (1 << 15);
  339. }
  340. #if 0
  341. /* Vary rate of adaptation depending on position in the file
  342. * Do not do this for the first (DEFAULT_UPDATE_TIME) secs after speech
  343. * has begun of the file to allow the echo cancellor to estimate the
  344. * channel accurately
  345. * Still needs conversion!
  346. */
  347. if (pvt->start_speech_d != 0) {
  348. if (pvt->i_d > (DEFAULT_T0 + pvt->start_speech_d)*(SAMPLE_FREQ)) {
  349. 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)));
  350. }
  351. } else {
  352. pvt->beta2_d = DEFAULT_BETA1;
  353. }
  354. #endif
  355. /* Fixed point, inverted */
  356. pvt->beta2_i = DEFAULT_BETA1_I;
  357. /* Fixed point version, inverted */
  358. two_beta_i = (pvt->beta2_i * Py_i) >> 15;
  359. if (!two_beta_i)
  360. two_beta_i++;
  361. /* Update the Suppressed signal power estimate accumulator */
  362. /* ------------------------------------------------------- */
  363. /* Delete the oldest sample from the power estimate accumulator */
  364. pvt->Lu_i -= abs(get_cc_s(&pvt->u_s, (1 << DEFAULT_SIGMA_LU_I) - 1));
  365. /* Add the new sample to the power estimate accumulator */
  366. pvt->Lu_i += abs(u);
  367. /* Update the Far-end reference signal power estimate accumulator */
  368. /* -------------------------------------------------------------- */
  369. /* eq. (10): update power estimate of the reference */
  370. /* Delete the oldest sample from the power estimate accumulator */
  371. pvt->Ly_i -= abs(get_cc_s(&pvt->y_s, (1 << DEFAULT_SIGMA_LY_I) - 1)) ;
  372. /* Add the new sample to the power estimate accumulator */
  373. pvt->Ly_i += abs(iref);
  374. if (pvt->Ly_i < DEFAULT_CUTOFF_I)
  375. pvt->Ly_i = DEFAULT_CUTOFF_I;
  376. /* Update the Peak far-end receive signal detected */
  377. /* ----------------------------------------------- */
  378. if (pvt->y_tilde_i > pvt->max_y_tilde) {
  379. /* New highest y_tilde with full life */
  380. pvt->max_y_tilde = pvt->y_tilde_i;
  381. pvt->max_y_tilde_pos = pvt->N_d - 1;
  382. } else if (--pvt->max_y_tilde_pos < 0) {
  383. /* Time to find new max y tilde... */
  384. 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);
  385. }
  386. /* Determine if near end speech was detected in this sample */
  387. /* -------------------------------------------------------- */
  388. if (((pvt->s_tilde_i >> (DEFAULT_ALPHA_ST_I - 1)) > pvt->max_y_tilde)
  389. && (pvt->max_y_tilde > 0)) {
  390. /* Then start the Hangover counter */
  391. pvt->HCNTR_d = DEFAULT_HANGT;
  392. #ifdef MEC2_STATS_DETAILED
  393. 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);
  394. #endif
  395. #ifdef MEC2_STATS
  396. ++pvt->cntr_nearend_speech_frames;
  397. #endif
  398. } else if (pvt->HCNTR_d > (int)0) {
  399. /* otherwise, if it's still non-zero, decrement the Hangover counter by one sample */
  400. #ifdef MEC2_STATS
  401. ++pvt->cntr_nearend_speech_frames;
  402. #endif
  403. pvt->HCNTR_d--;
  404. }
  405. /* Update coefficients if no near-end speech in this sample (ie. HCNTR_d = 0)
  406. * and we have enough signal to bother trying to update.
  407. * --------------------------------------------------------------------------
  408. */
  409. if (!pvt->HCNTR_d && /* no near-end speech present */
  410. !(pvt->i_d % DEFAULT_M)) { /* we only update on every DEFAULM_M'th sample from the stream */
  411. if (pvt->Lu_i > MIN_UPDATE_THRESH_I) { /* there is sufficient energy above the noise floor to contain meaningful data */
  412. /* so loop over all the filter coefficients */
  413. #ifdef MEC2_STATS_DETAILED
  414. printk(KERN_INFO "updating coefficients with: pvt->Lu_i %9d\n", pvt->Lu_i);
  415. #endif
  416. #ifdef MEC2_STATS
  417. pvt->avg_Lu_i_ok = pvt->avg_Lu_i_ok + pvt->Lu_i;
  418. ++pvt->cntr_coeff_updates;
  419. #endif
  420. for (k = 0; k < pvt->N_d; k++) {
  421. /* eq. (7): compute an expectation over M_d samples */
  422. int grad2;
  423. grad2 = CONVOLVE2(pvt->u_s.buf_d + pvt->u_s.idx_d,
  424. pvt->y_s.buf_d + pvt->y_s.idx_d + k,
  425. DEFAULT_M);
  426. /* eq. (7): update the coefficient */
  427. pvt->a_i[k] += grad2 / two_beta_i;
  428. pvt->a_s[k] = pvt->a_i[k] >> 16;
  429. }
  430. } else {
  431. #ifdef MEC2_STATS_DETAILED
  432. printk(KERN_INFO "insufficient signal to update coefficients pvt->Lu_i %5d < %5d\n", pvt->Lu_i, MIN_UPDATE_THRESH_I);
  433. #endif
  434. #ifdef MEC2_STATS
  435. pvt->avg_Lu_i_toolow = pvt->avg_Lu_i_toolow + pvt->Lu_i;
  436. ++pvt->cntr_coeff_missedupdates;
  437. #endif
  438. }
  439. }
  440. /* paragraph below eq. (15): if no near-end speech in the sample and
  441. * the reference signal power estimate > cutoff threshold
  442. * then perform residual error suppression
  443. */
  444. #ifdef MEC2_STATS_DETAILED
  445. if (pvt->HCNTR_d == 0)
  446. 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)));
  447. #endif
  448. #ifndef NO_ECHO_SUPPRESSOR
  449. if (pvt->use_nlp) {
  450. if (pvt->aggressive) {
  451. if ((pvt->HCNTR_d < AGGRESSIVE_HCNTR) && (pvt->Ly_i > (pvt->Lu_i << 1))) {
  452. for (k = 0; k < 2; k++) {
  453. u = u * (pvt->Lu_i >> DEFAULT_SIGMA_LU_I) / ((pvt->Ly_i >> (DEFAULT_SIGMA_LY_I)) + 1);
  454. }
  455. #ifdef MEC2_STATS_DETAILED
  456. 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)));
  457. #endif
  458. #ifdef MEC2_STATS
  459. ++pvt->cntr_residualcorrected_frames;
  460. #endif
  461. }
  462. } else {
  463. if (pvt->HCNTR_d == 0) {
  464. if ((pvt->Ly_i/(pvt->Lu_i + 1)) > DEFAULT_SUPPR_I) {
  465. for (k = 0; k < 1; k++) {
  466. u = u * (pvt->Lu_i >> DEFAULT_SIGMA_LU_I) / ((pvt->Ly_i >> (DEFAULT_SIGMA_LY_I + 2)) + 1);
  467. }
  468. #ifdef MEC2_STATS_DETAILED
  469. 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)));
  470. #endif
  471. #ifdef MEC2_STATS
  472. ++pvt->cntr_residualcorrected_frames;
  473. #endif
  474. }
  475. #ifdef MEC2_STATS
  476. else {
  477. ++pvt->cntr_residualcorrected_framesskipped;
  478. }
  479. #endif
  480. }
  481. }
  482. }
  483. #endif
  484. #if 0
  485. /* This will generate a non-linear supression factor, once converted */
  486. if ((pvt->HCNTR_d == 0) &&
  487. ((pvt->Lu_d/pvt->Ly_d) < DEFAULT_SUPPR) &&
  488. (pvt->Lu_d/pvt->Ly_d > EC_MIN_DB_VALUE)) {
  489. suppr_factor = (10 / (float)(SUPPR_FLOOR - SUPPR_CEIL)) * log(pvt->Lu_d/pvt->Ly_d)
  490. - SUPPR_CEIL / (float)(SUPPR_FLOOR - SUPPR_CEIL);
  491. u_suppr = pow(10.0, (suppr_factor) * RES_SUPR_FACTOR / 10.0) * u_suppr;
  492. }
  493. #endif
  494. #ifdef MEC2_STATS
  495. /* Periodically dump performance stats */
  496. if ((pvt->i_d % MEC2_STATS) == 0) {
  497. /* make sure to avoid div0's! */
  498. if (pvt->cntr_coeff_missedupdates > 0)
  499. pvt->avg_Lu_i_toolow = (int)(pvt->avg_Lu_i_toolow / pvt->cntr_coeff_missedupdates);
  500. else
  501. pvt->avg_Lu_i_toolow = -1;
  502. if (pvt->cntr_coeff_updates > 0)
  503. pvt->avg_Lu_i_ok = (pvt->avg_Lu_i_ok / pvt->cntr_coeff_updates);
  504. else
  505. pvt->avg_Lu_i_ok = -1;
  506. 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",
  507. pvt->id,
  508. pvt->cntr_nearend_speech_frames,
  509. pvt->cntr_residualcorrected_frames, pvt->cntr_residualcorrected_framesskipped,
  510. pvt->cntr_coeff_updates, pvt->cntr_coeff_missedupdates,
  511. pvt->avg_Lu_i_ok, pvt->avg_Lu_i_toolow);
  512. pvt->cntr_nearend_speech_frames = 0;
  513. pvt->cntr_residualcorrected_frames = 0;
  514. pvt->cntr_residualcorrected_framesskipped = 0;
  515. pvt->cntr_coeff_updates = 0;
  516. pvt->cntr_coeff_missedupdates = 0;
  517. pvt->avg_Lu_i_ok = 0;
  518. pvt->avg_Lu_i_toolow = 0;
  519. }
  520. #endif
  521. /* Increment the sample index and return the corrected sample */
  522. pvt->i_d++;
  523. return u;
  524. }
  525. static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size)
  526. {
  527. struct ec_pvt *pvt = dahdi_to_pvt(ec);
  528. u32 x;
  529. short result;
  530. for (x = 0; x < size; x++) {
  531. result = sample_update(pvt, *iref, *isig);
  532. *isig++ = result;
  533. ++iref;
  534. }
  535. }
  536. static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
  537. struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec)
  538. {
  539. int maxy;
  540. int maxu;
  541. size_t size;
  542. unsigned int x;
  543. char *c;
  544. struct ec_pvt *pvt;
  545. maxy = ecp->tap_length + DEFAULT_M;
  546. maxu = DEFAULT_M;
  547. if (maxy < (1 << DEFAULT_ALPHA_YT_I))
  548. maxy = (1 << DEFAULT_ALPHA_YT_I);
  549. if (maxy < (1 << DEFAULT_SIGMA_LY_I))
  550. maxy = (1 << DEFAULT_SIGMA_LY_I);
  551. if (maxu < (1 << DEFAULT_SIGMA_LU_I))
  552. maxu = (1 << DEFAULT_SIGMA_LU_I);
  553. size = sizeof(*ec) +
  554. 4 + /* align */
  555. sizeof(int) * ecp->tap_length + /* a_i */
  556. sizeof(short) * ecp->tap_length + /* a_s */
  557. 2 * sizeof(short) * (maxy) + /* y_s */
  558. 2 * sizeof(short) * (1 << DEFAULT_ALPHA_ST_I) + /* s_s */
  559. 2 * sizeof(short) * (maxu) + /* u_s */
  560. 2 * sizeof(short) * ecp->tap_length; /* y_tilde_s */
  561. pvt = kzalloc(size, GFP_KERNEL);
  562. if (!pvt)
  563. return -ENOMEM;
  564. pvt->dahdi.ops = &my_ops;
  565. pvt->aggressive = aggressive;
  566. pvt->dahdi.features = my_features;
  567. for (x = 0; x < ecp->param_count; x++) {
  568. for (c = p[x].name; *c; c++)
  569. *c = tolower(*c);
  570. if (!strcmp(p[x].name, "aggressive")) {
  571. pvt->aggressive = p[x].value ? 1 : 0;
  572. } else {
  573. printk(KERN_WARNING "Unknown parameter supplied to KB1 echo canceler: '%s'\n", p[x].name);
  574. kfree(pvt);
  575. return -EINVAL;
  576. }
  577. }
  578. init_cc(pvt, ecp->tap_length, maxy, maxu);
  579. /* Non-linear processor - a fancy way to say "zap small signals, to avoid
  580. accumulating noise". */
  581. pvt->use_nlp = TRUE;
  582. *ec = &pvt->dahdi;
  583. return 0;
  584. }
  585. static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val)
  586. {
  587. struct ec_pvt *pvt = dahdi_to_pvt(ec);
  588. /* Set the hangover counter to the length of the can to
  589. * avoid adjustments occuring immediately after initial forced training
  590. */
  591. pvt->HCNTR_d = pvt->N_d << 1;
  592. if (pos >= pvt->N_d)
  593. return 1;
  594. pvt->a_i[pos] = val << 17;
  595. pvt->a_s[pos] = val << 1;
  596. if (++pos >= pvt->N_d)
  597. return 1;
  598. return 0;
  599. }
  600. static void echocan_NLP_toggle(struct dahdi_echocan_state *ec, unsigned int enable)
  601. {
  602. struct ec_pvt *pvt = dahdi_to_pvt(ec);
  603. pvt->use_nlp = enable ? 1 : 0;
  604. }
  605. static int __init mod_init(void)
  606. {
  607. if (dahdi_register_echocan_factory(&my_factory)) {
  608. module_printk(KERN_ERR, "could not register with DAHDI core\n");
  609. return -EPERM;
  610. }
  611. module_printk(KERN_NOTICE, "Registered echo canceler '%s'\n",
  612. my_factory.get_name(NULL));
  613. return 0;
  614. }
  615. static void __exit mod_exit(void)
  616. {
  617. dahdi_unregister_echocan_factory(&my_factory);
  618. }
  619. module_param(debug, int, S_IRUGO | S_IWUSR);
  620. module_param(aggressive, int, S_IRUGO | S_IWUSR);
  621. MODULE_DESCRIPTION("DAHDI 'KB1' Echo Canceler");
  622. MODULE_AUTHOR("Kris Boutilier");
  623. MODULE_LICENSE("GPL v2");
  624. module_init(mod_init);
  625. module_exit(mod_exit);