2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * Extract DTMF signals from 16 bit PCM audio
11 * Originally written by Poul-Henning Kamp <phk@freebsd.org>
12 * Made into a C++ class by Roger Hardiman <roger@freebsd.org>, January 2002
15 * Revision 1.7 2003/03/17 07:39:25 robertj
16 * Fixed possible invalid value causing DTMF detector to crash.
18 * Revision 1.6 2002/02/20 02:59:34 yurik
19 * Added end of line to trace statement
21 * Revision 1.5 2002/02/12 10:21:56 rogerh
22 * Stop sending '?' when a bad DTMF tone is detected.
24 * Revision 1.4 2002/01/24 11:14:45 rogerh
25 * Back out robert's change. It did not work (no sign extending)
26 * and replace it with a better solution which should be happy on both big
27 * endian and little endian systems.
29 * Revision 1.3 2002/01/24 10:40:17 rogerh
36 #pragma implementation "dtmf.h"
40 #include <ptclib/dtmf.h>
43 /* Integer math scaling factor */
46 /* This is the Q of the filter (pole radius) */
49 #define P2 ((int)(POLRAD*POLRAD*FSC))
53 PDTMFDecoder::PDTMFDecoder()
55 // Initialise the class
57 for (kk
= 0; kk
< 8; kk
++) {
58 y
[kk
] = h
[kk
] = k
[kk
] = 0;
65 for (i
= 0; i
< 256; i
++) {
69 /* We encode the tones in 8 bits, translate those to symbol */
70 key
[0x11] = '1'; key
[0x12] = '4'; key
[0x14] = '7'; key
[0x18] = '*';
71 key
[0x21] = '2'; key
[0x22] = '5'; key
[0x24] = '8'; key
[0x28] = '0';
72 key
[0x41] = '3'; key
[0x42] = '6'; key
[0x44] = '9'; key
[0x48] = '#';
73 key
[0x81] = 'A'; key
[0x82] = 'B'; key
[0x84] = 'C'; key
[0x88] = 'D';
75 /* The frequencies we're trying to detect */
76 /* These are precalculated to save processing power */
77 /* static int dtmf[8] = {697, 770, 852, 941, 1209, 1336, 1477, 1633}; */
78 /* p1[kk] = (-cos(2 * 3.141592 * dtmf[kk] / 8000.0) * FSC) */
79 p1
[0] = -3497; p1
[1] = -3369; p1
[2] = -3212; p1
[3] = -3027;
80 p1
[4] = -2384; p1
[5] = -2040; p1
[6] = -1635; p1
[7] = -1164;
84 PString
PDTMFDecoder::Decode(const void *buf
, PINDEX bytes
)
89 short *buffer
= (short *)buf
;
91 PINDEX numSamples
= bytes
>> 1;
96 for (pos
= 0; pos
< numSamples
; pos
++) {
98 /* Read (and scale) the next 16 bit sample */
99 x
= ((int)(*buffer
++)) / (32768/FSC
);
101 /* Input amplitude */
103 ia
+= (x
- ia
) / 128;
105 ia
+= (-x
- ia
) / 128;
109 for(kk
= 0; kk
< 8; kk
++) {
112 c
= (P2
* (x
- k
[kk
])) / FSC
;
114 f
= (p1
[kk
] * (d
- h
[kk
])) / FSC
;
119 /* Detect and Average */
121 y
[kk
] += (n
- y
[kk
]) / 64;
123 y
[kk
] += (-n
- y
[kk
]) / 64;
126 if (y
[kk
] > FSC
/10 && y
[kk
] > ia
)
130 /* Hysteresis and noise supressor */
134 } else if (nn
++ == 520 && s
< 256 && key
[s
] != '?') {
135 PTRACE(3,"DTMF\tDetected '" << key
[s
] << "' in PCM-16 stream");