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  * $Revision: 21788 $
00015  * $Author: rjongbloed $
00016  * $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
00017  */
00018  
00019 #ifndef PTLIB_DTMF_H
00020 #define PTLIB_DTMF_H
00021 
00022 #ifdef P_USE_PRAGMA
00023 #pragma interface
00024 #endif
00025 
00026 
00027 class PDTMFDecoder : public PObject
00028 {
00029   PCLASSINFO(PDTMFDecoder, PObject)
00030 
00031   public:
00032     PDTMFDecoder();
00033     PString Decode(const short * sampleData, PINDEX numSamples, unsigned mult = 1, unsigned div = 1);
00034 
00035   protected:
00036     enum {
00037       NumTones = 10
00038     };
00039 
00040     // key lookup table (initialised once)
00041     char key[256];
00042 
00043     // frequency table (initialised once)
00044     int p1[NumTones];
00045 
00046     // variables to be retained on each cycle of the decode function
00047     int h[NumTones], k[NumTones], y[NumTones];
00048     int nn, so, ia;
00049 };
00050 
00051 
00095 class PTones : public PShortArray
00096 {
00097   PCLASSINFO(PTones, PShortArray)
00098 
00099   public:
00100     enum {
00101         MaxVolume = 100,
00102         SampleRate = 8000,
00103         MaxFrequency = (SampleRate/4),
00104         MinFrequency = 30,
00105         MinModulation = 5,
00106         SineScale = 1000
00107     };
00108 
00112     PTones(
00113         unsigned masterVolume = MaxVolume 
00114     );
00115 
00118     PTones(
00119       const PString & descriptor,    
00120       unsigned masterVolume = MaxVolume 
00121     );
00122 
00126     bool Generate(
00127       const PString & descriptor    
00128     );
00129 
00136     bool Generate(
00137       char operation,
00138       unsigned frequency1,        
00139       unsigned frequency2,        
00140       unsigned milliseconds,      
00141       unsigned volume = MaxVolume 
00142     );
00143 
00144   protected:
00145     bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
00146     bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
00147     bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
00148     bool Silence  (unsigned milliseconds);
00149 
00150     unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
00151 
00152     void AddSample(int sample, unsigned volume);
00153 
00154     unsigned masterVolume;
00155     char     lastOperation;
00156     unsigned lastFrequency1, lastFrequency2;
00157     int      angle1, angle2;
00158 };
00159 
00160 
00165 class PDTMFEncoder : public PTones
00166 {
00167   PCLASSINFO(PDTMFEncoder, PTones)
00168 
00169   public:
00170     enum { DefaultToneLen = 100 };
00171 
00175     PDTMFEncoder(
00176         const char * dtmf = NULL,      
00177         unsigned milliseconds = DefaultToneLen  
00178     );
00179 
00183     PDTMFEncoder(
00184         char key,      
00185         unsigned milliseconds = DefaultToneLen  
00186     );    
00187 
00191     void AddTone(
00192         const char * str,              
00193         unsigned milliseconds = DefaultToneLen  
00194     );
00195 
00199     void AddTone(
00200         char ch,                       
00201         unsigned milliseconds = DefaultToneLen  
00202     );
00203 
00208     void AddTone(
00209         double frequency1,                  // primary frequency
00210         double frequency2 = 0,              // secondary frequency, or 0 if no secondary frequency
00211         unsigned milliseconds = DefaultToneLen  // length of DTMF tone in milliseconds
00212     );
00213 
00218     void GenerateRingBackTone()
00219     {
00220       Generate("440+480:2-4");
00221     }
00222 
00227     void GenerateDialTone()
00228     {
00229       Generate("350+440:1");
00230     }
00231 
00236     void GenerateBusyTone()
00237     {
00238       Generate("480+620:0.5-0.5");
00239     }
00240 
00248     char DtmfChar(
00249         PINDEX i    
00250     );
00251     // Overiding GetSize() screws up the SetSize()
00252 };
00253 
00254 
00255 #endif // PTLIB_DTMF_H
00256 
00257 
00258 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:47 2010 for PTLib by  doxygen 1.4.7