📄 recordhwnddlg.cpp
字号:
// TODO: Add your message handler code here and/or call default
// Reallocate save buffer memory
//////////////////////////////////////////////////////////////////////////
pNewBuffer = (PBYTE)realloc (pSaveBuffer, dwDataLength +
((PWAVEHDR) lParam)->dwBytesRecorded) ;
if (pNewBuffer == NULL)
{
waveInClose (hWaveIn) ;
MessageBeep (MB_ICONEXCLAMATION) ;
AfxMessageBox("erro memory");
return ;
}
pSaveBuffer = pNewBuffer ;
//////////////////////////////////////////////////////////////////////////
CopyMemory (pSaveBuffer + dwDataLength, ((PWAVEHDR) lParam)->lpData,
((PWAVEHDR) lParam)->dwBytesRecorded) ;
dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;
CWnd* pWnd = GetDlgItem(IDC_SHOW);
CDC* pDC;
pDC = pWnd->GetDC();
DrawWave(pDC,(PBYTE)((PWAVEHDR) lParam)->lpData,((PWAVEHDR) lParam)->dwBytesRecorded);
pWnd->ReleaseDC(pDC);
if (bEnding)
{
waveInClose (hWaveIn) ;
return ;
}
// Send out a new buffer
waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
TRACE("done input data\n");
return ;
}
void CRecordHWndDlg::OnMM_WIM_CLOSE(UINT wParam, LONG lParam)
{
// TODO: Add your message handler code here and/or call default
KillTimer(1);
TRACE("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)
{
//enable play
((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 CRecordHWndDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
CDialog::OnClose();
if (bRecording) {
bTerminating=TRUE;
bEnding=TRUE;
waveInReset(hWaveIn);
TRACE("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 CRecordHWndDlg::OnPlayStart()
{
// TODO: Add your control notification handler code here
if (bPlaying) {
waveOutReset(hWaveOut);
}
//open waveform audio for output
waveform.wFormatTag = WAVE_FORMAT_PCM;
waveform.nChannels = 1;
waveform.nSamplesPerSec =40000;
waveform.nAvgBytesPerSec=40000;
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("Audio output erro");
}
return ;
}
void CRecordHWndDlg::OnPlayPause()
{
// TODO: Add your control notification handler code here
if (!bPlaying) {
return;
}
if (!bPaused) {
waveOutPause(hWaveOut);
bPaused = TRUE;
}
else
{
waveOutRestart(hWaveOut);
bPaused=FALSE;
}
return ;
}
void CRecordHWndDlg::OnPlayStop()
{
// TODO: Add your control notification handler code here
if (!bPlaying) {
return ;
}
bEnding=TRUE;
waveOutReset(hWaveOut);
return ;
}
void CRecordHWndDlg::OnMM_WOM_OPEN(UINT wParam, LONG lParam){
TRACE("open MM_WOM_OPEN\n");
// Set up header
pWaveHdr1->lpData = (LPTSTR)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 ;
// Prepare and write
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 CRecordHWndDlg::OnMM_WOM_DONE(UINT wParam, LONG lParam){
TRACE("open MM_WOM_DONE\n");
waveOutUnprepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
waveOutClose (hWaveOut) ;
bPaused = FALSE ;
dwRepetitions = 1 ;
bPlaying = FALSE ;
return ;
}
void CRecordHWndDlg::OnMM_WOM_CLOSE(UINT wParam, LONG lParam){
TRACE("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 CRecordHWndDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
/*CDialog::OnTimer(nIDEvent);
MMTIME mmTime;
mmTime.wType=TIME_MS;
TCHAR time[20];
switch(nIDEvent) {
case 1:
waveInGetPosition(hWaveIn,&mmTime,sizeof(MMTIME));
itoa(mmTime.u.ms/10000,time,10);
((CWnd *)GetDlgItem(IDC_STATIC))->SetWindowText(time);
}*/
return;
}
void CRecordHWndDlg::DrawWave(CDC *pDC,PBYTE pData,DWORD dwDrawLength)
{
if(!bEnding)
{
CWnd* pWnd = GetDlgItem(IDC_SHOW);
CRect rect;
pWnd->GetClientRect(rect);
pDC->Rectangle(rect);
pDC->MoveTo(0,rect.Height()/2);
pDC->LineTo(rect.Width(),rect.Height()/2);
pDC->MoveTo(10,0);
pDC->LineTo(10,rect.Height());
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(dwDrawLength,256);
pDC->SetViewportExt(rect.Width(),-rect.Height());
pDC->SetViewportOrg(0,rect.Height()/2);
CPen pen;
pen.CreatePen(PS_SOLID,0,RGB(0,0,255));
CPen *pOldPen = pDC->SelectObject(&pen);
//Draw Wave here
for(int i=0;i<(unsigned short)dwDrawLength;i++)
{
pDC->MoveTo(i,pData[i]-128);
pDC->LineTo(i+1,pData[i+1]-128);
}
pDC->SelectObject(pOldPen);
pen.DeleteObject();
pOldPen->DeleteObject();
pWnd->ReleaseDC(pDC);
}
}
void CRecordHWndDlg::OnButton3()
{
// TODO: Add your control notification handler code here
exit(0);
}
void CRecordHWndDlg::Onexit()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -