dsp_dtmf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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->dtmf.enable)
  48. return;
  49. if (!dsp->features.hfc_dtmf)
  50. hardware = 0;
  51. /* check for volume change */
  52. if (dsp->tx_volume) {
  53. if (dsp_debug & DEBUG_DSP_DTMF)
  54. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  55. "because tx_volume is changed\n",
  56. __func__, dsp->name);
  57. hardware = 0;
  58. }
  59. if (dsp->rx_volume) {
  60. if (dsp_debug & DEBUG_DSP_DTMF)
  61. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  62. "because rx_volume is changed\n",
  63. __func__, dsp->name);
  64. hardware = 0;
  65. }
  66. /* check if encryption is enabled */
  67. if (dsp->bf_enable) {
  68. if (dsp_debug & DEBUG_DSP_DTMF)
  69. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  70. "because encryption is enabled\n",
  71. __func__, dsp->name);
  72. hardware = 0;
  73. }
  74. /* check if pipeline exists */
  75. if (dsp->pipeline.inuse) {
  76. if (dsp_debug & DEBUG_DSP_DTMF)
  77. printk(KERN_DEBUG "%s dsp %s cannot do hardware DTMF, "
  78. "because pipeline exists.\n",
  79. __func__, dsp->name);
  80. hardware = 0;
  81. }
  82. dsp->dtmf.hardware = hardware;
  83. dsp->dtmf.software = !hardware;
  84. }
  85. /*************************************************************
  86. * calculate the coefficients of the given sample and decode *
  87. *************************************************************/
  88. /* the given sample is decoded. if the sample is not long enough for a
  89. * complete frame, the decoding is finished and continued with the next
  90. * call of this function.
  91. *
  92. * the algorithm is very good for detection with a minimum of errors. i
  93. * tested it allot. it even works with very short tones (40ms). the only
  94. * disadvantage is, that it doesn't work good with different volumes of both
  95. * tones. this will happen, if accoustically coupled dialers are used.
  96. * it sometimes detects tones during speech, which is normal for decoders.
  97. * use sequences to given commands during calls.
  98. *
  99. * dtmf - points to a structure of the current dtmf state
  100. * spl and len - the sample
  101. * fmt - 0 = alaw, 1 = ulaw, 2 = coefficients from HFC DTMF hw-decoder
  102. */
  103. u8
  104. *dsp_dtmf_goertzel_decode(struct dsp *dsp, u8 *data, int len, int fmt)
  105. {
  106. u8 what;
  107. int size;
  108. signed short *buf;
  109. s32 sk, sk1, sk2;
  110. int k, n, i;
  111. s32 *hfccoeff;
  112. s32 result[NCOEFF], tresh, treshl;
  113. int lowgroup, highgroup;
  114. s64 cos2pik_;
  115. dsp->dtmf.digits[0] = '\0';
  116. /* Note: The function will loop until the buffer has not enough samples
  117. * left to decode a full frame.
  118. */
  119. again:
  120. /* convert samples */
  121. size = dsp->dtmf.size;
  122. buf = dsp->dtmf.buffer;
  123. switch (fmt) {
  124. case 0: /* alaw */
  125. case 1: /* ulaw */
  126. while (size < DSP_DTMF_NPOINTS && len) {
  127. buf[size++] = dsp_audio_law_to_s32[*data++];
  128. len--;
  129. }
  130. break;
  131. case 2: /* HFC coefficients */
  132. default:
  133. if (len < 64) {
  134. if (len > 0)
  135. printk(KERN_ERR "%s: coefficients have invalid "
  136. "size. (is=%d < must=%d)\n",
  137. __func__, len, 64);
  138. return dsp->dtmf.digits;
  139. }
  140. hfccoeff = (s32 *)data;
  141. for (k = 0; k < NCOEFF; k++) {
  142. sk2 = (*hfccoeff++) >> 4;
  143. sk = (*hfccoeff++) >> 4;
  144. if (sk > 32767 || sk < -32767 || sk2 > 32767
  145. || sk2 < -32767)
  146. printk(KERN_WARNING
  147. "DTMF-Detection overflow\n");
  148. /* compute |X(k)|**2 */
  149. result[k] =
  150. (sk * sk) -
  151. (((cos2pik[k] * sk) >> 15) * sk2) +
  152. (sk2 * sk2);
  153. }
  154. data += 64;
  155. len -= 64;
  156. goto coefficients;
  157. break;
  158. }
  159. dsp->dtmf.size = size;
  160. if (size < DSP_DTMF_NPOINTS)
  161. return dsp->dtmf.digits;
  162. dsp->dtmf.size = 0;
  163. /* now we have a full buffer of signed long samples - we do goertzel */
  164. for (k = 0; k < NCOEFF; k++) {
  165. sk = 0;
  166. sk1 = 0;
  167. sk2 = 0;
  168. buf = dsp->dtmf.buffer;
  169. cos2pik_ = cos2pik[k];
  170. for (n = 0; n < DSP_DTMF_NPOINTS; n++) {
  171. sk = ((cos2pik_ * sk1) >> 15) - sk2 + (*buf++);
  172. sk2 = sk1;
  173. sk1 = sk;
  174. }
  175. sk >>= 8;
  176. sk2 >>= 8;
  177. if (sk > 32767 || sk < -32767 || sk2 > 32767 || sk2 < -32767)
  178. printk(KERN_WARNING "DTMF-Detection overflow\n");
  179. /* compute |X(k)|**2 */
  180. result[k] =
  181. (sk * sk) -
  182. (((cos2pik[k] * sk) >> 15) * sk2) +
  183. (sk2 * sk2);
  184. }
  185. /* our (squared) coefficients have been calculated, we need to process
  186. * them.
  187. */
  188. coefficients:
  189. tresh = 0;
  190. for (i = 0; i < NCOEFF; i++) {
  191. if (result[i] < 0)
  192. result[i] = 0;
  193. if (result[i] > dsp->dtmf.treshold) {
  194. if (result[i] > tresh)
  195. tresh = result[i];
  196. }
  197. }
  198. if (tresh == 0) {
  199. what = 0;
  200. goto storedigit;
  201. }
  202. if (dsp_debug & DEBUG_DSP_DTMFCOEFF) {
  203. s32 tresh_100 = tresh/100;
  204. if (tresh_100 == 0) {
  205. tresh_100 = 1;
  206. printk(KERN_DEBUG
  207. "tresh(%d) too small set tresh/100 to 1\n",
  208. tresh);
  209. }
  210. printk(KERN_DEBUG "a %3d %3d %3d %3d %3d %3d %3d %3d"
  211. " tr:%3d r %3d %3d %3d %3d %3d %3d %3d %3d\n",
  212. result[0] / 10000, result[1] / 10000, result[2] / 10000,
  213. result[3] / 10000, result[4] / 10000, result[5] / 10000,
  214. result[6] / 10000, result[7] / 10000, tresh / 10000,
  215. result[0] / (tresh_100), result[1] / (tresh_100),
  216. result[2] / (tresh_100), result[3] / (tresh_100),
  217. result[4] / (tresh_100), result[5] / (tresh_100),
  218. result[6] / (tresh_100), result[7] / (tresh_100));
  219. }
  220. /* calc digit (lowgroup/highgroup) */
  221. lowgroup = -1;
  222. highgroup = -1;
  223. treshl = tresh >> 3; /* tones which are not on, must be below 9 dB */
  224. tresh = tresh >> 2; /* touchtones must match within 6 dB */
  225. for (i = 0; i < NCOEFF; i++) {
  226. if (result[i] < treshl)
  227. continue; /* ignore */
  228. if (result[i] < tresh) {
  229. lowgroup = -1;
  230. highgroup = -1;
  231. break; /* noise in between */
  232. }
  233. /* good level found. This is allowed only one time per group */
  234. if (i < NCOEFF / 2) {
  235. /* lowgroup */
  236. if (lowgroup >= 0) {
  237. /* Bad. Another tone found. */
  238. lowgroup = -1;
  239. break;
  240. } else
  241. lowgroup = i;
  242. } else {
  243. /* higroup */
  244. if (highgroup >= 0) {
  245. /* Bad. Another tone found. */
  246. highgroup = -1;
  247. break;
  248. } else
  249. highgroup = i - (NCOEFF / 2);
  250. }
  251. }
  252. /* get digit or null */
  253. what = 0;
  254. if (lowgroup >= 0 && highgroup >= 0)
  255. what = dtmf_matrix[lowgroup][highgroup];
  256. storedigit:
  257. if (what && (dsp_debug & DEBUG_DSP_DTMF))
  258. printk(KERN_DEBUG "DTMF what: %c\n", what);
  259. if (dsp->dtmf.lastwhat != what)
  260. dsp->dtmf.count = 0;
  261. /* the tone (or no tone) must remain 3 times without change */
  262. if (dsp->dtmf.count == 2) {
  263. if (dsp->dtmf.lastdigit != what) {
  264. dsp->dtmf.lastdigit = what;
  265. if (what) {
  266. if (dsp_debug & DEBUG_DSP_DTMF)
  267. printk(KERN_DEBUG "DTMF digit: %c\n",
  268. what);
  269. if ((strlen(dsp->dtmf.digits) + 1)
  270. < sizeof(dsp->dtmf.digits)) {
  271. dsp->dtmf.digits[strlen(
  272. dsp->dtmf.digits) + 1] = '\0';
  273. dsp->dtmf.digits[strlen(
  274. dsp->dtmf.digits)] = what;
  275. }
  276. }
  277. }
  278. } else
  279. dsp->dtmf.count++;
  280. dsp->dtmf.lastwhat = what;
  281. goto again;
  282. }