Click here to Skip to main content
12,898,174 members (56,996 online)
Click here to Skip to main content
Add your own
alternative version


84 bookmarked
Posted 12 Dec 2008

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

, 24 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

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?

You may also be interested in...

Comments and Discussions

GeneralRe: new version released. Pin
feels9-Apr-09 22:28
memberfeels9-Apr-09 22:28 
GeneralRe: new version released. Pin
jacky_zz9-Apr-09 22:35
memberjacky_zz9-Apr-09 22:35 
GeneralRe: new version released. Pin
araknoid18-Jan-11 1:36
memberaraknoid18-Jan-11 1:36 
GeneralRe: new version released. Pin
jacky_zz20-Jan-11 13:46
memberjacky_zz20-Jan-11 13:46 
GeneralComments about spectrum display Pin
lao_o8-Jan-09 20:43
memberlao_o8-Jan-09 20:43 
GeneralRe: Comments about spectrum display Pin
jacky_zz11-Jan-09 16:41
memberjacky_zz11-Jan-09 16:41 
GeneralRe: Comments about spectrum display Pin
hdw1912-Feb-09 20:46
memberhdw1912-Feb-09 20:46 
GeneralRe: Comments about spectrum display Pin
jacky_zz3-Feb-09 14:52
memberjacky_zz3-Feb-09 14:52 
when we get 512 value(those are all float number), then we can draw it to screen in a window with boundary.
here has a way:
fisrt, we should get spectrum bar count, then use 512/count to get step in a range of sound frequency.
second, use stepes in a loop, sum it, we will get a float number.
third, use this float number multiply the height of window what you want to draw, then you can get real height of this range of sound frequency.

best regards
GeneralRe: Comments about spectrum display Pin
hdw1913-Feb-09 17:22
memberhdw1913-Feb-09 17:22 
GeneralRe: Comments about spectrum display Pin
jacky_zz4-Feb-09 13:29
memberjacky_zz4-Feb-09 13:29 
GeneralRe: Comments about spectrum display Pin
lao_o20-Feb-09 14:51
memberlao_o20-Feb-09 14:51 
GeneralRe: Comments about spectrum display Pin
jacky_zz22-Feb-09 18:47
memberjacky_zz22-Feb-09 18:47 
GeneralRe: Comments about spectrum display Pin
lao_o15-Mar-09 19:25
memberlao_o15-Mar-09 19:25 
Generaladd equalizer Pin
Member 179275118-Dec-08 16:34
memberMember 179275118-Dec-08 16:34 
GeneralRe: add equalizer Pin
jacky_zz20-Dec-08 20:46
memberjacky_zz20-Dec-08 20:46 
GeneralWorks very well, thanks! Pin
Tage Lejon14-Dec-08 10:56
memberTage Lejon14-Dec-08 10:56 
GeneralRe: Works very well, thanks! Pin
jacky_zz14-Dec-08 13:24
memberjacky_zz14-Dec-08 13:24 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 24 Dec 2008
Article Copyright 2008 by jackyxinli
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid