www.pudn.com > ÓïÒôAudioCap.rar > AudioCapDlg.cpp


// AudioCapDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "AudioCap.h" 
#include "AudioCapDlg.h" 
 
#include "WaveForm.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
CWaveIn G_WaveIn; 
CWaveOut G_WaveOut; 
 
char lpTemp[256]=""; 
FILE * fp; 
DWORD FCC(LPSTR lpStr) 
{ 
   DWORD Number = lpStr[0] + lpStr[1] *0x100 + lpStr[2] *0x10000 + lpStr[3] *0x1000000 ; 
   return Number; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAudioCapDlg dialog 
 
CAudioCapDlg::CAudioCapDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CAudioCapDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CAudioCapDlg) 
	m_sDataRec = _T(""); 
	m_sDataPlay = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CAudioCapDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAudioCapDlg) 
	DDX_Control(pDX, IDC_DATAREC, m_ctrlRec); 
	DDX_Text(pDX, IDC_DATAREC, m_sDataRec); 
	DDX_Text(pDX, IDC_DATAPLAY, m_sDataPlay); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAudioCapDlg, CDialog) 
	//{{AFX_MSG_MAP(CAudioCapDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_DESTROY() 
	ON_BN_CLICKED(IDC_TEST, OnTest) 
	ON_BN_CLICKED(IDC_CAPINIT, OnCapInit) 
	ON_BN_CLICKED(IDC_CAPDEINIT, OnCapDeinit) 
	ON_BN_CLICKED(IDC_PLAYINIT, OnPlayInit) 
	ON_BN_CLICKED(IDC_PLAYDEINIT, OnPlayDeinit) 
	ON_BN_CLICKED(IDC_PLAY, OnPlay) 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(MM_WIM_DATA, OnWaveInData) 
	ON_MESSAGE(MM_WOM_DONE, OnWaveOutDone) 
	 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAudioCapDlg message handlers 
 
BOOL CAudioCapDlg::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 
 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CAudioCapDlg::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 CAudioCapDlg::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 CAudioCapDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CAudioCapDlg::OnDestroy()  
{ 
	CDialog::OnDestroy(); 
	 
	// TODO: Add your message handler code here 
 
} 
 
void CAudioCapDlg::OnWaveInData(WPARAM wParam, LPARAM lParam) 
{ 
	if (G_WaveIn.m_bRecord) 
	{ 
		G_WaveIn.ContinueRecording(); 
		int index=(G_WaveIn.m_iWaveInNextBuffer+WAVE_FORM_BUFFER_NUMBER-1)%WAVE_FORM_BUFFER_NUMBER; 
		char * sound =(char *)malloc(G_WaveIn.m_iWaveLength); 
		ZeroMemory(sound,G_WaveIn.m_iWaveLength); 
		VoiceFileTest.Write(G_WaveIn.m_pWaveInData[index],G_WaveIn.m_iWaveLength); 
		memcpy(sound,G_WaveIn.m_pWaveInData[index],G_WaveIn.m_iWaveLength); 
		G_WaveOut.Play(sound,G_WaveIn.m_iWaveLength); 
		VoiceFile.Write(sound,G_WaveIn.m_iWaveLength); 
		MixFileHeader.dwDataSize += G_WaveIn.m_iWaveLength; 
		m_sDataRec.Format("%d",MixFileHeader.dwDataSize); 
		m_ctrlRec.SetWindowText(m_sDataRec); 
		TRACE("%d\n",MixFileHeader.dwDataSize); 
		free(sound); 
	} 
} 
 
 
 
void CAudioCapDlg::OnTest()  
{ 
	// TODO: Add your control notification handler code here 
 
} 
 
void CAudioCapDlg::OnCapInit()  
{ 
	// TODO: Add your control notification handler code here 
	CFile	SrcFile1; 
	SrcFile1.Open("16b.wav",CFile::modeRead); 
	SrcFile1.Seek(0,CFile::begin); 
	SrcFile1.Read((char *)&MixFileHeader,sizeof(WAVEFILEHEADER)); 
	SrcFile1.Close(); 
 
	VoiceFile.Open("voice.wav",CFile::modeCreate|CFile::modeReadWrite); 
	VoiceFile.Write((char *)&MixFileHeader,sizeof(WAVEFILEHEADER)); 
	MixFileHeader.dwDataSize = 0; 
	VoiceFileTest.Open("voice.hex",CFile::modeCreate|CFile::modeReadWrite); 
 
	G_WaveIn.Init(GetSafeHwnd(),40,0);	 
	G_WaveIn.Start(); 
	 
} 
 
void CAudioCapDlg::OnCapDeinit()  
{ 
	// TODO: Add your control notification handler code here 
	G_WaveIn.Stop(); 
 	G_WaveIn.m_bRecord=false; 
 	G_WaveIn.Free(); 
 
	MixFileHeader.dwFileSize = MixFileHeader.dwDataSize + 0x30; 
	VoiceFile.Seek(0,CFile::begin); 
	VoiceFile.Write((CHAR *)&MixFileHeader,sizeof(WAVEFILEHEADER)); 
	VoiceFile.Close(); 
	VoiceFileTest.Close(); 
	 
} 
 
void CAudioCapDlg::OnPlayInit()  
{ 
	// TODO: Add your control notification handler code here 
	G_WaveOut.Init(GetSafeHwnd(),40,0); 
	G_WaveOut.Start(); 
} 
 
void CAudioCapDlg::OnPlayDeinit()  
{ 
	// TODO: Add your control notification handler code here 
 	G_WaveOut.Stop(); 
 	G_WaveOut.m_bPlay=false; 
 	G_WaveOut.Free();//so serious error ,just have a try 
} 
 
void CAudioCapDlg::OnWaveOutDone(WPARAM, LPARAM) 
{ 
	static int playlength=0; 
	playlength +=640; 
	m_sDataPlay.Format("%d",playlength); 
	UpdateData(FALSE); 
	if(G_WaveOut.m_bPlay) 
		G_WaveOut.ContinuePlaying(); 
	TRACE("*******************************%d\n",playlength); 
} 
 
void CAudioCapDlg::OnPlay()  
{ 
	// TODO: Add your control notification handler code here 
	CFile WavFile; 
	WavFile.Open("voice.wav",CFile::modeRead); 
	WavFile.SeekToBegin(); 
	WavFile.Seek(sizeof(WAVEFILEHEADER),CFile::begin); 
	int nDataSize=WavFile.GetLength(); 
	nDataSize = 8000*5*2; 
/* 
	AfxMessageBox(CString(nDataSize)); 
	WavFile.Close(); 
	return; 
*/ 
	int i=0; 
	char sound[1024]; 
//	while(i