📄 wavedlg.cpp
字号:
// WAVEDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WAVE.h"
#include "WAVEDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWAVEDlg dialog
CWAVEDlg::CWAVEDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWAVEDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWAVEDlg)
// 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 CWAVEDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWAVEDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWAVEDlg, CDialog)
//{{AFX_MSG_MAP(CWAVEDlg)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
ON_BN_CLICKED(IDC_PLAY, OnPlay)
ON_BN_CLICKED(IDC_SAVE, OnSave)
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_TIMER()
ON_BN_CLICKED(IDC_PLAY_PAUSE, OnPlayPause)
ON_BN_CLICKED(IDC_PLAY_STOP, OnPlayStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWAVEDlg message handlers
BOOL CWAVEDlg::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
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 CWAVEDlg::OnStart()
{
//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(_T("Memory erro!"));
return ;
}
/* if(!outWaveFile.Open(_T("data.wav"),CFile::modeCreate | CFile::modeWrite))
{
AfxMessageBox(_T("Can not open this file"),MB_OK,0);
return ;
}
else
return ;
*/
outWaveFile.Open(_T("data.wav"),CFile::modeWrite|CFile::modeCreate);
WvH.dwSamplingRate = 11025;
WvH.dwRIFF = 0x46464952;
WvH.dwWAVE = 0x45564157;
WvH.dw_fmt = 0x20746d66;
WvH.dwFmtLen = 0x10;
WvH.wDataType = 1;
WvH.wNChannels = 1;
WvH.wNBitsPerSam = 8;
WvH.dwdata = 0x61746164;
WvH.dwNBytesPerSec = WvH.dwSamplingRate*1*2;
WvH.wAlignment = WvH.wNChannels * WvH.wNBitsPerSam / 8;
WvH.dwDataLen=dwDataLength;
WvH.dwFileLen=WvH.dwDataLen + 16 + 20;
outWaveFile.Seek(0,CFile::begin);
outWaveFile.Write(&WvH,sizeof(WAVEHEADER));
//open waveform audo for input
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("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;
waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
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
waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
// Begin sampling
bRecording = TRUE ;
bEnding = FALSE ;
dwDataLength = 0 ;
waveInStart (hWaveIn) ;
}
void CWAVEDlg::OnClose()
{
//TRACE("rec stop \n");
bEnding=TRUE;
//Sleep(1500);
WvH.dwDataLen=dwDataLength;
WvH.dwFileLen=WvH.dwDataLen + 16 + 20;
outWaveFile.SeekToBegin();
outWaveFile.Write(&WvH,sizeof(WAVEHEADER));
waveInReset(hWaveIn);
waveInReset(hWaveIn);
((CWnd *)(this->GetDlgItem(IDC_START)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_CLOSE)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_PLAY)))->EnableWindow(TRUE);
}
void CWAVEDlg::OnSave()
{
sndPlaySound(_T("data.wav"),SND_ASYNC);
}
void CWAVEDlg::OnMM_WIM_OPEN(UINT wParam, LONG lParam)
{
// TODO: Add your message handler code here and/or call default
((CWnd *)(this->GetDlgItem(IDC_START)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_CLOSE)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_PLAY)))->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("MM_WIM_OPEN\n");
}
void CWAVEDlg::OnMM_WIM_DATA(UINT wParam, LONG lParam)
{
// 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(_T("erro memory"));
return ;
}
pSaveBuffer = pNewBuffer ;
//////////////////////////////////////////////////////////////////////////
CopyMemory (pSaveBuffer + dwDataLength, ((PWAVEHDR) lParam)->lpData,
((PWAVEHDR) lParam)->dwBytesRecorded) ;
dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;
*/
// pSaveBuffer = (PBYTE)realloc (pSaveBuffer, ((PWAVEHDR) lParam)->dwBytesRecorded) ;
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, ((PWAVEHDR) lParam)->lpData,
((PWAVEHDR) lParam)->dwBytesRecorded) ;
dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;
outWaveFile.Write(pSaveBuffer,((PWAVEHDR) lParam)->dwBytesRecorded);
/* if (bEnding)
{
waveInClose (hWaveIn) ;
return ;
}
*/
/*
CFile file;
file.Open(_T("data.wav"),CFile::modeWrite|CFile::modeCreate);
file.Write(pSaveBuffer,dwDataLength);
file.Close();
*/
//LPMMIOINFO lpmmioinfo;
//mmioOpen( _T("data.wav"), lpmmioinfo,MMIO_CREATE);
// Send out a new buffer
waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
//TRACE("done input data\n");
return ;
}
void CWAVEDlg::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_START)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_CLOSE)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_PLAY)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
}
bRecording = FALSE ;
((CWnd *)(this->GetDlgItem(IDC_START)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_CLOSE)))->EnableWindow(FALSE);
return ;
}
void CWAVEDlg::OnMM_WOM_OPEN(UINT wParam, LONG lParam){
//TRACE("open MM_WOM_OPEN\n");
// Set up header
pWaveHdr1->lpData = (LPSTR)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_START)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_CLOSE)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_PLAY)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(TRUE);
}
void CWAVEDlg::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 CWAVEDlg::OnMM_WOM_CLOSE(UINT wParam, LONG lParam){
// TRACE("open MM_WOM_CLOSE\n");
bPaused = FALSE ;
dwRepetitions = 1 ;
bPlaying = FALSE ;
((CWnd *)(this->GetDlgItem(IDC_START)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_CLOSE)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_PLAY)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
return ;
}
void CWAVEDlg::OnTimer(UINT nIDEvent)
{
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 CWAVEDlg::OnPlay()
{
if (bPlaying)
{
waveOutReset(hWaveOut);
}
//open waveform audio for output
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"));
}
return ;
}
void CWAVEDlg::OnPlayPause()
{
if (!bPlaying) {
return;
}
if (!bPaused) {
waveOutPause(hWaveOut);
bPaused = TRUE;
}
else
{
waveOutRestart(hWaveOut);
bPaused=FALSE;
}
return ;
}
void CWAVEDlg::OnPlayStop()
{
if (!bPlaying) {
return ;
}
bEnding=TRUE;
waveOutReset(hWaveOut);
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -