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.9  2006/12/13 04:56:03  csoutheren
00016  * Applied 1613270 - fixed for dtmfEncoder
00017  * Thanks to Frederic Heem
00018  *
00019  * Revision 1.8  2006/10/25 08:18:20  rjongbloed
00020  * Major upgrade of tone generation subsystem.
00021  *
00022  * Revision 1.7  2005/11/30 12:47:37  csoutheren
00023  * Removed tabs, reformatted some code, and changed tags for Doxygen
00024  *
00025  * Revision 1.6  2004/11/11 07:34:50  csoutheren
00026  * Added #include <ptlib.h>
00027  *
00028  * Revision 1.5  2004/09/09 23:50:48  csoutheren
00029  * Fixed problem with duplicate definition of sinetab causing problems
00030  *
00031  * Revision 1.4  2004/09/09 05:23:37  dereksmithies
00032  * Add utility function to report on dtmf characters used.
00033  *
00034  * Revision 1.3  2004/09/09 04:00:00  csoutheren
00035  * Added DTMF encoding functions
00036  *
00037  * Revision 1.2  2002/09/16 01:08:59  robertj
00038  * Added #define so can select if #pragma interface/implementation is used on
00039  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00040  *
00041  * Revision 1.1  2002/01/23 11:43:26  rogerh
00042  * Add DTMF Decoder class. This can be passed PCM audio data
00043  * (at 16 bit, 8 KHz) and returns any DTMF codes detected.
00044  * Tested with NetMeeting sending DTMF over a G.711 stream.
00045  *
00046  */
00047  
00048 #ifndef _DTMF_H
00049 #define _DTMF_H
00050 
00051 #ifdef P_USE_PRAGMA
00052 #pragma interface
00053 #endif
00054 
00055 #include <ptlib.h>
00056 
00057 class PDTMFDecoder : public PObject
00058 {
00059   PCLASSINFO(PDTMFDecoder, PObject)
00060 
00061   public:
00062     PDTMFDecoder();
00063     PString Decode(const short * sampleData, PINDEX numSamples);
00064 
00065   protected:
00066     // key lookup table (initialised once)
00067     char key[256];
00068 
00069     // frequency table (initialised once)
00070     int p1[8];
00071 
00072     // variables to be retained on each cycle of the decode function
00073     int h[8], k[8], y[8];
00074     int nn, so, ia;
00075 };
00076 
00077 
00121 class PTones : public PShortArray
00122 {
00123   PCLASSINFO(PTones, PShortArray)
00124 
00125   public:
00126     enum {
00127         MaxVolume = 100,
00128         SampleRate = 8000,
00129         MaxFrequency = (SampleRate/4),
00130         MinFrequency = 30,
00131         MinModulation = 5,
00132         SineScale = 1000
00133     };
00134 
00138     PTones(
00139         unsigned masterVolume = MaxVolume 
00140     );
00141 
00144     PTones(
00145       const PString & descriptor,    
00146       unsigned masterVolume = MaxVolume 
00147     );
00148 
00152     bool Generate(
00153       const PString & descriptor    
00154     );
00155 
00162     bool Generate(
00163       char operation,
00164       unsigned frequency1,        
00165       unsigned frequency2,        
00166       unsigned milliseconds,      
00167       unsigned volume = MaxVolume 
00168     );
00169 
00170   protected:
00171     bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
00172     bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
00173     bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
00174     bool Silence  (unsigned milliseconds);
00175 
00176     unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
00177 
00178     void AddSample(int sample, unsigned volume);
00179 
00180     unsigned masterVolume;
00181     char     lastOperation;
00182     unsigned lastFrequency1, lastFrequency2;
00183     int      angle1, angle2;
00184 };
00185 
00186 
00191 class PDTMFEncoder : public PTones
00192 {
00193   PCLASSINFO(PDTMFEncoder, PTones)
00194 
00195   public:
00196     enum { DefaultToneLen = 100 };
00197 
00201     PDTMFEncoder(
00202         const char * dtmf = NULL,      
00203         unsigned milliseconds = DefaultToneLen  
00204     );
00205 
00209     PDTMFEncoder(
00210         char key,      
00211         unsigned milliseconds = DefaultToneLen  
00212     );    
00213 
00217     void AddTone(
00218         const char * str,              
00219         unsigned milliseconds = DefaultToneLen  
00220     );
00221 
00225     void AddTone(
00226         char ch,                       
00227         unsigned milliseconds = DefaultToneLen  
00228     );
00229 
00234     void AddTone(
00235         double frequency1,                  // primary frequency
00236         double frequency2 = 0,              // secondary frequency, or 0 if no secondary frequency
00237         unsigned milliseconds = DefaultToneLen  // length of DTMF tone in milliseconds
00238     );
00239 
00244     void GenerateRingBackTone()
00245     {
00246       Generate("440+480:2-4");
00247     }
00248 
00253     void GenerateDialTone()
00254     {
00255       Generate("350+440:1");
00256     }
00257 
00262     void GenerateBusyTone()
00263     {
00264       Generate("480+620:0.5-0.5");
00265     }
00266 
00274     char DtmfChar(
00275         PINDEX i    
00276     );
00277     // Overiding GetSize() screws up the SetSize()
00278 };
00279 
00280 #endif /* _DTMF_H */

Generated on Sun Sep 6 03:50:33 2009 for PWLib by  doxygen 1.6.1