Click here to Skip to main content
15,446,232 members
Articles / Desktop Programming / Win32
Posted 12 Dec 2008


86 bookmarked

Play Audio Files with DirectSound and Display its Spectrum in Real Time - Part 3

Rate me:
Please Sign up or sign in to vote.
4.93/5 (21 votes)
24 Dec 2008CPOL3 min read
An article to show how to play audio file with DirectSound and display its spectrum in real time accurately


This article is an improved edition of my article: Play Wave Files with DirectSound and Display its Spectrum in Real Time - Part 2. In this article, I add MP3, WMA, WAV, and OGG Vorbis support, which need Windows Media 9 or higher support, and I do some extra work to reorganize code. Here have two articles were posted on Code Project before this article, they are listed below.


The input stream includes file, CD, microphone and so on. In this article, the input stream is audio file. So, I define CInput as base classes for all input types, and have some pure virtual functions defined in this class. The function GetDataInternal must be implemented by derived class, which reads audio data from audio file and fills it to a buffer.

In this article, I use WMF (Windows Media Format) decodes WMA format. The main interfaces that were used are IWMSyncReader, INSSBuffer, IWMHeaderInfo. The IWMSyncReader interface provides the ability to read WMA files using synchronous calls. The INSSBuffer interface is the basic interface of a buffer object. A buffer object is a wrapper around a memory buffer. The methods exposed by this interface are used to manipulate the buffer. The IWMHeaderInfo interface sets and retrieves information in the header section of an ASF file. You can manipulate three types of header information by using the methods of this interface: metadata attributes, markers, and script commands. You can read MSDN for more information.

The libraries like libmad, libogg and libvorbis are all integrated in this application. And I defined four classes for MP3, WMA, WAV and OGG Vorbis files reading, these are CWMIn, CWaveIn, CMP3In and CVorbisIn. For more details, please read the code.

The application decodes mp3 files with libmad, it does not depend on WMF (Windows Media Format) anymore, this library is so cool, I like it. :)


This component already implemented in Play Wave Files with DirectSound and Display its Spectrum in Real Time - Part 2 uses DirectSound for output. So, if you have some questions about it, you should read it first. You can also search CodeProject with the keyword DirectSound; you will get many articles about it.

The Expectation

I would always feel that the spectrum displayed in the window is not right, especially when playing some sound files. So, I hope I can get help on that, and thoroughly deal with it.

Bugs Fixes

There have some bugs that exist in the article Play Wave Files with DirectSound and Display its Spectrum in Real Time - Part 2. I have done a lot of work to fix it. There may exist unknown bugs, which require time to find. If you find any, let me know. Thanks.


  • 23th December, 2008: New version is posted, which supports mp3, wav, wma, and ogg. The another important thing is what decodes mp3 files with libmad without depends on WMF (Windows Media Format) anymore.
  • 12th December, 2008: Initial post


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

Written By
Software Developer none
China China
To be, or not to be, this is question. That's are all depend on your decision. What do you think?

Comments and Discussions

PraiseThank U! Pin
Member 1335700711-Aug-17 1:40
MemberMember 1335700711-Aug-17 1:40 
QuestionThank you Pin
koujunyu30-Oct-12 5:08
Memberkoujunyu30-Oct-12 5:08 
AnswerRe: Thank you Pin
jackyxinli27-Jan-13 20:45
Memberjackyxinli27-Jan-13 20:45 
AnswerRe: Thank you Pin
tianlunzhizi16-Jul-13 15:46
Membertianlunzhizi16-Jul-13 15:46 
GeneralMy vote of 5 Pin
bh2007720-Nov-11 13:41
Memberbh2007720-Nov-11 13:41 
QuestionHow to calculate VU and peak values Pin
29044790722-Aug-11 18:06
Member29044790722-Aug-11 18:06 
Questionaudio bars for rtsp streaming [modified] Pin
arunnnnnkumar19-Aug-11 22:47
Memberarunnnnnkumar19-Aug-11 22:47 
Questioncontrol volume Pin
kinani10-Aug-11 4:48
Memberkinani10-Aug-11 4:48 
AnswerRe: control volume Pin
jackyxinli10-Aug-11 14:24
Memberjackyxinli10-Aug-11 14:24 
Questionplaying WAV Pin
kinani10-Aug-11 1:08
Memberkinani10-Aug-11 1:08 
AnswerRe: playing WAV Pin
jackyxinli10-Aug-11 14:27
Memberjackyxinli10-Aug-11 14:27 
QuestionDo you have a .NET version? Pin
Tinhiuvanoinho23-Jul-11 18:08
MemberTinhiuvanoinho23-Jul-11 18:08 
AnswerRe: Do you have a .NET version? Pin
jackyxinli24-Jul-11 14:55
Memberjackyxinli24-Jul-11 14:55 
GeneralPause and Resume...makes some noise Pin
youndie3-Aug-09 23:31
Memberyoundie3-Aug-09 23:31 
GeneralRe: Pause and Resume...makes some noise Pin
jackyxinli4-Aug-09 21:32
Memberjackyxinli4-Aug-09 21:32 
GeneralRe: Pause and Resume...makes some noise Pin
youndie10-Aug-09 16:04
Memberyoundie10-Aug-09 16:04 
GeneralRe: Pause and Resume...makes some noise Pin
jackyxinli11-Aug-09 17:10
Memberjackyxinli11-Aug-09 17:10 
News0.2009.5.6065 released Pin
jackyxinli5-May-09 16:19
Memberjackyxinli5-May-09 16:19 
Generalnew version released. Pin
jackyxinli9-Feb-09 22:20
Memberjackyxinli9-Feb-09 22:20 
GeneralRe: new version released. Pin
chube22106-Apr-09 20:51
Memberchube22106-Apr-09 20:51 
GeneralRe: new version released. Pin
jackyxinli8-Apr-09 21:23
Memberjackyxinli8-Apr-09 21:23 
GeneralRe: new version released. Pin
feels7-Apr-09 22:07
Memberfeels7-Apr-09 22:07 
GeneralRe: new version released. Pin
jackyxinli8-Apr-09 21:24
Memberjackyxinli8-Apr-09 21:24 
GeneralRe: new version released. Pin
feels9-Apr-09 22:28
Memberfeels9-Apr-09 22:28 
GeneralRe: new version released. Pin
jackyxinli9-Apr-09 22:35
Memberjackyxinli9-Apr-09 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.