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: 24177 $
00015  * $Author: rjongbloed $
00016  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
00017  */
00018  
00019 #ifndef PTLIB_DTMF_H
00020 #define PTLIB_DTMF_H
00021 
00022 #if P_DTMF
00023 
00024 #ifdef P_USE_PRAGMA
00025 #pragma interface
00026 #endif
00027 
00028 
00029 class PDTMFDecoder : public PObject
00030 {
00031   PCLASSINFO(PDTMFDecoder, PObject)
00032 
00033   public:
00034     enum {
00035       DetectSamples = 520,
00036       DetectTime = DetectSamples/8  // Milliseconds
00037     };
00038 
00039     PDTMFDecoder();
00040     PString Decode(const short * sampleData, PINDEX numSamples, unsigned mult = 1, unsigned div = 1);
00041 
00042   protected:
00043     enum {
00044       NumTones = 10
00045     };
00046 
00047     // key lookup table (initialised once)
00048     char key[256];
00049 
00050     // frequency table (initialised once)
00051     int p1[NumTones];
00052 
00053     // variables to be retained on each cycle of the decode function
00054     int h[NumTones], k[NumTones], y[NumTones];
00055     int sampleCount, tonesDetected, inputAmplitude;
00056 };
00057 
00058 
00102 class PTones : public PShortArray
00103 {
00104   PCLASSINFO(PTones, PShortArray)
00105 
00106   public:
00107     enum {
00108       MaxVolume = 100,
00109       DefaultSampleRate = 8000,
00110       MinFrequency = 30,
00111       MinModulation = 5,
00112       SineScale = 1000
00113     };
00114 
00118     PTones(
00119       unsigned masterVolume = MaxVolume,      
00120       unsigned sampleRate = DefaultSampleRate 
00121     );
00122 
00125     PTones(
00126       const PString & descriptor,             
00127       unsigned masterVolume = MaxVolume,      
00128       unsigned sampleRate = DefaultSampleRate 
00129     );
00130 
00134     bool Generate(
00135       const PString & descriptor    
00136     );
00137 
00144     bool Generate(
00145       char operation,             
00146       unsigned frequency1,        
00147       unsigned frequency2,        
00148       unsigned milliseconds,      
00149       unsigned volume = MaxVolume 
00150     );
00151 
00152   protected:
00153     void Construct();
00154 
00155     bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
00156     bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
00157     bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
00158     bool Silence  (unsigned milliseconds);
00159 
00160     unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
00161 
00162     void AddSample(int sample, unsigned volume);
00163 
00164     unsigned m_sampleRate;
00165     unsigned m_maxFrequency;
00166     unsigned m_masterVolume;
00167     char     m_lastOperation;
00168     unsigned m_lastFrequency1, m_lastFrequency2;
00169     int      m_angle1, m_angle2;
00170 };
00171 
00172 
00177 class PDTMFEncoder : public PTones
00178 {
00179   PCLASSINFO(PDTMFEncoder, PTones)
00180 
00181   public:
00182     enum { DefaultToneLen = 100 };
00183 
00187     PDTMFEncoder(
00188         const char * dtmf = NULL,      
00189         unsigned milliseconds = DefaultToneLen  
00190     );
00191 
00195     PDTMFEncoder(
00196         char key,      
00197         unsigned milliseconds = DefaultToneLen  
00198     );    
00199 
00203     void AddTone(
00204         const char * str,              
00205         unsigned milliseconds = DefaultToneLen  
00206     );
00207 
00211     void AddTone(
00212         char ch,                       
00213         unsigned milliseconds = DefaultToneLen  
00214     );
00215 
00220     void AddTone(
00221         double frequency1,                  // primary frequency
00222         double frequency2 = 0,              // secondary frequency, or 0 if no secondary frequency
00223         unsigned milliseconds = DefaultToneLen  // length of DTMF tone in milliseconds
00224     );
00225 
00230     void GenerateRingBackTone()
00231     {
00232       Generate("440+480:2-4");
00233     }
00234 
00239     void GenerateDialTone()
00240     {
00241       Generate("350+440:1");
00242     }
00243 
00248     void GenerateBusyTone()
00249     {
00250       Generate("480+620:0.5-0.5");
00251     }
00252 
00260     char DtmfChar(
00261         PINDEX i    
00262     );
00263     // Overiding GetSize() screws up the SetSize()
00264 };
00265 
00266 
00267 #endif // P_DTMF
00268 
00269 #endif // PTLIB_DTMF_H
00270 
00271 
00272 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:09 2011 for PTLib by  doxygen 1.4.7