(DirectX系列04)DirectShow 音频录制

    在DirectX的Bin目录下有一个很好的工具-GraphEdit,通过这个工具能够很好的反映音频录制的过程。可以总结一点,DirectShow音频的录制过程就是,枚举、绑定、连接这三个步骤。接下来看看这三个步骤是如何实现的呢?

    在此之前,先来介绍下几个涉及到的COM对象。

    ICaptureGraphBuilder2

     IBaseFilter

    The IBaseFilter interface is the primary interface for DirectShow filters. All DirectShow filters must expose this interface. The Filter Graph Manager uses this interface to control filters. Applications can use this interface to enumerate pins and query for filter information, but should not use it to change the state of a filter. Instead, use the IMediaControl interface on the Filter Graph Manager.

    Filter developers: Implement this interface on every DirectShow filter. The CBaseFilter base class implements this interface.

 

    The ICaptureGraphBuilder2 interface builds capture graphs and other custom filter graphs. The Capture Graph Builder object implements this interface.

    在这里主要用于获取捕获链表的管理接口,通过这个接口可以对创建的IGraphBuilder进行绑定和管理。

     IGraphBuilder

     The IGraphBuilder interface allows applications to call upon the filter graph manager to attempt to build a complete filter graph, or parts of a filter graph given only partial information, such as the name of a file or the interfaces of two separate pins. The filter mapper looks up filters in the registry to configure the filter graph in a meaningful way.

 

   IGraphBuilder inherits from the IFilterGraph interface and exposes all its methods. For this reason, IFilterGraph should normally not be used directly.

     通常可以通过IGraphBuilder这个COM对象,我们获取滤波器链表接口,对构建的Graph进行管理。

     IMediaControl

     The IMediaControl interface provides methods for controlling the flow of data through the filter graph. It includes methods for running, pausing, and stopping the graph. The Filter Graph Manager implements this interface. For more information on filter graph states, see Data Flow in the Filter Graph.

   IMoniker

      Enables you to use a moniker object, which contains information that uniquely identifies a COM object. An object that has a pointer to the moniker object's IMoniker interface can locate, activate, and get access to the identified object without having any other specific information on where the object is actually located in a distributed system.

     Monikers are used as the basis for linking in COM. A linked object contains a moniker that identifies its source. When the user activates the linked object to edit it, the moniker is bound; this loads the link source into memory.

 

接下来看看实现代码,其中有相关的注释,清晰明了。

 

// 获取捕获链表管理器接口
CoCreateInstance(CLSID_CaptureGraphBuilder2,NULL,
CLSCTX_INPROC_SERVER,
IID_ICaptureGraphBuilder2,
(void**)&pBuilder);

// 获取滤波器借口
CoCreateInstance(CLSID_FilterGraph, NULL,
CLSCTX_INPROC_SERVER,
IID_IGraphBuilder,
(void **)&pGraph);

// 将获取到的滤波器接口帮定到链表管理器接口上
pBuilder->SetFiltergraph(pGraph);

// 查询煤体控制接口
pGraph->QueryInterface(IID_IMediaControl,(void**)&pMC);

ICreateDevEnum *pDevEnum = NULL;

// 创建设备枚举接口
CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
CLSCTX_INPROC,
IID_ICreateDevEnum,
(void **)&pDevEnum);

IEnumMoniker *pClassEnum = NULL;

// 创建音频设备输入接口
pDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pClassEnum, 0);
ULONG cFetched;
if (pClassEnum->Next(1, &pMoniker, &cFetched) == S_OK)
{
// 绑定到基类滤波器,以作为捕获滤波器
pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);
pMoniker->Release();
}
pClassEnum->Release();

// 创建CLSID_WavDest
CoCreateInstance(CLSID_WavDest, NULL, CLSCTX_ALL,
IID_IBaseFilter, (void **)&pWaveDest);

// 创建CLSID_FileWriter
CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_ALL,
IID_IBaseFilter, (void **)&pWriter);

// 添加滤波器
pGraph->AddFilter(pSrc,L"Wav");
pGraph->AddFilter(pWaveDest,L"WavDest");
pGraph->AddFilter(pWriter,L"FileWriter");

// 枚举查询文件设置接口
pWriter->QueryInterface(IID_IFileSinkFilter2,(void**)&pSink);

pSink->SetFileName(strName.AllocSysString(),NULL);

IPin* pOutpin = FindPin(pSrc,PINDIR_OUTPUT);

IPin* pInpin,*pOut;

pOut= FindPin(pWaveDest,PINDIR_OUTPUT);

AM_MEDIA_TYPE type;
type.majortype = MEDIATYPE_Stream;
type.subtype =MEDIASUBTYPE_WAVE;
type.formattype = FORMAT_None;
type.bFixedSizeSamples = FALSE;
type.bTemporalCompression = FALSE;
type.pUnk = NULL;

//查找滤波器引脚
pInpin = FindPin(pWaveDest,PINDIR_INPUT);
IPin* pInpin2= FindPin(pWriter,PINDIR_INPUT);

//连接滤波器的引脚
pGraph->ConnectDirect(pOutpin,pInpin,NULL);
pGraph->ConnectDirect(pOut,pInpin2,NULL);

pMC->Run();

时间: 2024-08-04 09:17:32

(DirectX系列04)DirectShow 音频录制的相关文章

(DirectX系列09)DirectShow EDS应用编码分析

(DirectX系列09)DirectShow EDS应用编码分析 DES (DirectShow Editing Services),是一套基于DirectShow核心框架的编程接口.DES的出现,简化了视频编辑任务,弥补了DirectShow对于媒体文件非线性编辑支持的先天性不足.但是,就技术本身而言,DES并没有超越DirectShow Filter架构,而只是DirectShow Filter的一种增强应用. 本章通过DirectX 下timelinetest为例子详细介绍Eds应用编码

(DirectX系列06)DirectShow 字符叠加Filter编码分析

(DirectX系列06)DirectShow 字符叠加Filter编码分析 在很多视频播放的软件当中,字幕的处理是免不了的,有些可能本身已经加载到图像当中未做处理,但大部分都是通过字符叠加来进行处理的.DirectShow 的字符叠加Filter在这些软件中都扮演这绝佳的作用.这一节来介绍DirectShow字符叠加Filter编码的实现,如下详细介绍: 这个Filter的大概作用是在视频流指定的一段时间内进行字符叠加,字符字体.大小.颜色都进行控制,普遍支持RGB 的各种编码格式,同时实现字

(DirectX系列05)DirectShow 视频采集

    前几小节陆续介绍了Directsound.Directshow音频处理方面的技术,还没有介绍视频方面的技术,从这节开始介绍视频采集方面的技术.今日刚好就介绍Directshow视频采集技术,其实DirectShow视频采集并不复杂,复杂的部分微软已经帮做好了,只需按照顺序将API连接起来即可,如下所叙述: 系统设备枚举     系统设备枚举器为我们按类型枚举已注册在系统中的Fitler提供了统一的方法.而且它能够区分不同的硬件设备,即便是同一个Filter支持它们.这对那些使用Windo

(DirectX系列03)DirectShow简单媒体文件播放

    Directshow SDK提供了一种开放的开发环境,开发人员可以根据自己的需要制定各种组件.Directshow使用一种滤波器链表(Filter Graph)的模型来管理整个数据流的处理流程,每个部分都是COM组件,形成模块化,参与数据处理的模块就是滤波器(Filter).      上几周学习DirectSound做了些总结,今天开始学习DirectShow今天主要介绍一个简单的媒体文件播放的程序.      首先来介绍下DirectShow的环境搭建,其实环境的搭建并不是很难,但是

(DirectX系列08)DirectShow WavDest编码分析

     在DirectShow 中有很多Samples,WavDest就是其中一个,这个Fliter主要用于将采集到的视频流写入到指定的文件,文件格式是.WAV.如下来看看具体的代码实现.     对于每一个Filter都有一个固定的注册区代码,这是必不可少,如下代码: // {3C78B8E2-6C4D-11d1-ADE2-0000F8754B99} static const GUID CLSID_WavDest = { 0x3c78b8e2, 0x6c4d, 0x11d1, { 0xad,

(DirectX系列07)DirectShow MFC下Filter编码分析

    MFC下Filter的编写和Win32下Filter的编写极其相似,但又存在很多不同点,在Win32中需要去实现CreateInstance函数,而在MFC直接用NEW 动态产生,不过这并不代表不需要去维护Filter对象计数,因此在MFC 下Filter中依然需要调用AddRef函数来维护这个平衡:其它还不需要实现注册表注册等功能,如下代码: ////////////////////////////////////////////////////////////////////////

(DirectX系列01)DirectSound 3D语音特效

       近日正在学习DirectX,主要用于视频监控和流媒体方面应用,学习<Visual C++音视频处理技术和工程实践>已经有大半,一直想写些感受,兴趣所致,今天重新学习了下DirectSound 3D语音特效,并编写一些代码,在此分享出来,希望对大家能有所帮助.        大家都有这种感觉,当我们离发声源越来越远的时候,声音越来越小.DirectSound就是模仿这些现象,从数学理论角度加以描述.当然,影响音效的因素不光只有这些,还有如Doppler效应等,但是DirectSou

Android音频录制MediaRecorder之简易的录音软件实现代码_Android

使用MediaRecorder的步骤:1.创建MediaRecorder对象2.调用MediRecorder对象的setAudioSource()方法设置声音的来源,一般传入MediaRecorder.MIC3.调用MediaRecorder对象的setOutputFormat()设置所录制的音频文件的格式4.调用MediaRecorder对象的setAudioRncoder().setAudioEncodingBitRate(int bitRate).setAudioSamlingRate(i

Android音频录制MediaRecorder之简易的录音软件实现代码

使用MediaRecorder的步骤:1.创建MediaRecorder对象2.调用MediRecorder对象的setAudioSource()方法设置声音的来源,一般传入MediaRecorder.MIC3.调用MediaRecorder对象的setOutputFormat()设置所录制的音频文件的格式4.调用MediaRecorder对象的setAudioRncoder().setAudioEncodingBitRate(int bitRate).setAudioSamlingRate(i