dsp.c 50 KB

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