看看这个。不能用不怪我啊,我也没用过
头文件:
#ifndef _DTMF_H
#define _DTMF_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
class PDTMFDecoder : public PObject
{
PCLASSINFO(PDTMFDecoder, PObject)
public:
PDTMFDecoder();
PString Decode(const void *buf, PINDEX bytes);
protected:
// key lookup table (initialised once)
char key[256];
// frequency table (initialised once)
int p1[8];
// variables to be retained on each cycle of the decode function
int h[8], k[8], y[8];
int nn, so, ia;
};
#endif /* _DTMF_H */
实现文件:
#ifdef __GNUC__
#pragma implementation "dtmf.h"
#endif
/* Integer math scaling factor */
#define FSC (1<<12)
/* This is the Q of the filter (pole radius) */
#define POLRAD .99
#define P2 ((int)(POLRAD*POLRAD*FSC))
PDTMFDecoder:
DTMFDecoder() {
// Initialise the class
int i,kk;
for (kk = 0;
kk < 8;
kk++) {
y[kk] = h[kk] = k[kk] = 0;
}
nn = 0;
ia = 0;
so = 0;
for (i = 0;
i < 256;
i++) {
key
= '?';
}
/* We encode the tones in 8 bits, translate those to symbol */
key[0x00] = '/0';
key[0x11] = '1';
key[0x12] = '4';
key[0x14] = '7';
key[0x18] = '*';
key[0x21] = '2';
key[0x22] = '5';
key[0x24] = '8';
key[0x28] = '0';
key[0x41] = '3';
key[0x42] = '6';
key[0x44] = '9';
key[0x48] = '#';
key[0x81] = 'A';
key[0x82] = 'B';
key[0x84] = 'C';
key[0x88] = 'D';
/* The frequencies we're trying to detect */
/* These are precalculated to save processing power */
/* static int dtmf[8] = {697, 770, 852, 941, 1209, 1336, 1477, 1633};
*/
/* p1[kk] = (-cos(2 * 3.141592 * dtmf[kk] / 8000.0) * FSC) */
p1[0] = -3497;
p1[1] = -3369;
p1[2] = -3212;
p1[3] = -3027;
p1[4] = -2384;
p1[5] = -2040;
p1[6] = -1635;
p1[7] = -1164;
}
PString PDTMFDecoder:ecode(const void *buf, PINDEX bytes) {
int x;
int s, kk;
int c, d, f, n;
short *buffer = (short *)buf;
PINDEX numSamples = bytes >> 1;
PString keyString;
PINDEX pos;
for (pos = 0;
pos < numSamples;
pos++) {
/* Read (and scale) the next 16 bit sample */
x = ((int)(*buffer++)) / (32768/FSC);
/* Input amplitude */
if (x > 0)
ia += (x - ia) / 128;
else
ia += (-x - ia) / 128;
/* For each tone */
s = 0;
for(kk = 0;
kk < 8;
kk++) {
/* Turn the crank */
c = (P2 * (x - k[kk])) / FSC;
d = x + c;
f = (p1[kk] * (d - h[kk])) / FSC;
n = x - k[kk] - c;
k[kk] = h[kk] + f;
h[kk] = f + d;
/* Detect and Average */
if (n > 0)
y[kk] += (n - y[kk]) / 64;
else
y[kk] += (-n - y[kk]) / 64;
/* Threshold */
if (y[kk] > FSC/10 &&
y[kk] > ia)
s |= 1 << kk;
}
/* Hysteresis and noise supressor */
if (s != so) {
nn = 0;
so = s;
} else
if ((nn++ == 520) &&
(key != '?')) {
PTRACE(1,"Got 16bit PCM DTMF " << key << endl);
keyString = keyString + key;
}
}
return keyString;
}