dtmf.h
Go to the documentation of this file.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* Extract DTMF signals from 16 bit PCM audio
*
* Originally written by Poul-Henning Kamp <phk@freebsd.org>
* Made into a C++ class by Roger Hardiman <roger@freebsd.org>, January 2002
*
* $Revision: 24177 $
* $Author: rjongbloed $
* $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
*/
#ifndef PTLIB_DTMF_H
#define PTLIB_DTMF_H
#if P_DTMF
#ifdef P_USE_PRAGMA
#pragma interface
#endif
class PDTMFDecoder : public PObject
{
PCLASSINFO(PDTMFDecoder, PObject)
public:
enum {
DetectSamples = 520,
DetectTime = DetectSamples/8 // Milliseconds
};
PDTMFDecoder();
PString Decode(const short * sampleData, PINDEX numSamples, unsigned mult = 1, unsigned div = 1);
protected:
enum {
NumTones = 10
};
// key lookup table (initialised once)
char key[256];
// frequency table (initialised once)
int p1[NumTones];
// variables to be retained on each cycle of the decode function
int h[NumTones], k[NumTones], y[NumTones];
int sampleCount, tonesDetected, inputAmplitude;
};
class PTones : public PShortArray
{
PCLASSINFO(PTones, PShortArray)
public:
enum {
MaxVolume = 100,
DefaultSampleRate = 8000,
MinFrequency = 30,
MinModulation = 5,
SineScale = 1000
};
PTones(
unsigned masterVolume = MaxVolume,
unsigned sampleRate = DefaultSampleRate
);
PTones(
const PString & descriptor,
unsigned masterVolume = MaxVolume,
unsigned sampleRate = DefaultSampleRate
);
bool Generate(
const PString & descriptor
);
bool Generate(
char operation,
unsigned frequency1,
unsigned frequency2,
unsigned milliseconds,
unsigned volume = MaxVolume
);
protected:
void Construct();
bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume);
bool Modulate (unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume);
bool PureTone (unsigned frequency, unsigned milliseconds, unsigned volume);
bool Silence (unsigned milliseconds);
unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2 = 0);
void AddSample(int sample, unsigned volume);
unsigned m_sampleRate;
unsigned m_maxFrequency;
unsigned m_masterVolume;
char m_lastOperation;
unsigned m_lastFrequency1, m_lastFrequency2;
int m_angle1, m_angle2;
};
class PDTMFEncoder : public PTones
{
PCLASSINFO(PDTMFEncoder, PTones)
public:
enum { DefaultToneLen = 100 };
PDTMFEncoder(
const char * dtmf = NULL,
unsigned milliseconds = DefaultToneLen
);
PDTMFEncoder(
char key,
unsigned milliseconds = DefaultToneLen
);
void AddTone(
const char * str,
unsigned milliseconds = DefaultToneLen
);
void AddTone(
char ch,
unsigned milliseconds = DefaultToneLen
);
void AddTone(
double frequency1, // primary frequency
double frequency2 = 0, // secondary frequency, or 0 if no secondary frequency
unsigned milliseconds = DefaultToneLen // length of DTMF tone in milliseconds
);
void GenerateRingBackTone()
{
Generate("440+480:2-4");
}
void GenerateDialTone()
{
Generate("350+440:1");
}
void GenerateBusyTone()
{
Generate("480+620:0.5-0.5");
}
char DtmfChar(
PINDEX i
);
// Overiding GetSize() screws up the SetSize()
};
#endif // P_DTMF
#endif // PTLIB_DTMF_H
// End Of File ///////////////////////////////////////////////////////////////
PDTMFDecoder::PDTMFDecoder
PDTMFDecoder()
PDTMFEncoder::GenerateDialTone
void GenerateDialTone()
Generate PCM data for 1 second of US standard dial tone of 350/440hz.
Definition: dtmf.h:239
PDTMFDecoder::h
int h[NumTones]
Definition: dtmf.h:54
PTones::Generate
bool Generate(const PString &descriptor)
Generate a tone using the specified descriptor.
PDTMFDecoder::DetectSamples
Definition: dtmf.h:35
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PDTMFDecoder::key
char key[256]
Definition: dtmf.h:48
PDTMFEncoder
this class can be used to generate PCM data for DTMF tones at a sample rate of 8khz ...
Definition: dtmf.h:177
PTones::MinModulation
Definition: dtmf.h:111
PDTMFEncoder::PDTMFEncoder
PDTMFEncoder(const char *dtmf=NULL, unsigned milliseconds=DefaultToneLen)
Create PCM data for the specified DTMF sequence.
PDTMFDecoder::p1
int p1[NumTones]
Definition: dtmf.h:51
PTones
This class can be used to generate PCM data for tones (such as telephone calling tones and DTMF) at a...
Definition: dtmf.h:102
PTones::m_sampleRate
unsigned m_sampleRate
Definition: dtmf.h:164
PDTMFDecoder::inputAmplitude
int inputAmplitude
Definition: dtmf.h:55
PDTMFEncoder::GenerateBusyTone
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::PTones
PTones(unsigned masterVolume=MaxVolume, unsigned sampleRate=DefaultSampleRate)
Create an empty tone buffer.
PDTMFEncoder::AddTone
void AddTone(const char *str, unsigned milliseconds=DefaultToneLen)
Add the PCM data for the specified tone sequence to the buffer.
PTones::SineScale
Definition: dtmf.h:112
PTones::Construct
void Construct()
PDTMFEncoder::DtmfChar
char DtmfChar(PINDEX i)
Convenience function to get the ASCII character for a DTMF index, where the index varies from 0 to 15...
PDTMFDecoder::sampleCount
int sampleCount
Definition: dtmf.h:55
PDTMFDecoder::k
int k[NumTones]
Definition: dtmf.h:54
PTones::m_masterVolume
unsigned m_masterVolume
Definition: dtmf.h:166
PTones::AddSample
void AddSample(int sample, unsigned volume)
PTones::Juxtapose
bool Juxtapose(unsigned frequency1, unsigned frequency2, unsigned milliseconds, unsigned volume)
PTones::m_lastFrequency2
unsigned m_lastFrequency2
Definition: dtmf.h:168
PDTMFDecoder::DetectTime
Definition: dtmf.h:36
PTones::m_angle2
int m_angle2
Definition: dtmf.h:169
PString
The character string class.
Definition: pstring.h:108
PTones::m_angle1
int m_angle1
Definition: dtmf.h:169
PDTMFDecoder
Definition: dtmf.h:29
PDTMFDecoder::NumTones
Definition: dtmf.h:44
PTones::DefaultSampleRate
Definition: dtmf.h:109
PTones::m_maxFrequency
unsigned m_maxFrequency
Definition: dtmf.h:165
PShortArray
Array of short integers.
Definition: array.h:589
PTones::PureTone
bool PureTone(unsigned frequency, unsigned milliseconds, unsigned volume)
PTones::MaxVolume
Definition: dtmf.h:108
PDTMFDecoder::tonesDetected
int tonesDetected
Definition: dtmf.h:55
PTones::m_lastOperation
char m_lastOperation
Definition: dtmf.h:167
PDTMFEncoder::DefaultToneLen
Definition: dtmf.h:182
PTones::CalcSamples
unsigned CalcSamples(unsigned milliseconds, unsigned frequency1, unsigned frequency2=0)
PTones::m_lastFrequency1
unsigned m_lastFrequency1
Definition: dtmf.h:168
PDTMFDecoder::y
int y[NumTones]
Definition: dtmf.h:54
PTones::MinFrequency
Definition: dtmf.h:110
PTones::Silence
bool Silence(unsigned milliseconds)
PDTMFEncoder::GenerateRingBackTone
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
PDTMFDecoder::Decode
PString Decode(const short *sampleData, PINDEX numSamples, unsigned mult=1, unsigned div=1)
PTones::Modulate
bool Modulate(unsigned frequency, unsigned modulate, unsigned milliseconds, unsigned volume)
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118