PTLib  Version 2.18.8
 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 
15 #ifndef PTLIB_DTMF_H
16 #define PTLIB_DTMF_H
17 
18 #if P_DTMF
19 
20 #ifdef P_USE_PRAGMA
21 #pragma interface
22 #endif
23 
24 
25 class PDTMFDecoder : public PObject
26 {
28 
29  public:
30  enum {
32  DetectTime = DetectSamples/8 // Milliseconds
33  };
34 
35  PDTMFDecoder();
36  PString Decode(const short * sampleData, PINDEX numSamples, unsigned mult = 1, unsigned div = 1);
37 
38  protected:
39  enum {
40  NumTones = 10
41  };
42 
43  // key lookup table (initialised once)
44  char key[256];
45 
46  // frequency table (initialised once)
47  int p1[NumTones];
48 
49  // variables to be retained on each cycle of the decode function
52 };
53 
54 
103 class PTones : public PShortArray
104 {
106 
107  public:
108  enum {
109  MaxVolume = 100,
113  SineScale = 1000
114  };
115 
119  PTones(
120  unsigned masterVolume = MaxVolume,
121  unsigned sampleRate = DefaultSampleRate
122  );
123 
126  PTones(
127  const PString & descriptor,
128  unsigned masterVolume = MaxVolume,
129  unsigned sampleRate = DefaultSampleRate
130  );
131 
135  bool Generate(
136  const PString & descriptor,
137  unsigned sampleRate = 0,
138  unsigned masterVolume = 0
139  );
140 
147  bool Generate(
148  char operation,
149  unsigned frequency1,
150  unsigned frequency2,
151  unsigned milliseconds,
152  unsigned volume = MaxVolume
153  );
154 
156  bool Write(
157  PChannel & channel
158  ) { return channel.Write(GetPointer(), GetSize()*sizeof(short)); }
159 
161  unsigned GetSampleRate() const { return m_sampleRate; }
162 
164  void SetSampleRate(unsigned rate);
165 
166  virtual PBoolean SetSize(PINDEX newSize);
167 
168  protected:
169  void Reset();
170 
171  bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
172  bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
173  bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
174  bool Silence (unsigned milliseconds);
175 
176  unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
177 
178  void AddSample(int sample, unsigned volume);
179 
180  unsigned m_sampleRate;
181  unsigned m_maxFrequency;
182  unsigned m_masterVolume;
187 };
188 
189 
194 class PDTMFEncoder : public PTones
195 {
197 
198  public:
199  enum { DefaultToneLen = 100 };
200 
204  PDTMFEncoder(
205  const char * dtmf = NULL,
206  unsigned milliseconds = DefaultToneLen
207  );
208 
212  PDTMFEncoder(
213  char key,
214  unsigned milliseconds = DefaultToneLen
215  );
216 
220  void AddTone(
221  const char * str,
222  unsigned milliseconds = DefaultToneLen
223  );
224 
228  void AddTone(
229  char ch,
230  unsigned milliseconds = DefaultToneLen
231  );
232 
237  void AddTone(
238  double frequency1, // primary frequency
239  double frequency2 = 0, // secondary frequency, or 0 if no secondary frequency
240  unsigned milliseconds = DefaultToneLen // length of DTMF tone in milliseconds
241  );
242 
248  {
249  Generate("440+480:2-4");
250  }
251 
257  {
258  Generate("350+440:1");
259  }
260 
266  {
267  Generate("480+620:0.5-0.5");
268  }
269 
277  char DtmfChar(
278  PINDEX i
279  );
280  // Overiding GetSize() screws up the SetSize()
281 };
282 
283 
284 #endif // P_DTMF
285 
286 #endif // PTLIB_DTMF_H
287 
288 
289 // End Of File ///////////////////////////////////////////////////////////////
Definition: dtmf.h:113
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the channel.
void GenerateDialTone()
Generate PCM data for 1 second of US standard dial tone of 350/440hz.
Definition: dtmf.h:256
int h[NumTones]
Definition: dtmf.h:50
virtual PBoolean SetSize(PINDEX newSize)
Set the size of the array in elements.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
char key[256]
Definition: dtmf.h:44
this class can be used to generate PCM data for DTMF tones at a sample rate of 8khz ...
Definition: dtmf.h:194
Definition: dtmf.h:110
PDTMFEncoder(const char *dtmf=NULL, unsigned milliseconds=DefaultToneLen)
Create PCM data for the specified DTMF sequence.
bool Write(PChannel &channel)
Write tones to channel.
Definition: dtmf.h:156
void Reset()
int p1[NumTones]
Definition: dtmf.h:47
This class can be used to generate PCM data for tones (such as telephone calling tones and DTMF) at a...
Definition: dtmf.h:103
unsigned m_sampleRate
Definition: dtmf.h:180
int inputAmplitude
Definition: dtmf.h:51
Definition: dtmf.h:32
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:265
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.
char DtmfChar(PINDEX i)
Convenience function to get the ASCII character for a DTMF index, where the index varies from 0 to 15...
void SetSampleRate(unsigned rate)
Set sample rate for tones, note will clear tone buffer.
int sampleCount
Definition: dtmf.h:51
int k[NumTones]
Definition: dtmf.h:50
unsigned m_masterVolume
Definition: dtmf.h:182
Definition: dtmf.h:31
bool Generate(const PString &descriptor, unsigned sampleRate=0, unsigned masterVolume=0)
Generate a tone using the specified descriptor.
void AddSample(int sample, unsigned volume)
Definition: dtmf.h:109
Abstract class defining I/O channel semantics.
Definition: channel.h:103
bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume)
virtual PINDEX GetSize() const
Get the current size of the container.
bool PBoolean
Definition: object.h:174
unsigned m_lastFrequency2
Definition: dtmf.h:184
int m_angle2
Definition: dtmf.h:185
PINDEX m_addPosition
Definition: dtmf.h:186
The character string class.
Definition: pstring.h:108
int m_angle1
Definition: dtmf.h:185
const void * GetPointer() const
Definition: array.h:204
Definition: dtmf.h:25
Definition: dtmf.h:112
unsigned m_maxFrequency
Definition: dtmf.h:181
Definition: dtmf.h:40
unsigned GetSampleRate() const
Get the sample rate for tones.
Definition: dtmf.h:161
bool PureTone(unsigned frequency, unsigned milliseconds, unsigned volume)
Definition: dtmf.h:199
int tonesDetected
Definition: dtmf.h:51
char m_lastOperation
Definition: dtmf.h:183
Definition: dtmf.h:111
unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2=0)
unsigned m_lastFrequency1
Definition: dtmf.h:184
int y[NumTones]
Definition: dtmf.h:50
This template class maps the PAbstractArray to a specific element type.
Definition: array.h:504
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:247
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:2204