续,仍然还是soundbase.cpp的定义
//----------------------------------------------------------------------
//Decription:
// Write the wave file header.
//
//Parameter:
// pszFilename: [in] The path to save
// pWFX: [in] The information to write
// dwBufferSize: [in] The size of wave buffer
// bCover: [in] Cover writing or not
//
//Retrun Values:
// TRUE: Succeed.
// FASLE: Failed.
//----------------------------------------------------------------------
BOOL CSoundBase::WriteWaveFileHeader(TCHAR *pszFilename, const PWAVEFORMATEX pWFX, DWORD dwBufferSize, BOOL bCover)
{
RIFF_FILEHEADER FileHeader;
RIFF_CHUNKHEADER WaveHeader;
RIFF_CHUNKHEADER DataHeader;
DWORD dwBytesWritten;
HANDLE fh;
BOOL bResult = FALSE;
// 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.dwSize = sizeof(WaveHeader) + WaveHeader.dwSize + sizeof(DataHeader) + DataHeader.dwSize;
FileHeader.dwWave = RIFF_WAVE;
// Open wave file
if(bCover==TRUE)
{
//If the file existed , cover writng
fh = CreateFile(pszFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
}
else
{
//Open the file existed
fh = CreateFile(pszFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
//Move the pointer to the begin
SetFilePointer(fh,0,NULL,FILE_BEGIN);
}
if( fh == INVALID_HANDLE_VALUE ) {
DEBUGMSG(1, (TEXT("Error opening %s. Error code = 0x%08x "), pszFilename, GetLastError()));
goto ERROR_EXIT;
}
// write the riff file
if (! WriteFile(fh, &FileHeader, sizeof(FileHeader), &dwBytesWritten, NULL)) {
DEBUGMSG(1, (TEXT("Error writing file header ")));
goto ERROR_EXIT;
}
// write the wave header
if (! WriteFile(fh, &WaveHeader, sizeof(WaveHeader), &dwBytesWritten, NULL)) {
DEBUGMSG(1, (TEXT("Error writing wave header ")));
goto ERROR_EXIT;
}
// write the wave format
if (! WriteFile(fh, pWFX, WaveHeader.dwSize, &dwBytesWritten, NULL)) {
DEBUGMSG(1, (TEXT("Error writing wave format ")));
goto ERROR_EXIT;
}
// write the data header
if (! WriteFile(fh, &DataHeader, sizeof(DataHeader), &dwBytesWritten, NULL)) {
DEBUGMSG(1, (TEXT("Error writing PCM data header ")));
goto ERROR_EXIT;
}
// Success
bResult = TRUE;
ERROR_EXIT:
if(fh != INVALID_HANDLE_VALUE)
{
CloseHandle(fh);
}
return bResult;
}
//----------------------------------------------------------------------
//Decription:
// On WIM_CLOSE
//------------------------------------------------------------------------
void CSoundBase::OnWIM_CLOSE(WPARAM wParam, LPARAM lParam)
{
if(m_hSaveFile != NULL)
{
CloseHandle(m_hSaveFile);
m_hSaveFile = NULL;
}
if (0 != m_dwDataLength)
{
//Write the data length to the file.
WriteWaveFileHeader(m_szSavePath,&m_WaveFormatEx,m_dwDataLength,FALSE);
}
}
//----------------------------------------------------------------------
//Decription:
// On WIM_DATA
//------------------------------------------------------------------------
void CSoundBase::OnWIM_DATA(WPARAM wParam, LPARAM lParam)
{
DWORD dwBytesRecorded = ((PWAVEHDR)wParam)->dwBytesRecorded;
PBYTE pSaveBuffer;
//allocate memory for save buffer
pSaveBuffer = reinterpret_cast
(malloc(dwBytesRecorded));
if(pSaveBuffer == NULL)
{
waveInClose (m_hWaveIn) ;
return;
}
//Copy the data to the save buffer.
CopyMemory (pSaveBuffer, ((PWAVEHDR)wParam)->lpData, dwBytesRecorded) ;
//Write the memory data to the file
DWORD dwBytesWritten;
if(WriteFile(m_hSaveFile, pSaveBuffer, dwBytesRecorded, &dwBytesWritten, NULL) != TRUE)
{
if(m_bRecording == TRUE)
{
waveInClose (m_hWaveIn) ;
}
}
m_dwDataLength += dwBytesWritten ;
free(pSaveBuffer);
//If m_bRecording is FALSE, it may call the function waveInReset().So don't add buffer.
if(m_bRecording == TRUE)
{
// Send out a new buffer.The new buffer is the original full buffer, used again.
waveInAddBuffer (m_hWaveIn, (PWAVEHDR) wParam, sizeof (WAVEHDR)) ;
}
}
//----------------------------------------------------------------------
//Decription:
// On WIM_OPEN
//------------------------------------------------------------------------
void CSoundBase::OnWIM_OPEN(WPARAM wParam, LPARAM lParam)
{
m_bRecording = TRUE;
m_dwDataLength = 0;
}