dsp.c 48 KB

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