SpeakEazy.c



#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#include <process.h>
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>
#include <winbase.h>

#define MSG_LEN  400
#define PLAYBACK_BUFFER_SIZEE  1024
#define RECORD_BUFFER_SIZE   1024
#define USR_INBLOCK  (WM_USER+101)
#define MAX_BUFFERS  1500

WAVEHDR* lpwh[MAX_BUFFERS];
WAVEHDR *lpwho[MAX_BUFFERS];
HWAVEIN hwi;
HWAVEOUT hwo;
HMENU hMenu;

static short record_buffer[RECORD_BUFFER_SIZE];
static unsigned long total_amplitude;
static unsigned long average_amplitude;
static long sql=130;
static int buff=0;
static int stopped=0;
static int waveinresetnumber;
static int rcvvv,Plvvv;
static int count =0;
int cournt;
HINSTANCE hInst;
MMRESULT rc;
WAVEINCAPS wic;
WAVEFORMATEX wfx;
WAVEOUTCAPS woc;
PAINTSTRUCT ps;
HDC hdcText;
MSG msg;
WNDCLASS wc;
UINT nDevId;
UINT nMaxDevices;
HWND hWnd;
LPCTSTR lpszAppName = "MyApp";
LPCTSTR lpszTitle   = "SPEAK  EAZY";

char aa[600];
char HostAddr[100];
char mssg[400];
char TxtOut[100];
long z;
long i;
int cut_off,j,ddd,calling_function,sthreadnr,cthreadnr;
int Terminateconn;
int count_for_Threshold_Squelch=0;
UINT oMaxDevices;
extern SOCKET s,c;

FILE* fplog;

long _stdcall WndProc(UINT,UINT,UINT,long);
BOOL Open_Dev_For_Rec(hWnd);
BOOL Open_Dev_For_Play(hWnd);
VOID MemGiveRecord();
VOID MemGivePlayback();
VOID MemFreeRecord();
VOID MemFreePlayback();
VOID CALLBACK waveInProc(HWAVEOUT,UINT,DWORD,DWORD,DWORD);
VOID WaveStart();
VOID WaveStop();
VOID WaveReset();
VOID WaveHeadUnprepare();
VOID WaveClose();
VOID WaveHeadPrepare();
NumOfWaveDevice();
VOID StopRecord();
VOID squelch(long i);
VOID Declare_Variables();
VOID StartPlay(int);
VOID StopPlay();
VOID StartRecording();
VOID log(char *);
VOID Server();
VOID Client();
VOID transmit();
BOOL APIENTRY GetHostName(HWND,UINT,UINT,LONG);
BOOL APIENTRY AboutBox(HWND,UINT,UINT,LONG);

int status;
HINSTANCE hInstance;
char HostNo[10];

//===================================================================
int _stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow)
//===================================================================
{
   wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
   wc.lpfnWndProc   = (WNDPROC)WndProc;
   wc.cbClsExtra    = 0;
   wc.cbWndExtra    = 0;
   wc.hInstance     = hInstance;
   wc.hIcon         = LoadIcon (hInstance,300);
   wc.hCursor       = LoadCursor(hInstance, 400);
   wc.hbrBackground = GetStockObject(WHITE_BRUSH);  //(HBRUSH)(COLOR_WINDOW+1);
   wc.lpszMenuName  = lpszAppName;
   wc.lpszClassName = lpszAppName;
   wc.lpszMenuName = "MMM";

   RegisterClass( &wc );
   hInst = hInstance;
   hWnd = CreateWindow( lpszAppName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);

   ShowWindow( hWnd,3);
 

 MemGiveRecord();
 MemGivePlayback();

   _beginthread(Server,0,sthreadnr);

   while( GetMessage( &msg, NULL, 0, 0) )
   {
    TranslateMessage(&msg);
         DispatchMessage( &msg );
   }
   return(msg.wParam);
}

//=====================================
//  WNDPROC
//======================================
long CALLBACK WndProc(UINT w, UINT x, UINT y, long z)
{
 hMenu=GetMenu(w);
if(x==WM_COMMAND)
{
 if(y==100)
 {
  if(strcmp(TxtOut,"You can Start Talking Now......")==0)
  {
   Terminateconn=1;
   StopRecord();
   MemFreeRecord();
   closesocket(s);
   SendMessage(hWnd,WM_CLOSE,0,0L);
   WinExec("Trial.exe",2);
  }
  else if(strcmp(TxtOut,"Please Stop Talking... Packets are incomming")==0)
  {
   Terminateconn=1;
   closesocket(c);
   for(i=0;i<100000;i++);
   StopPlay();
   MemFreePlayback();
   Declare_Variables();
   SendMessage(hWnd,WM_CLOSE,0,0L);
   WinExec("Trial.exe",2);
  }
  else if(strcmp(TxtOut,"Playback Started.......")==0)
  {
   Terminateconn=1;
   closesocket(c);
   for(i=0;i<100000;i++);
   StopPlay();
   MemFreePlayback();
   Declare_Variables();
   SendMessage(hWnd,WM_CLOSE,0,0L);
   WinExec("Trial.exe",2);
  }
  closesocket(s);
  closesocket(c);
 }
 else if(y==103)
 {
  status=DialogBox(hInstance,"IDD_DIALOG1",hWnd,&GetHostName);
  if (status==TRUE)
  {
   if(strlen(HostNo)<8)
   {
    MessageBox(0,"Please key in a proper IP Address","SPEAK EAZY",0);
   }
   else
   {
    EnableMenuItem(hMenu,103,MF_GRAYED);
    sprintf(HostAddr,"%s",HostNo);
    calling_function =0;
    _beginthread(Client,0,cthreadnr);
   }
  }
 }

 else if(y==200)
 {
  WinHelp(hWnd,"speakeazy.hlp",3,0);
 }
 

 else if(y==201)
 {
  status=DialogBox(hInstance,"IDD_DIALOG2",hWnd,&AboutBox);
  if (status==TRUE)
  {
  }
 }
 else if(y==109)
 {
  closesocket(s);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
}

else if(x==USR_INBLOCK)
{
 if(count_for_Threshold_Squelch==0)
 {
  squelch(buff);
  if (ddd>=4)
  {
   transmit(ddd-4);
  }
  if(ddd==MAX_BUFFERS-1)
  {
   InvalidateRgn(hWnd,NULL,TRUE);
   sprintf(TxtOut,"Please Stop Talking... Packets are incomming");
   PostMessage(hWnd,WM_PAINT,0,0L);
   MessageBox(0,"BUFFERS ARE FULL","SPEAK EAZY - BUFFERS",0);
   WaveStop();
   transmitStoppedTalk();
   StopRecord();
  }
  ddd++;
  buff++;
 }
 else if(stopped==1 && waveinresetnumber==0)
 {
  while(waveinresetnumber==0)
  {
   waveinresetnumber++;
   InvalidateRgn(hWnd,NULL,TRUE);
   sprintf(TxtOut,"Please Stop Talking... Packets are incomming");
   SendMessage(hWnd,WM_PAINT,0,0L);
   WaveStop();
   transmitStoppedTalk();
   StopRecord();
  }
 }
}

else if(x==WM_PAINT)
{
 hdcText = BeginPaint(hWnd,&ps);
  TextOut(hdcText,35,35,TxtOut,strlen(TxtOut));
 EndPaint(hWnd,&ps);
}
//-------------------------------------------
else if(x==WM_DESTROY)
{

 MemFreePlayback();
 MemFreeRecord();
 PostQuitMessage(0);
}
return DefWindowProc(w,x,y,z);
}

//========================================
BOOL Open_Dev_For_Rec(HWND hWnd)
//========================================
{
 rc=waveInGetDevCaps(nDevId,&wic,sizeof(wic));
 sprintf(aa,"szName = %s",wic.szPname);
 log(aa);
 sprintf(aa,"nMaxDevices = %ld",wic.vDriverVersion);
 log(aa);
 sprintf(aa,"wic.Mid = %ld",wic.wMid);
 log(aa);
 if (rc==MMSYSERR_NOERROR)
 {
  if(wic.dwFormats & WAVE_FORMAT_1M08) //4S16
  {
   wfx.nChannels=1;
   wfx.nSamplesPerSec=11025;
  }
  else
  {
   wfx.nChannels=wic.wChannels;
   wfx.nSamplesPerSec=11025; //22050;
  }
  sprintf(aa,"wfx.nChannels = %ld",wfx.nChannels);
  log(aa);
  wfx.wFormatTag=WAVE_FORMAT_PCM;
  wfx.wBitsPerSample=8;
  wfx.nBlockAlign=wfx.nChannels*wfx.wBitsPerSample/8;
  wfx.nAvgBytesPerSec=wfx.nBlockAlign*wfx.nSamplesPerSec;
  wfx.cbSize=0;
 }
 else
 {
  MessageBox(0,"Rec Format not supported","SPEAK EAZY - Terminating ...",0);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
 waveInGetErrorText(rc,mssg,MSG_LEN);
 sprintf(aa,"WaveInGetErrorText = %s",mssg);
 log(aa);
 rc=waveInOpen(&hwi,nDevId,&wfx,(DWORD)(VOID*)waveInProc,(DWORD)hWnd,CALLBACK_FUNCTION );
 waveInGetErrorText(rc,mssg,MSG_LEN);
 sprintf(aa,"WaveInOpen Error Text = %s",mssg);
 log(aa);
 if (rc!=MMSYSERR_NOERROR)
 {
  MessageBox(0,"Device could not be opened","SPEAK EAZY - Terminating ...",0);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
}
//========================================
VOID MemFreeRecord()
//========================================
{
for(i=0;i<MAX_BUFFERS;i++)
{
     if (lpwh[i] != NULL)
     {
///----------------------Record Buffer --------------
       rc=HeapFree(GetProcessHeap(), 0, lpwh[i]->lpData);
    if(rc!=0)
     rcvvv++;
    rc=HeapFree(GetProcessHeap(), 0, lpwh[i]);
    if(rc!=0)
   rcvvv++;
         lpwh[i] = NULL;
    }
}
sprintf(aa,"%d",rcvvv);
log(aa);
}

//========================================
VOID MemFreePlayback()
//========================================
{
for(i=0;i<MAX_BUFFERS;i++)
{
     if (lpwho[i] != NULL)
     {
///--------------------- PlayBack Buffer -----------------
       rc=HeapFree(GetProcessHeap(), 0, lpwho[i]->lpData);
    if(rc!=0)
     Plvvv++;
    rc=HeapFree(GetProcessHeap(), 0, lpwho[i]);
  if(rc!=0)
   Plvvv++;
         lpwho[i] = NULL;
    }
}
sprintf(aa,"HeapFree = %d",Plvvv);
log(aa);
}

//========================================
VOID MemGiveRecord()
//========================================
{
 for(i=0;i<MAX_BUFFERS;i++)
 {
  lpwh[i]=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
  lpwh[i]->lpData=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,RECORD_BUFFER_SIZE);
  lpwh[i]->dwBufferLength=RECORD_BUFFER_SIZE;
 }
}

//========================================
VOID MemGivePlayback()
//========================================
{
 for(i=0;i<MAX_BUFFERS;i++)
 {
  lpwho[i]=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
  lpwho[i]->lpData=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,PLAYBACK_BUFFER_SIZEE);
  lpwho[i]->dwBufferLength=PLAYBACK_BUFFER_SIZEE;
 }
}

//=====================================================
VOID CALLBACK waveInProc(HWAVEOUT hwo,UINT b,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
//=====================================================
{
 switch (b)
 {case WIM_DATA:
 PostMessage((HWND)dwInstance,USR_INBLOCK,0,dwParam1);
 break;
 }
}

//=========================================
BOOL Open_Dev_For_Play(hWnd)
//=========================================
{
 UINT oMaxDevices;
 WAVEOUTCAPS woc;
 WAVEFORMATEX wfx;
 UINT nDevId;
 MMRESULT rc;
 hwo =NULL;

 oMaxDevices=waveOutGetNumDevs();
 sprintf(aa,"oMaxDevices = %ld",oMaxDevices);
 log(aa);
 if (oMaxDevices==0)
 {
   MessageBox(0,"\tNo Audio Device Detected.\n Check the AudioDevice and then restart the Application","SPEAK EAZY - Terminating ...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
 }
 nDevId=0;

 rc=waveOutGetDevCaps(nDevId,&woc,sizeof(woc));
 sprintf(aa,"nMaxDevices = %ld",woc.vDriverVersion);
 log(aa);
 sprintf(aa,"woc.Mid = %ld",woc.wMid);
 log(aa);
 sprintf(aa,"szName = %s",woc.szPname);
 log(aa);
 if(rc==MMSYSERR_NOERROR)
 {
  if(woc.dwFormats & WAVE_FORMAT_1M08)
  {
   wfx.nChannels=1;
   wfx.nSamplesPerSec=11025;
  }
  else
  {
   wfx.nChannels=woc.wChannels;
   wfx.nSamplesPerSec=11025;  //220500;
  }
 wfx.wFormatTag=WAVE_FORMAT_PCM;
 wfx.wBitsPerSample=8;
 wfx.nBlockAlign=wfx.nChannels*wfx.wBitsPerSample/8;
 wfx.nAvgBytesPerSec=wfx.nSamplesPerSec*wfx.nBlockAlign;
 wfx.cbSize=0;
 sprintf(aa,"wchannels = %ld",wfx.nChannels);
 log(aa);
 }
 else
 {
  MessageBox(0,"Play Format not supported","SPEAK EAZY - Terminating ...",0);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
 

 rc=waveOutOpen(&hwo,nDevId,&wfx,(DWORD)hWnd,0,CALLBACK_WINDOW);
 if(rc!=MMSYSERR_NOERROR)
 {
  waveOutGetErrorText(rc,mssg,MSG_LEN);
  sprintf(aa,"WaveOutGetErrorText = %s",mssg);
  log(aa);
  MessageBox(0,"Error in WaveOutOpen","SPEAK EAZY - Terminating",0);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
 else
 {
  sprintf(aa,"NoError in WAVEoutOPEN");
  log(aa);
 }
}

//========================================
VOID WaveStart()
//========================================
{
 rc=waveInStart(hwi);
 if(rc==MMSYSERR_NOERROR);
 else
 {
  waveInGetErrorText(rc,mssg,MSG_LEN);
  sprintf(aa,"WaveInGetErrorText = %s",mssg);
  log(aa);
  MessageBox(0,"Error in Device While Recording","SPEAK EAZY - Terminating ...",0);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
}

//================================
VOID WaveStop()
//================================
{
 rc=waveInStop(hwi);
 waveInGetErrorText(rc,mssg,MSG_LEN);
 sprintf(aa,"WaveInStopGetErrorText = %s",mssg);
 log(aa);
 if(rc!=MMSYSERR_NOERROR)
 {
  MessageBox(0,"Error in Stopping Record Device","SPEAK EAZY - Terminating ...",0);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
}

//========================================
VOID WaveReset()
//========================================
{
 rc=waveInReset(hwi);
 waveInGetErrorText(rc,mssg,MSG_LEN);
 sprintf(aa,"WaveInResetGetErrorText = %s",mssg);
 log(aa);
 if(rc!=MMSYSERR_NOERROR)
 {
  MessageBox(0,"Error in Resetting Record Device","SPEAK EAZY - Terminating ...",0);
  PostMessage(hWnd,WM_CLOSE,0,0L);
 }
}

//========================================
VOID WaveHeadUnprepare()
//========================================
{
  rc=waveInUnprepareHeader(hwi,lpwh[i],sizeof(WAVEHDR));
  waveInGetErrorText(rc,mssg,MSG_LEN);
  if(rc!=MMSYSERR_NOERROR)
  {
   MessageBox(0,"Error in UNPREPARING HEADER","SPEAK EAZY - Terminating ...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
  }
}

//========================================
VOID WaveClose()
//========================================
{
  rc=waveInClose(hwi);
  waveInGetErrorText(rc,mssg,MSG_LEN);
  sprintf(aa,"WaveInClose ErrorText = %s",mssg);
  log(aa);
  if(rc!=MMSYSERR_NOERROR)
  {
   MessageBox(0,"Error in Closing Device","SPEAK EAZY - Terminating ...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
  }
}

//========================================
VOID WaveHeadPrepare()
//========================================
{
   rc=waveInPrepareHeader(hwi,lpwh[i],sizeof(WAVEHDR));
   if (rc==MMSYSERR_NOERROR)
   { rc=waveInAddBuffer(hwi,lpwh[i],sizeof(WAVEHDR));
    waveInGetErrorText(rc,mssg,MSG_LEN);
   }
   else
   {
    waveInGetErrorText(rc,mssg,MSG_LEN);
    sprintf(aa,"WaveInPrepareHeaderErrorText = %s",mssg);
    log(aa);
    MessageBox(0,"Error in Preparing Header","SPEAK EAZY - Terminating ...",0);
    PostMessage(hWnd,WM_CLOSE,0,0L);
   }
}

//========================================
NumOfWaveDevice()
//========================================
{
 nMaxDevices=waveInGetNumDevs();
  if (nMaxDevices==0)
  {
   MessageBox(0,"\tNo Audio Device Detected \n Check the AudioDevice and then restart the Application","SPEAK EAZY - Terminating ...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
  }
  sprintf(aa,"nMaxDevices = %ld",nMaxDevices);
  log(aa);
}

//========================================
VOID squelch(long i)
//========================================
{
 total_amplitude=0;
 sql=0.963*sql+0.03*average_amplitude+1.5;
 for(j=0;j<RECORD_BUFFER_SIZE;j++)
 {
  total_amplitude+=abs(*(lpwh[i]->lpData+j));
 }
 average_amplitude=total_amplitude/RECORD_BUFFER_SIZE;
 

 if (average_amplitude>sql)
  count++;
 else
 {
  cut_off=0;
  count=count-1;
  if(count<0)
   count=0;
 }
 if(count>10 && i>15)
 {
  count=count-2;
  cut_off++;

  if(cut_off==10)
  {
  stopped=1;
  count_for_Threshold_Squelch=1;

  }
 }
}
//========================================
VOID StopRecord()
//========================================
{
 sprintf(aa,"StopRecord has been called");
 log(aa);
 WaveReset();
 sprintf(aa,"Stoped=%d",stopped);
 stopped=0;
 sprintf(aa,"%d",sql);
 for(i=0;i<MAX_BUFFERS;i++)
 {
   WaveHeadUnprepare();
 }
 WaveClose();

 if(Terminateconn==0)
 {
  Open_Dev_For_Play(hWnd);
 }
}

//========================================
VOID Declare_Variables()
//========================================
{
average_amplitude=0;
sql=130;
buff=0;
stopped=0;
waveinresetnumber=0;
count=0;
z=0;
buff=0;
i=0;
j=0;
count_for_Threshold_Squelch=0;
ddd=0;
}

//========================================
VOID StartPlay(int ibuff)
//========================================
{
  rc=waveOutPrepareHeader(hwo,lpwho[ibuff],sizeof(WAVEHDR));
  if(rc!=MMSYSERR_NOERROR)
  {
   MessageBox(0,"Cannot Prepare Playback Header","SPEAK EAZY - Terminating...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
  }
  rc = waveOutWrite(hwo,lpwho[ibuff],sizeof(WAVEHDR));
  if(rc!=MMSYSERR_NOERROR)
  {
   MessageBox(0,"Cannot Write to Output Device","SPEAK EAZY - Terminating...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
  }
}

//========================================
VOID StopPlay()
//========================================
{
  rc=waveOutReset(hwo);
  waveOutGetErrorText(rc,mssg,MSG_LEN);
  sprintf(aa,"waveOutGetErrorText=%s\n",mssg);
  log(aa);
  if(rc!=MMSYSERR_NOERROR)
  {
   MessageBox(0,"Cannot Reset Output Device","SPEAK EAZY - Terminating...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
  }

  for (i=0;i<MAX_BUFFERS;i++)
  {
   rc=waveOutUnprepareHeader(hwo,lpwho[i],sizeof(WAVEHDR));
   waveOutGetErrorText(rc,mssg,MSG_LEN);
   if(rc!=MMSYSERR_NOERROR)
   {
    MessageBox(0,"Cannot Unprepare Output Header","SPEAK EAZY - Terminating...",0);
    PostMessage(hWnd,WM_CLOSE,0,0L);
   }
  }
  rc=waveOutClose(hwo);
  waveOutGetErrorText(rc,mssg,MSG_LEN);
  sprintf(aa,"WaveOutClose=%s\n",mssg);
  if(rc!=MMSYSERR_NOERROR)
  {
   MessageBox(0,"Cannot Close Playback Device","SPEAK EAZY - Terminating...",0);
   PostMessage(hWnd,WM_CLOSE,0,0L);
  }
}

//========================================
VOID StartRecording()
//========================================
{

  sprintf(aa,"\n==============================================\n");
  log(aa);
  Declare_Variables();
  NumOfWaveDevice();
  count_for_Threshold_Squelch=0;
  nDevId=0;
  Open_Dev_For_Rec(hWnd);
  for(i=0;i<MAX_BUFFERS;i++)
  {
   WaveHeadPrepare();
  }
  InvalidateRgn(hWnd,NULL,TRUE);
  sprintf(TxtOut,"You can Start Talking Now......");
  PostMessage(hWnd,WM_PAINT,0,0L);
  WaveStart();
}

//========================================
void log(char *logtxt)
//========================================
{
 fplog=fopen("log.txt","a+");
 fprintf(fplog,"%s\n",logtxt);
 fclose(fplog);
}



Back to View/Download Index                                                                                               Back to Vijay Mukhi.com