| Using DirectShow Editing Services (DES) with C# |
|
|
|
|
I recently answered a couple of questions about audio files processing in .Net. I though I could write a short tutorial on using DirectShow Editing Services (DES) to append two wav files (which was one of the questions asked). Again a zip file with the code is provided. We are not going to cover all the interfaces provided for editing services but we'll show a simple use of them by creating a timeline with a single track that plays the two wav files one after the other. In this way, you'll get an introduction to the basic objects of DES and how to use them in C#. DES manipulate media files using the concept of a timeline. A timeline is a container for audio or video groups which themselves contain compositions and tracks. The tracks are the objects that contain our media files. In this tutorial, we'll construct a timeline with a single audio group and a single track; that track will contain our two wav files scheduled to play one after another. First, let's start by bringing in the assembly that contains these interfaces. In VS, you can add a reference to the library qedit.dll, it will generate an assembly named "DexterLib" that we import into our program:
We'll also use the DirectShowLib from sourceforge for the other DirectShow interfaces. Then, we declare variables for the timeline object and its components:
As the comments describe, we have one timeline with one group and one track but two sources objects. Building the timeline is going to be done in the BuildTimeline method which start by creating a AMTimelineClass object, then add an audio group and a track:
You don't create groups, (compositions), tracks and sources by using the typical
"new" keyword but by calling the method CreateEmptyNode with the
corresponding type in this method second argument. For a group, we have to
set its media type using the SetMediaTypeForVB method, which comes
from the VB access to the qedit.dll.
Then we call AddGroup to add the
group to the timeline. To add a track to the timeline, we have to go through
the IAMTimelineComp interface to a composition object and call the
method VTrackInsBefore which insert a track in the composition. In
this code, we treat the group object as a kind of composition. The group,
compostion and track distinctions are a bit arbitrary in my opinion, and the
design of this part of the DES library sounds a little artificial. Anyway, once we've added the group and track to our timeline. We need to create our two source objects:
The code for the two sources is similar. We create a source object with a call to CreateEmptyNode as before. Then, we use a MediaDet object to retrieve the stream length (we assume that the file contains only one audio stream) and multiply this value by 1000000 since the source object method SetStartStop required a long argument which represents 100 nanoseconds unit. The call to SetStartStop for the second source use the length and length+length2+1 as start and stop time. This is how we set our timeline to play one file after the other. Then, we add the source to the track by calling the SrcAdd method. Once, we have created our timeline, we are ready to create our RenderEngine object:
The graph filters that are built in DES are create by calling ConnectFrontEnd after setting its timeline object. This filter graph will have one output pin for every group in the timeline. Now, we'll build the "back end" of the filter graph by adding a file writer and a WAV Dest filter to it. But first, we need to access the filter graph created by our call to ConnectFrontEnd, this is done like this:
We call GetFilterGraph and GetGroupOutputPin then call BuildBackEnd and use Intelligent Connect to build the rest of the filter graph from the RenderEngine's outpin pin. BuildBackEnd just add two filters to the graph:
We create a FileWriter filter and access its IFileSinkFilter interface to set its name. Then we create a WAV Dest filter, which you need to compile and install (WAV Dest is a filter in the Samples directory of the SDK). After that, the call to Render with the front end output pin connects the necessary pins. We are left to issue the Run command on the MediaControl object to write the file to disk. That's it. We have appended two wav files using the DirectShow Editing Services with C#. We've looked at timeline, group, track and source objects. And use the RenderEngine to create a filter group that could be connected to any desired "back end". |
| < Prev | Next > |
|---|














