2009-10-25 112 views
1

我想从使用DirectSound的行中捕获原始数据。C#DirectSound - 捕获缓冲区不连续

我的问题是,从缓冲区到另一个数据只是不一致,如果我捕获一个正弦,我看到从我的最后一个缓冲区和新的跳转。为了检测这一点,我使用的图形控件从新划清最后一个缓冲区的第一个500元和500元:

snapshot 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"); 

这里是浩w我处理事件。

/* 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的相同应用程序,它运行良好。

非常感谢......

回答

0

在_dwCapBuffer.Read
的最后一个参数 它的类型很仔细一看,则params INT []居
我有同样的问题,只是发现了这一点。我希望这可以帮助你和我。

+1

我有点解决了它,这是我如何打印出图表的错误: -/ 在任何情况下,我仍然有缓冲区之间的incogruens,但是真的很小。现在我已经切换到NAudio库,它完美的工作! – Wizche 2009-11-07 13:32:19