PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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: 24177 $
15 * $Author: rjongbloed $
16 * $Date: 2010-04-05 21:52:04 +1000 (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 {
31 PCLASSINFO(PDTMFDecoder, PObject)
32
33 public:
34 enum {
35 DetectSamples = 520,
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
54 int h[NumTones], k[NumTones], y[NumTones];
55 int sampleCount, tonesDetected, inputAmplitude;
56 };
57
58
102 class PTones : public PShortArray
103 {
104 PCLASSINFO(PTones, PShortArray)
105
106 public:
107 enum {
108 MaxVolume = 100,
109 DefaultSampleRate = 8000,
110 MinFrequency = 30,
111 MinModulation = 5,
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;
167 char m_lastOperation;
168 unsigned m_lastFrequency1, m_lastFrequency2;
169 int m_angle1, m_angle2;
170 };
171
172
177 class PDTMFEncoder : public PTones
178 {
179 PCLASSINFO(PDTMFEncoder, PTones)
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
230 void GenerateRingBackTone()
231 {
232 Generate("440+480:2-4");
233 }
234
239 void GenerateDialTone()
240 {
241 Generate("350+440:1");
242 }
243
248 void GenerateBusyTone()
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 ///////////////////////////////////////////////////////////////