📄 recorddlg.cpp
字号:
CurTime++;
swprintf(time,L"%d/%d",CurTime,TotalTime);
}
//_itow(mmTime.u.cb,time,10);
((CWnd *)GetDlgItem(IDC_BUTTON1))->SetWindowText(time);
//RETAILMSG(1, (TEXT("time=%08x\r\n"), mmTime.u.ms/10000));
}
return;
}
void CRecordDlg::OnMM_WIM_OPEN(UINT wParam, LONG lParam)
{
((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);
((CWnd *)(this->GetDlgItem(IDC_SAVE)))->EnableWindow(FALSE);
SetTimer(1,1000,NULL);
bRecording=TRUE;
}
void CRecordDlg::OnMM_WIM_DATA(UINT wParam, LONG lParam)
{
// Reallocate save buffer memory
//////////////////////////////////////////////////////////////////////////
/*TCHAR time[20];
_itow(((PWAVEHDR) lParam)->dwBytesRecorded,time,10);
AfxMessageBox(time);*/
pNewBuffer = (PBYTE)realloc (pSaveBuffer, dwDataLength +
((PWAVEHDR) lParam)->dwBytesRecorded) ;
if (pNewBuffer == NULL)
{
waveInClose (hWaveIn) ;
MessageBeep (MB_ICONEXCLAMATION) ;
RETAILMSG(1,(TEXT("erro memory")));
return ;
}
pSaveBuffer = pNewBuffer ;
//////////////////////////////////////////////////////////////////////////
CopyMemory (pSaveBuffer + dwDataLength, ((PWAVEHDR) lParam)->lpData,
((PWAVEHDR) lParam)->dwBytesRecorded) ;
dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;
//********************
//dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;
//pSaveBuffer=(PBYTE)realloc (pSaveBuffer, ((PWAVEHDR) lParam)->dwBytesRecorded);
//CopyMemory (pSaveBuffer, ((PWAVEHDR) lParam)->lpData,((PWAVEHDR) lParam)->dwBytesRecorded) ;
//********************
if (bEnding)
{
waveInClose (hWaveIn) ;
return ;
}
// Send out a new buffer
waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
RETAILMSG(1,(TEXT("done input data\n")));
return ;
}
void CRecordDlg::OnMM_WIM_CLOSE(UINT wParam, LONG lParam)
{
KillTimer(1);
/*TCHAR time[20];
_itow(dwDataLength,time,10);
AfxMessageBox(time);*/
RETAILMSG(1,(TEXT("MM_WIM_CLOSE\n")));
if (0==dwDataLength) {
AfxMessageBox(L"长度为0");
return;
}
//WriteWaveFile (L"\\b.wav", &waveform, dwDataLength,pSaveBuffer);
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);
((CWnd *)(this->GetDlgItem(IDC_SAVE)))->EnableWindow(TRUE);
}
bRecording = FALSE ;
((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
((CWnd *)GetDlgItem(IDC_BUTTON1))->SetWindowText(_T("0"));
return ;
}
void CRecordDlg::OnMM_WOM_OPEN(UINT wParam, LONG lParam)
{
RETAILMSG(1,(TEXT("open MM_WOM_OPEN\n")));
// Set up 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 ;
// Prepare and write
waveOutPrepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
SetTimer(1,1000,NULL);
bEnding = FALSE ;
bPlaying = TRUE ;
((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(FALSE);
((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);
((CWnd *)(this->GetDlgItem(IDC_SAVE)))->EnableWindow(FALSE);
}
void CRecordDlg::OnMM_WOM_DONE(UINT wParam, LONG lParam)
{
RETAILMSG(1,(TEXT("open MM_WOM_DONE\n")));
waveOutUnprepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
waveOutClose (hWaveOut) ;
bPaused = FALSE ;
dwRepetitions = 1 ;
bPlaying = FALSE ;
return ;
}
void CRecordDlg::OnMM_WOM_CLOSE(UINT wParam, LONG lParam)
{
KillTimer(1);
RETAILMSG(1,(TEXT("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);
((CWnd *)(this->GetDlgItem(IDC_SAVE)))->EnableWindow(TRUE);
((CWnd *)GetDlgItem(IDC_PLAY_PAUSE))->SetWindowText(_T("Pause"));
((CWnd *)GetDlgItem(IDC_BUTTON1))->SetWindowText(_T("0"));
return ;
}
BOOL CRecordDlg::WriteWaveFile(LPCTSTR pszFilename, PWAVEFORMATEX pWFX, DWORD dwBufferSize, PBYTE pBufferBits)
{
RIFF_FILEHEADER FileHeader;
RIFF_CHUNKHEADER WaveHeader;
RIFF_CHUNKHEADER DataHeader;
DWORD dwBytesWritten;
HANDLE fh;
// Fill in the file, wave and data headers
WaveHeader.dwCKID = RIFF_FORMAT;
WaveHeader.dwSize = sizeof(WAVEFORMATEX) + pWFX->cbSize;
// the DataHeader chunk contains the audio data
DataHeader.dwCKID = RIFF_CHANNEL;
DataHeader.dwSize = dwBufferSize;
// The FileHeader
FileHeader.dwRiff = RIFF_FILE;
FileHeader.dwWave = RIFF_WAVE;
FileHeader.dwSize = 38 + DataHeader.dwSize;//sizeof(WaveHeader) + WaveHeader.dwSize + sizeof(DataHeader) + DataHeader.dwSize;
// Open wave file
fh = CreateFile(pszFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
if( fh == INVALID_HANDLE_VALUE ) {
RETAILMSG(1, (TEXT("Error opening %s. Error code = 0x%08x\n"), pszFilename, GetLastError()));
return FALSE;
}
// write the riff file
if (! WriteFile(fh, &FileHeader, sizeof(FileHeader), &dwBytesWritten, NULL)) {
RETAILMSG(1, (TEXT("Error writing file header\r\n")));
return FALSE;
}
// write the wave header
if (! WriteFile(fh, &WaveHeader, sizeof(WaveHeader), &dwBytesWritten, NULL)) {
RETAILMSG(1, (TEXT("Error writing wave header\r\n")));
return FALSE;
}
// write the wave format
if (! WriteFile(fh, pWFX, WaveHeader.dwSize, &dwBytesWritten, NULL)) {
RETAILMSG(1, (TEXT("Error writing wave format\r\n")));
return FALSE;
}
// write the data header
if (! WriteFile(fh, &DataHeader, sizeof(DataHeader), &dwBytesWritten, NULL)) {
RETAILMSG(1, (TEXT("Error writing PCM data header\r\n")));
return FALSE;
}
// write the PCM data
if (! WriteFile(fh, pBufferBits, DataHeader.dwSize, &dwBytesWritten, NULL)) {
RETAILMSG(1, (TEXT("Error writing PCM data\r\n")));
return FALSE;
}
CloseHandle(fh);
// Success
return TRUE;
}
int CRecordDlg::OpenFile(TCHAR *filename)
{
HANDLE fh;
DWORD dwBytesRead;
TCHAR a[10];
BYTE tmp[8];
int datalen;
// Open the existing wave file
fh = CreateFile(filename, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if( fh == INVALID_HANDLE_VALUE )
{
AfxMessageBox(L"error open file");
return 0;
}
//将文件指针移到
//SetFilePointer(fh,4,NULL,FILE_CURRENT);
if (! ReadFile(fh, tmp, 8, &dwBytesRead, NULL))
{
AfxMessageBox(L"error read file");
return 0;
}
//datalen = MAKELONG(tmp[2],tmp[3]) - 38;//音频数据长度
datalen = MAKELONG(MAKEWORD(tmp[4],tmp[5]),MAKEWORD(tmp[6],tmp[7])) - 38;
//_itow(datalen,a,10);
//AfxMessageBox(a);
//return 0;
//TCHAR *buf = new TCHAR[2];
/*if(!buf) AfxMessageBox(L"memory error2");
else
AfxMessageBox(L"memory success");*/
SetFilePointer(fh,38,NULL,FILE_CURRENT);
free(pSaveBuffer) ;
pSaveBuffer = (PBYTE)malloc(datalen);
if (!ReadFile(fh, pSaveBuffer, datalen, &dwBytesRead, NULL))
{
AfxMessageBox(L"error2 read file");
}
//datalen = WideCharToMultiByte(CP_ACP,0,(PWSTR)buf,-1,pBuffer,0,NULL,NULL);
//WideCharToMultiByte(CP_ACP,0,(PWSTR)buf,-1,pBuffer,6,NULL,NULL);
_itow(pSaveBuffer[datalen-8],a,10);
AfxMessageBox(a);
//delete []buf;
//buf = NULL;
CloseHandle(fh);
return 1;
}
void CRecordDlg::OnButton2()
{
// TODO: Add your control notification handler code here
//OpenFile(L"\\b.wav");
CFileDialog dlg(FALSE, NULL, NULL, NULL,L"*.wav|(声音*.wav)",NULL);
if(IDOK == dlg.DoModal())
{
CString f = dlg.GetFileName();
if(f.GetLength() > 4)
{
CString s = f.Right(4);
s.MakeUpper();
if(s != L".WAV")
f += L".wav";
}
else
f += L".wav";
//AfxMessageBox(f);
WriteWaveFile (f, &waveform, dwDataLength,pSaveBuffer);
}
}
void CRecordDlg::OnExit()
{
// TODO: Add your control notification handler code here
EndDialog(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -