我正在尝试使用 DirectSound 从我的线路中捕获原始数据。
我的问题是,从一个缓冲区到另一个缓冲区的数据只是不一致的,例如,如果我捕获一个正弦波,我会看到我的上一个缓冲区和新缓冲区的跳跃。为了检测到这一点,我使用图形小部件来绘制最后一个缓冲区的前 500 个元素和新缓冲区的 500 个元素:
http://img199.imageshack.us/img199/206/demodsnap.jpg
我以这种方式初始化了我的缓冲区:
format = new WaveFormat {
SamplesPerSecond = 44100,
BitsPerSample = (short)bitpersample,
Channels = (short)channels,
FormatTag = WaveFormatTag.Pcm
};
format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));
format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign;
_dwNotifySize = Math.Max(4096, format.AverageBytesPerSecond / 8);
_dwNotifySize -= _dwNotifySize % format.BlockAlign;
_dwCaptureBufferSize = NUM_BUFFERS * _dwNotifySize; // my capture buffer
_dwOutputBufferSize = NUM_BUFFERS * _dwNotifySize / channels; // my output buffer
我将通知设置为缓冲区的一半和末尾的通知:
_resetEvent = new AutoResetEvent(false);
_notify = new Notify(_dwCapBuffer);
bpn1 = new BufferPositionNotify();
bpn1.Offset = ((_dwCapBuffer.Caps.BufferBytes) / 2) - 1;
bpn1.EventNotifyHandle = _resetEvent.SafeWaitHandle.DangerousGetHandle();
bpn2 = new BufferPositionNotify();
bpn2.Offset = (_dwCapBuffer.Caps.BufferBytes) - 1;
bpn2.EventNotifyHandle = _resetEvent.SafeWaitHandle.DangerousGetHandle();
_notify.SetNotificationPositions(new BufferPositionNotify[] { bpn1, bpn2 });
observer.updateSamplerStatus("Events listener initialization complete!\r\n");
这就是我处理事件的方式。
/* Process thread */
private void eventReceived()
{
int offset = 0;
_dwCaptureThread = new Thread((ThreadStart)delegate
{
_dwCapBuffer.Start(true);
while (isReady)
{
_resetEvent.WaitOne(); // Notification received
/* Read the captured buffer */
Array read = _dwCapBuffer.Read(offset, typeof(short), LockFlag.None, _dwOutputBufferSize - 1);
observer.updateTextPacket("Buffer: " + count.ToString() + " # " + read.GetValue(read.Length - 1).ToString() + " # " + read.GetValue(0).ToString() + "\r\n");
/* Print last/new part of the buffer to the debug graph */
short[] graphData = new short[1001];
Array.Copy(read, graphData, 1000);
db.SetBufferDebug(graphData, 500);
observer.updateGraph(db.getBufferDebug());
offset = (offset + _dwOutputBufferSize) % _dwCaptureBufferSize;
/* Out buffer not used */
/*_dwDevBuffer.Write(0, read, LockFlag.EntireBuffer);
_dwDevBuffer.SetCurrentPosition(0);
_dwDevBuffer.Play(0, BufferPlayFlags.Default);*/
}
_dwCapBuffer.Stop();
});
_dwCaptureThread.Start();
}
有什么建议吗?我确定我在事件处理的某个地方失败了,但我找不到哪里。
我使用 WaveIn API 开发了相同的应用程序,它运行良好。
非常感谢...