================================================================
音频发送端:
=================================================================
RecTestDlg.h
--------------------------------------------------------------
// RecTestDlg.h : header file
//
#if
!defined(AFX_RECTESTDLG_H__290A608A_1E69_46C6_BB47_939AA3598904__INCLUDED_)
#define
AFX_RECTESTDLG_H__290A608A_1E69_46C6_BB47_939AA3598904__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/
// CRecTestDlg dialog
#define InBlocks 4//存储输入音频数据的单元数
#define INP_BUFFER_SIZE 16384
#include "stdafx.h"
struct CAudioData
{
PBYTE lpdata;
DWORD dwLength;
};
class CRecTestDlg : public CDialog
{
// Construction
public:
CRecTestDlg(CWnd* pParent =
NULL); // standard constructor
CString
GetError(DWORD error);
// Dialog Data
//{{AFX_DATA(CRecTestDlg)
enum { IDD = IDD_RECTEST_DIALOG };
CString m_ip;
//}}AFX_DATA
// ClassWizard generated virtual function
overrides
//{{AFX_VIRTUAL(CRecTestDlg)
protected:
virtual void DoDataExchange(CDataExchange*
pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
HWAVEIN
hWaveIn; //声音输入
//输出设备
PWAVEHDR
pWaveHdr1,pWaveHdr2; //相关"头结构体" WAVEFORMATEX
m_waveformin;
PBYTE
pBuffer1,pBuffer2;//输入设备所用缓冲区
public:
//用于暂存录入后要发送的及接收到的即将要播放的声音文件的循环队列,
CAudioData m_AudioDataIn[InBlocks];
int nAudioIn,
nSend; //录入、发送指针
//接收、播放指针
//对于录音和放音都存在和网络的同步问题,主要靠这些指针进行协调
// Generated message map functions
//{{AFX_MSG(CRecTestDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM
lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnBegin();
afx_msg void OnMM_WIM_DATA(UINT wParam,LONG
lParam);
afx_msg void OnMM_WIM_CLOSE(UINT wParam,LONG
lParam);
afx_msg void OnMM_WOM_CLOSE(UINT wParam,LONG
lParam);
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations
immediately before the previous line.
#endif //
!defined(AFX_RECTESTDLG_H__290A608A_1E69_46C6_BB47_939AA3598904__INCLUDED_)
-----------------------------------------------------------------
RecTestDlg.cpp
---------------------------------------------------------------
// RecTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RecTest.h"
#include "RecTestDlg.h"
# include "SockInfo.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function
overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange*
pDX); //
DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CRecTestDlg dialog
CRecTestDlg::CRecTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRecTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRecTestDlg)
m_ip = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a
subsequent DestroyIcon in Win32
m_hIcon =
AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRecTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRecTestDlg)
DDX_Text(pDX, IDC_IP, m_ip);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRecTestDlg, CDialog)
//{{AFX_MSG_MAP(CRecTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BEGIN, OnBegin)
ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)
ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)
ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CRecTestDlg message handlers
BOOL CRecTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system
menu.
// IDM_ABOUTBOX must be in the system
command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) ==
IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu =
GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if
(!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,
IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this
dialog. The framework does this
automatically
// when the application's main
window is not a dialog
SetIcon(m_hIcon,
TRUE); // Set
big icon
SetIcon(m_hIcon,
FALSE); // Set small icon
// TODO: Add extra initialization here
//allocate memory for wave header
pWaveHdr1=reinterpret_cast(malloc(sizeof(WAVEHDR)));
pWaveHdr2=reinterpret_cast(malloc(sizeof(WAVEHDR)));
//***reinterpret_cast **** operation is used to convert any type of
//pointer to other type
//allocate memory for save buffer
for(int i=0;i
{
m_AudioDataIn[i].dwLength =
0;
m_AudioDataIn[i].lpdata =
reinterpret_cast(malloc(1));
}
nAudioIn = 0;
nSend = 0;
return TRUE; // return
TRUE unless you set the focus to a control
}
void CRecTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID,
lParam);
}
}
// If you add a minimize button to your dialog, you will need
the code below
// to draw the icon. For MFC
applications using the document/view model,
// this is automatically done for you by the
framework.
void CRecTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device
context for painting
SendMessage(WM_ICONERASEBKGND,
(WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client
rectangle
int cxIcon =
GetSystemMetrics(SM_CXICON);
int cyIcon =
GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon
+ 1) / 2;
int y = (rect.Height() - cyIcon
+ 1) / 2;
// Draw the icon
dc.DrawIcon(x, y,
m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while
the user drags
// the minimized window.
HCURSOR CRecTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT Audio_Send_Thread(LPVOID lParam)
{
CRecTestDlg *pdlg = (CRecTestDlg*)lParam;
CSockInfo
m_info;
CSocket m_Server;
CString ip;
ip=pdlg->m_ip;//从对话框获取接收端IP
m_Server.Create(6800,SOCK_DGRAM,NULL);
//数据报(UDP),端口6800
//绑定本地套接口
m_Server.Bind(6800,m_info.GetLocalHostIP());
DWORD ret,length;
while(1)//循环使用指针nSend
{
length
=pdlg->m_AudioDataIn[pdlg->nSend].dwLength; if(length !=0)
{ //首先发送块的长度
if(((ret = m_Server.SendTo(&length,sizeof(DWORD),6900,ip,0)
)!=sizeof(DWORD))||(ret==SOCKET_ERROR))
{ AfxMessageBox("声音文件头传输错误!"+pdlg->GetError(GetLastError()));
pdlg->OnOK();
break; }//其次发送块的内容 ,循环检测发送
DWORD
dwSent = 0;//已经发送掉的字节数
while(1)//==============================发送声音文件开始
{
ret
=
m_Server.SendTo((pdlg->m_AudioDataIn[pdlg->nSend].lpdata+dwSent),(length-dwSent),6900,ip,0);
if(ret==SOCKET_ERROR)//检错
{
AfxMessageBox("声音文件传输错误!"+pdlg->GetError(GetLastError()));
pdlg->OnOK();
break; }
else
//发送未发送完的
{
dwSent
+= ret;
if(dwSent
==length)
{ free(pdlg->m_AudioDataIn[pdlg->nSend].lpdata);
pdlg->m_AudioDataIn[pdlg->nSend].dwLength
= 0;
break;
}
} //======================================发送声音文件结束
}
}
pdlg->nSend
= (pdlg->nSend +1)% InBlocks;
}
m_Server.Close();
pdlg->OnOK();
return 0;
}
CString CRecTestDlg::GetError(DWORD
error) //返回错误信息
{
CString strError;
switch(error)
{
case WSANOTINITIALISED:
strError="初始化错误";
break;
case WSAENOTCONN:
strError="对方没有启动";
break;
case WSAEWOULDBLOCK :
strError="对方已经关闭";
break;
case WSAECONNREFUSED:
strError="连接的尝试被拒绝";
break;
case WSAENOTSOCK:
strError="在一个非套接字上尝试了一个操作";
break;
case WSAEADDRINUSE:
strError="特定的地址已在使用中";
break;
case WSAECONNRESET:
strError="与主机的连接被关闭";
break;
default:
strError="一般错误"; }
return strError;
}
void CRecTestDlg::OnBegin()
{
// TODO: Add your control notification handler
code here
GetDlgItem(IDC_BEGIN)->EnableWindow(false);
UpdateData(true);
//allocate buffer memory
pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE);
pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);
if (!pBuffer1 || !pBuffer2)
{
if (pBuffer1)
free(pBuffer1);
if (pBuffer2)
free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
AfxMessageBox("Memory
erro!");
return ;
}
//open waveform audio for input
m_waveformin.wFormatTag=WAVE_FORMAT_PCM;
m_waveformin.nChannels=1;
m_waveformin.nSamplesPerSec=10600;//采样频率
m_waveformin.nAvgBytesPerSec=10600;
m_waveformin.nBlockAlign=1;
m_waveformin.wBitsPerSample=16;
m_waveformin.cbSize=0;
if
(waveInOpen(&hWaveIn,WAVE_MAPPER,&m_waveformin,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW))
{ //打开录音设备函数
free(pBuffer1);
free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
AfxMessageBox("Audio can not be
open!");
}
pWaveHdr1->lpData=(LPTSTR)pBuffer1;
pWaveHdr1->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr1->dwBytesRecorded=0;
pWaveHdr1->dwUser=0;
pWaveHdr1->dwFlags=0;
pWaveHdr1->dwLoops=1;
pWaveHdr1->lpNext=NULL;
pWaveHdr1->reserved=0;
waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
pWaveHdr2->lpData=(LPTSTR)pBuffer2;
pWaveHdr2->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr2->dwBytesRecorded=0;
pWaveHdr2->dwUser=0;
pWaveHdr2->dwFlags=0;
pWaveHdr2->dwLoops=1;
pWaveHdr2->lpNext=NULL;
pWaveHdr2->reserved=0;
waveInPrepareHeader(hWaveIn,pWaveHdr2,sizeof(WAVEHDR));
// Add the buffers
waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof
(WAVEHDR)) ;
waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof
(WAVEHDR)) ;
// Begin sampling
waveInStart (hWaveIn) ;
::AfxBeginThread(Audio_Send_Thread,this);
return ;
}
void CRecTestDlg::OnMM_WIM_DATA(UINT wParam,LONG lParam)
{
int
nextBlock = (nAudioIn+1)% InBlocks; if(m_AudioDataIn[nextBlock].dwLength!=0)//下一“块”没发走
{ //把PWAVEHDR(即pBUfferi)里的数据拷贝到当前“块”中
m_AudioDataIn[nAudioIn].lpdata = (PBYTE)realloc
(m_AudioDataIn[nAudioIn].lpdata , (((PWAVEHDR)
lParam)->dwBytesRecorded+m_AudioDataIn[nAudioIn].dwLength))
;
if
(m_AudioDataIn[nAudioIn].lpdata == NULL)
{
waveInClose
(hWaveIn) ;
MessageBeep
(MB_ICONEXCLAMATION) ;
AfxMessageBox("erro
memory OnMM_WIM_DATA");
return
;
}
CopyMemory
((m_AudioDataIn[nAudioIn].lpdata+m_AudioDataIn[nAudioIn].dwLength),
((PWAVEHDR) lParam)->lpData,
((PWAVEHDR) lParam)->dwBytesRecorded)
;//(*destination,*resource,nLen);
m_AudioDataIn[nAudioIn].dwLength
+=((PWAVEHDR) lParam)->dwBytesRecorded;
}
else
//把PWAVEHDR(即pBUfferi)里的数据拷贝到下一“块”中
{
nAudioIn = (nAudioIn+1)%
InBlocks;
m_AudioDataIn[nAudioIn].lpdata
= (PBYTE)realloc
(0,((PWAVEHDR)
lParam)->dwBytesRecorded);
CopyMemory(m_AudioDataIn[nAudioIn].lpdata,
((PWAVEHDR) lParam)->lpData,
((PWAVEHDR)
lParam)->dwBytesRecorded) ;
m_AudioDataIn[nAudioIn].dwLength =((PWAVEHDR)
lParam)->dwBytesRecorded;
}
// Send out a new
buffer waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam,
sizeof (WAVEHDR)) ;
return ;
}
void CRecTestDlg::OnMM_WIM_CLOSE(UINT wParam,LONG
lParam)
{
waveInUnprepareHeader (hWaveIn, pWaveHdr1,
sizeof (WAVEHDR)) ;
waveInUnprepareHeader (hWaveIn, pWaveHdr2, sizeof
(WAVEHDR)) ;
free (pBuffer1) ;
free (pBuffer2) ;
return ;
}
void CRecTestDlg::OnMM_WOM_CLOSE(UINT wParam,LONG lParam)
{
//release all the memory of the
AudioData
for(int i=0;i
{
if(m_AudioDataIn[i].dwLength !=
0)
free(m_AudioDataIn[i].lpdata);
}
}
void CRecTestDlg::OnOK()
{
// TODO: Add extra validation here
waveInReset(hWaveIn);
waveInClose(hWaveIn);
CDialog::OnOK();
}