4 * Copyright by Andreas Eversberg (jolly@eversberg.eu)
5 * based on different decoders such as ISDN4Linux
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/mISDNif.h>
13 #include <linux/mISDNdsp.h>
17 #define NCOEFF 8 /* number of frequencies to be analyzed */
19 /* For DTMF recognition:
20 * 2 * cos(2 * PI * k / N) precalculated for all k
22 static u64 cos2pik
[NCOEFF
] =
24 /* k << 15 (source: hfc-4s/8s documentation (www.colognechip.de)) */
25 55960, 53912, 51402, 48438, 38146, 32650, 26170, 18630
29 static char dtmf_matrix
[4][4] =
37 /* dtmf detection using goertzel algorithm
40 void dsp_dtmf_goertzel_init(struct dsp
*dsp
)
43 dsp
->dtmf
.lastwhat
= '\0';
44 dsp
->dtmf
.lastdigit
= '\0';
48 /* check for hardware or software features
50 void dsp_dtmf_hardware(struct dsp
*dsp
)
54 if (!dsp
->dtmf
.enable
)
57 if (!dsp
->features
.hfc_dtmf
)
60 /* check for volume change */
62 if (dsp_debug
& DEBUG_DSP_DTMF
)
63 printk(KERN_DEBUG
"%s dsp %s cannot do hardware DTMF, "
64 "because tx_volume is changed\n",
69 if (dsp_debug
& DEBUG_DSP_DTMF
)
70 printk(KERN_DEBUG
"%s dsp %s cannot do hardware DTMF, "
71 "because rx_volume is changed\n",
75 /* check if encryption is enabled */
77 if (dsp_debug
& DEBUG_DSP_DTMF
)
78 printk(KERN_DEBUG
"%s dsp %s cannot do hardware DTMF, "
79 "because encryption is enabled\n",
83 /* check if pipeline exists */
84 if (dsp
->pipeline
.inuse
) {
85 if (dsp_debug
& DEBUG_DSP_DTMF
)
86 printk(KERN_DEBUG
"%s dsp %s cannot do hardware DTMF, "
87 "because pipeline exists.\n",
92 dsp
->dtmf
.hardware
= hardware
;
93 dsp
->dtmf
.software
= !hardware
;
97 /*************************************************************
98 * calculate the coefficients of the given sample and decode *
99 *************************************************************/
101 /* the given sample is decoded. if the sample is not long enough for a
102 * complete frame, the decoding is finished and continued with the next
103 * call of this function.
105 * the algorithm is very good for detection with a minimum of errors. i
106 * tested it allot. it even works with very short tones (40ms). the only
107 * disadvantage is, that it doesn't work good with different volumes of both
108 * tones. this will happen, if accoustically coupled dialers are used.
109 * it sometimes detects tones during speech, which is normal for decoders.
110 * use sequences to given commands during calls.
112 * dtmf - points to a structure of the current dtmf state
113 * spl and len - the sample
114 * fmt - 0 = alaw, 1 = ulaw, 2 = coefficients from HFC DTMF hw-decoder
118 *dsp_dtmf_goertzel_decode(struct dsp
*dsp
, u8
*data
, int len
, int fmt
)
126 s32 result
[NCOEFF
], tresh
, treshl
;
127 int lowgroup
, highgroup
;
130 dsp
->dtmf
.digits
[0] = '\0';
132 /* Note: The function will loop until the buffer has not enough samples
133 * left to decode a full frame.
136 /* convert samples */
137 size
= dsp
->dtmf
.size
;
138 buf
= dsp
->dtmf
.buffer
;
142 while (size
< DSP_DTMF_NPOINTS
&& len
) {
143 buf
[size
++] = dsp_audio_law_to_s32
[*data
++];
148 case 2: /* HFC coefficients */
152 printk(KERN_ERR
"%s: coefficients have invalid "
153 "size. (is=%d < must=%d)\n",
155 return dsp
->dtmf
.digits
;
157 hfccoeff
= (s32
*)data
;
158 for (k
= 0; k
< NCOEFF
; k
++) {
159 sk2
= (*hfccoeff
++) >> 4;
160 sk
= (*hfccoeff
++) >> 4;
161 if (sk
> 32767 || sk
< -32767 || sk2
> 32767
164 "DTMF-Detection overflow\n");
165 /* compute |X(k)|**2 */
168 (((cos2pik
[k
] * sk
) >> 15) * sk2
) +
176 dsp
->dtmf
.size
= size
;
178 if (size
< DSP_DTMF_NPOINTS
)
179 return dsp
->dtmf
.digits
;
183 /* now we have a full buffer of signed long samples - we do goertzel */
184 for (k
= 0; k
< NCOEFF
; k
++) {
188 buf
= dsp
->dtmf
.buffer
;
189 cos2pik_
= cos2pik
[k
];
190 for (n
= 0; n
< DSP_DTMF_NPOINTS
; n
++) {
191 sk
= ((cos2pik_
* sk1
) >> 15) - sk2
+ (*buf
++);
197 if (sk
> 32767 || sk
< -32767 || sk2
> 32767 || sk2
< -32767)
198 printk(KERN_WARNING
"DTMF-Detection overflow\n");
199 /* compute |X(k)|**2 */
202 (((cos2pik
[k
] * sk
) >> 15) * sk2
) +
206 /* our (squared) coefficients have been calculated, we need to process
211 for (i
= 0; i
< NCOEFF
; i
++) {
214 if (result
[i
] > dsp
->dtmf
.treshold
) {
215 if (result
[i
] > tresh
)
225 if (dsp_debug
& DEBUG_DSP_DTMFCOEFF
) {
226 s32 tresh_100
= tresh
/100;
228 if (tresh_100
== 0) {
231 "tresh(%d) too small set tresh/100 to 1\n",
234 printk(KERN_DEBUG
"a %3d %3d %3d %3d %3d %3d %3d %3d"
235 " tr:%3d r %3d %3d %3d %3d %3d %3d %3d %3d\n",
236 result
[0] / 10000, result
[1] / 10000, result
[2] / 10000,
237 result
[3] / 10000, result
[4] / 10000, result
[5] / 10000,
238 result
[6] / 10000, result
[7] / 10000, tresh
/ 10000,
239 result
[0] / (tresh_100
), result
[1] / (tresh_100
),
240 result
[2] / (tresh_100
), result
[3] / (tresh_100
),
241 result
[4] / (tresh_100
), result
[5] / (tresh_100
),
242 result
[6] / (tresh_100
), result
[7] / (tresh_100
));
245 /* calc digit (lowgroup/highgroup) */
248 treshl
= tresh
>> 3; /* tones which are not on, must be below 9 dB */
249 tresh
= tresh
>> 2; /* touchtones must match within 6 dB */
250 for (i
= 0; i
< NCOEFF
; i
++) {
251 if (result
[i
] < treshl
)
252 continue; /* ignore */
253 if (result
[i
] < tresh
) {
256 break; /* noise in between */
258 /* good level found. This is allowed only one time per group */
259 if (i
< NCOEFF
/ 2) {
262 /* Bad. Another tone found. */
269 if (highgroup
>= 0) {
270 /* Bad. Another tone found. */
274 highgroup
= i
- (NCOEFF
/ 2);
278 /* get digit or null */
280 if (lowgroup
>= 0 && highgroup
>= 0)
281 what
= dtmf_matrix
[lowgroup
][highgroup
];
284 if (what
&& (dsp_debug
& DEBUG_DSP_DTMF
))
285 printk(KERN_DEBUG
"DTMF what: %c\n", what
);
287 if (dsp
->dtmf
.lastwhat
!= what
)
290 /* the tone (or no tone) must remain 3 times without change */
291 if (dsp
->dtmf
.count
== 2) {
292 if (dsp
->dtmf
.lastdigit
!= what
) {
293 dsp
->dtmf
.lastdigit
= what
;
295 if (dsp_debug
& DEBUG_DSP_DTMF
)
296 printk(KERN_DEBUG
"DTMF digit: %c\n",
298 if ((strlen(dsp
->dtmf
.digits
) + 1)
299 < sizeof(dsp
->dtmf
.digits
)) {
300 dsp
->dtmf
.digits
[strlen(
301 dsp
->dtmf
.digits
) + 1] = '\0';
302 dsp
->dtmf
.digits
[strlen(
303 dsp
->dtmf
.digits
)] = what
;
310 dsp
->dtmf
.lastwhat
= what
;