PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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: 29286 $
15  * $Author: rjongbloed $
16  * $Date: 2013-03-21 12:47:08 +1100 (Thu, 21 Mar 2013) $
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 
107 class PTones : public PShortArray
108 {
110 
111  public:
112  enum {
113  MaxVolume = 100,
117  SineScale = 1000
118  };
119 
123  PTones(
124  unsigned masterVolume = MaxVolume,
125  unsigned sampleRate = DefaultSampleRate
126  );
127 
130  PTones(
131  const PString & descriptor,
132  unsigned masterVolume = MaxVolume,
133  unsigned sampleRate = DefaultSampleRate
134  );
135 
139  bool Generate(
140  const PString & descriptor,
141  unsigned sampleRate = 0,
142  unsigned masterVolume = 0
143  );
144 
151  bool Generate(
152  char operation,
153  unsigned frequency1,
154  unsigned frequency2,
155  unsigned milliseconds,
156  unsigned volume = MaxVolume
157  );
158 
160  bool Write(
161  PChannel & channel
162  ) { return channel.Write(GetPointer(), GetSize()*sizeof(short)); }
163 
165  unsigned GetSampleRate() const { return m_sampleRate; }
166 
167  virtual PBoolean SetSize(PINDEX newSize);
168 
169  protected:
170  void Reset();
171 
172  bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
173  bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
174  bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
175  bool Silence (unsigned milliseconds);
176 
177  unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
178 
179  void AddSample(int sample, unsigned volume);
180 
181  unsigned m_sampleRate;
182  unsigned m_maxFrequency;
183  unsigned m_masterVolume;
188 };
189 
190 
195 class PDTMFEncoder : public PTones
196 {
198 
199  public:
200  enum { DefaultToneLen = 100 };
201 
205  PDTMFEncoder(
206  const char * dtmf = NULL,
207  unsigned milliseconds = DefaultToneLen
208  );
209 
213  PDTMFEncoder(
214  char key,
215  unsigned milliseconds = DefaultToneLen
216  );
217 
221  void AddTone(
222  const char * str,
223  unsigned milliseconds = DefaultToneLen
224  );
225 
229  void AddTone(
230  char ch,
231  unsigned milliseconds = DefaultToneLen
232  );
233 
238  void AddTone(
239  double frequency1, // primary frequency
240  double frequency2 = 0, // secondary frequency, or 0 if no secondary frequency
241  unsigned milliseconds = DefaultToneLen // length of DTMF tone in milliseconds
242  );
243 
249  {
250  Generate("440+480:2-4");
251  }
252 
258  {
259  Generate("350+440:1");
260  }
261 
267  {
268  Generate("480+620:0.5-0.5");
269  }
270 
278  char DtmfChar(
279  PINDEX i
280  );
281  // Overiding GetSize() screws up the SetSize()
282 };
283 
284 
285 #endif // P_DTMF
286 
287 #endif // PTLIB_DTMF_H
288 
289 
290 // End Of File ///////////////////////////////////////////////////////////////