Friday, September 20, 2013

Generate Lib From DLL

Introduction
To avoid installing and fighting against MSYS and Cygwin, you can just extract exported symbols from libvlc.dll to generate a .lib (libvlc.lib) and link your program against it.

Open Visual Studio Command Prompt
It can be found within the Visual Studio Tools menu entry:
Start / Program Files / Microsoft Visual Studio / Visual Studio Tools / Visual Studio Command Prompt.

Extract Symbols
Within the command prompt type:
dumpbin /exports "C:\Program Files\VideoLAN\VLC\libvlc.dll" > "C:\Program Files\VideoLAN\VLC\libvlc.def"
Edit the libvlc.def file and modify it to get something like this:
EXPORTS
libvlc_add_intf
libvlc_audio_get_channel
libvlc_audio_get_mute
libvlc_audio_get_track
libvlc_audio_get_track_count
libvlc_audio_get_track_description
libvlc_audio_get_volume
...
Generate the .lib
Still within the command prompt type:
lib /def:"C:\Program Files\VideoLAN\VLC\libvlc.def" /out:"C:\Program Files\VideoLAN\VLC\libvlc.lib" /machine:x86
Of course, you'll need to adapt the path according to your configuration.

Source
Generate Lib From DLL

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