扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:智慧的鱼 来源:天极网 2007年10月19日
关键字:
DWORD WINAPI ThreadRecord(LPVOID lpParameter) { DWORD dwResult =0; g_bRecording = TRUE; while(g_bRecording) { dwResult = WaitForMultipleObjects(1, &g_hNotificationEvent,FALSE,INFINITE ); switch( dwResult ) { case WAIT_OBJECT_0 + 0: RecordCapturedData(); } } return 0; } |
CWaveFile g_pWaveFile; WAVEFORMATEX wfxInput; ZeroMemory( &wfxInput, sizeof(wfxInput)); wfxInput.wFormatTag = WAVE_FORMAT_PCM; wfxInput.nSamplesPerSec = 22050 wfxInput.wBitsPerSample = 8; wfxInput.nChannels = 1; wfxInput.nBlockAlign = wfxInput.nChannels * (wfxInput.wBitsPerSample / 8); wfxInput.nAvgBytesPerSec = wfxInput.nBlockAlign * wfxInput.nSamplesPerSec; g_pWaveFile = new CWaveFile; if (FAILED(g_pWaveFile->Open("mywave.wav", &wfxInput, WAVEFILE_WRITE))) { g_pWaveFile->Close(); } |
HRESULT CCaptureSoundDlg::RecordCapturedData() { HRESULT hr; VOID *pbCaptureData = NULL; DWORD dwCaptureLength; VOID *pbCaptureData2 = NULL; DWORD dwCaptureLength2; UINT dwDataWrote; DWORD dwReadPos; DWORD dwCapturePos; LONG lLockSize; if(g_pDSBCapture == NULL ) return S_FALSE; if( NULL == g_pWaveFile ) return S_FALSE; if(FAILED( hr = g_pDSBCapture->GetCurrentPosition(&dwCapturePos,&dwReadPos))) return hr; lLockSize = dwReadPos -g_dwNextCaptureOffset; if( lLockSize < 0 ) lLockSize += g_dwCaptureBufferSize; //锁住内存的大小 //这里取模是为了使得我们读取的数据大小为g_dwNotifySize整数倍,这样buffer里剩下的也是notify的倍数 lLockSize -= (lLockSize % g_dwNotifySize); if( lLockSize == 0 ) return S_FALSE; //锁住内存 if( FAILED( hr = g_pDSBCapture->Lock( g_dwNextCaptureOffset, lLockSize, &pbCaptureData, &dwCaptureLength, &pbCaptureData2, &dwCaptureLength2, 0L ) ) ) return hr; // 将内存中的数据拷贝到wave文件中 if( FAILED( hr = g_pWaveFile->Write( dwCaptureLength, (BYTE*)pbCaptureData, &dwDataWrote ) ) ) return hr; // 移动偏移标志,循环移动 g_dwNextCaptureOffset += dwCaptureLength; g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer if( pbCaptureData2 != NULL ) { // 将内存中的数据拷贝到wave文件中 if( FAILED( hr = g_pWaveFile->Write( dwCaptureLength2, (BYTE*)pbCaptureData2, &dwDataWrote ) ) ) return hr; // 移动偏移标志 g_dwNextCaptureOffset += dwCaptureLength2; g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer } //内存解锁 g_pDSBCapture->Unlock( pbCaptureData, dwCaptureLength, pbCaptureData2, dwCaptureLength2 ); return S_OK; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者