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.11  2007/07/19 08:10:18  csoutheren
00016  * Add detection of CNG
00017  *
00018  * Revision 1.10  2007/04/19 04:33:53  csoutheren
00019  * Fixed problems with pre-compiled headers
00020  *
00021  * Revision 1.9  2006/12/13 04:56:03  csoutheren
00022  * Applied 1613270 - fixed for dtmfEncoder
00023  * Thanks to Frederic Heem
00024  *
00025  * Revision 1.8  2006/10/25 08:18:20  rjongbloed
00026  * Major upgrade of tone generation subsystem.
00027  *
00028  * Revision 1.7  2005/11/30 12:47:37  csoutheren
00029  * Removed tabs, reformatted some code, and changed tags for Doxygen
00030  *
00031  * Revision 1.6  2004/11/11 07:34:50  csoutheren
00032  * Added #include <ptlib.h>
00033  *
00034  * Revision 1.5  2004/09/09 23:50:48  csoutheren
00035  * Fixed problem with duplicate definition of sinetab causing problems
00036  *
00037  * Revision 1.4  2004/09/09 05:23:37  dereksmithies
00038  * Add utility function to report on dtmf characters used.
00039  *
00040  * Revision 1.3  2004/09/09 04:00:00  csoutheren
00041  * Added DTMF encoding functions
00042  *
00043  * Revision 1.2  2002/09/16 01:08:59  robertj
00044  * Added #define so can select if #pragma interface/implementation is used on
00045  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00046  *
00047  * Revision 1.1  2002/01/23 11:43:26  rogerh
00048  * Add DTMF Decoder class. This can be passed PCM audio data
00049  * (at 16 bit, 8 KHz) and returns any DTMF codes detected.
00050  * Tested with NetMeeting sending DTMF over a G.711 stream.
00051  *
00052  */
00053  
00054 #ifndef _DTMF_H
00055 #define _DTMF_H
00056 
00057 #ifdef P_USE_PRAGMA
00058 #pragma interface
00059 #endif
00060 
00061 #ifndef _PTLIB_H
00062 #include <ptlib.h>
00063 #endif
00064 
00065 class PDTMFDecoder : public PObject
00066 {
00067   PCLASSINFO(PDTMFDecoder, PObject)
00068 
00069   public:
00070     PDTMFDecoder();
00071     PString Decode(const short * sampleData, PINDEX numSamples);
00072 
00073   protected:
00074     enum {
00075       NumTones = 9
00076     };
00077 
00078     // key lookup table (initialised once)
00079     char key[256];
00080 
00081     // frequency table (initialised once)
00082     int p1[NumTones];
00083 
00084     // variables to be retained on each cycle of the decode function
00085     int h[NumTones], k[NumTones], y[NumTones];
00086     int nn, so, ia;
00087 };
00088 
00089 
00133 class PTones : public PShortArray
00134 {
00135   PCLASSINFO(PTones, PShortArray)
00136 
00137   public:
00138     enum {
00139         MaxVolume = 100,
00140         SampleRate = 8000,
00141         MaxFrequency = (SampleRate/4),
00142         MinFrequency = 30,
00143         MinModulation = 5,
00144         SineScale = 1000
00145     };
00146 
00150     PTones(
00151         unsigned masterVolume = MaxVolume 
00152     );
00153 
00156     PTones(
00157       const PString & descriptor,    
00158       unsigned masterVolume = MaxVolume 
00159     );
00160 
00164     bool Generate(
00165       const PString & descriptor    
00166     );
00167 
00174     bool Generate(
00175       char operation,
00176       unsigned frequency1,        
00177       unsigned frequency2,        
00178       unsigned milliseconds,      
00179       unsigned volume = MaxVolume 
00180     );
00181 
00182   protected:
00183     bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
00184     bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
00185     bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
00186     bool Silence  (unsigned milliseconds);
00187 
00188     unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
00189 
00190     void AddSample(int sample, unsigned volume);
00191 
00192     unsigned masterVolume;
00193     char     lastOperation;
00194     unsigned lastFrequency1, lastFrequency2;
00195     int      angle1, angle2;
00196 };
00197 
00198 
00203 class PDTMFEncoder : public PTones
00204 {
00205   PCLASSINFO(PDTMFEncoder, PTones)
00206 
00207   public:
00208     enum { DefaultToneLen = 100 };
00209 
00213     PDTMFEncoder(
00214         const char * dtmf = NULL,      
00215         unsigned milliseconds = DefaultToneLen  
00216     );
00217 
00221     PDTMFEncoder(
00222         char key,      
00223         unsigned milliseconds = DefaultToneLen  
00224     );    
00225 
00229     void AddTone(
00230         const char * str,              
00231         unsigned milliseconds = DefaultToneLen  
00232     );
00233 
00237     void AddTone(
00238         char ch,                       
00239         unsigned milliseconds = DefaultToneLen  
00240     );
00241 
00246     void AddTone(
00247         double frequency1,                  // primary frequency
00248         double frequency2 = 0,              // secondary frequency, or 0 if no secondary frequency
00249         unsigned milliseconds = DefaultToneLen  // length of DTMF tone in milliseconds
00250     );
00251 
00256     void GenerateRingBackTone()
00257     {
00258       Generate("440+480:2-4");
00259     }
00260 
00265     void GenerateDialTone()
00266     {
00267       Generate("350+440:1");
00268     }
00269 
00274     void GenerateBusyTone()
00275     {
00276       Generate("480+620:0.5-0.5");
00277     }
00278 
00286     char DtmfChar(
00287         PINDEX i    
00288     );
00289     // Overiding GetSize() screws up the SetSize()
00290 };
00291 
00292 #endif /* _DTMF_H */

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1