Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 204587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:28:51+00:00 2026-05-11T17:28:51+00:00

What I would like to do is to pass an arbitrary audio file to

  • 0

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 1 Answer
  • 5 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:28:51+00:00Added an answer on May 11, 2026 at 5:28 pm

    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((IBaseFilter)wavedest, "WAV Dest");
                    DsError.ThrowExceptionForHR(hr);
    
                    // manually tell the graph builder to try to hook up the pin that is left
                    IPin pWaveDestOut = null;
                    hr = icgb.FindPin(wavedest, PinDirection.Output, null, null, true, 0, out pWaveDestOut);
                    DsError.ThrowExceptionForHR(hr);
    
                    // render step 1
                    hr = icgb.RenderStream(null, null, ibfSrcFile, (IBaseFilter)ffdshow, (IBaseFilter)wavedest);
                    DsError.ThrowExceptionForHR(hr);
    
                     // Configure the sample grabber
                    IBaseFilter baseGrabFlt = sg as IBaseFilter;
                    ConfigSampleGrabber(sg);
                    IPin pGrabberIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);
                    IPin pGrabberOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);
                    hr = gb.AddFilter((IBaseFilter)sg, "SampleGrabber");
                    DsError.ThrowExceptionForHR(hr);
                    AMMediaType mediatype = new AMMediaType();
                    sg.GetConnectedMediaType(mediatype);
    
                    hr = gb.Connect(pWaveDestOut, pGrabberIn);
                    DsError.ThrowExceptionForHR(hr);
    
                    // file writer
                    FileWriter file_writer = new FileWriter();
                    IFileSinkFilter fs = (IFileSinkFilter)file_writer;
                    fs.SetFileName(targetfile, null);
                    hr = gb.AddFilter((DirectShowLib.IBaseFilter)file_writer, "File Writer");
                    DsError.ThrowExceptionForHR(hr);
    
                    // render step 2
                    AMMediaType mediatype2 = new AMMediaType();
                    pWaveDestOut.ConnectionMediaType(mediatype2);
                    gb.Render(pGrabberOut);
    
                    // alternatively to the file writer use the NullRenderer() to just discard the rest
    
                    // assign control
                    m_mediaCtrl = gb as IMediaControl;
    
                    // run
                    hr = m_mediaCtrl.Run();
                    DsError.ThrowExceptionForHR(hr);
    
    
                }
    
    
                //
                // configure the SampleGrabber filter of the graph
                //
                void ConfigSampleGrabber(ISampleGrabber sampGrabber)
                {
                    AMMediaType media;
    
                    // set the media type. works with "stream" somehow...
                    media = new AMMediaType();
                    media.majorType = MediaType.Stream;
                    //media.subType = MediaSubType.WAVE;
                    //media.formatType = FormatType.WaveEx;
    
                    // that's the call to the ISampleGrabber interface
                    sg.SetMediaType(media);
    
                    DsUtils.FreeAMMediaType(media);
                    media = null;
    
                    // set BufferCB as the desired Callback function
                    sg.SetCallback(this, 1);
                }
    
                public int SampleCB(double a, IMediaSample b)
                {
                    return 0;
                }
    
                /// <summary>
                /// Called on each SampleGrabber hit.
                /// </summary>
                /// <param name="SampleTime">Starting time of the sample, in seconds.</param>
                /// <param name="pBuffer">Pointer to a buffer that contains the sample data.</param>
                /// <param name="BufferLen">Length of the buffer pointed to by pBuffer, in bytes.</param>
                /// <returns></returns>
                public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
                {
                    byte[] buffer = new byte[BufferLen];
                    Marshal.Copy(pBuffer, buffer, 0, BufferLen);
                    using (BinaryWriter binWriter = new BinaryWriter(File.Open(@"C:\directshowoutput.pcm", FileMode.Append)))
                    {
                        binWriter.Write(buffer);
                    }
                    return 0;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to pass arbitrary container as an argument of function and iterate
I have an arbitrary number of lists which I would like to process using
I would like to pass to R a txt file with a matrix, where
I would like to create a function where I can pass in an arbitrary
I would like to pass an argument(s) to a method being defined using define_method,
I would like to pass data from a database column to another page via
I would like to pass a MYSQL query via Coldfusion the following date: 03/13/2010
I would like to pass values into the constructor on the class that implements
I would like to pass the multiple arguments with positive or negative values. Is
I would like to pass to a jQuery function a regular function, instead of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.