VisualizeDataMiddleWare.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Bmp.Core.Lite.Playback.Inputs;
  2. using Bmp.Core.Lite.Playback.Outputs.NAudioASIO.Originals;
  3. using NAudio.Wave;
  4. namespace Bmp.Core.Lite.Playback.MiddleWare;
  5. public class VisualizeDataMiddleWare(WaveStream provider) : IWaveProvider, IHaveBitPerRawSample, IHaveAsioSampleFormat
  6. {
  7. public event EventHandler<VisualizeDataEventArgs>? DataTransferred;
  8. protected virtual void OnDataTransferred(VisualizeDataEventArgs e)
  9. {
  10. DataTransferred?.Invoke(this, e);
  11. }
  12. public int? BitPerRawSample => provider is IHaveBitPerRawSample bpr ? bpr.BitPerRawSample : null;
  13. public AsioSampleType? AsioSampleFormat => provider is IHaveAsioSampleFormat asf ? asf.AsioSampleFormat : null;
  14. public WaveFormat WaveFormat => provider.WaveFormat;
  15. public int Read(byte[] buffer, int offset, int count)
  16. {
  17. try
  18. {
  19. var read = provider.Read(buffer, offset, count);
  20. OnDataTransferred(new VisualizeDataEventArgs(WaveFormat, new ReadOnlyMemory<byte>(buffer, 0, read)));
  21. return read;
  22. }
  23. catch //(Exception e)
  24. {
  25. if (provider.Position < provider.Length)
  26. {
  27. //TODO: 消息机制 错误 流读取中断
  28. }
  29. return 0;
  30. }
  31. }
  32. }