dsp_dtmf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * DTMF decoder.
  3. *
  4. * Copyright by Andreas Eversberg (jolly@eversberg.eu)
  5. * based on different decoders such as ISDN4Linux
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/mISDNif.h>
  12. #include <linux/mISDNdsp.h>
  13. #include "core.h"
  14. #include "dsp.h"
  15. #define NCOEFF 8 /* number of frequencies to be analyzed */
  16. /* For DTMF recognition:
  17. * 2 * cos(2 * PI * k / N) precalculated for all k
  18. */
  19. static u64 cos2pik[NCOEFF] =
  20. {
  21. /* k << 15 (source: hfc-4s/8s documentation (www.colognechip.de)) */
  22. 55960, 53912, 51402, 48438, 38146, 32650, 26170, 18630
  23. };
  24. /* digit matrix */
  25. static char dtmf_matrix[4][4] =
  26. {
  27. {'1', '2', '3', 'A'},
  28. {'4', '5', '6', 'B'},
  29. {'7', '8', '9', 'C'},
  30. {'*', '0', '#', 'D'}
  31. };
  32. /* dtmf detection using goertzel algorithm
  33. * init function
  34. */
  35. void dsp_dtmf_goertzel_init(struct dsp *dsp)
  36. {
  37. dsp->dtmf.size = 0;
  38. dsp->dtmf.lastwhat = '\0';
  39. dsp->dtmf.lastdigit = '\0';
  40. dsp->dtmf.count = 0;
  41. }
  42. /* check for hardware or software features
  43. */
  44. void dsp_dtmf_hardware(struct dsp *dsp)
  45. {
  46. int hardware = 1;
  47. if (!dsp->features.hfc_dtmf)
  48. hardware = 0;
  49. /* check for volume change */
  50. if (dsp->tx_volume) {
  51. if (dsp_debug & DEBUG_DSP_DTMF)
  52. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  53. "because tx_volume is changed\n",
  54. __func__, dsp->name);
  55. hardware = 0;
  56. }
  57. if (dsp->rx_volume) {
  58. if (dsp_debug & DEBUG_DSP_DTMF)
  59. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  60. "because rx_volume is changed\n",
  61. __func__, dsp->name);
  62. hardware = 0;
  63. }
  64. /* check if encryption is enabled */
  65. if (dsp->bf_enable) {
  66. if (dsp_debug & DEBUG_DSP_DTMF)
  67. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  68. "because encryption is enabled\n",
  69. __func__, dsp->name);
  70. hardware = 0;
  71. }
  72. /* check if pipeline exists */
  73. if (dsp->pipeline.inuse) {
  74. if (dsp_debug & DEBUG_DSP_DTMF)
  75. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  76. "because pipeline exists.\n",
  77. __func__, dsp->name);
  78. hardware = 0;
  79. }
  80. dsp->dtmf.hardware = hardware;
  81. dsp->dtmf.software = !hardware;
  82. }
  83. /*************************************************************
  84. * calculate the coefficients of the given sample and decode *
  85. *************************************************************/
  86. /* the given sample is decoded. if the sample is not long enough for a
  87. * complete frame, the decoding is finished and continued with the next
  88. * call of this function.
  89. *
  90. * the algorithm is very good for detection with a minimum of errors. i
  91. * tested it allot. it even works with very short tones (40ms). the only
  92. * disadvantage is, that it doesn't work good with different volumes of both
  93. * tones. this will happen, if accoustically coupled dialers are used.
  94. * it sometimes detects tones during speach, which is normal for decoders.
  95. * use sequences to given commands during calls.
  96. *
  97. * dtmf - points to a structure of the current dtmf state
  98. * spl and len - the sample
  99. * fmt - 0 = alaw, 1 = ulaw, 2 = coefficients from HFC DTMF hw-decoder
  100. */
  101. u8
  102. *dsp_dtmf_goertzel_decode(struct dsp *dsp, u8 *data, int len, int fmt)
  103. {
  104. u8 what;
  105. int size;
  106. signed short *buf;
  107. s32 sk, sk1, sk2;
  108. int k, n, i;
  109. s32 *hfccoeff;
  110. s32 result[NCOEFF], tresh, treshl;
  111. int lowgroup, highgroup;
  112. s64 cos2pik_;
  113. dsp->dtmf.digits[0] = '\0';
  114. /* Note: The function will loop until the buffer has not enough samples
  115. * left to decode a full frame.
  116. */
  117. again:
  118. /* convert samples */
  119. size = dsp->dtmf.size;
  120. buf = dsp->dtmf.buffer;
  121. switch (fmt) {
  122. case 0: /* alaw */
  123. case 1: /* ulaw */
  124. while (size < DSP_DTMF_NPOINTS && len) {
  125. buf[size++] = dsp_audio_law_to_s32[*data++];
  126. len--;
  127. }
  128. break;
  129. case 2: /* HFC coefficients */
  130. default:
  131. if (len < 64) {
  132. if (len > 0)
  133. printk(KERN_ERR "%s: coefficients have invalid "
  134. "size. (is=%d < must=%d)\n",
  135. __func__, len, 64);
  136. return dsp->dtmf.digits;
  137. }
  138. hfccoeff = (s32 *)data;
  139. for (k = 0; k < NCOEFF; k++) {
  140. sk2 = (*hfccoeff++)>>4;
  141. sk = (*hfccoeff++)>>4;
  142. if (sk > 32767 || sk < -32767 || sk2 > 32767
  143. || sk2 < -32767)
  144. printk(KERN_WARNING
  145. "DTMF-Detection overflow\n");
  146. /* compute |X(k)|**2 */
  147. result[k] =
  148. (sk * sk) -
  149. (((cos2pik[k] * sk) >> 15) * sk2) +
  150. (sk2 * sk2);
  151. }
  152. data += 64;
  153. len -= 64;
  154. goto coefficients;
  155. break;
  156. }
  157. dsp->dtmf.size = size;
  158. if (size < DSP_DTMF_NPOINTS)
  159. return dsp->dtmf.digits;
  160. dsp->dtmf.size = 0;
  161. /* now we have a full buffer of signed long samples - we do goertzel */
  162. for (k = 0; k < NCOEFF; k++) {
  163. sk = 0;
  164. sk1 = 0;
  165. sk2 = 0;
  166. buf = dsp->dtmf.buffer;
  167. cos2pik_ = cos2pik[k];
  168. for (n = 0; n < DSP_DTMF_NPOINTS; n++) {
  169. sk = ((cos2pik_*sk1)>>15) - sk2 + (*buf++);
  170. sk2 = sk1;
  171. sk1 = sk;
  172. }
  173. sk >>= 8;
  174. sk2 >>= 8;
  175. if (sk > 32767 || sk < -32767 || sk2 > 32767 || sk2 < -32767)
  176. printk(KERN_WARNING "DTMF-Detection overflow\n");
  177. /* compute |X(k)|**2 */
  178. result[k] =
  179. (sk * sk) -
  180. (((cos2pik[k] * sk) >> 15) * sk2) +
  181. (sk2 * sk2);
  182. }
  183. /* our (squared) coefficients have been calculated, we need to process
  184. * them.
  185. */
  186. coefficients:
  187. tresh = 0;
  188. for (i = 0; i < NCOEFF; i++) {
  189. if (result[i] < 0)
  190. result[i] = 0;
  191. if (result[i] > dsp->dtmf.treshold) {
  192. if (result[i] > tresh)
  193. tresh = result[i];
  194. }
  195. }
  196. if (tresh == 0) {
  197. what = 0;
  198. goto storedigit;
  199. }
  200. if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
  201. printk(KERN_DEBUG "a %3d %3d %3d %3d %3d %3d %3d %3d"
  202. " tr:%3d r %3d %3d %3d %3d %3d %3d %3d %3d\n",
  203. result[0]/10000, result[1]/10000, result[2]/10000,
  204. result[3]/10000, result[4]/10000, result[5]/10000,
  205. result[6]/10000, result[7]/10000, tresh/10000,
  206. result[0]/(tresh/100), result[1]/(tresh/100),
  207. result[2]/(tresh/100), result[3]/(tresh/100),
  208. result[4]/(tresh/100), result[5]/(tresh/100),
  209. result[6]/(tresh/100), result[7]/(tresh/100));
  210. /* calc digit (lowgroup/highgroup) */
  211. lowgroup = -1;
  212. highgroup = -1;
  213. treshl = tresh >> 3; /* tones which are not on, must be below 9 dB */
  214. tresh = tresh >> 2; /* touchtones must match within 6 dB */
  215. for (i = 0; i < NCOEFF; i++) {
  216. if (result[i] < treshl)
  217. continue; /* ignore */
  218. if (result[i] < tresh) {
  219. lowgroup = -1;
  220. highgroup = -1;
  221. break; /* noise inbetween */
  222. }
  223. /* good level found. This is allowed only one time per group */
  224. if (i < NCOEFF/2) {
  225. /* lowgroup */
  226. if (lowgroup >= 0) {
  227. /* Bad. Another tone found. */
  228. lowgroup = -1;
  229. break;
  230. } else
  231. lowgroup = i;
  232. } else {
  233. /* higroup */
  234. if (highgroup >= 0) {
  235. /* Bad. Another tone found. */
  236. highgroup = -1;
  237. break;
  238. } else
  239. highgroup = i-(NCOEFF/2);
  240. }
  241. }
  242. /* get digit or null */
  243. what = 0;
  244. if (lowgroup >= 0 && highgroup >= 0)
  245. what = dtmf_matrix[lowgroup][highgroup];
  246. storedigit:
  247. if (what && (dsp_debug & DEBUG_DSP_DTMF))
  248. printk(KERN_DEBUG "DTMF what: %c\n", what);
  249. if (dsp->dtmf.lastwhat != what)
  250. dsp->dtmf.count = 0;
  251. /* the tone (or no tone) must remain 3 times without change */
  252. if (dsp->dtmf.count == 2) {
  253. if (dsp->dtmf.lastdigit != what) {
  254. dsp->dtmf.lastdigit = what;
  255. if (what) {
  256. if (dsp_debug & DEBUG_DSP_DTMF)
  257. printk(KERN_DEBUG "DTMF digit: %c\n",
  258. what);
  259. if ((strlen(dsp->dtmf.digits)+1)
  260. < sizeof(dsp->dtmf.digits)) {
  261. dsp->dtmf.digits[strlen(
  262. dsp->dtmf.digits)+1] = '\0';
  263. dsp->dtmf.digits[strlen(
  264. dsp->dtmf.digits)] = what;
  265. }
  266. }
  267. }
  268. } else
  269. dsp->dtmf.count++;
  270. dsp->dtmf.lastwhat = what;
  271. goto again;
  272. }