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: 20385 $
00015  * $Author: rjongbloed $
00016  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00017  */
00018  
00019 #ifndef _DTMF_H
00020 #define _DTMF_H
00021 
00022 #ifdef P_USE_PRAGMA
00023 #pragma interface
00024 #endif
00025 
00026 #ifndef _PTLIB_H
00027 #include <ptlib.h>
00028 #endif
00029 
00030 class PDTMFDecoder : public PObject
00031 {
00032   PCLASSINFO(PDTMFDecoder, PObject)
00033 
00034   public:
00035     PDTMFDecoder();
00036     PString Decode(const short * sampleData, PINDEX numSamples, unsigned mult = 1, unsigned div = 1);
00037 
00038   protected:
00039     enum {
00040       NumTones = 10
00041     };
00042 
00043     // key lookup table (initialised once)
00044     char key[256];
00045 
00046     // frequency table (initialised once)
00047     int p1[NumTones];
00048 
00049     // variables to be retained on each cycle of the decode function
00050     int h[NumTones], k[NumTones], y[NumTones];
00051     int nn, so, ia;
00052 };
00053 
00054 
00098 class PTones : public PShortArray
00099 {
00100   PCLASSINFO(PTones, PShortArray)
00101 
00102   public:
00103     enum {
00104         MaxVolume = 100,
00105         SampleRate = 8000,
00106         MaxFrequency = (SampleRate/4),
00107         MinFrequency = 30,
00108         MinModulation = 5,
00109         SineScale = 1000
00110     };
00111 
00115     PTones(
00116         unsigned masterVolume = MaxVolume 
00117     );
00118 
00121     PTones(
00122       const PString & descriptor,    
00123       unsigned masterVolume = MaxVolume 
00124     );
00125 
00129     bool Generate(
00130       const PString & descriptor    
00131     );
00132 
00139     bool Generate(
00140       char operation,
00141       unsigned frequency1,        
00142       unsigned frequency2,        
00143       unsigned milliseconds,      
00144       unsigned volume = MaxVolume 
00145     );
00146 
00147   protected:
00148     bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
00149     bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
00150     bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
00151     bool Silence  (unsigned milliseconds);
00152 
00153     unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
00154 
00155     void AddSample(int sample, unsigned volume);
00156 
00157     unsigned masterVolume;
00158     char     lastOperation;
00159     unsigned lastFrequency1, lastFrequency2;
00160     int      angle1, angle2;
00161 };
00162 
00163 
00168 class PDTMFEncoder : public PTones
00169 {
00170   PCLASSINFO(PDTMFEncoder, PTones)
00171 
00172   public:
00173     enum { DefaultToneLen = 100 };
00174 
00178     PDTMFEncoder(
00179         const char * dtmf = NULL,      
00180         unsigned milliseconds = DefaultToneLen  
00181     );
00182 
00186     PDTMFEncoder(
00187         char key,      
00188         unsigned milliseconds = DefaultToneLen  
00189     );    
00190 
00194     void AddTone(
00195         const char * str,              
00196         unsigned milliseconds = DefaultToneLen  
00197     );
00198 
00202     void AddTone(
00203         char ch,                       
00204         unsigned milliseconds = DefaultToneLen  
00205     );
00206 
00211     void AddTone(
00212         double frequency1,                  // primary frequency
00213         double frequency2 = 0,              // secondary frequency, or 0 if no secondary frequency
00214         unsigned milliseconds = DefaultToneLen  // length of DTMF tone in milliseconds
00215     );
00216 
00221     void GenerateRingBackTone()
00222     {
00223       Generate("440+480:2-4");
00224     }
00225 
00230     void GenerateDialTone()
00231     {
00232       Generate("350+440:1");
00233     }
00234 
00239     void GenerateBusyTone()
00240     {
00241       Generate("480+620:0.5-0.5");
00242     }
00243 
00251     char DtmfChar(
00252         PINDEX i    
00253     );
00254     // Overiding GetSize() screws up the SetSize()
00255 };
00256 
00257 #endif /* _DTMF_H */

Generated on Mon Feb 23 01:57:54 2009 for PTLib by  doxygen 1.5.1