/**//////////////////////////////////////////////////////////////////////
// SoundBase.cpp: implementation of the CSoundBase class. //
/**//////////////////////////////////////////////////////////////////////
//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;
//---------------------------------------------------------------------------------
//-------------------------------------------------------------------
//Static member initialize
CSoundBase *CSoundBase::m_pInstance = NULL; //If you don't initialize,the GetInstance() will link erro.
//Please don't call waveInUnprepareHeader() in the WIM_CLOSE,
//or it will work badly !
waveInUnprepareHeader (m_hWaveIn, m_pWaveHdr1, sizeof (WAVEHDR)) ;
waveInUnprepareHeader (m_hWaveIn, m_pWaveHdr2, sizeof (WAVEHDR)) ;
//----------------------------------------------------------------------
//Decription:
// Get instance
//
//Parameter:
// Null
//
//Retrun Values:
// The pointer to object
//----------------------------------------------------------------------
CSoundBase* CSoundBase::GetInstance()
{
if(m_pInstance == NULL)
{
m_pInstance = new CSoundBase;
}
return m_pInstance;
}
//----------------------------------------------------------------------
//Decription:
// WaveIn Process
//
//Parameter:
// hwi: [in] Handle to the waveform-audio device associated with the callback function
// uMsg: [in] Waveform-audio input message. It can be one of the messages shown :WIM_CLOSE,WIM_DATA,WIM_OPEN
// dwInstance: [in] User instance data specified with waveInOpen.
// dwParam1: [in] Message parameter.
// dwParam2: [in] Message parameter.
//
//Retrun Values:
// DWORD type.
//----------------------------------------------------------------------
void CALLBACK CSoundBase::WaveInProc(HWAVEIN hWi, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
switch(uMsg)
{
case WIM_CLOSE:
m_pInstance->OnWIM_CLOSE(dwParam1,dwParam2);
break;
case WIM_DATA:
m_pInstance->OnWIM_DATA(dwParam1,dwParam2);
break;
case WIM_OPEN:
m_pInstance->OnWIM_OPEN(dwParam1,dwParam2);
break;
}
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);
}
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)) ;
}
}