📄 recorddlg.cpp
字号:
// RecordDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Record.h"
#include "RecordDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MRCHECK(mr,str)\
if ((mr != MMSYSERR_NOERROR)) { RETAILMSG(1, (TEXT(#str) TEXT(" failed. mr=%08x\r\n"), mr));}
// -----------------------------------------------------------------------------
// FileHeader
// -----------------------------------------------------------------------------
typedef struct
{
DWORD dwRiff; // Type of file header.
DWORD dwSize; // Size of file header.
DWORD dwWave; // Type of wave.
} RIFF_FILEHEADER, *PRIFF_FILEHEADER;
// -----------------------------------------------------------------------------
// ChunkHeader
// -----------------------------------------------------------------------------
typedef struct
{
DWORD dwCKID; // Type Identification for current chunk header.
DWORD dwSize; // Size of current chunk header.
} RIFF_CHUNKHEADER, *PRIFF_CHUNKHEADER;
/* Chunk Types
*/
#define RIFF_FILE mmioFOURCC('R','I','F','F')
#define RIFF_WAVE mmioFOURCC('W','A','V','E')
#define RIFF_FORMAT mmioFOURCC('f','m','t',' ')
#define RIFF_CHANNEL mmioFOURCC('d','a','t','a')
/////////////////////////////////////////////////////////////////////////////
// CRecordDlg dialog
CRecordDlg::CRecordDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRecordDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRecordDlg)
// 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);
bRecording=FALSE;
bPlaying=FALSE;
bReverse=FALSE;
bPaused=FALSE;
bEnding=FALSE;
bTerminating=FALSE;
dwDataLength=0;
dwRepetitions=1;
}
void CRecordDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRecordDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRecordDlg, CDialog)
//{{AFX_MSG_MAP(CRecordDlg)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_PLAY_PAUSE, OnPlayPause)
ON_BN_CLICKED(IDC_PLAY_START, OnPlayStart)
ON_BN_CLICKED(IDC_PLAY_STOP, OnPlayStop)
ON_BN_CLICKED(IDC_REC_START, OnRecStart)
ON_BN_CLICKED(IDC_REC_STOP, OnRecStop)
ON_WM_TIMER()
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_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_EXIT, OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRecordDlg message handlers
BOOL CRecordDlg::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
SetWindowPos(this,0,0,320,240,SWP_SHOWWINDOW);
// TODO: Add extra initialization here
//allocate memory for wave header
pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
//allocate memory for save buffer
pSaveBuffer = reinterpret_cast<PBYTE>(malloc(1));
return TRUE; // return TRUE unless you set the focus to a control
}
void CRecordDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
CDialog::OnClose();
if (bRecording)
{
bTerminating=TRUE;
bEnding=TRUE;
waveInReset(hWaveIn);
RETAILMSG(1,(TEXT("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 CRecordDlg::OnPlayPause()
{
// TODO: Add your control notification handler code here
if (!bPlaying)
return;
if (!bPaused)
{
waveOutPause(hWaveOut);
bPaused = TRUE;
((CWnd *)GetDlgItem(IDC_PLAY_PAUSE))->SetWindowText(_T("Continue"));
}
else
{
waveOutRestart(hWaveOut);
bPaused=FALSE;
((CWnd *)GetDlgItem(IDC_PLAY_PAUSE))->SetWindowText(_T("Pause"));
}
return ;
}
void CRecordDlg::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=11025;
waveform.nAvgBytesPerSec=11025;
//waveform.nSamplesPerSec=22050;
//waveform.nAvgBytesPerSec=22050;
//waveform.nBlockAlign =1;
//waveform.wBitsPerSample =8;
waveform.nBlockAlign=2;
waveform.wBitsPerSample=16;
waveform.nAvgBytesPerSec=22050;
waveform.cbSize =0;
if (waveOutOpen(&hWaveOut,0,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
MessageBeep(MB_ICONEXCLAMATION);
RETAILMSG(1,(TEXT("Audio output erro")));
}
CurTime = 0;
return ;
}
void CRecordDlg::OnPlayStop()
{
// TODO: Add your control notification handler code here
if (!bPlaying)
return ;
bEnding=TRUE;
waveOutReset(hWaveOut);
return ;
}
void CRecordDlg::OnRecStart()
{
// TODO: Add your control notification handler code here
//allocate buffer memory
MMRESULT mr;
pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE);
pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);
if (!pBuffer1) {
if (pBuffer1) free(pBuffer1);
if (pBuffer2) free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
RETAILMSG(1,(TEXT("Memory erro!")));
return ;
}
//open waveform audo for input
waveform.wFormatTag=WAVE_FORMAT_PCM;
waveform.nChannels=1;
waveform.nSamplesPerSec=11025;
//waveform.nAvgBytesPerSec=11025;
//waveform.nSamplesPerSec=22050;
//waveform.nAvgBytesPerSec=22050;
//waveform.nBlockAlign=1;
//waveform.wBitsPerSample=8;
waveform.nBlockAlign=2;
waveform.wBitsPerSample=16;
waveform.nAvgBytesPerSec=22050;
waveform.cbSize=0;
if (waveInOpen(&hWaveIn,0,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) {
free(pBuffer1);
free(pBuffer2);
MessageBeep(MB_ICONEXCLAMATION);
RETAILMSG(1,(TEXT("Audio can not be open!")));
}
pWaveHdr1->lpData=(LPSTR)pBuffer1;
pWaveHdr1->dwBufferLength=INP_BUFFER_SIZE;
pWaveHdr1->dwBytesRecorded=0;
pWaveHdr1->dwUser=0;
pWaveHdr1->dwFlags=0;
pWaveHdr1->dwLoops=1;
pWaveHdr1->lpNext=NULL;
pWaveHdr1->reserved=0;
mr=waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
MRCHECK(mr, waveInPrepareHeader);
pWaveHdr2->lpData=(LPSTR)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) ;
// Add the buffers
mr=waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
MRCHECK(mr, waveInAddBuffer);
waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
// Begin sampling
bRecording = TRUE ;
bEnding = FALSE ;
dwDataLength = 0 ;
CurTime = 0;
TotalTime = 0;
mr=waveInStart(hWaveIn);
MRCHECK(mr, waveInStart);
}
void CRecordDlg::OnRecStop()
{
// TODO: Add your control notification handler code here
bEnding=TRUE;
waveInReset(hWaveIn);
RETAILMSG(1,(TEXT("waveInReset\n")));
}
void CRecordDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnTimer(nIDEvent);
MMTIME mmTime;
mmTime.wType=TIME_BYTES;
TCHAR time[20];
switch(nIDEvent)
{
case 1:
if(bRecording)
{
waveInGetPosition(hWaveIn,&mmTime,sizeof(MMTIME));
CurTime++;
TotalTime++;
_itow(CurTime,time,10);
}
if(bPlaying)
{
waveOutGetPosition(hWaveOut,&mmTime,sizeof(MMTIME));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -