Click here to Skip to main content
15,880,543 members
Articles / Multimedia / DirectX

Audio Capture with DirectShow - Part 3

Rate me:
Please Sign up or sign in to vote.
4.60/5 (7 votes)
10 Feb 2009CPOL2 min read 68.1K   2.7K   28   13
A console program that captures audio and saves it with simultaneous preview.

Introduction

This tutorial is the final part of the three part tutorial on audio capture with DirectShow. This part uses the infinite pin tee filter to provide preview (pre-listen) while saving the file.

Background

Since I have written two other tutorials which are the building grounds for this one, they should be read through thoroughly. Following are the other two tutorials:

Using the code

The only code that is extra from the second part of the tutorial is that of the infinite pin tee filter. The filter connects with the audio input, and the outputs of the tee filters connect with two filters, the AudioRecorder WAV Dest filter and the speakers, and in my case, the headphones, too.

cacsp.jpg

Following is the code where the filter is read, added to the graph, and the connections are made:

C++
//Infinite Pin Tee filter
// the DirectShow filters category
DEVICE_CLSID = CLSID_LegacyAmFilterCategory;
// device name as seen in Graphedit.exe, 
// using SysAllicString(..) to properly allocate string 
bstrDeviceName = SysAllocString(L"Infinite Pin Tee Filter");
//read the required device
pDeviceMonik = Device_Read(pDeviceEnum,pDeviceMonik, 
                           DEVICE_CLSID,bstrDeviceName);
//Return the device after initializing it
pPinTeeFilter = Device_Init(pDeviceMonik,pPinTeeFilter);
//add device to graph
Device_Addition(pGraph,pPinTeeFilter,bstrDeviceName);

//Connect input to output, Mic to Infinite Pin Tee filter
//Input pin name, as seen in GraphEdit
bstrInPinName = SysAllocString(L"Capture");
//Output pin name, this one will be input for Infinite Pin Tee Filter
bstrOutPinName = SysAllocString(L"Input");
//connect
Device_Connect(pInputDevice,pPinTeeFilter,bstrInPinName,bstrOutPinName);

//Connect input to output, Infinite Pin Tee filter
//to AudioRecorder WAV Dest filter
//As seen in GraphEdit
bstrInPinName = SysAllocString(L"Output1");
//Output pin name 
bstrOutPinName = SysAllocString(L"In");
//connect
Device_Connect(pPinTeeFilter,pWAVRecorder,bstrInPinName,bstrOutPinName);

///Connect input to output, Infinite Pin Tee filter to Default
//sound output i.e. headphones or speakers etc.
//As seen in GraphEdit
bstrInPinName = SysAllocString(L"Output2");
//Output pin name 
bstrOutPinName = SysAllocString(L"Audio Input pin (rendered)");
//connect
Device_Connect(pPinTeeFilter,pOutputDevice,bstrInPinName,bstrOutPinName);

After the connections are made, we are ready to go. The run() method kick starts the graph.

Final word

I hope this small series of tutorials on getting to know the use of DirectShow filters would be helpful for beginners. I deliberately broke apart the whole process into three steps so that a newbie could have a better insight at how, at each step, things are built around filters. How filters are read, instantiated, connected, and finally used to do something useful for us. To be honest, when I started DirectShow programming, it was very difficult to get to a starting point. MSDN tells a lot about filters, but I personally found MSDN quite hard to grasp as you need to loop back into your past to get a grasp of what is being explained.

I enjoyed my journey here, I hope you did too. Thanks for all your time!

The code is built with the following

  • Windows Server 2008
  • Microsoft Visual Studio 2008
  • DirectX 10.1
  • Microsoft Windows SDK 6.1

References

History

  • 08/02/2008: Article update.
  • Fixed Bstr_Compare(...) that correctly compares two BSTR type strings.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer -
Pakistan Pakistan
Computer Programming

Comments and Discussions

 
GeneralNo such filter Audiorecorder wav dest in my system? Pin
Du Sijun6-Jun-11 23:20
Du Sijun6-Jun-11 23:20 
GeneralRe: No such filter Audiorecorder wav dest in my system? Pin
WajihUllahBaig7-Jun-11 6:58
WajihUllahBaig7-Jun-11 6:58 
GeneralRe: No such filter Audiorecorder wav dest in my system? Pin
WajihUllahBaig7-Jun-11 7:02
WajihUllahBaig7-Jun-11 7:02 
GeneralAudio Capture in C# Pin
stuartmclark9-Nov-10 0:01
stuartmclark9-Nov-10 0:01 
GeneralRe: Audio Capture in C# Pin
WajihUllahBaig9-Nov-10 14:19
WajihUllahBaig9-Nov-10 14:19 
GeneralCapturing audio from window application Pin
audi0228-Mar-09 12:23
audi0228-Mar-09 12:23 
GeneralRe: Capturing audio from window application Pin
WajihUllahBaig28-Mar-09 20:47
WajihUllahBaig28-Mar-09 20:47 
GeneralIf you can display value of temprature on computer screen Pin
Rai Shahid10-Feb-09 20:04
Rai Shahid10-Feb-09 20:04 
GeneralRe: If you can display value of temprature on computer screen Pin
WajihUllahBaig11-Feb-09 18:51
WajihUllahBaig11-Feb-09 18:51 
GeneralRe: If you can display value of temprature on computer screen Pin
Rai Shahid12-Feb-09 0:02
Rai Shahid12-Feb-09 0:02 
GeneralRe: If you can display value of temprature on computer screen Pin
WajihUllahBaig12-Feb-09 19:11
WajihUllahBaig12-Feb-09 19:11 
GeneralMy vote of 1 Pin
J.Xing10-Feb-09 11:28
J.Xing10-Feb-09 11:28 
GeneralRe: My vote of 1 Pin
WajihUllahBaig11-Feb-09 0:39
WajihUllahBaig11-Feb-09 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.