Click here to Skip to main content
15,892,674 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 701.7K   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.
// DSCodecFormat.cpp: implementation of the CDSCodecFormat class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DShowEncoder.h"
#include "DSCodecFormat.h"

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

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

CDSCodecFormat::CDSCodecFormat()
{
	m_pMediaType = NULL;
}

CDSCodecFormat::~CDSCodecFormat()
{
	if (m_pMediaType != NULL) {
		// Free pMediaType
		if (m_pMediaType->cbFormat != 0) {
			CoTaskMemFree((PVOID)m_pMediaType->pbFormat);

			// Strictly unnecessary but tidier
			m_pMediaType->cbFormat = 0;
			m_pMediaType->pbFormat = NULL;
		}
		if (m_pMediaType->pUnk != NULL) {
			m_pMediaType->pUnk->Release();
			m_pMediaType->pUnk = NULL;
		}
		CoTaskMemFree((PVOID)m_pMediaType);
	}
}

WORD CDSCodecFormat::NumberOfChannels()
{
	if (m_pMediaType != NULL) {
		WAVEFORMATEX* pFormat = (WAVEFORMATEX*) m_pMediaType->pbFormat;
		return pFormat->nChannels;
	}
	return -1;
}

DWORD CDSCodecFormat::SamplesPerSecond()
{
	if (m_pMediaType != NULL) {
		WAVEFORMATEX* pFormat = (WAVEFORMATEX*) m_pMediaType->pbFormat;
		return pFormat->nSamplesPerSec;
	}
	return -1;
}

DWORD CDSCodecFormat::BytesPerSec()
{
	if (m_pMediaType != NULL) {
		WAVEFORMATEX* pFormat = (WAVEFORMATEX*) m_pMediaType->pbFormat;
		return pFormat->nAvgBytesPerSec;
	}
	return -1;
}

WORD CDSCodecFormat::BitsPerSample()
{
	if (m_pMediaType != NULL) {
		WAVEFORMATEX* pFormat = (WAVEFORMATEX*) m_pMediaType->pbFormat;
		return pFormat->wBitsPerSample;
	}
	return -1;
}

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