Click here to Skip to main content
15,896,497 members
Articles / Desktop Programming / MFC

Converting Wav file to MP3 or other format using DirectShow

Rate me:
Please Sign up or sign in to vote.
4.84/5 (49 votes)
30 Jul 2002CPOL3 min read 702.3K   22.2K   188  
Simple class to convert stereo 44 kHz, 16 bit wav file to another format, including MP3. The class shows how to use DirectShow API for audio conversion.
// DSCodec.cpp: implementation of the CDSCodec class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DShowEncoder.h"
#include "DSCodec.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDSCodec::CDSCodec()
{
	m_pMoniker = NULL;
	m_szCodecName.Empty();
}

CDSCodec::~CDSCodec()
{
	int nNumberOfFormat = GetSize();
	for (int i=0; i<nNumberOfFormat; i++) {
		delete GetAt(i);
	}
	RemoveAll();

	if (m_pMoniker != NULL) {
		m_pMoniker->Release();
		m_pMoniker = NULL;
	}
}

void CDSCodec::BuildCodecFormatArray()
{
	if (m_pMoniker == NULL) return;

	HRESULT			hr;
	IBaseFilter		*pBaseFilter = NULL;

	// Retrieve the IBaseFilter
	hr = m_pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, (void**) &pBaseFilter);
	if (FAILED(hr)) {
		// ERROR HERE
		return;
	}

	// Enumerate Pin
	IEnumPins	*pEnumPins = NULL;
	hr = pBaseFilter->EnumPins(&pEnumPins);
	if (FAILED(hr)) {
		// ERROR HERE
		pBaseFilter->Release();
		return;
	}

	// Find the output Pin
	IPin	* pPin = NULL;
	while (pEnumPins->Next(1, &pPin, 0) == S_OK) {
		PIN_DIRECTION direction;
		pPin->QueryDirection(&direction);
		if (direction == PINDIR_OUTPUT) {
			// Retrieve the IAMStreamConfig
			IAMStreamConfig	*pStreamConfig = NULL;
			hr = pPin->QueryInterface(IID_IAMStreamConfig, (void**) &pStreamConfig);
			if (SUCCEEDED(hr)) {
				int nCount = 0, nSize = 0;
				pStreamConfig->GetNumberOfCapabilities(&nCount, &nSize);
				for (int i=0; i<nCount; i++) {
					AM_MEDIA_TYPE* pMediaType = NULL;
					AUDIO_STREAM_CONFIG_CAPS confCaps;
					hr = pStreamConfig->GetStreamCaps(i, &pMediaType, (BYTE*)&confCaps);
					if (SUCCEEDED(hr)) {
						CDSCodecFormat *pCodecFormat = new CDSCodecFormat();
						pCodecFormat->m_pMediaType = pMediaType;
						Add(pCodecFormat);
					}
				}
				pStreamConfig->Release();
			}
		}
		pPin->Release();
	}

	pEnumPins->Release();
	pBaseFilter->Release();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) G. LABOURE
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions