⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.cpp

📁 网上搜的关于语音聊天的语音聊天系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
////////////////////////////////////////////////////////////////////
// Recording and playing Thread program concept is based on the 
// code by ***  Paul Chauffers..***...from www.codeguru.com
//                                         
// I am greatful to him  for helping me to turn my dream project into reality.
//
// Display.cpp: implementation of the Display class.
//
//////////////////////////////////////////////////////////////////////


#include<afxwin.h>
#include<afxcmn.h>
#include<afxdlgs.h>
#include "about.h"
#include "Display.h"
#include "PlaySound1.h"
#include "resource.h"
#include "Volume.h"
#include "Save.h"
#include "WriteSound.h"
#include "Broadcast.h"

//////////////////////////
//	MESSAGE MAPPING     //
//////////////////////////

BEGIN_MESSAGE_MAP(Display,CDialog)
ON_COMMAND(IDC_BUTTON1,Onconnect)
ON_COMMAND(IDC_BUTTON2,OnSave)
ON_COMMAND(IDC_BUTTON3,OnVolume)
ON_COMMAND(IDC_BUTTON4,OnPlay)
ON_COMMAND(IDC_BUTTON5,OnStart)
ON_COMMAND(IDC_BUTTON6,OnStop)
ON_COMMAND_RANGE(IDC_RADIO1,IDC_RADIO2,OnChange)
ON_WM_DESTROY()
ON_WM_PAINT()
//ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()



//
//Constructor
//
Display::Display(int n):CDialog(n)
{

isconnected=FALSE;
sendcount=0;
reccount=0;
//create the buffers for playing.....buffers are reused..
PreCreateHeader();
log.Open("log.txt",CFile::modeCreate | CFile::modeWrite);

}



//
// Destructor
//
Display::~Display()
{
log.WriteString("\n Deallocating the memory");
log.Close();

//if(anicon && success)
//anicon->Close();
	
	//Release the allocated memory
	for(int i=0;i<MAXBUFFER;i++)
	{
	if(playhead[i]->lpData)
	delete playhead[i]->lpData;
	if(playhead[i])
	delete playhead[i];
	}
}



/*                                                           */
/* This function precreates the waveblocks for storing       */
/* voice data before playing.This will allow the REUSE of    */
/* waveblocks.                                               */
/*                                                           */ 

void Display::PreCreateHeader()
{
	//Initial waveblock
	curhead=0;
	
	isstart=1;

	for(int i=0;i<MAXBUFFER;i++)
	{
	playhead[i] = new WAVEHDR;
	ZeroMemory(playhead[i], sizeof(WAVEHDR));  //Initialize it to zero
	char *temp=new char[PLAYBUFFER+50];	
	
	playhead[i]->lpData =temp;
	playhead[i]->dwBufferLength = PLAYBUFFER;
	playhead[i]->dwFlags=0;
	}

}


int Display::OnEraseBkgnd(CDC *pdc)
{
	log.WriteString("\nIn the Onerase function");

	return CDialog::OnEraseBkgnd(pdc);

}


/*                                                           */
/* This function does initial operations such                */
/* creating Record and Play threads...                       */
/*                                                           */ 

int Display::OnInitDialog()
{
CFont myfont;	
	

	isSave=FALSE;

	myfont.CreateFont(20,12,0,0,0,0,0,0,0,0,0,0,0,"System");	

	SetDlgItemText(IDC_EDIT1,"localhost");
	SetDlgItemInt(IDC_EDIT2,1051);
	SetDlgItemText(IDC_EDIT3,"logname");

	cbox=(CComboBox*)GetDlgItem(IDC_COMBO1);

	anicon=(CAnimateCtrl*)GetDlgItem(IDC_ANIMATE1);
	talk=(CAnimateCtrl*)GetDlgItem(IDC_ANIMATE2);
	

	if(!anicon->Open("conn.avi"))
	{
	success=FALSE;
	log.WriteString("\nUnable to open the avi file ****");
	}
	else
	success=TRUE;
	
	if(!talk->Open("talk.avi"))
	{
	doit=FALSE;
	log.WriteString("\n Unable to open the talk.avi file");
	}
	else
	doit=TRUE;

//	from=(CListBox *)GetDlgItem(IDC_LIST1);
//	from->SetFont(&myfont);

	curuser.Empty();


	//Set the Icon for the  application
	HICON m_hicon=AfxGetApp()->LoadIcon(IDI_ICON1);
	SetIcon(m_hicon,TRUE);
	SetIcon(m_hicon,FALSE);

	
	//Initializes  socket 
	if(!::AfxSocketInit())	
	{
	log.WriteString("\nSocket initialization failed");
	return 0;
	}

	//Create Client socket object
	sockclt.setparent(this);
	sockclt.Create();

	//Create and Start Recorder Thread
	record=new RecordSound(this);
	record->CreateThread();
	
	//Create and Start Player Thread
	play=new PlaySound1(this);
	play->CreateThread();
	
	//Create the Writer Thread to save the sound
	write=new WriteSound();
	write->CreateThread();

	selectflag=0;
	start=stop=NULL;

return CDialog::OnInitDialog();
}



/*                                                           */
/* OnPaint() will fill the client area with                  */
/* the selected gradient color.                              */
/*                                                           */ 

void Display::OnPaint()
{
CRect r;
CPen p[64];
int i,factor;

//log.WriteString("\n In the OnPaint fun");
CPaintDC pdc(this);


	GetClientRect(&r);
	factor=r.bottom/63;

	for(i=0;i<64;i++)
	p[i].CreatePen(PS_SOLID,1,RGB(250,120+i*2,0));
	
	for(i=0;i<r.bottom;i++)
	{
	pdc.SelectObject(&p[i/factor]);
	pdc.MoveTo(0,i);
	pdc.LineTo(r.right,i);
	}

}


/*                                                           */
/* This function will paint the bkgnd of individual          */
/* components of dialog box                                  */
/*                                                           */ 

HBRUSH Display::OnCtlColor(CDC *pdc,CWnd *pwnd,UINT ctrl)
{
int id=pwnd->GetDlgCtrlID();
//log.WriteString("\n In the on ctrl color fun");

pdc->SetTextColor(RGB(0,0,255));

	switch(id)
	{

	case 1098:case 6051:case 1099:case 1086:case 1075:case 1071:
    case IDC_RADIO1:case IDC_RADIO2:
	pdc->SetBkMode(TRANSPARENT);
	HBRUSH hbr=CreateSolidBrush(RGB(255,250,220));
	return hbr;

	}

return NULL;
}



/*                                                           */
/* When user presses the Connect Button this                 */
/* method will be called                                     */
/*                                                           */ 

void Display::Onconnect()
{
int port;
CString sername,username,mesg;	

	
	//Write to log file
	log.WriteString("\n Connecting to Server");

	//Get data from dialog box
	GetDlgItemText(IDC_EDIT1,sername);
	GetDlgItemText(IDC_EDIT3,username);
	port=GetDlgItemInt(IDC_EDIT2);
	
	if(username=="" || sername=="" )
	{
		AfxMessageBox("Please Enter the data properly");
		return;
	}
	
	if(success)
	{
		anicon->Play(0,-1,-1);
		log.WriteString("\n Playing data");
	}
	
	//Connect the client to server
	username.MakeUpper();
	sockclt.name=username;
	
	SetDlgItemText(IDC_BUTTON1,"Connecting");

	if(sockclt.Connect(sername,port))
		log.WriteString("\n Successfully Connected to Server");
	else
	{
		SetDlgItemText(IDC_BUTTON1,"&Connect");
		if(success)
		anicon->Stop();
		
		MessageBox("Unable to Connect to Server\nVerify if Server name and Port are correct");
		log.WriteString("\n Unable to connect to Server");
		
		
		return;
	}
	
	isconnected=TRUE;
	

	if(success)
	anicon->Stop();
	
	
	//Send the login message to server 
	mesg="NEW:"+username;

	if(sockclt.Send(mesg,mesg.GetLength()))
	log.WriteString("\n login Mesg sent to Server");
	else
	log.WriteString("\n Unable to send mesg to Server");
	
	updateState(FALSE,TRUE);

}




/*                                                           */
/* This function updates the state of dialog box             */
/* when the client is successfully connected to server.       */
/*                                                           */ 
void Display::updateState(BOOL pstate,BOOL nstate)
{
CEdit *eser,*eport,*euser;
CStatic *sser,*sport,*suser,*sgroup1,*sgroup2;
CButton *con;

		//Destroy server related items	
		con=(CButton*)GetDlgItem(IDC_BUTTON1);
		con->ShowWindow(pstate); 
		eser=(CEdit*)GetDlgItem(IDC_EDIT1);
		eser->ShowWindow(pstate); 
		
		
		eport=(CEdit*)GetDlgItem(IDC_EDIT2);
		eport->ShowWindow(pstate); 	
		
		euser=(CEdit*)GetDlgItem(IDC_EDIT3);
		euser->ShowWindow(pstate); 
		
		sser=(CStatic*)GetDlgItem(1098);
		sser->ShowWindow(pstate); 
		
		suser=(CStatic*)GetDlgItem(1086);
		suser->ShowWindow(pstate);

		
		sport=(CStatic*)GetDlgItem(1099);
		sport->ShowWindow(pstate); 

		sgroup1=(CStatic*)GetDlgItem(1097);
		sgroup1->ShowWindow(pstate); 



		//Enable Send to items
		sgroup2=(CStatic*)GetDlgItem(1075);
		sgroup2->ShowWindow(nstate);

		radio1=(CButton*)GetDlgItem(IDC_RADIO1);
		radio2=(CButton*)GetDlgItem(IDC_RADIO2);
		start=(CButton*)GetDlgItem(IDC_BUTTON5);
		stop=(CButton*)GetDlgItem(IDC_BUTTON6);
		
		
		radio1->ShowWindow(nstate);
		radio2->ShowWindow(nstate);

		radio1->SetCheck(1);
		radio2->SetCheck(0);

		start->ShowWindow(nstate);
		cbox->ShowWindow(nstate);
		if(doit==TRUE)
		talk->ShowWindow(TRUE);
		anicon->ShowWindow(FALSE);
	
		
}



/*											                  */	
/*  This function will be caled when the client               */
/*  receives message from server                              */ 
/*	(called form "mysocket" OnReceive function )              */	
/*											                  */	

void Display::Receive()
{
char buff[PLAYBUFFER+25],str[PLAYBUFFER+60];
int size=PLAYBUFFER+20,rcount,index,i,j,length;
CString mesg,header,disp;



rcount=sockclt.Receive(buff,size);

	if(rcount==SOCKET_ERROR)
	{
	log.WriteString("\nError in Receiving the data");
	return;
	}

	if(rcount>(PLAYBUFFER+20))
	{
		log.WriteString("\nMesg size exceeded buffer capacity");
		return;
	}
	

	buff[rcount]=NULL;
	
	mesg=buff;
	
//	sprintf(str,"Received data len= %d , %s ",rcount,&buff[20]);
//	log.WriteString(str);

	
	
			index=mesg.Find(':');
			
			
			//Invalid message Format --must have atleast one tag
			if(index==-1)
			return;
			
						
			header=mesg.Left(index);
						
			//Check if server has sent the list of clients
			if(header=="USER")
			{
				mesg=mesg.Right(mesg.GetLength()-index-1);
				updateList(mesg);
				return;
			}
			
			if(header=="WAIT")
			{
			    if(isstart==0)  //stop the chat
				OnStop();
				start->EnableWindow(FALSE);
				mesg=mesg.Right(mesg.GetLength()-index-1);
				showFlash();
				//disp=" Please wait....\n "+mesg+" is broadcasting";
				//SetDlgItemText(6051,(char*)(LPCTSTR)disp);
				MessageBox("Please wait....\n "+mesg+" is broadcasting");
				return;				
			}
			
			if(header=="RESUME")
			{
				start->EnableWindow(TRUE);
				showFlash();
				disp=" Broadcasting is over \n Now You can continue...";
				SetDlgItemText(6051,(char*)(LPCTSTR)disp);
			//	MessageBox(" Broadcasting is over \n Now You can continue...");
				log.WriteString("\n****Received the resume message****");
				return;
			}
			
			
			else           //play the voice data
			{
				//No of messages received
				
				//***  Part of this code has to be removed later  ***\\
				
				
				//Get the legth of buffer
				length=0;
				sscanf(&buff[15],"%d",&length);
				
				if(length<1 || length>PLAYBUFFER)
				return;

				//If audio is not playing, start playing	
			
				
				if(play->Playing==FALSE)
				play->PostThreadMessage(WM_PLAYSOUND_STARTPLAYING,0,0);
				

				LPWAVEHDR lpHdr=playhead[curhead];
				curhead=(curhead+1)%MAXBUFFER;
			
				//	playmesg=new char[2020];
				for(i=20,j=0;j<length;i++,j++)
				lpHdr->lpData[j]=buff[i];

				lpHdr->dwBufferLength=length;
				lpHdr->dwFlags=0;

								
				play->PostThreadMessage(WM_PLAYSOUND_PLAYBLOCK,0,(LPARAM)lpHdr);
				
				//If Save button is pressed....
				//Save the Voice data from perticular user
				if(isSave==TRUE && writeuser.CompareNoCase(header)==0)
				{
				write->PostThreadMessage(WM_WRITESOUND_WRITEDATA,0,(LPARAM)lpHdr);
				log.WriteString("\n Writing to store");			
				}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -