⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adpcm.cpp

📁 This code demonstrates how to use installed audio codecs to compress a WAVE file.
💻 CPP
字号:
#include <afx.h>
#include <dshow.h>
#include <iostream>
using namespace std;
HRESULT AddFilterByCLSID(
    IGraphBuilder *pGraph,  // Pointer to the Filter Graph Manager.
    LPCWSTR wszName,        // A name for the filter.
	const GUID& clsid,      // CLSID of the filter to create.
    IBaseFilter **ppF)      // Receives a pointer to the filter.
{
    if (!pGraph || ! ppF) return E_POINTER;
    *ppF = 0;
    IBaseFilter *pF = 0;
    HRESULT hr = CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER,
        IID_IBaseFilter, reinterpret_cast<void**>(&pF));
    if (SUCCEEDED(hr))
    {
        hr = pGraph->AddFilter(pF, wszName);
        if (SUCCEEDED(hr))
            *ppF = pF;
        else
            pF->Release();
    }
    return hr;
}

int main(int argc,char **argv)
{
	HRESULT hr;
	IBaseFilter *pParser=NULL,*pDest=NULL,*pSourceFilter=NULL,*pADPCM=NULL,*pWriter=NULL;
	IFileSinkFilter2 *pSink=NULL;
	IFileSourceFilter* pSource = NULL;
	IFilterGraph2 *pGraph = NULL;    // Graph builder interface
    IMediaControl *pControl = NULL;  // Media control interface
    IMediaEvent   *pEvent = NULL;    // Media event interface

	CLSID CLSID_WavParser;
	CLSID CLSID_MS_ADPCM;
	CLSID CLSID_WavDest;
	long code;

	UuidFromString((unsigned char*)"3C78B8E2-6C4D-11D1-ADE2-0000F8754B99", &CLSID_WavParser);
	UuidFromString((unsigned char*)"33D9A761-90C8-11D0-BD43-00A0C911CE86", &CLSID_MS_ADPCM);//MS ADPCM
	UuidFromString((unsigned char*)"D51BD5A1-7548-11CF-A520-0080C77EF58A", &CLSID_WavDest);
	
	hr = CoInitialize(NULL);
	if (!SUCCEEDED(hr))
	{
		cout<<"Error:COM";
		return -1;
	}
	
	hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IFilterGraph2, (void**) &pGraph);

	if (!SUCCEEDED(hr))
	{
		cout<<"Error:Graph Builder";
		return -1;
	}
	
	//Obtain MediaContol and MediaEvent interfaces
	pGraph->QueryInterface(IID_IMediaControl, reinterpret_cast<void**>(&pControl));
	pGraph->QueryInterface(IID_IMediaEventEx, reinterpret_cast<void**>(&pEvent));

	if (SUCCEEDED(hr)) {
		//File Source
		hr = AddFilterByCLSID(pGraph, L"File Source", CLSID_AsyncReader, &pSourceFilter);
		if (!SUCCEEDED(hr))
		{
			cout<<"Error:File Source";
			pControl->Release();
			pEvent->Release();
			pSourceFilter->Release();
			return -1;
		}
//		WCHAR wFileName[MAX_PATH];
		//MultiByteToWideChar(CP_ACP, 0, argv[1], -1, wFileName, MAX_PATH);
		
		//Wave File Parser
		hr = AddFilterByCLSID(pGraph, L"Wav Parser", CLSID_WavParser, &pParser);
		if (!SUCCEEDED(hr))
		{
			cout<<"Error:Wav Parser";
			pControl->Release();
			pEvent->Release();
			pSourceFilter->Release();
			return -1;
		}

		HRESULT hr;
		
		ICreateDevEnum *pSysDevEnum = NULL;
		hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,IID_ICreateDevEnum, (void **)&pSysDevEnum);
		if (FAILED(hr))
		{
			return hr;
		}

		// Obtain a class enumerator for the audio compressor category.
		IEnumMoniker *pEnumCat = NULL;
		hr = pSysDevEnum->CreateClassEnumerator(CLSID_AudioCompressorCategory, &pEnumCat, 0);

		if (hr == S_OK) 
		{
			// Enumerate the monikers.
			IMoniker *pMoniker = NULL;
			ULONG cFetched;
			while(pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)
			{
				IPropertyBag *pPropBag;
				hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag);
				if (SUCCEEDED(hr))
				{
					// To retrieve the filter's friendly name, do the following:
					VARIANT varName;
					VariantInit(&varName);
					hr = pPropBag->Read(L"FriendlyName", &varName, 0);
					if (SUCCEEDED(hr))
					{
						CString s(varName.bstrVal);
						cout<<s.GetBuffer(s.GetLength())<<"\n";
						s.ReleaseBuffer();
						if(s=="Microsoft ADPCM")
						{
							hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter,(void**)&pADPCM);
							pGraph->AddFilter(pADPCM,L"ADPCM");
							pPropBag->Release();
							pMoniker->Release();
							VariantClear(&varName);
							break;
						}
						// Display the name in your UI somehow.
					}
					VariantClear(&varName);
					pPropBag->Release();
				}
				pMoniker->Release();
			}
			pEnumCat->Release();
		}
		pSysDevEnum->Release();

		hr = AddFilterByCLSID(pGraph, L"Wav Dest", CLSID_WavDest, &pDest);
		
//		MultiByteToWideChar(CP_ACP, 0, argv[2], -1, wFileName, MAX_PATH);
		hr = AddFilterByCLSID(pGraph, L"File Writer", CLSID_FileWriter, &pWriter);
		hr=pWriter->QueryInterface(IID_IFileSinkFilter2,reinterpret_cast<void**>(&pSink));
		if (!SUCCEEDED(hr))
		{
			cout<<"Error:Itf:File Destination";
			pControl->Release();
			pEvent->Release();
			pSourceFilter->Release();
			pParser->Release();
			pADPCM->Release();
			pDest->Release();
			return -1;
		}
		
		pSink->SetFileName(/*wFileName*/L"c:\\out2.wav",NULL);
		pSink->Release();

		//Now run the graph
		if(SUCCEEDED(pGraph->RenderFile(L"c:\\out.wav", NULL)))
		{
			hr=pControl->Run();
			pEvent->WaitForCompletion(5000/*INFINITE*/,&code);
			hr=pControl->Stop();
		}

		//Cleanup
		pControl->Release();
		pEvent->Release();
		pSourceFilter->Release();
		pParser->Release();
		pADPCM->Release();
		pDest->Release();
		pWriter->Release();
		CoUninitialize();

	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -