bufferdescription.BufferBytes = mBufferSize;
bufferdescription.Format = mWavFormat;
mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev);
mNextCaptureOffset = 0;
}
private bool InitNotifications()
{
if (null == mRecBuffer)
{
MessageBox.Show("Record buffer is not found!");
return false;
}
mNotificationEvent = new AutoResetEvent(false);
if (null == mNotifyThread)
{
mNotifyThread = new Thread(new ThreadStart(WaitThread));
mNotifyThread.Start();
}
BufferPositionNotify[] PositionNotify = new BufferPositionNotify[cNotifyNum + 1];
for (int i = 0; i < cNotifyNum; i++)
{
PositionNotify[i].Offset = (mNotifySize * i) + mNotifySize - 1;
PositionNotify[i].EventNotifyHandle = mNotificationEvent.Handle;
}
mNotify = new Notify(mRecBuffer);
mNotify.SetNotificationPositions(PositionNotify, cNotifyNum);
return true;
}
private bool InitCaptureDevice()
{
CaptureDevicesCollection devices = new CaptureDevicesCollection();
Guid deviceGuid = Guid.Empty;
if (devices.Count > 0)
deviceGuid = devices[0].DriverGuid;
else
{
MessageBox.Show("No device to capture wave!");
return false;
}
try
{
mCapDev = new Capture(deviceGuid);
}
catch (DirectXException e)
{
MessageBox.Show(e.ToString());
return false;
}
return true;
}
private WaveFormat CreateWaveFormat()
{
WaveFormat format = new WaveFormat();
format.FormatTag = WaveFormatTag.Pcm; // PCM
format.SamplesPerSecond = 16000; // 16KHz
format.BitsPerSample = 16; // 16Bit
format.Channels = 1; // Mono
format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));
format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
return format;
}
private void WaitThread()
{
while (true)
{
// waiting for notification
mNotificationEvent.WaitOne(Timeout.Infinite, true);
// process data captured
RecordCapturedData();
}
}
#endregion
}
说的是mixer的大小
还是采集到的电平值?
第一个用api,网上有很多资料
http:-//dev.yesky.-com/386/2252386.shtml
最后的解决方法就是计算缓冲区波形数据,算法是找了一些开源项目,然后分析这些类似项目的算法得到的。
大体算法如下,具体的项目文档我会在近期整理出来分享给大家哈:
C# code private void RecordCapturedData()
{
byte[] CaptureData = null;
int ReadPos;
int CapturePos;
int LockSize;
mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);
LockSize = ReadPos - mNextCaptureOffset;
if (LockSize < 0)
LockSize += mBufferSize;
LockSize -= (LockSize % mNotifySize);
if (0 == LockSize)
return;
CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);
mNextCaptureOffset += CaptureData.Length;
mNextCaptureOffset %= mBufferSize;