求:各位麻烦帮我看看我的DirectSound录音为什么取缓冲区数据好像有问题,我找了好久也没找到原因
my15y 2006-03-09 11:10:40 下面代码中有一个函数是用directshow枚举设备的。这里不用。流程是这样的。
点击录音调用录音函数 OnBnClickedButton1() 然后函数创建录音缓冲区-->创建通知事件--->创建工作线程--》等待事件触发-->调用处理函数 RecordCapturedData 取走缓冲区数据,并写入wav文件。出问题的地方。可能在通知事件和RecordCapturedData函数。能写文件。但是文件中有费数据,用程序打开文件就提示不是有效的wav文件。文件中也有波形。高手帮忙看看。。。。
HRESULT RecordCapturedData(void)
{
HRESULT hr;
VOID* pbCaptureData = NULL;
DWORD dwCaptureLength;
VOID* pbCaptureData2 = NULL;
DWORD dwCaptureLength2;
VOID* pbPlayData = NULL;
UINT dwDataWrote;
DWORD dwReadPos;
LONG lLockSize;
if (NULL == g_pDSBCapture)
return S_FALSE;
if (NULL == g_pWaveFile)
return S_FALSE;
if (FAILED (hr = g_pDSBCapture->GetCurrentPosition(
NULL, &dwReadPos)))
return hr;
// Lock everything between the private cursor
// and the read cursor, allowing for wraparound.
lLockSize = dwReadPos - g_dwNextCaptureOffset;
if( lLockSize < 0 ) lLockSize += g_dwCaptureBufferSize;
if( lLockSize == 0 ) return S_FALSE;
if (FAILED(hr = g_pDSBCapture->Lock(
g_dwNextCaptureOffset, lLockSize,
&pbCaptureData, &dwCaptureLength,
&pbCaptureData2, &dwCaptureLength2, 0L)))
return hr;
// Write the data. This is done in two steps
// to account for wraparound.
if (FAILED( hr = g_pWaveFile->Write( dwCaptureLength,
(BYTE*)pbCaptureData, &dwDataWrote)))
return hr;
if (pbCaptureData2 != NULL)
{
if (FAILED(hr = g_pWaveFile->Write(
dwCaptureLength2, (BYTE*)pbCaptureData2,
&dwDataWrote)))
return hr;
}
// Unlock the capture buffer.
g_pDSBCapture->Unlock( pbCaptureData, dwCaptureLength,
pbCaptureData2, dwCaptureLength2 );
// Move the capture offset forward.
g_dwNextCaptureOffset += dwCaptureLength;
g_dwNextCaptureOffset %= g_dwCaptureBufferSize;
g_dwNextCaptureOffset += dwCaptureLength2;
g_dwNextCaptureOffset %= g_dwCaptureBufferSize;
return S_OK;
}
//thread worker....
DWORD WINAPI ThreadRecord(LPVOID lpParameter)
{
DWORD dwResult =1;
g_bRecording = TRUE;
while(g_bRecording)
{
dwResult = WaitForMultipleObjects(3, rghEvent,FALSE,INFINITE );
switch(dwResult)
{
case WAIT_OBJECT_0 +0:
ListBox_AddString(hmsg,"收到事件1");
RecordCapturedData();
case WAIT_OBJECT_0 + 1:
ListBox_AddString(hmsg,"收到事件2");
RecordCapturedData();
// case WAIT_OBJECT_0+2:
//m_dlg->MessageBox("调用函数 3");
}
}
m_dlg->MessageBox("线程退出");
return 0;
}