dsp.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Convenience Signal Processing routines
  5. *
  6. * Copyright (C) 2002, Digium
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License.
  12. *
  13. * Goertzel routines are borrowed from Steve Underwood's tremendous work on the
  14. * DTMF detector.
  15. *
  16. */
  17. /* Some routines from tone_detect.c by Steven Underwood as published under the zapata library */
  18. /*
  19. tone_detect.c - General telephony tone detection, and specific
  20. detection of DTMF.
  21. Copyright (C) 2001 Steve Underwood <steveu@coppice.org>
  22. Despite my general liking of the GPL, I place this code in the
  23. public domain for the benefit of all mankind - even the slimy
  24. ones who might try to proprietize my work and use it to my
  25. detriment.
  26. */
  27. #include <sys/types.h>
  28. #include <asterisk/frame.h>
  29. #include <asterisk/channel.h>
  30. #include <asterisk/channel_pvt.h>
  31. #include <asterisk/logger.h>
  32. #include <asterisk/dsp.h>
  33. #include <asterisk/ulaw.h>
  34. #include <asterisk/alaw.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. #include <math.h>
  39. #include <errno.h>
  40. #include <stdio.h>
  41. #define DEFAULT_THRESHOLD 1024
  42. #define BUSY_PERCENT 10 /* The percentage diffrence between the two last silence periods */
  43. #define BUSY_THRESHOLD 100 /* Max number of ms difference between max and min times in busy */
  44. #define BUSY_MIN 75 /* Busy must be at least 80 ms in half-cadence */
  45. #define BUSY_MAX 1100 /* Busy can't be longer than 1100 ms in half-cadence */
  46. /* Remember last 15 units */
  47. #define DSP_HISTORY 15
  48. /* Number of goertzels for progress detect */
  49. #define GSAMP_SIZE 183
  50. /* Define if you want the fax detector -- NOT RECOMMENDED IN -STABLE */
  51. /* #define FAX_DETECT */
  52. #define HZ_350 0
  53. #define HZ_440 1
  54. #define HZ_480 2
  55. #define HZ_620 3
  56. #define HZ_950 4
  57. #define HZ_1400 5
  58. #define HZ_1800 6
  59. #define TONE_THRESH 10.0 /* How much louder the tone should be than channel energy */
  60. #define TONE_MIN_THRESH 1e8 /* How much tone there should be at least to attempt */
  61. #define COUNT_THRESH 3 /* Need at least 50ms of stuff to count it */
  62. #define TONE_STATE_SILENCE 0
  63. #define TONE_STATE_RINGING 1
  64. #define TONE_STATE_DIALTONE 2
  65. #define TONE_STATE_TALKING 3
  66. #define TONE_STATE_BUSY 4
  67. #define TONE_STATE_SPECIAL1 5
  68. #define TONE_STATE_SPECIAL2 6
  69. #define TONE_STATE_SPECIAL3 7
  70. #define MAX_DTMF_DIGITS 128
  71. /* Basic DTMF specs:
  72. *
  73. * Minimum tone on = 40ms
  74. * Minimum tone off = 50ms
  75. * Maximum digit rate = 10 per second
  76. * Normal twist <= 8dB accepted
  77. * Reverse twist <= 4dB accepted
  78. * S/N >= 15dB will detect OK
  79. * Attenuation <= 26dB will detect OK
  80. * Frequency tolerance +- 1.5% will detect, +-3.5% will reject
  81. */
  82. #define DTMF_THRESHOLD 8.0e7
  83. #define FAX_THRESHOLD 8.0e7
  84. #define FAX_2ND_HARMONIC 2.0 /* 4dB */
  85. #define DTMF_NORMAL_TWIST 6.3 /* 8dB */
  86. #define DTMF_REVERSE_TWIST ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 4.0 : 2.5) /* 4dB normal */
  87. #define DTMF_RELATIVE_PEAK_ROW 6.3 /* 8dB */
  88. #define DTMF_RELATIVE_PEAK_COL 6.3 /* 8dB */
  89. #define DTMF_2ND_HARMONIC_ROW ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 1.7 : 2.5) /* 4dB normal */
  90. #define DTMF_2ND_HARMONIC_COL 63.1 /* 18dB */
  91. #ifndef OLD_DSP_ROUTINES
  92. #define DTMF_TO_TOTAL_ENERGY 42.0
  93. #endif
  94. #ifdef OLD_DSP_ROUTINES
  95. #define MF_THRESHOLD 8.0e7
  96. #define MF_NORMAL_TWIST 5.3 /* 8dB */
  97. #define MF_REVERSE_TWIST 4.0 /* was 2.5 */
  98. #define MF_RELATIVE_PEAK 5.3 /* 8dB */
  99. #define MF_2ND_HARMONIC 1.7 /* was 2.5 */
  100. #else
  101. #define BELL_MF_THRESHOLD 1.6e9
  102. #define BELL_MF_TWIST 4.0 /* 6dB */
  103. #define BELL_MF_RELATIVE_PEAK 12.6 /* 11dB */
  104. #endif
  105. typedef struct {
  106. float v2;
  107. float v3;
  108. float fac;
  109. #ifndef OLD_DSP_ROUTINES
  110. int samples;
  111. #endif
  112. } goertzel_state_t;
  113. typedef struct
  114. {
  115. goertzel_state_t row_out[4];
  116. goertzel_state_t col_out[4];
  117. #ifdef FAX_DETECT
  118. goertzel_state_t fax_tone;
  119. #endif
  120. #ifdef OLD_DSP_ROUTINES
  121. goertzel_state_t row_out2nd[4];
  122. goertzel_state_t col_out2nd[4];
  123. #ifdef FAX_DETECT
  124. goertzel_state_t fax_tone2nd;
  125. #endif
  126. int hit1;
  127. int hit2;
  128. int hit3;
  129. int hit4;
  130. #else
  131. int hits[3];
  132. #endif
  133. int mhit;
  134. float energy;
  135. int current_sample;
  136. char digits[MAX_DTMF_DIGITS + 1];
  137. int current_digits;
  138. int detected_digits;
  139. int lost_digits;
  140. int digit_hits[16];
  141. #ifdef FAX_DETECT
  142. int fax_hits;
  143. #endif
  144. } dtmf_detect_state_t;
  145. typedef struct
  146. {
  147. goertzel_state_t tone_out[6];
  148. int mhit;
  149. #ifdef OLD_DSP_ROUTINES
  150. int hit1;
  151. int hit2;
  152. int hit3;
  153. int hit4;
  154. goertzel_state_t tone_out2nd[6];
  155. float energy;
  156. #else
  157. int hits[5];
  158. #endif
  159. int current_sample;
  160. char digits[MAX_DTMF_DIGITS + 1];
  161. int current_digits;
  162. int detected_digits;
  163. int lost_digits;
  164. #ifdef FAX_DETECT
  165. int fax_hits;
  166. #endif
  167. } mf_detect_state_t;
  168. static float dtmf_row[] =
  169. {
  170. 697.0, 770.0, 852.0, 941.0
  171. };
  172. static float dtmf_col[] =
  173. {
  174. 1209.0, 1336.0, 1477.0, 1633.0
  175. };
  176. static float mf_tones[] =
  177. {
  178. 700.0, 900.0, 1100.0, 1300.0, 1500.0, 1700.0
  179. };
  180. #ifdef FAX_DETECT
  181. static float fax_freq = 1100.0;
  182. #endif
  183. static char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
  184. #ifdef OLD_DSP_ROUTINES
  185. static char mf_hit[6][6] = {
  186. /* 700 + */ { 0, '1', '2', '4', '7', 'C' },
  187. /* 900 + */ { '1', 0, '3', '5', '8', 'A' },
  188. /* 1100 + */ { '2', '3', 0, '6', '9', '*' },
  189. /* 1300 + */ { '4', '5', '6', 0, '0', 'B' },
  190. /* 1500 + */ { '7', '8', '9', '0', 0, '#' },
  191. /* 1700 + */ { 'C', 'A', '*', 'B', '#', 0 },
  192. };
  193. #else
  194. static char bell_mf_positions[] = "1247C-358A--69*---0B----#";
  195. #endif
  196. static inline void goertzel_sample(goertzel_state_t *s, short sample)
  197. {
  198. float v1;
  199. float fsamp = sample;
  200. v1 = s->v2;
  201. s->v2 = s->v3;
  202. s->v3 = s->fac * s->v2 - v1 + fsamp;
  203. }
  204. static inline void goertzel_update(goertzel_state_t *s, short *samps, int count)
  205. {
  206. int i;
  207. for (i=0;i<count;i++)
  208. goertzel_sample(s, samps[i]);
  209. }
  210. static inline float goertzel_result(goertzel_state_t *s)
  211. {
  212. return s->v3 * s->v3 + s->v2 * s->v2 - s->v2 * s->v3 * s->fac;
  213. }
  214. static inline void goertzel_init(goertzel_state_t *s, float freq, int samples)
  215. {
  216. s->v2 = s->v3 = 0.0;
  217. s->fac = 2.0 * cos(2.0 * M_PI * (freq / 8000.0));
  218. #ifndef OLD_DSP_ROUTINES
  219. s->samples = samples;
  220. #endif
  221. }
  222. static inline void goertzel_reset(goertzel_state_t *s)
  223. {
  224. s->v2 = s->v3 = 0.0;
  225. }
  226. struct ast_dsp {
  227. struct ast_frame f;
  228. int threshold;
  229. int totalsilence;
  230. int totalnoise;
  231. int features;
  232. int busymaybe;
  233. int busycount;
  234. int historicnoise[DSP_HISTORY];
  235. int historicsilence[DSP_HISTORY];
  236. goertzel_state_t freqs[7];
  237. int gsamps;
  238. int tstate;
  239. int tcount;
  240. int digitmode;
  241. int thinkdigit;
  242. float genergy;
  243. union {
  244. dtmf_detect_state_t dtmf;
  245. mf_detect_state_t mf;
  246. } td;
  247. };
  248. static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
  249. {
  250. int i;
  251. #ifdef OLD_DSP_ROUTINES
  252. s->hit1 =
  253. s->mhit =
  254. s->hit3 =
  255. s->hit4 =
  256. s->hit2 = 0;
  257. #else
  258. s->hits[0] = s->hits[1] = s->hits[2] = 0;
  259. #endif
  260. for (i = 0; i < 4; i++)
  261. {
  262. goertzel_init (&s->row_out[i], dtmf_row[i], 102);
  263. goertzel_init (&s->col_out[i], dtmf_col[i], 102);
  264. #ifdef OLD_DSP_ROUTINES
  265. goertzel_init (&s->row_out2nd[i], dtmf_row[i] * 2.0, 102);
  266. goertzel_init (&s->col_out2nd[i], dtmf_col[i] * 2.0, 102);
  267. #endif
  268. s->energy = 0.0;
  269. }
  270. #ifdef FAX_DETECT
  271. /* Same for the fax dector */
  272. goertzel_init (&s->fax_tone, fax_freq, 102);
  273. #ifdef OLD_DSP_ROUTINES
  274. /* Same for the fax dector 2nd harmonic */
  275. goertzel_init (&s->fax_tone2nd, fax_freq * 2.0, 102);
  276. #endif
  277. #endif /* FAX_DETECT */
  278. s->current_sample = 0;
  279. s->detected_digits = 0;
  280. s->current_digits = 0;
  281. memset(&s->digits, 0, sizeof(s->digits));
  282. s->lost_digits = 0;
  283. s->digits[0] = '\0';
  284. }
  285. static void ast_mf_detect_init (mf_detect_state_t *s)
  286. {
  287. int i;
  288. #ifdef OLD_DSP_ROUTINES
  289. s->hit1 =
  290. s->hit2 = 0;
  291. #else
  292. s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
  293. #endif
  294. for (i = 0; i < 6; i++)
  295. {
  296. goertzel_init (&s->tone_out[i], mf_tones[i], 160);
  297. #ifdef OLD_DSP_ROUTINES
  298. goertzel_init (&s->tone_out2nd[i], mf_tones[i] * 2.0, 160);
  299. s->energy = 0.0;
  300. #endif
  301. }
  302. s->current_digits = 0;
  303. memset(&s->digits, 0, sizeof(s->digits));
  304. s->current_sample = 0;
  305. s->detected_digits = 0;
  306. s->lost_digits = 0;
  307. s->digits[0] = '\0';
  308. s->mhit = 0;
  309. }
  310. static int dtmf_detect (dtmf_detect_state_t *s,
  311. int16_t amp[],
  312. int samples,
  313. int digitmode, int *writeback)
  314. {
  315. float row_energy[4];
  316. float col_energy[4];
  317. #ifdef FAX_DETECT
  318. float fax_energy;
  319. #ifdef OLD_DSP_ROUTINES
  320. float fax_energy_2nd;
  321. #endif
  322. #endif /* FAX_DETECT */
  323. float famp;
  324. float v1;
  325. int i;
  326. int j;
  327. int sample;
  328. int best_row;
  329. int best_col;
  330. int hit;
  331. int limit;
  332. hit = 0;
  333. for (sample = 0; sample < samples; sample = limit)
  334. {
  335. /* 102 is optimised to meet the DTMF specs. */
  336. if ((samples - sample) >= (102 - s->current_sample))
  337. limit = sample + (102 - s->current_sample);
  338. else
  339. limit = samples;
  340. #if defined(USE_3DNOW)
  341. _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
  342. _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
  343. #ifdef OLD_DSP_ROUTINES
  344. _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
  345. _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
  346. #endif
  347. /* XXX Need to fax detect for 3dnow too XXX */
  348. #warning "Fax Support Broken"
  349. #else
  350. /* The following unrolled loop takes only 35% (rough estimate) of the
  351. time of a rolled loop on the machine on which it was developed */
  352. for (j = sample; j < limit; j++)
  353. {
  354. famp = amp[j];
  355. s->energy += famp*famp;
  356. /* With GCC 2.95, the following unrolled code seems to take about 35%
  357. (rough estimate) as long as a neat little 0-3 loop */
  358. v1 = s->row_out[0].v2;
  359. s->row_out[0].v2 = s->row_out[0].v3;
  360. s->row_out[0].v3 = s->row_out[0].fac*s->row_out[0].v2 - v1 + famp;
  361. v1 = s->col_out[0].v2;
  362. s->col_out[0].v2 = s->col_out[0].v3;
  363. s->col_out[0].v3 = s->col_out[0].fac*s->col_out[0].v2 - v1 + famp;
  364. v1 = s->row_out[1].v2;
  365. s->row_out[1].v2 = s->row_out[1].v3;
  366. s->row_out[1].v3 = s->row_out[1].fac*s->row_out[1].v2 - v1 + famp;
  367. v1 = s->col_out[1].v2;
  368. s->col_out[1].v2 = s->col_out[1].v3;
  369. s->col_out[1].v3 = s->col_out[1].fac*s->col_out[1].v2 - v1 + famp;
  370. v1 = s->row_out[2].v2;
  371. s->row_out[2].v2 = s->row_out[2].v3;
  372. s->row_out[2].v3 = s->row_out[2].fac*s->row_out[2].v2 - v1 + famp;
  373. v1 = s->col_out[2].v2;
  374. s->col_out[2].v2 = s->col_out[2].v3;
  375. s->col_out[2].v3 = s->col_out[2].fac*s->col_out[2].v2 - v1 + famp;
  376. v1 = s->row_out[3].v2;
  377. s->row_out[3].v2 = s->row_out[3].v3;
  378. s->row_out[3].v3 = s->row_out[3].fac*s->row_out[3].v2 - v1 + famp;
  379. v1 = s->col_out[3].v2;
  380. s->col_out[3].v2 = s->col_out[3].v3;
  381. s->col_out[3].v3 = s->col_out[3].fac*s->col_out[3].v2 - v1 + famp;
  382. #ifdef FAX_DETECT
  383. /* Update fax tone */
  384. v1 = s->fax_tone.v2;
  385. s->fax_tone.v2 = s->fax_tone.v3;
  386. s->fax_tone.v3 = s->fax_tone.fac*s->fax_tone.v2 - v1 + famp;
  387. #endif /* FAX_DETECT */
  388. #ifdef OLD_DSP_ROUTINES
  389. v1 = s->col_out2nd[0].v2;
  390. s->col_out2nd[0].v2 = s->col_out2nd[0].v3;
  391. s->col_out2nd[0].v3 = s->col_out2nd[0].fac*s->col_out2nd[0].v2 - v1 + famp;
  392. v1 = s->row_out2nd[0].v2;
  393. s->row_out2nd[0].v2 = s->row_out2nd[0].v3;
  394. s->row_out2nd[0].v3 = s->row_out2nd[0].fac*s->row_out2nd[0].v2 - v1 + famp;
  395. v1 = s->col_out2nd[1].v2;
  396. s->col_out2nd[1].v2 = s->col_out2nd[1].v3;
  397. s->col_out2nd[1].v3 = s->col_out2nd[1].fac*s->col_out2nd[1].v2 - v1 + famp;
  398. v1 = s->row_out2nd[1].v2;
  399. s->row_out2nd[1].v2 = s->row_out2nd[1].v3;
  400. s->row_out2nd[1].v3 = s->row_out2nd[1].fac*s->row_out2nd[1].v2 - v1 + famp;
  401. v1 = s->col_out2nd[2].v2;
  402. s->col_out2nd[2].v2 = s->col_out2nd[2].v3;
  403. s->col_out2nd[2].v3 = s->col_out2nd[2].fac*s->col_out2nd[2].v2 - v1 + famp;
  404. v1 = s->row_out2nd[2].v2;
  405. s->row_out2nd[2].v2 = s->row_out2nd[2].v3;
  406. s->row_out2nd[2].v3 = s->row_out2nd[2].fac*s->row_out2nd[2].v2 - v1 + famp;
  407. v1 = s->col_out2nd[3].v2;
  408. s->col_out2nd[3].v2 = s->col_out2nd[3].v3;
  409. s->col_out2nd[3].v3 = s->col_out2nd[3].fac*s->col_out2nd[3].v2 - v1 + famp;
  410. v1 = s->row_out2nd[3].v2;
  411. s->row_out2nd[3].v2 = s->row_out2nd[3].v3;
  412. s->row_out2nd[3].v3 = s->row_out2nd[3].fac*s->row_out2nd[3].v2 - v1 + famp;
  413. #ifdef FAX_DETECT
  414. /* Update fax tone */
  415. v1 = s->fax_tone.v2;
  416. s->fax_tone2nd.v2 = s->fax_tone2nd.v3;
  417. s->fax_tone2nd.v3 = s->fax_tone2nd.fac*s->fax_tone2nd.v2 - v1 + famp;
  418. #endif /* FAX_DETECT */
  419. #endif
  420. }
  421. #endif
  422. s->current_sample += (limit - sample);
  423. if (s->current_sample < 102) {
  424. if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
  425. /* If we had a hit last time, go ahead and clear this out since likely it
  426. will be another hit */
  427. for (i=sample;i<limit;i++)
  428. amp[i] = 0;
  429. *writeback = 1;
  430. }
  431. continue;
  432. }
  433. #ifdef FAX_DETECT
  434. /* Detect the fax energy, too */
  435. fax_energy = goertzel_result(&s->fax_tone);
  436. #endif
  437. /* We are at the end of a DTMF detection block */
  438. /* Find the peak row and the peak column */
  439. row_energy[0] = goertzel_result (&s->row_out[0]);
  440. col_energy[0] = goertzel_result (&s->col_out[0]);
  441. for (best_row = best_col = 0, i = 1; i < 4; i++)
  442. {
  443. row_energy[i] = goertzel_result (&s->row_out[i]);
  444. if (row_energy[i] > row_energy[best_row])
  445. best_row = i;
  446. col_energy[i] = goertzel_result (&s->col_out[i]);
  447. if (col_energy[i] > col_energy[best_col])
  448. best_col = i;
  449. }
  450. hit = 0;
  451. /* Basic signal level test and the twist test */
  452. if (row_energy[best_row] >= DTMF_THRESHOLD
  453. &&
  454. col_energy[best_col] >= DTMF_THRESHOLD
  455. &&
  456. col_energy[best_col] < row_energy[best_row]*DTMF_REVERSE_TWIST
  457. &&
  458. col_energy[best_col]*DTMF_NORMAL_TWIST > row_energy[best_row])
  459. {
  460. /* Relative peak test */
  461. for (i = 0; i < 4; i++)
  462. {
  463. if ((i != best_col && col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col])
  464. ||
  465. (i != best_row && row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row]))
  466. {
  467. break;
  468. }
  469. }
  470. #ifdef OLD_DSP_ROUTINES
  471. /* ... and second harmonic test */
  472. if (i >= 4
  473. &&
  474. (row_energy[best_row] + col_energy[best_col]) > 42.0*s->energy
  475. &&
  476. goertzel_result (&s->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col]
  477. &&
  478. goertzel_result (&s->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row])
  479. #else
  480. /* ... and fraction of total energy test */
  481. if (i >= 4
  482. &&
  483. (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY*s->energy)
  484. #endif
  485. {
  486. /* Got a hit */
  487. hit = dtmf_positions[(best_row << 2) + best_col];
  488. if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
  489. /* Zero out frame data if this is part DTMF */
  490. for (i=sample;i<limit;i++)
  491. amp[i] = 0;
  492. *writeback = 1;
  493. }
  494. /* Look for two successive similar results */
  495. /* The logic in the next test is:
  496. We need two successive identical clean detects, with
  497. something different preceeding it. This can work with
  498. back to back differing digits. More importantly, it
  499. can work with nasty phones that give a very wobbly start
  500. to a digit. */
  501. #ifdef OLD_DSP_ROUTINES
  502. if (hit == s->hit3 && s->hit3 != s->hit2)
  503. {
  504. s->mhit = hit;
  505. s->digit_hits[(best_row << 2) + best_col]++;
  506. s->detected_digits++;
  507. if (s->current_digits < MAX_DTMF_DIGITS)
  508. {
  509. s->digits[s->current_digits++] = hit;
  510. s->digits[s->current_digits] = '\0';
  511. }
  512. else
  513. {
  514. s->lost_digits++;
  515. }
  516. }
  517. #else
  518. if (hit == s->hits[2] && hit != s->hits[1] && hit != s->hits[0])
  519. {
  520. s->mhit = hit;
  521. s->digit_hits[(best_row << 2) + best_col]++;
  522. s->detected_digits++;
  523. if (s->current_digits < MAX_DTMF_DIGITS)
  524. {
  525. s->digits[s->current_digits++] = hit;
  526. s->digits[s->current_digits] = '\0';
  527. }
  528. else
  529. {
  530. s->lost_digits++;
  531. }
  532. }
  533. #endif
  534. }
  535. }
  536. #ifdef FAX_DETECT
  537. #ifdef OLD_DSP_ROUTINES
  538. if (!hit && (fax_energy >= FAX_THRESHOLD) && (fax_energy > s->energy * 21.0)) {
  539. fax_energy_2nd = goertzel_result(&s->fax_tone2nd);
  540. fax_energy_2nd = goertzel_result(&s->fax_tone2nd);
  541. if (fax_energy_2nd * FAX_2ND_HARMONIC < fax_energy) {
  542. #if 0
  543. printf("Fax energy/Second Harmonic: %f/%f\n", fax_energy, fax_energy_2nd);
  544. #endif
  545. /* XXX Probably need better checking than just this the energy XXX */
  546. hit = 'f';
  547. s->fax_hits++;
  548. } /* Don't reset fax hits counter */
  549. }
  550. #else /* OLD_DSP_ROUTINES */
  551. if (!hit && (fax_energy >= FAX_THRESHOLD) && (fax_energy >= DTMF_TO_TOTAL_ENERGY*s->energy)) {
  552. #if 0
  553. printf("Fax energy/Second Harmonic: %f\n", fax_energy);
  554. #endif
  555. /* XXX Probably need better checking than just this the energy XXX */
  556. hit = 'f';
  557. s->fax_hits++;
  558. }
  559. #endif /* OLD_DSP_ROUTINES */
  560. else {
  561. if (s->fax_hits > 5) {
  562. hit = 'f';
  563. s->mhit = 'f';
  564. s->detected_digits++;
  565. if (s->current_digits < MAX_DTMF_DIGITS)
  566. {
  567. s->digits[s->current_digits++] = hit;
  568. s->digits[s->current_digits] = '\0';
  569. }
  570. else
  571. {
  572. s->lost_digits++;
  573. }
  574. }
  575. s->fax_hits = 0;
  576. }
  577. #endif /* FAX_DETECT */
  578. #ifdef OLD_DSP_ROUTINES
  579. s->hit1 = s->hit2;
  580. s->hit2 = s->hit3;
  581. s->hit3 = hit;
  582. #else
  583. s->hits[0] = s->hits[1];
  584. s->hits[1] = s->hits[2];
  585. s->hits[2] = hit;
  586. #endif
  587. /* Reinitialise the detector for the next block */
  588. for (i = 0; i < 4; i++)
  589. {
  590. goertzel_reset(&s->row_out[i]);
  591. goertzel_reset(&s->col_out[i]);
  592. #ifdef OLD_DSP_ROUTINES
  593. goertzel_reset(&s->row_out2nd[i]);
  594. goertzel_reset(&s->col_out2nd[i]);
  595. #endif
  596. }
  597. #ifdef FAX_DETECT
  598. goertzel_reset (&s->fax_tone);
  599. #ifdef OLD_DSP_ROUTINES
  600. goertzel_reset (&s->fax_tone2nd);
  601. #endif
  602. #endif
  603. s->energy = 0.0;
  604. s->current_sample = 0;
  605. }
  606. if ((!s->mhit) || (s->mhit != hit))
  607. {
  608. s->mhit = 0;
  609. return(0);
  610. }
  611. return (hit);
  612. }
  613. /* MF goertzel size */
  614. #ifdef OLD_DSP_ROUTINES
  615. #define MF_GSIZE 160
  616. #else
  617. #define MF_GSIZE 120
  618. #endif
  619. static int mf_detect (mf_detect_state_t *s,
  620. int16_t amp[],
  621. int samples,
  622. int digitmode, int *writeback)
  623. {
  624. #ifdef OLD_DSP_ROUTINES
  625. float tone_energy[6];
  626. int best1;
  627. int best2;
  628. float max;
  629. int sofarsogood;
  630. #else
  631. float energy[6];
  632. int best;
  633. int second_best;
  634. #endif
  635. float famp;
  636. float v1;
  637. int i;
  638. int j;
  639. int sample;
  640. int hit;
  641. int limit;
  642. hit = 0;
  643. for (sample = 0; sample < samples; sample = limit)
  644. {
  645. /* 80 is optimised to meet the MF specs. */
  646. if ((samples - sample) >= (MF_GSIZE - s->current_sample))
  647. limit = sample + (MF_GSIZE - s->current_sample);
  648. else
  649. limit = samples;
  650. #if defined(USE_3DNOW)
  651. _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
  652. _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
  653. #ifdef OLD_DSP_ROUTINES
  654. _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
  655. _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
  656. #endif
  657. /* XXX Need to fax detect for 3dnow too XXX */
  658. #warning "Fax Support Broken"
  659. #else
  660. /* The following unrolled loop takes only 35% (rough estimate) of the
  661. time of a rolled loop on the machine on which it was developed */
  662. for (j = sample; j < limit; j++)
  663. {
  664. famp = amp[j];
  665. #ifdef OLD_DSP_ROUTINES
  666. s->energy += famp*famp;
  667. #endif
  668. /* With GCC 2.95, the following unrolled code seems to take about 35%
  669. (rough estimate) as long as a neat little 0-3 loop */
  670. v1 = s->tone_out[0].v2;
  671. s->tone_out[0].v2 = s->tone_out[0].v3;
  672. s->tone_out[0].v3 = s->tone_out[0].fac*s->tone_out[0].v2 - v1 + famp;
  673. v1 = s->tone_out[1].v2;
  674. s->tone_out[1].v2 = s->tone_out[1].v3;
  675. s->tone_out[1].v3 = s->tone_out[1].fac*s->tone_out[1].v2 - v1 + famp;
  676. v1 = s->tone_out[2].v2;
  677. s->tone_out[2].v2 = s->tone_out[2].v3;
  678. s->tone_out[2].v3 = s->tone_out[2].fac*s->tone_out[2].v2 - v1 + famp;
  679. v1 = s->tone_out[3].v2;
  680. s->tone_out[3].v2 = s->tone_out[3].v3;
  681. s->tone_out[3].v3 = s->tone_out[3].fac*s->tone_out[3].v2 - v1 + famp;
  682. v1 = s->tone_out[4].v2;
  683. s->tone_out[4].v2 = s->tone_out[4].v3;
  684. s->tone_out[4].v3 = s->tone_out[4].fac*s->tone_out[4].v2 - v1 + famp;
  685. v1 = s->tone_out[5].v2;
  686. s->tone_out[5].v2 = s->tone_out[5].v3;
  687. s->tone_out[5].v3 = s->tone_out[5].fac*s->tone_out[5].v2 - v1 + famp;
  688. #ifdef OLD_DSP_ROUTINES
  689. v1 = s->tone_out2nd[0].v2;
  690. s->tone_out2nd[0].v2 = s->tone_out2nd[0].v3;
  691. s->tone_out2nd[0].v3 = s->tone_out2nd[0].fac*s->tone_out2nd[0].v2 - v1 + famp;
  692. v1 = s->tone_out2nd[1].v2;
  693. s->tone_out2nd[1].v2 = s->tone_out2nd[1].v3;
  694. s->tone_out2nd[1].v3 = s->tone_out2nd[1].fac*s->tone_out2nd[1].v2 - v1 + famp;
  695. v1 = s->tone_out2nd[2].v2;
  696. s->tone_out2nd[2].v2 = s->tone_out2nd[2].v3;
  697. s->tone_out2nd[2].v3 = s->tone_out2nd[2].fac*s->tone_out2nd[2].v2 - v1 + famp;
  698. v1 = s->tone_out2nd[3].v2;
  699. s->tone_out2nd[3].v2 = s->tone_out2nd[3].v3;
  700. s->tone_out2nd[3].v3 = s->tone_out2nd[3].fac*s->tone_out2nd[3].v2 - v1 + famp;
  701. v1 = s->tone_out2nd[4].v2;
  702. s->tone_out2nd[4].v2 = s->tone_out2nd[4].v3;
  703. s->tone_out2nd[4].v3 = s->tone_out2nd[4].fac*s->tone_out2nd[2].v2 - v1 + famp;
  704. v1 = s->tone_out2nd[3].v2;
  705. s->tone_out2nd[5].v2 = s->tone_out2nd[6].v3;
  706. s->tone_out2nd[5].v3 = s->tone_out2nd[6].fac*s->tone_out2nd[3].v2 - v1 + famp;
  707. #endif
  708. }
  709. #endif
  710. s->current_sample += (limit - sample);
  711. if (s->current_sample < MF_GSIZE) {
  712. if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
  713. /* If we had a hit last time, go ahead and clear this out since likely it
  714. will be another hit */
  715. for (i=sample;i<limit;i++)
  716. amp[i] = 0;
  717. *writeback = 1;
  718. }
  719. continue;
  720. }
  721. #ifdef OLD_DSP_ROUTINES
  722. /* We're at the end of an MF detection block. Go ahead and calculate
  723. all the energies. */
  724. for (i=0;i<6;i++) {
  725. tone_energy[i] = goertzel_result(&s->tone_out[i]);
  726. }
  727. /* Find highest */
  728. best1 = 0;
  729. max = tone_energy[0];
  730. for (i=1;i<6;i++) {
  731. if (tone_energy[i] > max) {
  732. max = tone_energy[i];
  733. best1 = i;
  734. }
  735. }
  736. /* Find 2nd highest */
  737. if (best1) {
  738. max = tone_energy[0];
  739. best2 = 0;
  740. } else {
  741. max = tone_energy[1];
  742. best2 = 1;
  743. }
  744. for (i=0;i<6;i++) {
  745. if (i == best1) continue;
  746. if (tone_energy[i] > max) {
  747. max = tone_energy[i];
  748. best2 = i;
  749. }
  750. }
  751. hit = 0;
  752. if (best1 != best2) sofarsogood=1;
  753. else sofarsogood=0;
  754. /* Check for relative energies */
  755. for (i=0;i<6;i++) {
  756. if (i == best1) continue;
  757. if (i == best2) continue;
  758. if (tone_energy[best1] < tone_energy[i] * MF_RELATIVE_PEAK) {
  759. sofarsogood = 0;
  760. break;
  761. }
  762. if (tone_energy[best2] < tone_energy[i] * MF_RELATIVE_PEAK) {
  763. sofarsogood = 0;
  764. break;
  765. }
  766. }
  767. if (sofarsogood) {
  768. /* Check for 2nd harmonic */
  769. if (goertzel_result(&s->tone_out2nd[best1]) * MF_2ND_HARMONIC > tone_energy[best1])
  770. sofarsogood = 0;
  771. else if (goertzel_result(&s->tone_out2nd[best2]) * MF_2ND_HARMONIC > tone_energy[best2])
  772. sofarsogood = 0;
  773. }
  774. if (sofarsogood) {
  775. hit = mf_hit[best1][best2];
  776. if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
  777. /* Zero out frame data if this is part DTMF */
  778. for (i=sample;i<limit;i++)
  779. amp[i] = 0;
  780. *writeback = 1;
  781. }
  782. /* Look for two consecutive clean hits */
  783. if ((hit == s->hit3) && (s->hit3 != s->hit2)) {
  784. s->mhit = hit;
  785. s->detected_digits++;
  786. if (s->current_digits < MAX_DTMF_DIGITS - 2) {
  787. s->digits[s->current_digits++] = hit;
  788. s->digits[s->current_digits] = '\0';
  789. } else {
  790. s->lost_digits++;
  791. }
  792. }
  793. }
  794. s->hit1 = s->hit2;
  795. s->hit2 = s->hit3;
  796. s->hit3 = hit;
  797. /* Reinitialise the detector for the next block */
  798. for (i = 0; i < 6; i++)
  799. {
  800. goertzel_reset(&s->tone_out[i]);
  801. goertzel_reset(&s->tone_out2nd[i]);
  802. }
  803. s->energy = 0.0;
  804. s->current_sample = 0;
  805. }
  806. #else
  807. /* We're at the end of an MF detection block. */
  808. /* Find the two highest energies. The spec says to look for
  809. two tones and two tones only. Taking this literally -ie
  810. only two tones pass the minimum threshold - doesn't work
  811. well. The sinc function mess, due to rectangular windowing
  812. ensure that! Find the two highest energies and ensure they
  813. are considerably stronger than any of the others. */
  814. energy[0] = goertzel_result(&s->tone_out[0]);
  815. energy[1] = goertzel_result(&s->tone_out[1]);
  816. if (energy[0] > energy[1])
  817. {
  818. best = 0;
  819. second_best = 1;
  820. }
  821. else
  822. {
  823. best = 1;
  824. second_best = 0;
  825. }
  826. /*endif*/
  827. for (i = 2; i < 6; i++)
  828. {
  829. energy[i] = goertzel_result(&s->tone_out[i]);
  830. if (energy[i] >= energy[best])
  831. {
  832. second_best = best;
  833. best = i;
  834. }
  835. else if (energy[i] >= energy[second_best])
  836. {
  837. second_best = i;
  838. }
  839. }
  840. /* Basic signal level and twist tests */
  841. hit = 0;
  842. if (energy[best] >= BELL_MF_THRESHOLD
  843. &&
  844. energy[second_best] >= BELL_MF_THRESHOLD
  845. &&
  846. energy[best] < energy[second_best]*BELL_MF_TWIST
  847. &&
  848. energy[best]*BELL_MF_TWIST > energy[second_best])
  849. {
  850. /* Relative peak test */
  851. hit = -1;
  852. for (i = 0; i < 6; i++)
  853. {
  854. if (i != best && i != second_best)
  855. {
  856. if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best])
  857. {
  858. /* The best two are not clearly the best */
  859. hit = 0;
  860. break;
  861. }
  862. }
  863. }
  864. }
  865. if (hit)
  866. {
  867. /* Get the values into ascending order */
  868. if (second_best < best)
  869. {
  870. i = best;
  871. best = second_best;
  872. second_best = i;
  873. }
  874. best = best*5 + second_best - 1;
  875. hit = bell_mf_positions[best];
  876. /* Look for two successive similar results */
  877. /* The logic in the next test is:
  878. For KP we need 4 successive identical clean detects, with
  879. two blocks of something different preceeding it. For anything
  880. else we need two successive identical clean detects, with
  881. two blocks of something different preceeding it. */
  882. if (hit == s->hits[4]
  883. &&
  884. hit == s->hits[3]
  885. &&
  886. ((hit != '*' && hit != s->hits[2] && hit != s->hits[1])
  887. ||
  888. (hit == '*' && hit == s->hits[2] && hit != s->hits[1] && hit != s->hits[0])))
  889. {
  890. s->detected_digits++;
  891. if (s->current_digits < MAX_DTMF_DIGITS)
  892. {
  893. s->digits[s->current_digits++] = hit;
  894. s->digits[s->current_digits] = '\0';
  895. }
  896. else
  897. {
  898. s->lost_digits++;
  899. }
  900. }
  901. }
  902. else
  903. {
  904. hit = 0;
  905. }
  906. s->hits[0] = s->hits[1];
  907. s->hits[1] = s->hits[2];
  908. s->hits[2] = s->hits[3];
  909. s->hits[3] = s->hits[4];
  910. s->hits[4] = hit;
  911. /* Reinitialise the detector for the next block */
  912. for (i = 0; i < 6; i++)
  913. goertzel_reset(&s->tone_out[i]);
  914. s->current_sample = 0;
  915. }
  916. #endif
  917. if ((!s->mhit) || (s->mhit != hit))
  918. {
  919. s->mhit = 0;
  920. return(0);
  921. }
  922. return (hit);
  923. }
  924. static int __ast_dsp_digitdetect(struct ast_dsp *dsp, short *s, int len, int *writeback)
  925. {
  926. int res;
  927. if (dsp->digitmode & DSP_DIGITMODE_MF)
  928. res = mf_detect(&dsp->td.mf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback);
  929. else
  930. res = dtmf_detect(&dsp->td.dtmf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback);
  931. return res;
  932. }
  933. int ast_dsp_digitdetect(struct ast_dsp *dsp, struct ast_frame *inf)
  934. {
  935. short *s;
  936. int len;
  937. int ign=0;
  938. if (inf->frametype != AST_FRAME_VOICE) {
  939. ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
  940. return 0;
  941. }
  942. if (inf->subclass != AST_FORMAT_SLINEAR) {
  943. ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
  944. return 0;
  945. }
  946. s = inf->data;
  947. len = inf->datalen / 2;
  948. return __ast_dsp_digitdetect(dsp, s, len, &ign);
  949. }
  950. static inline int pair_there(float p1, float p2, float i1, float i2, float e)
  951. {
  952. /* See if p1 and p2 are there, relative to i1 and i2 and total energy */
  953. /* Make sure absolute levels are high enough */
  954. if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH))
  955. return 0;
  956. /* Amplify ignored stuff */
  957. i2 *= TONE_THRESH;
  958. i1 *= TONE_THRESH;
  959. e *= TONE_THRESH;
  960. /* Check first tone */
  961. if ((p1 < i1) || (p1 < i2) || (p1 < e))
  962. return 0;
  963. /* And second */
  964. if ((p2 < i1) || (p2 < i2) || (p2 < e))
  965. return 0;
  966. /* Guess it's there... */
  967. return 1;
  968. }
  969. int ast_dsp_getdigits (struct ast_dsp *dsp,
  970. char *buf,
  971. int max)
  972. {
  973. if (dsp->digitmode & DSP_DIGITMODE_MF) {
  974. if (max > dsp->td.mf.current_digits)
  975. max = dsp->td.mf.current_digits;
  976. if (max > 0)
  977. {
  978. memcpy (buf, dsp->td.mf.digits, max);
  979. memmove (dsp->td.mf.digits, dsp->td.mf.digits + max, dsp->td.mf.current_digits - max);
  980. dsp->td.mf.current_digits -= max;
  981. }
  982. buf[max] = '\0';
  983. return max;
  984. } else {
  985. if (max > dsp->td.dtmf.current_digits)
  986. max = dsp->td.dtmf.current_digits;
  987. if (max > 0)
  988. {
  989. memcpy (buf, dsp->td.dtmf.digits, max);
  990. memmove (dsp->td.dtmf.digits, dsp->td.dtmf.digits + max, dsp->td.dtmf.current_digits - max);
  991. dsp->td.dtmf.current_digits -= max;
  992. }
  993. buf[max] = '\0';
  994. return max;
  995. }
  996. }
  997. static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
  998. {
  999. int x;
  1000. int pass;
  1001. int newstate = TONE_STATE_SILENCE;
  1002. int res = 0;
  1003. while(len) {
  1004. /* Take the lesser of the number of samples we need and what we have */
  1005. pass = len;
  1006. if (pass > GSAMP_SIZE - dsp->gsamps)
  1007. pass = GSAMP_SIZE - dsp->gsamps;
  1008. for (x=0;x<pass;x++) {
  1009. goertzel_sample(&dsp->freqs[HZ_350], s[x]);
  1010. goertzel_sample(&dsp->freqs[HZ_440], s[x]);
  1011. goertzel_sample(&dsp->freqs[HZ_480], s[x]);
  1012. goertzel_sample(&dsp->freqs[HZ_620], s[x]);
  1013. goertzel_sample(&dsp->freqs[HZ_950], s[x]);
  1014. goertzel_sample(&dsp->freqs[HZ_1400], s[x]);
  1015. goertzel_sample(&dsp->freqs[HZ_1800], s[x]);
  1016. dsp->genergy += s[x] * s[x];
  1017. }
  1018. s += pass;
  1019. dsp->gsamps += pass;
  1020. len -= pass;
  1021. if (dsp->gsamps == GSAMP_SIZE) {
  1022. float hz_350;
  1023. float hz_440;
  1024. float hz_480;
  1025. float hz_620;
  1026. float hz_950;
  1027. float hz_1400;
  1028. float hz_1800;
  1029. hz_350 = goertzel_result(&dsp->freqs[HZ_350]);
  1030. hz_440 = goertzel_result(&dsp->freqs[HZ_440]);
  1031. hz_480 = goertzel_result(&dsp->freqs[HZ_480]);
  1032. hz_620 = goertzel_result(&dsp->freqs[HZ_620]);
  1033. hz_950 = goertzel_result(&dsp->freqs[HZ_950]);
  1034. hz_1400 = goertzel_result(&dsp->freqs[HZ_1400]);
  1035. hz_1800 = goertzel_result(&dsp->freqs[HZ_1800]);
  1036. #if 0
  1037. printf("Got whole dsp state: 350: %e, 440: %e, 480: %e, 620: %e, 950: %e, 1400: %e, 1800: %e, Energy: %e\n",
  1038. hz_350, hz_440, hz_480, hz_620, hz_950, hz_1400, hz_1800, dsp->genergy);
  1039. #endif
  1040. if (pair_there(hz_480, hz_620, hz_350, hz_440, dsp->genergy)) {
  1041. newstate = TONE_STATE_BUSY;
  1042. } else if (pair_there(hz_440, hz_480, hz_350, hz_620, dsp->genergy)) {
  1043. newstate = TONE_STATE_RINGING;
  1044. } else if (pair_there(hz_350, hz_440, hz_480, hz_620, dsp->genergy)) {
  1045. newstate = TONE_STATE_DIALTONE;
  1046. } else if (hz_950 > TONE_MIN_THRESH * TONE_THRESH) {
  1047. newstate = TONE_STATE_SPECIAL1;
  1048. } else if (hz_1400 > TONE_MIN_THRESH * TONE_THRESH) {
  1049. if (dsp->tstate == TONE_STATE_SPECIAL1)
  1050. newstate = TONE_STATE_SPECIAL2;
  1051. } else if (hz_1800 > TONE_MIN_THRESH * TONE_THRESH) {
  1052. if (dsp->tstate == TONE_STATE_SPECIAL2)
  1053. newstate = TONE_STATE_SPECIAL3;
  1054. } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
  1055. newstate = TONE_STATE_TALKING;
  1056. } else
  1057. newstate = TONE_STATE_SILENCE;
  1058. if (newstate == dsp->tstate) {
  1059. dsp->tcount++;
  1060. if (dsp->tcount == COUNT_THRESH) {
  1061. if (dsp->tstate == TONE_STATE_BUSY) {
  1062. res = AST_CONTROL_BUSY;
  1063. dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
  1064. } else if (dsp->tstate == TONE_STATE_TALKING) {
  1065. res = AST_CONTROL_ANSWER;
  1066. dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
  1067. } else if (dsp->tstate == TONE_STATE_RINGING)
  1068. res = AST_CONTROL_RINGING;
  1069. else if (dsp->tstate == TONE_STATE_SPECIAL3) {
  1070. res = AST_CONTROL_CONGESTION;
  1071. dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
  1072. }
  1073. }
  1074. } else {
  1075. #if 0
  1076. printf("Newstate: %d\n", newstate);
  1077. #endif
  1078. dsp->tstate = newstate;
  1079. dsp->tcount = 1;
  1080. }
  1081. /* Reset goertzel */
  1082. for (x=0;x<7;x++)
  1083. dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
  1084. dsp->gsamps = 0;
  1085. dsp->genergy = 0.0;
  1086. }
  1087. }
  1088. #if 0
  1089. if (res)
  1090. printf("Returning %d\n", res);
  1091. #endif
  1092. return res;
  1093. }
  1094. int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf)
  1095. {
  1096. if (inf->frametype != AST_FRAME_VOICE) {
  1097. ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
  1098. return 0;
  1099. }
  1100. if (inf->subclass != AST_FORMAT_SLINEAR) {
  1101. ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
  1102. return 0;
  1103. }
  1104. return __ast_dsp_call_progress(dsp, inf->data, inf->datalen / 2);
  1105. }
  1106. static int __ast_dsp_silence(struct ast_dsp *dsp, short *s, int len, int *totalsilence)
  1107. {
  1108. int accum;
  1109. int x;
  1110. int res = 0;
  1111. accum = 0;
  1112. for (x=0;x<len; x++)
  1113. accum += abs(s[x]);
  1114. accum /= len;
  1115. if (accum < dsp->threshold) {
  1116. dsp->totalsilence += len/8;
  1117. if (dsp->totalnoise) {
  1118. /* Move and save history */
  1119. memmove(dsp->historicnoise + DSP_HISTORY - dsp->busycount, dsp->historicnoise + DSP_HISTORY - dsp->busycount +1, dsp->busycount*sizeof(dsp->historicnoise[0]));
  1120. dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise;
  1121. /* we don't want to check for busydetect that frequently */
  1122. #if 0
  1123. dsp->busymaybe = 1;
  1124. #endif
  1125. }
  1126. dsp->totalnoise = 0;
  1127. res = 1;
  1128. } else {
  1129. dsp->totalnoise += len/8;
  1130. if (dsp->totalsilence) {
  1131. int silence1 = dsp->historicsilence[DSP_HISTORY - 1];
  1132. int silence2 = dsp->historicsilence[DSP_HISTORY - 2];
  1133. /* Move and save history */
  1134. memmove(dsp->historicsilence + DSP_HISTORY - dsp->busycount, dsp->historicsilence + DSP_HISTORY - dsp->busycount + 1, dsp->busycount*sizeof(dsp->historicsilence[0]));
  1135. dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence;
  1136. /* check if the previous sample differs only by BUSY_PERCENT from the one before it */
  1137. if (silence1 < silence2) {
  1138. if (silence1 + silence1/BUSY_PERCENT >= silence2)
  1139. dsp->busymaybe = 1;
  1140. else
  1141. dsp->busymaybe = 0;
  1142. } else {
  1143. if (silence1 - silence1/BUSY_PERCENT <= silence2)
  1144. dsp->busymaybe = 1;
  1145. else
  1146. dsp->busymaybe = 0;
  1147. }
  1148. }
  1149. dsp->totalsilence = 0;
  1150. }
  1151. if (totalsilence)
  1152. *totalsilence = dsp->totalsilence;
  1153. return res;
  1154. }
  1155. #ifdef BUSYDETECT_MARTIN
  1156. int ast_dsp_busydetect(struct ast_dsp *dsp)
  1157. {
  1158. int res = 0, x;
  1159. #ifndef BUSYDETECT_TONEONLY
  1160. int avgsilence = 0, hitsilence = 0;
  1161. #endif
  1162. int avgtone = 0, hittone = 0;
  1163. if (!dsp->busymaybe)
  1164. return res;
  1165. for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
  1166. #ifndef BUSYDETECT_TONEONLY
  1167. avgsilence += dsp->historicsilence[x];
  1168. #endif
  1169. avgtone += dsp->historicnoise[x];
  1170. }
  1171. #ifndef BUSYDETECT_TONEONLY
  1172. avgsilence /= dsp->busycount;
  1173. #endif
  1174. avgtone /= dsp->busycount;
  1175. for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
  1176. #ifndef BUSYDETECT_TONEONLY
  1177. if (avgsilence > dsp->historicsilence[x]) {
  1178. if (avgsilence - (avgsilence / BUSY_PERCENT) <= dsp->historicsilence[x])
  1179. hitsilence++;
  1180. } else {
  1181. if (avgsilence + (avgsilence / BUSY_PERCENT) >= dsp->historicsilence[x])
  1182. hitsilence++;
  1183. }
  1184. #endif
  1185. if (avgtone > dsp->historicnoise[x]) {
  1186. if (avgtone - (avgtone / BUSY_PERCENT) <= dsp->historicsilence[x])
  1187. hittone++;
  1188. } else {
  1189. if (avgtone + (avgtone / BUSY_PERCENT) >= dsp->historicsilence[x])
  1190. hittone++;
  1191. }
  1192. }
  1193. #ifndef BUSYDETECT_TONEONLY
  1194. if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) && (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) {
  1195. #else
  1196. if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) {
  1197. #endif
  1198. #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
  1199. #ifdef BUSYDETECT_TONEONLY
  1200. #error You cant use BUSYDETECT_TONEONLY together with BUSYDETECT_COMPARE_TONE_AND_SILENCE
  1201. #endif
  1202. if (avgtone > avgsilence) {
  1203. if (avgtone - avgtone/(BUSY_PERCENT*2) <= avgsilence)
  1204. res = 1;
  1205. } else {
  1206. if (avgtone + avgtone/(BUSY_PERCENT*2) >= avgsilence)
  1207. res = 1;
  1208. }
  1209. #else
  1210. res = 1;
  1211. #endif
  1212. }
  1213. #if 0
  1214. if (res)
  1215. ast_log(LOG_NOTICE, "detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
  1216. #endif
  1217. return res;
  1218. }
  1219. #endif
  1220. #ifdef BUSYDETECT
  1221. int ast_dsp_busydetect(struct ast_dsp *dsp)
  1222. {
  1223. int x;
  1224. int res = 0;
  1225. int max, min;
  1226. #if 0
  1227. if (dsp->busy_hits > 5);
  1228. return 0;
  1229. #endif
  1230. if (dsp->busymaybe) {
  1231. #if 0
  1232. printf("Maybe busy!\n");
  1233. #endif
  1234. dsp->busymaybe = 0;
  1235. min = 9999;
  1236. max = 0;
  1237. for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
  1238. #if 0
  1239. printf("Silence: %d, Noise: %d\n", dsp->historicsilence[x], dsp->historicnoise[x]);
  1240. #endif
  1241. if (dsp->historicsilence[x] < min)
  1242. min = dsp->historicsilence[x];
  1243. if (dsp->historicnoise[x] < min)
  1244. min = dsp->historicnoise[x];
  1245. if (dsp->historicsilence[x] > max)
  1246. max = dsp->historicsilence[x];
  1247. if (dsp->historicnoise[x] > max)
  1248. max = dsp->historicnoise[x];
  1249. }
  1250. if ((max - min < BUSY_THRESHOLD) && (max < BUSY_MAX) && (min > BUSY_MIN)) {
  1251. #if 0
  1252. printf("Busy!\n");
  1253. #endif
  1254. res = 1;
  1255. }
  1256. #if 0
  1257. printf("Min: %d, max: %d\n", min, max);
  1258. #endif
  1259. }
  1260. return res;
  1261. }
  1262. #endif
  1263. int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
  1264. {
  1265. short *s;
  1266. int len;
  1267. if (f->frametype != AST_FRAME_VOICE) {
  1268. ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
  1269. return 0;
  1270. }
  1271. if (f->subclass != AST_FORMAT_SLINEAR) {
  1272. ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
  1273. return 0;
  1274. }
  1275. s = f->data;
  1276. len = f->datalen/2;
  1277. return __ast_dsp_silence(dsp, s, len, totalsilence);
  1278. }
  1279. struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af, int needlock)
  1280. {
  1281. int silence;
  1282. int res;
  1283. int digit;
  1284. int x;
  1285. unsigned short *shortdata;
  1286. unsigned char *odata;
  1287. int len;
  1288. int writeback = 0;
  1289. #define FIX_INF(inf) do { \
  1290. if (writeback) { \
  1291. switch(inf->subclass) { \
  1292. case AST_FORMAT_SLINEAR: \
  1293. break; \
  1294. case AST_FORMAT_ULAW: \
  1295. for (x=0;x<len;x++) \
  1296. odata[x] = AST_LIN2MU(shortdata[x]); \
  1297. break; \
  1298. case AST_FORMAT_ALAW: \
  1299. for (x=0;x<len;x++) \
  1300. odata[x] = AST_LIN2A(shortdata[x]); \
  1301. break; \
  1302. } \
  1303. } \
  1304. } while(0)
  1305. if (!af)
  1306. return NULL;
  1307. if (af->frametype != AST_FRAME_VOICE)
  1308. return af;
  1309. odata = af->data;
  1310. len = af->datalen;
  1311. /* Make sure we have short data */
  1312. switch(af->subclass) {
  1313. case AST_FORMAT_SLINEAR:
  1314. shortdata = af->data;
  1315. len = af->datalen / 2;
  1316. break;
  1317. case AST_FORMAT_ULAW:
  1318. shortdata = alloca(af->datalen * 2);
  1319. if (!shortdata) {
  1320. ast_log(LOG_WARNING, "Unable to allocate stack space for data: %s\n", strerror(errno));
  1321. return af;
  1322. }
  1323. for (x=0;x<len;x++)
  1324. shortdata[x] = AST_MULAW(odata[x]);
  1325. break;
  1326. case AST_FORMAT_ALAW:
  1327. shortdata = alloca(af->datalen * 2);
  1328. if (!shortdata) {
  1329. ast_log(LOG_WARNING, "Unable to allocate stack space for data: %s\n", strerror(errno));
  1330. return af;
  1331. }
  1332. for (x=0;x<len;x++)
  1333. shortdata[x] = AST_ALAW(odata[x]);
  1334. break;
  1335. default:
  1336. ast_log(LOG_WARNING, "Unable to process inband DTMF on %d frames\n", af->subclass);
  1337. return af;
  1338. }
  1339. silence = __ast_dsp_silence(dsp, shortdata, len, NULL);
  1340. if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
  1341. memset(&dsp->f, 0, sizeof(dsp->f));
  1342. dsp->f.frametype = AST_FRAME_NULL;
  1343. return &dsp->f;
  1344. }
  1345. if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) {
  1346. chan->_softhangup |= AST_SOFTHANGUP_DEV;
  1347. memset(&dsp->f, 0, sizeof(dsp->f));
  1348. dsp->f.frametype = AST_FRAME_CONTROL;
  1349. dsp->f.subclass = AST_CONTROL_BUSY;
  1350. ast_log(LOG_DEBUG, "Requesting Hangup because the busy tone was detected on channel %s\n", chan->name);
  1351. return &dsp->f;
  1352. }
  1353. if ((dsp->features & DSP_FEATURE_DTMF_DETECT)) {
  1354. digit = __ast_dsp_digitdetect(dsp, shortdata, len, &writeback);
  1355. #if 0
  1356. if (digit)
  1357. printf("Performing digit detection returned %d, digitmode is %d\n", digit, dsp->digitmode);
  1358. #endif
  1359. if (dsp->digitmode & (DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX)) {
  1360. if (!dsp->thinkdigit) {
  1361. if (digit) {
  1362. /* Looks like we might have something. Request a conference mute for the moment */
  1363. memset(&dsp->f, 0, sizeof(dsp->f));
  1364. dsp->f.frametype = AST_FRAME_DTMF;
  1365. dsp->f.subclass = 'm';
  1366. dsp->thinkdigit = 'x';
  1367. FIX_INF(af);
  1368. if (chan)
  1369. ast_queue_frame(chan, af, needlock);
  1370. ast_frfree(af);
  1371. return &dsp->f;
  1372. }
  1373. } else {
  1374. if (digit) {
  1375. /* Thought we saw one last time. Pretty sure we really have now */
  1376. if (dsp->thinkdigit) {
  1377. if ((dsp->thinkdigit != 'x') && (dsp->thinkdigit != digit)) {
  1378. /* If we found a digit, and we're changing digits, go
  1379. ahead and send this one, but DON'T stop confmute because
  1380. we're detecting something else, too... */
  1381. memset(&dsp->f, 0, sizeof(dsp->f));
  1382. dsp->f.frametype = AST_FRAME_DTMF;
  1383. dsp->f.subclass = dsp->thinkdigit;
  1384. FIX_INF(af);
  1385. if (chan)
  1386. ast_queue_frame(chan, af, needlock);
  1387. ast_frfree(af);
  1388. }
  1389. dsp->thinkdigit = digit;
  1390. return &dsp->f;
  1391. }
  1392. dsp->thinkdigit = digit;
  1393. } else {
  1394. if (dsp->thinkdigit) {
  1395. memset(&dsp->f, 0, sizeof(dsp->f));
  1396. if (dsp->thinkdigit != 'x') {
  1397. /* If we found a digit, send it now */
  1398. dsp->f.frametype = AST_FRAME_DTMF;
  1399. dsp->f.subclass = dsp->thinkdigit;
  1400. dsp->thinkdigit = 0;
  1401. } else {
  1402. dsp->f.frametype = AST_FRAME_DTMF;
  1403. dsp->f.subclass = 'u';
  1404. dsp->thinkdigit = 0;
  1405. }
  1406. FIX_INF(af);
  1407. if (chan)
  1408. ast_queue_frame(chan, af, needlock);
  1409. ast_frfree(af);
  1410. return &dsp->f;
  1411. }
  1412. }
  1413. }
  1414. } else if (!digit) {
  1415. /* Only check when there is *not* a hit... */
  1416. if (dsp->digitmode & DSP_DIGITMODE_MF) {
  1417. if (dsp->td.mf.current_digits) {
  1418. memset(&dsp->f, 0, sizeof(dsp->f));
  1419. dsp->f.frametype = AST_FRAME_DTMF;
  1420. dsp->f.subclass = dsp->td.mf.digits[0];
  1421. memmove(dsp->td.mf.digits, dsp->td.mf.digits + 1, dsp->td.mf.current_digits);
  1422. dsp->td.mf.current_digits--;
  1423. FIX_INF(af);
  1424. if (chan)
  1425. ast_queue_frame(chan, af, needlock);
  1426. ast_frfree(af);
  1427. return &dsp->f;
  1428. }
  1429. } else {
  1430. if (dsp->td.dtmf.current_digits) {
  1431. memset(&dsp->f, 0, sizeof(dsp->f));
  1432. dsp->f.frametype = AST_FRAME_DTMF;
  1433. dsp->f.subclass = dsp->td.dtmf.digits[0];
  1434. memmove(dsp->td.dtmf.digits, dsp->td.dtmf.digits + 1, dsp->td.dtmf.current_digits);
  1435. dsp->td.dtmf.current_digits--;
  1436. FIX_INF(af);
  1437. if (chan)
  1438. ast_queue_frame(chan, af, needlock);
  1439. ast_frfree(af);
  1440. return &dsp->f;
  1441. }
  1442. }
  1443. }
  1444. }
  1445. if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
  1446. res = __ast_dsp_call_progress(dsp, shortdata, len);
  1447. memset(&dsp->f, 0, sizeof(dsp->f));
  1448. dsp->f.frametype = AST_FRAME_CONTROL;
  1449. if (res) {
  1450. switch(res) {
  1451. case AST_CONTROL_ANSWER:
  1452. case AST_CONTROL_BUSY:
  1453. case AST_CONTROL_RINGING:
  1454. case AST_CONTROL_CONGESTION:
  1455. dsp->f.subclass = res;
  1456. if (chan)
  1457. ast_queue_frame(chan, &dsp->f, needlock);
  1458. break;
  1459. default:
  1460. ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
  1461. }
  1462. }
  1463. }
  1464. FIX_INF(af);
  1465. return af;
  1466. }
  1467. struct ast_dsp *ast_dsp_new(void)
  1468. {
  1469. struct ast_dsp *dsp;
  1470. dsp = malloc(sizeof(struct ast_dsp));
  1471. if (dsp) {
  1472. memset(dsp, 0, sizeof(struct ast_dsp));
  1473. dsp->threshold = DEFAULT_THRESHOLD;
  1474. dsp->features = DSP_FEATURE_SILENCE_SUPPRESS;
  1475. dsp->busycount = DSP_HISTORY;
  1476. /* Initialize goertzels */
  1477. goertzel_init(&dsp->freqs[HZ_350], 350.0, GSAMP_SIZE);
  1478. goertzel_init(&dsp->freqs[HZ_440], 440.0, GSAMP_SIZE);
  1479. goertzel_init(&dsp->freqs[HZ_480], 480.0, GSAMP_SIZE);
  1480. goertzel_init(&dsp->freqs[HZ_620], 620.0, GSAMP_SIZE);
  1481. goertzel_init(&dsp->freqs[HZ_950], 950.0, GSAMP_SIZE);
  1482. goertzel_init(&dsp->freqs[HZ_1400], 1400.0, GSAMP_SIZE);
  1483. goertzel_init(&dsp->freqs[HZ_1800], 1800.0, GSAMP_SIZE);
  1484. /* Initialize DTMF detector */
  1485. ast_dtmf_detect_init(&dsp->td.dtmf);
  1486. }
  1487. return dsp;
  1488. }
  1489. void ast_dsp_set_features(struct ast_dsp *dsp, int features)
  1490. {
  1491. dsp->features = features;
  1492. }
  1493. void ast_dsp_free(struct ast_dsp *dsp)
  1494. {
  1495. free(dsp);
  1496. }
  1497. void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
  1498. {
  1499. dsp->threshold = threshold;
  1500. }
  1501. void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences)
  1502. {
  1503. if (cadences < 4)
  1504. cadences = 4;
  1505. if (cadences > DSP_HISTORY)
  1506. cadences = DSP_HISTORY;
  1507. dsp->busycount = cadences;
  1508. }
  1509. void ast_dsp_digitreset(struct ast_dsp *dsp)
  1510. {
  1511. int i;
  1512. dsp->thinkdigit = 0;
  1513. if (dsp->digitmode & DSP_DIGITMODE_MF) {
  1514. memset(dsp->td.mf.digits, 0, sizeof(dsp->td.mf.digits));
  1515. dsp->td.mf.current_digits = 0;
  1516. /* Reinitialise the detector for the next block */
  1517. for (i = 0; i < 6; i++) {
  1518. goertzel_reset(&dsp->td.mf.tone_out[i]);
  1519. #ifdef OLD_DSP_ROUTINES
  1520. goertzel_reset(&dsp->td.mf.tone_out2nd[i]);
  1521. #endif
  1522. }
  1523. #ifdef OLD_DSP_ROUTINES
  1524. dsp->td.mf.energy = 0.0;
  1525. dsp->td.mf.hit1 = dsp->td.mf.hit2 = dsp->td.mf.hit3 = dsp->td.mf.hit4 = dsp->td.mf.mhit = 0;
  1526. #else
  1527. dsp->td.mf.hits[4] = dsp->td.mf.hits[3] = dsp->td.mf.hits[2] = dsp->td.mf.hits[1] = dsp->td.mf.hits[0] = dsp->td.mf.mhit = 0;
  1528. #endif
  1529. dsp->td.mf.current_sample = 0;
  1530. } else {
  1531. memset(dsp->td.dtmf.digits, 0, sizeof(dsp->td.dtmf.digits));
  1532. dsp->td.dtmf.current_digits = 0;
  1533. /* Reinitialise the detector for the next block */
  1534. for (i = 0; i < 4; i++) {
  1535. goertzel_reset(&dsp->td.dtmf.row_out[i]);
  1536. goertzel_reset(&dsp->td.dtmf.col_out[i]);
  1537. #ifdef OLD_DSP_ROUTINES
  1538. goertzel_reset(&dsp->td.dtmf.row_out2nd[i]);
  1539. goertzel_reset(&dsp->td.dtmf.col_out2nd[i]);
  1540. #endif
  1541. }
  1542. #ifdef FAX_DETECT
  1543. goertzel_reset (&dsp->td.dtmf.fax_tone);
  1544. #endif
  1545. #ifdef OLD_DSP_ROUTINES
  1546. #ifdef FAX_DETECT
  1547. goertzel_reset (&dsp->td.dtmf.fax_tone2nd);
  1548. #endif
  1549. dsp->td.dtmf.hit1 = dsp->td.dtmf.hit2 = dsp->td.dtmf.hit3 = dsp->td.dtmf.hit4 = dsp->td.dtmf.mhit = 0;
  1550. #else
  1551. dsp->td.dtmf.hits[2] = dsp->td.dtmf.hits[1] = dsp->td.dtmf.hits[0] = dsp->td.dtmf.mhit = 0;
  1552. #endif
  1553. dsp->td.dtmf.energy = 0.0;
  1554. dsp->td.dtmf.current_sample = 0;
  1555. }
  1556. }
  1557. void ast_dsp_reset(struct ast_dsp *dsp)
  1558. {
  1559. int x;
  1560. dsp->totalsilence = 0;
  1561. dsp->gsamps = 0;
  1562. for (x=0;x<4;x++)
  1563. dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
  1564. memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
  1565. memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
  1566. }
  1567. int ast_dsp_digitmode(struct ast_dsp *dsp, int digitmode)
  1568. {
  1569. int new, old;
  1570. old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
  1571. new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
  1572. if (old != new) {
  1573. /* Must initialize structures if switching from MF to DTMF or vice-versa */
  1574. if (new & DSP_DIGITMODE_MF)
  1575. ast_mf_detect_init(&dsp->td.mf);
  1576. else
  1577. ast_dtmf_detect_init(&dsp->td.dtmf);
  1578. }
  1579. dsp->digitmode = digitmode;
  1580. return 0;
  1581. }