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

📄 recorder1dlg.cpp

📁 EVC下的小录音程序
💻 CPP
字号:
// Recorder1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Recorder1.h"
#include "Recorder1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRecorder1Dlg dialog

CRecorder1Dlg::CRecorder1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRecorder1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRecorder1Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRecorder1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRecorder1Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRecorder1Dlg, CDialog)
	//{{AFX_MSG_MAP(CRecorder1Dlg)
	ON_BN_CLICKED(IDC_REC_START, OnRecStart)
	ON_BN_CLICKED(IDC_REC_STOP, OnRecStop)
	ON_BN_CLICKED(IDC_PLAY_START, OnPlayStart)
	ON_BN_CLICKED(IDC_PLAY_PAUSE, OnPlayPause)
	ON_BN_CLICKED(IDC_PLAY_STOP, OnPlayStop)
	ON_MESSAGE(MM_WIM_OPEN,OnMM_WIM_OPEN)
	ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)
	ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)
	ON_MESSAGE(MM_WOM_OPEN,OnMM_WOM_OPEN)
	ON_MESSAGE(MM_WOM_DONE,OnMM_WOM_DONE)
	ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRecorder1Dlg message handlers

BOOL CRecorder1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	//给wave header分配内存
	pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	
	//给保存声音数据的buffer分配内存
	pSaveBuffer = reinterpret_cast<PBYTE>(malloc(1));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CRecorder1Dlg::OnRecStart() 
{
	// TODO: Add your control notification handler code here
	//分配buffer内存
	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(_T("内存申请失败!"));
		return ;
	}
	
	//打开波形设备并录音
	
	waveform.wFormatTag=WAVE_FORMAT_PCM;
	waveform.nChannels=1;
	waveform.nSamplesPerSec=11025;
	waveform.nAvgBytesPerSec=11025;
	waveform.nBlockAlign=1;
	waveform.wBitsPerSample=8;
	waveform.cbSize=0;

	
	if (waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
		free(pBuffer1);
		free(pBuffer2);
		MessageBeep(MB_ICONEXCLAMATION);
		AfxMessageBox(_T("无法打开音频设备!"));
	}
	pWaveHdr1->lpData=(char*)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=(char*)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));
	
	//////////////////////////////////////////////////////////////////////////
	pSaveBuffer = (PBYTE)realloc (pSaveBuffer, 1) ;
	//添加缓冲
	
	waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
	
	//开始采样
	
	bRecording = TRUE ;
	bEnding = FALSE ;
	dwDataLength = 0 ;
	waveInStart (hWaveIn) ;
}

void CRecorder1Dlg::OnRecStop() 
{
	// TODO: Add your control notification handler code here
	TRACE(_T("停止录音 \n"));
	bEnding=TRUE;
	Sleep(500);
	
	waveInReset(hWaveIn);
}

void CRecorder1Dlg::OnPlayStart() 
{
	// TODO: Add your control notification handler code here
	if (bPlaying) {
		waveOutReset(hWaveOut);
	}

	//打开音频设备的输出
	waveform.wFormatTag		=	WAVE_FORMAT_PCM;
	waveform.nChannels		=	1;
	waveform.nSamplesPerSec	=	11025;
	waveform.nAvgBytesPerSec=	11025;
	waveform.nBlockAlign	=	1;
	waveform.wBitsPerSample	=	8;
	waveform.cbSize			=	0;
	
	
	if (waveOutOpen(&hWaveOut,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
		MessageBeep(MB_ICONEXCLAMATION);
		AfxMessageBox(_T("Audio output erro"));
	}	
}

void CRecorder1Dlg::OnPlayPause() 
{
	// TODO: Add your control notification handler code here
	if (!bPlaying) 
	{
		return;
	}
	if (!bPaused) 
	{
		waveOutPause(hWaveOut);
		bPaused = TRUE;
	}
	else
	{
		waveOutRestart(hWaveOut);
		bPaused=FALSE;
	}		
}

void CRecorder1Dlg::OnPlayStop() 
{
	// TODO: Add your control notification handler code here
	if (!bPlaying) 
	{
		return ;
	}
	bEnding=TRUE;
	waveOutReset(hWaveOut);	
}

void CRecorder1Dlg::OnMM_WIM_OPEN(UINT wParam, LONG lParam) 
{
	// TODO: Add your message handler code here and/or call default
	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
	SetTimer(1,100,NULL);
	bRecording=TRUE;


	TRACE(L"MM_WIM_OPEN\n");
	
	
}

void CRecorder1Dlg::OnMM_WIM_DATA(UINT wParam, LONG lParam) 
{
	// TODO: Add your message handler code here and/or call default
	//重新分配声音数据缓冲内存	
	//////////////////////////////////////////////////////////////////////////
	
	pNewBuffer = (PBYTE)realloc (pSaveBuffer, dwDataLength +
		((PWAVEHDR) lParam)->dwBytesRecorded) ;
	
	if (pNewBuffer == NULL)
	{
		waveInClose (hWaveIn) ;
		MessageBeep (MB_ICONEXCLAMATION) ;
		AfxMessageBox(_T("erro memory"));
		return ;
	}
	
	pSaveBuffer = pNewBuffer ;
	//////////////////////////////////////////////////////////////////////////
	
	CopyMemory (pSaveBuffer + dwDataLength, ((PWAVEHDR) lParam)->lpData,
		((PWAVEHDR) lParam)->dwBytesRecorded) ;
	
	dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;
	
	if (bEnding)
	{
		waveInClose (hWaveIn) ;
		return ;
	}

	
	//添加一个新的缓冲	
	waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
	TRACE(_T("done input data\n"));
	return ;

	
}

void CRecorder1Dlg::OnMM_WIM_CLOSE(UINT wParam, LONG lParam) 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(1);
	TRACE(_T("MM_WIM_CLOSE\n"));
	if (0==dwDataLength) {
		return;
	}
	

	waveInUnprepareHeader (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveInUnprepareHeader (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
	
	free (pBuffer1) ;
	free (pBuffer2) ;
	
	if (dwDataLength > 0)
	{
		//录音结束,有音频数据的时候,让播放按钮能够使用
		((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
		((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
		((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(TRUE);
		((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
		((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
	}
	bRecording = FALSE ;
	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);	
	
	
	return;	
}

void CRecorder1Dlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	if (bRecording) 
	{
		bTerminating=TRUE;
		bEnding=TRUE;
		waveInReset(hWaveIn);
		TRACE(_T("waveInReset\n"));
		Sleep(500);
		//return CWinThread::ExitInstance();
	}
	if (bPlaying) 
	{
		bTerminating=TRUE;
		bEnding=TRUE;
		waveOutReset(hWaveOut);
		Sleep(500);
		//return CWinThread::ExitInstance();
	}
	free (pWaveHdr1) ;
	free (pWaveHdr2) ;
	free (pSaveBuffer) ;
}

void CRecorder1Dlg::OnMM_WOM_OPEN(UINT wParam, LONG lParam)
{
	TRACE(_T("open MM_WOM_OPEN\n"));
	//建立音频设备header
	
	pWaveHdr1->lpData          = (char*)pSaveBuffer ;
	pWaveHdr1->dwBufferLength  = dwDataLength ;
	pWaveHdr1->dwBytesRecorded = 0 ;
	pWaveHdr1->dwUser          = 0 ;
	pWaveHdr1->dwFlags         = WHDR_BEGINLOOP | WHDR_ENDLOOP ;
	pWaveHdr1->dwLoops         = dwRepetitions ;
	pWaveHdr1->lpNext          = NULL ;
	pWaveHdr1->reserved        = 0 ;
	
	//准备输出Header并将数据写入,实现音频输出
	
	waveOutPrepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	
	bEnding = FALSE ;
	bPlaying = TRUE ;
	
	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(TRUE);	
	
}

void CRecorder1Dlg::OnMM_WOM_DONE(UINT wParam, LONG lParam)
{

	TRACE(_T("open MM_WOM_DONE\n"));
	waveOutUnprepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutClose (hWaveOut) ;
	
	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	
	
	return  ;
	
}
void CRecorder1Dlg::OnMM_WOM_CLOSE(UINT wParam, LONG lParam)
{
	TRACE(_T("open MM_WOM_CLOSE\n"));
	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	

	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
	return ;
	
}

void CRecorder1Dlg::OnTimer(UINT nIDEvent) 
{
	MMTIME mmTime;
	mmTime.wType=TIME_MS;
//	TCHAR time[20];
	switch(nIDEvent) 
	{
	case 1:
		//显示录音时间
		waveInGetPosition(hWaveIn,&mmTime,sizeof(MMTIME));
		CString time;
		time.Format(_T("录音时间:%d"),(int)(mmTime.u.ms/10000));
		SetDlgItemText(IDC_STATIC_TIME,time);
	}
	
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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