dtmf.h

Go to the documentation of this file.
00001 /*
00002  * ----------------------------------------------------------------------------
00003  * "THE BEER-WARE LICENSE" (Revision 42):
00004  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
00005  * can do whatever you want with this stuff. If we meet some day, and you think
00006  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
00007  * ----------------------------------------------------------------------------
00008  *
00009  * Extract DTMF signals from 16 bit PCM audio
00010  *
00011  * Originally written by Poul-Henning Kamp <phk@freebsd.org>
00012  * Made into a C++ class by Roger Hardiman <roger@freebsd.org>, January 2002
00013  *
00014  * $Log: dtmf.h,v $
00015  * Revision 1.7  2005/11/30 12:47:37  csoutheren
00016  * Removed tabs, reformatted some code, and changed tags for Doxygen
00017  *
00018  * Revision 1.6  2004/11/11 07:34:50  csoutheren
00019  * Added #include <ptlib.h>
00020  *
00021  * Revision 1.5  2004/09/09 23:50:48  csoutheren
00022  * Fixed problem with duplicate definition of sinetab causing problems
00023  *
00024  * Revision 1.4  2004/09/09 05:23:37  dereksmithies
00025  * Add utility function to report on dtmf characters used.
00026  *
00027  * Revision 1.3  2004/09/09 04:00:00  csoutheren
00028  * Added DTMF encoding functions
00029  *
00030  * Revision 1.2  2002/09/16 01:08:59  robertj
00031  * Added #define so can select if #pragma interface/implementation is used on
00032  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00033  *
00034  * Revision 1.1  2002/01/23 11:43:26  rogerh
00035  * Add DTMF Decoder class. This can be passed PCM audio data
00036  * (at 16 bit, 8 KHz) and returns any DTMF codes detected.
00037  * Tested with NetMeeting sending DTMF over a G.711 stream.
00038  *
00039  */
00040  
00041 #ifndef _DTMF_H
00042 #define _DTMF_H
00043 
00044 #ifdef P_USE_PRAGMA
00045 #pragma interface
00046 #endif
00047 
00048 #include <ptlib.h>
00049 
00050 class PDTMFDecoder : public PObject
00051 {
00052   PCLASSINFO(PDTMFDecoder, PObject)
00053 
00054   public:
00055     PDTMFDecoder();
00056     PString Decode(const void *buf, PINDEX bytes);
00057 
00058   protected:
00059     // key lookup table (initialised once)
00060     char key[256];
00061 
00062     // frequency table (initialised once)
00063     int p1[8];
00064 
00065     // variables to be retained on each cycle of the decode function
00066     int h[8], k[8], y[8];
00067     int nn, so, ia;
00068 };
00069 
00075 class PDTMFEncoder : public PBYTEArray
00076 {
00077   PCLASSINFO(PDTMFEncoder, PBYTEArray)
00078 
00079   public:
00080     enum { DefaultToneLen = 100 };
00081 
00085     inline PDTMFEncoder(
00086         const char * dtmf = NULL,      
00087         unsigned len = DefaultToneLen  
00088     )
00089     { if (dtmf != NULL) AddTone(dtmf, len); }
00090 
00091 
00095     void AddTone(
00096         char ch,                       
00097         unsigned len = DefaultToneLen  
00098     );
00099 
00103     void AddTone(
00104         const PString & str,           
00105         unsigned len = DefaultToneLen  
00106     );
00107 
00112     void AddTone(
00113         double freq1,                  // primary frequency
00114         double freq2 = 0,              // secondary frequency, or 0 if no secondary frequency
00115         unsigned len = DefaultToneLen  // length of DTMF tone in milliseconds
00116     );
00117 
00122     void GenerateRingBackTone()
00123     {
00124       AddTone(440, 480, 2000);
00125       AddTone(0,   0,   4000);
00126     }
00127 
00132     void GenerateDialTone()
00133     {
00134       AddTone(350, 440, 1000);
00135     }
00136 
00141     void GenerateBusyTone()
00142     {
00143       AddTone(480, 620, 500);
00144       AddTone(0,   0,   500);
00145     }
00146 
00154     char DtmfChar(
00155         PINDEX i    
00156     );
00157 
00158   protected:
00159     static PMutex & GetMutex();
00160     static BOOL sineTabInit;
00161     static void MakeSineTable();
00162     static inline double sine(unsigned int ptr)
00163     { return sinetab[ptr >> (32-11)]; }
00164     static double sinetab[1 << 11];
00165 };
00166 
00167 #endif /* _DTMF_H */

Generated on Fri Sep 21 14:40:11 2007 for PWLib by  doxygen 1.5.3