Showing posts with label win32. Show all posts
Showing posts with label win32. Show all posts

Saturday, September 7, 2013

Generating DTMF tones using C++

What are DTMF tones ?

DTMF tones are the tones used in telephones for tone dialing. The DTMF tones are sums of two sine wave tones at following frequencies:
                 1209 Hz 1336 Hz 1477 Hz 1633 Hz
                          ABC     DEF
   697 Hz          1       2       3       A
                  GHI     JKL     MNO
   770 Hz          4       5       6       B
                  PRS     TUV     WXY
   852 Hz          7       8       9       C
                          oper
   941 Hz          *       0       #       D

How to generate DTMF tone samples
Generating sine wave samples is easy using the following formula:
sample=sin(n*2*pi*f/samplerate)
Where
  • n is the sample number (starting from 0)
  • f is the frequency you wan to generate
  • samplerate is the rate you are playing the samples through your sound card
Generating DTMF tones using this method is quite easy by just summing two of those sine waves.For example, for calculating samples for 8 kHz sample rate at 8 bit (unsigned) data, use the following function:
sample(n) = 128 + 63*sin(n*2*pi*f1/8000) + 63*sin(n*2*pi*f2/8000)
Where f1 and f2 are the frequencies of the sine waves in DTMF tone.

C++ Code (For 8 KHz Sampling Rate)
#include <windows.h>
#include  <math.h>

#define M_PI       3.14159265358979323846

class DTMF 
{
public:
 DTMF(char digit, int iMilliSeconds = 100, WORD wSampleRate = 8000) 
 {
  m_iPacketLength = iMilliSeconds * 8000/1000;
  m_pTone = new BYTE[m_iPacketLength];
 
  if(m_pTone == NULL){
   return;
  }
  
  int lowtone_frequency = 0;
  int hightone_frequency = 0;
  
  switch(digit)
  {
   case '1': case '2': case '3': case 'A': lowtone_frequency =  697; break;
   case '4': case '5': case '6': case 'B': lowtone_frequency =  770; break;
   case '7': case '8': case '9': case 'C': lowtone_frequency =  852; break;
   case '*': case '0': case '#': case 'D': lowtone_frequency =  941; break;
  }
  switch(digit)
  {
   case '1': case '4': case '7': case '*': hightone_frequency =  1209; break;
   case '2': case '5': case '8': case '0': hightone_frequency =  1336; break;
   case '3': case '6': case '9': case '#': hightone_frequency =  1477; break;
   case 'A': case 'B': case 'C': case 'D': hightone_frequency =  1633; break;
  }
  
  double pi_prod_1 = (2.0 * M_PI * lowtone_frequency)/wSampleRate;
  double pi_prod_2 = (2.0 * M_PI * hightone_frequency)/wSampleRate;
  
  for(int i=0; i<m_iPacketLength; i++)
  {
   m_pTone[i] = 128 + BYTE(63*sin(i*pi_prod_1) + 63*sin(i*pi_prod_2));
  }
 }
 ~DTMF() {
  if(m_pTone != NULL){
   delete[] m_pTone;
   m_pTone = NULL;
  }
 }
public:
 PBYTE GetData() const {
  return m_pTone;
 }
 int GetLength() const {
  return m_iPacketLength;
 }
private:
 PBYTE m_pTone;
 int m_iPacketLength;
};

Links:
  1. DTMF Wikipedia 

Wednesday, June 19, 2013

Windows API: The relationship between Process, Handle and Windows

1)
HAVE: Process ID, NEED: Process handle
Solution: 
OpenProcess()

2)
HAVE: Process handle, NEED: Process ID
Solution: GetProcessId()

3)
HAVE: Window handle, NEED: Process ID
Solution: GetWindowThreadProcessId()

4)
HAVE: Window handle, NEED: Process handle
Solution: Use 3) and then 1)

5)
HAVE: Process ID, NEED: Window handle
Solution: EnumWindows(), then in the callback function do 3) and check if it matches your process ID.

6)
HAVE: Process handle, NEED: Window handle
Solution: 2) and then 5)

Tuesday, May 21, 2013

What is 'CPoint' : ambiguous symbol atltypes.h?

It was a dark and rainy night.. I was trying to do something useful.. what I don't remember right now.. But I was getting this weird problem...

Error 1 error C2872: 'CPoint' : ambiguous symbol atltypes.h
Error 2 error C2872: 'CRect' : ambiguous symbol atltypes.h
Error 3 error C2872: 'CSize' : ambiguous symbol atltypes.h
.
.
Error N error C2872: 'CReality' : ambiguous symbol life.h

Obviously (is it?) I was using ATL and WTL to do something... After scratching my head, googling and going through few WTL headers .. I found the solution. Just include the following, just after you add the #include <windows.h> :
#include <atlbase.h>

#if (_ATL_VER >= 0x0700)
#include <atlstr.h>
#include <atltypes.h>
#endif

#if (_ATL_VER >= 0x0700)
#define _WTL_NO_CSTRING
#define _WTL_NO_WTYPES
#define _WTL_NO_UNION_CLASSES
#endif

#include <atlapp.h>

Sunday, May 12, 2013

WTL::CRichEditCtrl

WTL::CRichEditCtrl needs the following line for initialization, add it in your DllMain or _tWinMain and it resolves the issue:
HINSTANCE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName());
Unfortunately I forgot that Rich Edit isn’t a common control and is not initialized with InitCommonControlsEx.

Tuesday, February 19, 2013

Some COM Books


Some books on COM. I just keep forgetting the name !!

  1. Inside OLE, 2nd Edition, by Kraig Brockschmidt (Microsoft Press)
  2. Understanding ActiveX and OLE, by David Chappell (Microsoft Press)
  3. Inside COM, by Dale Rogerson (Microsoft Press)

Monday, September 24, 2012

How to hide a window from Win32 Taskbar?


Argh !!! I don't know why but I keep forgetting the ex-style bit for this... 
To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style. As an alternative, you can create a hidden window and make this hidden window the owner of your visible window.
The Shell will remove a window's button from the taskbar only if the window's style supports visible taskbar buttons. If you want to dynamically change a window's style to one that doesn't support visible taskbar buttons, you must hide the window first (by calling ShowWindow with SW_HIDE), change the window style, and then show the window.
Taken from here: The Taskbar