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

标签 c# api audio directx sampling

我正在尝试使用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/

相关文章:

php - Trello 使用 api 获取卡片的创建日期

audio - FFmpeg - 与音频帧大小及其计算的概念混淆

c# - NSwag Wep Api 2 multipart/form-data 属性/文件上传

c# - 我可以使用启动期间配置的 MvcJsonOptions 在自定义中间件中进行序列化吗?

javascript - 如何解决服务 "of undefined"问题?

python - Python Wave音频采样率

javascript - p5.sound在手机上声音失真了吗?

c# - Visual Studio 2022 中的 Microsoft Identity Platform 依赖项配置

c# - 缺少适用于 Windows Phone 的 Microsoft Visual C++ 2013 运行时包

c# - JavaScriptSerializer 如何反序列化带有空格的标识符