PTLib  Version 2.10.11
dtmf.h
Go to the documentation of this file.
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * Extract DTMF signals from 16 bit PCM audio
10  *
11  * Originally written by Poul-Henning Kamp <phk@freebsd.org>
12  * Made into a C++ class by Roger Hardiman <roger@freebsd.org>, January 2002
13  *
14  * $Revision: 24177 $
15  * $Author: rjongbloed $
16  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
17  */
18 
19 #ifndef PTLIB_DTMF_H
20 #define PTLIB_DTMF_H
21 
22 #if P_DTMF
23 
24 #ifdef P_USE_PRAGMA
25 #pragma interface
26 #endif
27 
28 
29 class PDTMFDecoder : public PObject
30 {
32 
33  public:
34  enum {
36  DetectTime = DetectSamples/8 // Milliseconds
37  };
38 
39  PDTMFDecoder();
40  PString Decode(const short * sampleData, PINDEX numSamples, unsigned mult = 1, unsigned div = 1);
41 
42  protected:
43  enum {
44  NumTones = 10
45  };
46 
47  // key lookup table (initialised once)
48  char key[256];
49 
50  // frequency table (initialised once)
51  int p1[NumTones];
52 
53  // variables to be retained on each cycle of the decode function
56 };
57 
58 
102 class PTones : public PShortArray
103 {
105 
106  public:
107  enum {
108  MaxVolume = 100,
112  SineScale = 1000
113  };
114 
118  PTones(
119  unsigned masterVolume = MaxVolume,
120  unsigned sampleRate = DefaultSampleRate
121  );
122 
125  PTones(
126  const PString & descriptor,
127  unsigned masterVolume = MaxVolume,
128  unsigned sampleRate = DefaultSampleRate
129  );
130 
134  bool Generate(
135  const PString & descriptor
136  );
137 
144  bool Generate(
145  char operation,
146  unsigned frequency1,
147  unsigned frequency2,
148  unsigned milliseconds,
149  unsigned volume = MaxVolume
150  );
151 
152  protected:
153  void Construct();
154 
155  bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
156  bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
157  bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
158  bool Silence (unsigned milliseconds);
159 
160  unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
161 
162  void AddSample(int sample, unsigned volume);
163 
164  unsigned m_sampleRate;
165  unsigned m_maxFrequency;
166  unsigned m_masterVolume;
170 };
171 
172 
177 class PDTMFEncoder : public PTones
178 {
180 
181  public:
182  enum { DefaultToneLen = 100 };
183 
187  PDTMFEncoder(
188  const char * dtmf = NULL,
189  unsigned milliseconds = DefaultToneLen
190  );
191 
195  PDTMFEncoder(
196  char key,
197  unsigned milliseconds = DefaultToneLen
198  );
199 
203  void AddTone(
204  const char * str,
205  unsigned milliseconds = DefaultToneLen
206  );
207 
211  void AddTone(
212  char ch,
213  unsigned milliseconds = DefaultToneLen
214  );
215 
220  void AddTone(
221  double frequency1, // primary frequency
222  double frequency2 = 0, // secondary frequency, or 0 if no secondary frequency
223  unsigned milliseconds = DefaultToneLen // length of DTMF tone in milliseconds
224  );
225 
231  {
232  Generate("440+480:2-4");
233  }
234 
240  {
241  Generate("350+440:1");
242  }
243 
249  {
250  Generate("480+620:0.5-0.5");
251  }
252 
260  char DtmfChar(
261  PINDEX i
262  );
263  // Overiding GetSize() screws up the SetSize()
264 };
265 
266 
267 #endif // P_DTMF
268 
269 #endif // PTLIB_DTMF_H
270 
271 
272 // End Of File ///////////////////////////////////////////////////////////////
void GenerateDialTone()
Generate PCM data for 1 second of US standard dial tone of 350/440hz.
Definition: dtmf.h:239
int h[NumTones]
Definition: dtmf.h:54
bool Generate(const PString &descriptor)
Generate a tone using the specified descriptor.
Definition: dtmf.h:35
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
char key[256]
Definition: dtmf.h:48
this class can be used to generate PCM data for DTMF tones at a sample rate of 8khz ...
Definition: dtmf.h:177
Definition: dtmf.h:111
PDTMFEncoder(const char *dtmf=NULL, unsigned milliseconds=DefaultToneLen)
Create PCM data for the specified DTMF sequence.
int p1[NumTones]
Definition: dtmf.h:51
This class can be used to generate PCM data for tones (such as telephone calling tones and DTMF) at a...
Definition: dtmf.h:102
unsigned m_sampleRate
Definition: dtmf.h:164
int inputAmplitude
Definition: dtmf.h:55
void GenerateBusyTone()
Generate PCM data for a single cadence of the US standard busy tone of 480/620hz for 1/2 second...
Definition: dtmf.h:248
PTones(unsigned masterVolume=MaxVolume, unsigned sampleRate=DefaultSampleRate)
Create an empty tone buffer.
void AddTone(const char *str, unsigned milliseconds=DefaultToneLen)
Add the PCM data for the specified tone sequence to the buffer.
Definition: dtmf.h:112
void Construct()
char DtmfChar(PINDEX i)
Convenience function to get the ASCII character for a DTMF index, where the index varies from 0 to 15...
int sampleCount
Definition: dtmf.h:55
int k[NumTones]
Definition: dtmf.h:54
unsigned m_masterVolume
Definition: dtmf.h:166
void AddSample(int sample, unsigned volume)
bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume)
unsigned m_lastFrequency2
Definition: dtmf.h:168
Definition: dtmf.h:36
int m_angle2
Definition: dtmf.h:169
The character string class.
Definition: pstring.h:108
int m_angle1
Definition: dtmf.h:169
Definition: dtmf.h:29
Definition: dtmf.h:44
Definition: dtmf.h:109
unsigned m_maxFrequency
Definition: dtmf.h:165
Array of short integers.
Definition: array.h:589
bool PureTone(unsigned frequency, unsigned milliseconds, unsigned volume)
Definition: dtmf.h:108
int tonesDetected
Definition: dtmf.h:55
char m_lastOperation
Definition: dtmf.h:167
Definition: dtmf.h:182
unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2=0)
unsigned m_lastFrequency1
Definition: dtmf.h:168
int y[NumTones]
Definition: dtmf.h:54
Definition: dtmf.h:110
bool Silence(unsigned milliseconds)
void GenerateRingBackTone()
Generate PCM data for a single cadence of the US standard ring tone of 440/480hz for 2 seconds...
Definition: dtmf.h:230
PString Decode(const short *sampleData, PINDEX numSamples, unsigned mult=1, unsigned div=1)
bool Modulate(unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume)
Ultimate parent class for all objects in the class library.
Definition: object.h:1118