How to access an audio stream using DirectShow.NET C#

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

What I would like to do is to pass an arbitrary audio file to a DirectShow filtergraph and receive a (PCM audio) stream object in the end using .NET 3.5 C# and DirectShow.NET. I would like to reach the point that I can just say:

 Stream OpenFile(string filename) {...}

and

stream.Read(...)

I have been reading up on DirectShow for a couple of days and think I have started to grasp the idea of filters and filtergraphs. I found examples (to file / to device) how to play audio or write it to a file, but cannot seem to find the solution for a Stream object. Is this even possible? Could you point me in the right direction in case I missed something, please?

Best,

Hauke

回答1:

I would like to share my solution to my own problem with you (my focus was on the exotic bwf file format. hence the name.):

    using System;     using System.Collections.Generic;     using System.Text;     using DirectShowLib;     using System.Runtime.InteropServices;     using System.IO;      namespace ConvertBWF2WAV     {         public class BWF2WavConverter : ISampleGrabberCB         {             IFilterGraph2 gb = null;             ICaptureGraphBuilder2 icgb = null;             IBaseFilter ibfSrcFile = null;             DsROTEntry m_rot = null;             IMediaControl m_mediaCtrl = null;             ISampleGrabber sg = null;               public BWF2WavConverter()              {                  // Initialize                 int hr;                 icgb = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();                 gb = (IFilterGraph2) new FilterGraph();                 sg = (ISampleGrabber)new SampleGrabber();      #if DEBUG                 m_rot = new DsROTEntry(gb);     #endif                 hr = icgb.SetFiltergraph(gb);                 DsError.ThrowExceptionForHR(hr);             }              public void reset()             {                 gb = null;                 icgb = null;                 ibfSrcFile = null;                 m_rot = null;                 m_mediaCtrl = null;             }              public void convert(object obj)              {                 string[] pair = obj as string[];                 string srcfile = pair[0];                 string targetfile = pair[1];                  int hr;                  ibfSrcFile = (IBaseFilter)new AsyncReader();                 hr = gb.AddFilter(ibfSrcFile, "Reader");                 DsError.ThrowExceptionForHR(hr);                  IFileSourceFilter ifileSource = (IFileSourceFilter)ibfSrcFile;                 hr = ifileSource.Load(srcfile, null);                 DsError.ThrowExceptionForHR(hr);                  // the guid is the one from ffdshow                 Type fftype = Type.GetTypeFromCLSID(new Guid("0F40E1E5-4F79-4988-B1A9-CC98794E6B55"));                 object ffdshow = Activator.CreateInstance(fftype);                 hr = gb.AddFilter((IBaseFilter)ffdshow, "ffdshow");                 DsError.ThrowExceptionForHR(hr);                  // the guid is the one from the WAV Dest sample in the SDK                 Type type = Type.GetTypeFromCLSID(new Guid("3C78B8E2-6C4D-11d1-ADE2-0000F8754B99"));                 object wavedest = Activator.CreateInstance(type);                 hr = gb.AddFilter        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!