Click here to Skip to main content
6,294,871 members and growing! (17,111 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » DirectShow     Intermediate License: The Code Project Open License (CPOL)

Converting Wav file to MP3 or other format using DirectShow

By Guitool

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.
VC6WinXP, MFC, Dev
Posted:30 Jul 2002
Views:353,399
Bookmarked:141 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
48 votes for this article.
Popularity: 7.97 Rating: 4.74 out of 5
2 votes, 4.9%
1

2
1 vote, 2.4%
3
6 votes, 14.6%
4
32 votes, 78.0%
5

Sample Image - DShowEncoder.gif

Introduction

This article explains how to use the DirectShow API for simple audio conversion, particularly Wav to MP3 conversion. Audio codecs in the DirectShow API are of three type : native codecs, ACM codecs, and DMO (DirectX Media Object) codecs.

There are only few audio native codecs for audio compression. For MP3 encoding, the only one that I've found is the LAME DirectShow wrapper from Elecard. Most MP3 encoders are in the ACM (Audio Compression Manager) format, wich was introduced with the Windows Multimedia API. CDSEncoder class and it's relative classes CDSCodec and CDSCodecFormat enumerate ACM codecs and their respective compression parameters, construct a graph and do the encoding.

The GraphBuilder and other filters

The graph consist of five filters:

  • File source (async) for reading the input wav file,
  • WAV Parser for wav parsing,
  • ACM codec for audio compression (in this case : MP3 ACM, wrapped by the ACM Wrapper Filter),
  • WAV Dest, for wav output multiplexing,
  • File Writer for writing the output file.

Important note

The WAV Dest filter is not included in standard filters, but need to be compiled from the DirectX SDK (SDK_root\Samples\Multimedia\DirectShow\Filters\WavDest). For convenience, the compiled WAV Dest filter is included in the demo zip, but you have to register it by RegSrv32 wavdest.ax.

ACM codecs and the ACM Wrapper filter

All of the ACM codecs are listed in DirectShow in the Audio Compressors Filter Category (CLSID_AudioCompressorCategory) and cannot be instantiated directly. We have to use the Device Enumerator to use them.

Note : Depending of your configuration, several ACM codecs for a same format can be installed on your computer. This can be the case for MP3 codecs. You set priority or deactivate some of them by the use of control panel, as show in the following figure.

Codec configuration

The DeviceEnumerator or how to browse ACM codecs

The Device Enumerator must be used to retrieve an instance of an ACM codec. It returns the codecs list by the IEnumMoniker interface, so we can get the filter interface (IBaseFilter) by a call to IMoniker::BindToObject() and the filter name by a call to IMoniker::BindToStorage().

Configuring the ACM codec with IAMStreamConfig interface

Once the desired codec is instantiated, we can obtain an IBaseFilter interface for filter configuration. Since each IBaseFilter have one or more Pin, we have to search the output Pin by the use of the IEnumPins interface and IPin::QueryDirection() calls.
With the output Pin, we can query the IAMStreamConfig interface to configure the following property :

  • Numbers of channels,
  • Samples per second,
  • Average byte per second,
  • Bits per sample.

Note : For some codecs (including MP3), the call to IAMStreamConfig::SetFormat() must be after the graph rendering.

The classes

CDSEncoder

CDSEncoder assumes the following task :

  • Enumerate the Audio codecs (CLSID_AudioCompressorCategory),
  • Build, render, and run the graph.

class CDSEncoder : public CArray<CDSCodec*, CDSCodec*>
{
public:
  void BuildGraph(CString szSrcFileName, CString szDestFileName, 
    int nCodec, int nFormat);
  CDSEncoder();
  virtual ~CDSEncoder();

protected:
  void BuildCodecArray();
  HRESULT AddFilterByClsid(IGraphBuilder *pGraph, LPCWSTR wszName, 
    const GUID& clsid, IBaseFilter **ppF);
  BOOL SetFilterFormat(AM_MEDIA_TYPE* pStreamFormat, 
    IBaseFilter* pBaseFilter);

  IGraphBuilder *m_pGraphBuilder;
};

As CDSEncoder inherits from CArray, the collection of codecs is exposed by CArray methods with each codecs returned as CDSCodec object.

CDSCodec

CDSCodec assumes the following task :

  • Enumerate the codec supported parameters,
  • expose the codec name.

class CDSCodec : public CArray<CDSCodecFormat*, CDSCodecFormat*>
{
public:
  CDSCodec();
  virtual ~CDSCodec();

  CString m_szCodecName;
  IMoniker  *m_pMoniker;
  void BuildCodecFormatArray();
};

As CDSCodec inherits from CArray, the collection of codecs supported parameters is exposed by CArray methods with each parameters returned as CDSCodecFormat object.

CDSCodecFormat

CDSCodecFormat exposes the properties of one-codec parameters :

  • Number of channels,
  • Samples per second,
  • Bytes per second,
  • Bits per samples.

class CDSCodecFormat  
{
public:
  WORD BitsPerSample();
  DWORD BytesPerSec();
  DWORD SamplesPerSecond();
  WORD NumberOfChannels();
  CDSCodecFormat();
  virtual ~CDSCodecFormat();

public:
  AM_MEDIA_TYPE* m_pMediaType;
};

Known issues

Errors Checking

The article goal is to demonstrate the use of DirectShow for simple audio conversion. These classes are not as safe as they have to be. Please keep this in mind if you plan to use it in a production environment.

Source Wav format

There are no sampling conversion, so you can only generate 44 kHz output files if you use 44 kHz Wav.

Windows Media

Windows Media format can be used only with a certificate that can be obtained by the Windows Media SDK from Microsoft.

License

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

About the Author

Guitool


Member

Occupation: Software Developer (Senior)
Company: G. LABOURE
Location: France France

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 121 (Total in Forum: 121) (Refresh)FirstPrevNext
GeneralSource Licence PinmemberMichael CSoft4:52 12 Jun '08  
GeneralConvert PinmemberSayyeid Mostafa Hashemi22:47 23 Apr '08  
GeneralConvert PinmemberSayyeid Mostafa Hashemi22:47 23 Apr '08  
GeneralMedia player not supported the file Pinmemberraseena0090:05 13 Feb '08  
Generalfatal error C1083: Cannot open include file: 'dshow.h': No such file or directory Pinmemberv.j20:19 29 Jan '08  
GeneralRe: fatal error C1083: Cannot open include file: 'dshow.h': No such file or directory PinmemberYudhaPH3:28 9 Feb '08  
QuestionHow to convert wav to another wav with different sample rates Pinmemberruilinxiang22:04 25 Dec '07  
QuestionHow to convert wav to another wav with different sample rates??? Pinmemberruilinxiang22:04 25 Dec '07  
Generalbuilding lame dshow PinmemberR Thompson8:23 8 Nov '07  
NewsOpenSource Audio library and Server Shoutcast PinmemberDAXweb_IT7:44 31 Jul '07  
GeneralWave Parsing PinmemberNilish22:40 16 May '07  
GeneralA Question about DShowEncoder Pinmemberchocm3:22 7 Mar '07  
GeneralRe: A Question about DShowEncoder Pinmembereurekarex21:26 2 Jun '07  
Generalplz help Pinmembersisaym20:41 23 Jan '07  
GeneralRe: plz help PinmemberFlyingTinman15:28 26 Jan '07  
GeneralAnyone tried this under vista? Pinmembergourmettt2:59 29 Nov '06  
GeneralRe: Anyone tried this under vista? PinmemberAcAriA0:02 10 Apr '07  
GeneralRe: Anyone tried this under vista? Pinmemberfurbo23:34 26 Jun '07  
GeneralRe: Anyone tried this under vista? Pinmemberfurbo12:23 8 Jul '07  
QuestionHow to convert .flv files to mp3 file ? Pinmemberamany13/11/20063:18 19 Nov '06  
Generalconverting a recorded .wav file into 16 bit PCM format .wav file Pinmembersl_krns20:48 27 Jul '06  
GeneralSmail Pinmembersmailmustafa5:52 18 May '06  
Generala question about input and out put Pinmemberhobin092022:50 14 Mar '06  
GeneralLincence for using this code PinmemberAvikram20:04 2 Jan '06  
GeneralCDA To MP3 PinmemberCaneMascherato21:37 29 May '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jul 2002
Editor: Nishant Sivakumar
Copyright 2002 by Guitool
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project