Click here to Skip to main content
11,409,097 members (64,329 online)
Click here to Skip to main content

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. Smile | :)


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?

Comments and Discussions

QuestionThank you Pinmemberkoujunyu30-Oct-12 6:08 
AnswerRe: Thank you Pinmemberjacky_zz27-Jan-13 21:45 
AnswerRe: Thank you Pinmembertianlunzhizi16-Jul-13 16:46 
GeneralMy vote of 5 Pinmemberbh2007720-Nov-11 14:41 
QuestionHow to calculate VU and peak values Pinmember29044790722-Aug-11 19:06 
Questionaudio bars for rtsp streaming [modified] Pinmemberarunnnnnkumar19-Aug-11 23:47 
Questioncontrol volume Pinmemberkinani10-Aug-11 5:48 
AnswerRe: control volume Pinmemberjacky_zz10-Aug-11 15:24 
Questionplaying WAV Pinmemberkinani10-Aug-11 2:08 
AnswerRe: playing WAV Pinmemberjacky_zz10-Aug-11 15:27 
QuestionDo you have a .NET version? PinmemberTinhiuvanoinho23-Jul-11 19:08 
AnswerRe: Do you have a .NET version? Pinmemberjacky_zz24-Jul-11 15:55 
GeneralPause and Resume...makes some noise Pinmemberyoundie4-Aug-09 0:31 
GeneralRe: Pause and Resume...makes some noise Pinmemberjacky_zz4-Aug-09 22:32 
GeneralRe: Pause and Resume...makes some noise Pinmemberyoundie10-Aug-09 17:04 
GeneralRe: Pause and Resume...makes some noise Pinmemberjacky_zz11-Aug-09 18:10 
News0.2009.5.6065 released Pinmemberjacky_zz5-May-09 17:19 
Generalnew version released. Pinmemberjacky_zz9-Feb-09 23:20 
GeneralRe: new version released. Pinmemberchube22106-Apr-09 21:51 
GeneralRe: new version released. Pinmemberjacky_zz8-Apr-09 22:23 
GeneralRe: new version released. Pinmemberfeels7-Apr-09 23:07 
GeneralRe: new version released. Pinmemberjacky_zz8-Apr-09 22:24 
GeneralRe: new version released. Pinmemberfeels9-Apr-09 23:28 
GeneralRe: new version released. Pinmemberjacky_zz9-Apr-09 23:35 
GeneralRe: new version released. Pinmemberaraknoid18-Jan-11 2:36 
GeneralRe: new version released. Pinmemberjacky_zz20-Jan-11 14:46 
GeneralComments about spectrum display Pinmemberlao_o8-Jan-09 21:43 
Try the code from "", it is a fixed-point FFT, and much faster than float-point FFT. The supported max length of FFT is 1024.
To get the correct spectrum result, do as the following
// Assume there are N samples input, FR is an array for real components, and FI is imagminary.
window(FR, N); // Reduce frequency leakage by hanning window
fix_fft(FR, FI, N, 0); // The result will be stored in FR & FI
loud(FL, FR, FI, N/2, 0); // Convert loudness of each components, and save the result in array FL.
For the FFT result, Only [0, N/2) is valid for the positive frequency, [N/2, N) is for negative frequency, it should be ignored.
Then, you could display spectrum according to the data in FL. 0 means the max loudness(0 db), -100 is the min loudness(-100 db). In fact, if the sound is less than -60db, it is hardly heard, so, use a threshold to assign FL[i] as 0 if it is less than -60.
I added the code in my embedded solution, it works very well just as foobar2000_v0.9.5.2.
GeneralRe: Comments about spectrum display Pinmemberjacky_zz11-Jan-09 17:41 
GeneralRe: Comments about spectrum display Pinmemberhdw1912-Feb-09 21:46 
GeneralRe: Comments about spectrum display Pinmemberjacky_zz3-Feb-09 15:52 
GeneralRe: Comments about spectrum display Pinmemberhdw1913-Feb-09 18:22 
GeneralRe: Comments about spectrum display Pinmemberjacky_zz4-Feb-09 14:29 
GeneralRe: Comments about spectrum display Pinmemberlao_o20-Feb-09 15:51 
GeneralRe: Comments about spectrum display Pinmemberjacky_zz22-Feb-09 19:47 
GeneralRe: Comments about spectrum display Pinmemberlao_o15-Mar-09 20:25 
Generaladd equalizer PinmemberMember 179275118-Dec-08 17:34 
GeneralRe: add equalizer Pinmemberjacky_zz20-Dec-08 21:46 
GeneralWorks very well, thanks! PinmemberTage Lejon14-Dec-08 11:56 
GeneralRe: Works very well, thanks! Pinmemberjacky_zz14-Dec-08 14:24 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 24 Dec 2008
Article Copyright 2008 by jacky_zz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid