求:各位麻烦帮我看看我的DirectSound录音为什么取缓冲区数据好像有问题,我找了好久也没找到原因

my15y my15y 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面代码中有一个函数是用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;
}
...全文

相关推荐

DirectSound录音程序

自己整理编写的DirectSound录音程序,界面比较简单,可以自行修改。

君生我未生灬

C#中使用DirectSound录音

一.声卡录音的基本原理 为了实现一个录音的基本过程,至少需要以下对象的支持: 1. 录音设备,对我们的PC设备就是声卡。这个录音设备可以进行的操作应该有开始和关闭。 2. 缓冲区,也就是录制的声音放在哪里的问题。 二.DirectSound对录音的描述模型 1. DirectSound对录音的支持类  Capture,设备对象,可以看作是声卡的描述。  CaptureBuffer,缓冲区对象,存放录入的音频数据。  Notify,事件通知对象,由于录音是一个长时间的过程,因此使用一个缓冲队列(多个缓冲区)接收数据,每当一个缓冲区满的时候,系统使用这个对象通知应用程序取走这个缓冲区,并继续录音。 以上三个对象是进行录音操作的主要对象,由于在C++中对DirectSound的操作DirectX帮助文档中已经有很详细的说明,这里就不再赘述了。本文是针对Managed Code。除了以上三个主要的DirectSound类,还需要以下几个辅助类。

nandi_1

directsound 录音 捕获麦克风数据

DirectSound播放声音文件的例子网上可以找到不少。虽然有关DirectSound录音的文章不少,但是大部分都是智慧的鱼写的那篇,DEMO几乎没有,我这里在网上搜集整理了一些资料,做成了录音的DEMO供大家学习使用。 这个demo的主要代码是来自网络的,我没找到原作者是谁

yuxingmin1987

C#用DirectSound录音

C# DirectSound 实现录音 播放 查看WAV文件头等功能

u011388700

C# DirectSound录音(源码)

若看不懂是我的智商问题,跟读者没关系.

slienmeng
评论 3
收藏 已收藏
分享
返回顶部
my15y
VC/MFC 16,550 成员 421,606 内容