gpt4 book ai didi

c# - C#DirectSound-捕获缓冲区不连续

转载 作者:行者123 更新时间:2023-12-03 00:03:10 25 4
gpt4 key购买 nike

我正在尝试使用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开发了相同的应用程序,并且运行良好。

非常感谢...

最佳答案

仔细查看_dwCapBuffer.Read的最后一个参数
它的类型是,params int [] ranks
我有同样的问题,只是发现了这一点。我希望这对您和我都有帮助。

关于c# - C#DirectSound-捕获缓冲区不连续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1621012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com