Click here to Skip to main content
15,166,358 members
Articles / Desktop Programming / MFC
Posted 19 Jul 2006


190 bookmarked

Simple Audio Out Oscilloscope and Spectrum Analyzer

Rate me:
Please Sign up or sign in to vote.
4.71/5 (37 votes)
27 Jul 2006CPOL2 min read
An example of how to access the Audio Mix, sample audio output, and a simple DSP processing.

Sample Image


This is a simple application I built to understand the basics of the Windows Audio WAVE API, and to get a basic grasp on Fast Fourier Transforms (FFTs). Features of the application are: double buffering graphics, sampling an audio source, selecting an audio recording source using the mixer, calculating the FFT spectrum array, and plotting sampled data.


I won't discus the details of FFT here as the math is a little beyond me. Basically, it involves complex numbers (those number like sqrt(-1)) and calculating discrete Sin/Cos transformations on sampled date. Google for many and better explanations on FFT.

Using the code

If you look through the example, you will find it quite a simple affair that has not been touched by MFC. This means that the code is easy to follow and easy to transplant into your projects. The basic flow of the program is:

  • Load a dialog window and initialize it.
  • Select the recording device (which is harder than it should be!).
  • Initialize the WAVE structures.
  • Activate the sampling.
  • Determine the status of the sampling.
  • Process the sampled data using FFT.
  • Clean up.

In the FFT process, I load the sampled data into an iterator class for processing within the FFT class. This allow for selective processing of the sampled data. In this case, I sampled the recording source in stereo, and I can selectively process the left or the right channels within the FFT class, or both channels at once. Both classes are simple to follow and to implement.

Points of interest

The audio device API within Windows is far more complex than it should be. This is pretty common when dealing with Microsoft, they never seem to make things easy! A classic example is dealing with the Audio Mixer. It took some time to find some code that can do the most basic of tasks (select a recording device), and even that is not 100% perfect. Device drivers may incorrectly label a recording device as a WAVEOUT device and the program will return an error when trying to record from it, so look out for such errors (use the VC6 debugger to determine the fault in such occurrences).

In fact, I would not recommend using the WAVE API for anything too complex as it tends to be a bit tricky to use, instead look at the Simple Directmedia Layer library, and specifically the mixer lib, which is a good example of making things simple and useful to the programmer.

The FFT implementation probably isn't the fastest around. I originally converted the source from a Java class, and then optimized the Cos/Sin processing to implement a look up table to speed things up significantly, and added a few helper methods; otherwise it has remained unchanged.


  • Updated the spectrum graphics to be more equalised.
  • Fixed a number of small bugs.


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


About the Author

Steven De Toni
New Zealand New Zealand
No Biography provided

Comments and Discussions

QuestionSound output? Pin
stingray276-Feb-14 6:13
Memberstingray276-Feb-14 6:13 
QuestionNot working on Windows 7 Pin
Stéphane Lenclud8-Jan-14 8:17
MemberStéphane Lenclud8-Jan-14 8:17 
QuestionThank you Steven De Toni :) Pin
chaiein8-May-12 22:30
Memberchaiein8-May-12 22:30 
QuestionConvert for c#?? Pin
uelfox9-Nov-09 11:48
Memberuelfox9-Nov-09 11:48 
Questioncan u help me about writing such a program? Pin
sahand1984wolf28-Jun-09 12:01
Membersahand1984wolf28-Jun-09 12:01 
GeneralAmbiuous call to overloaded function Pin
Member 470880123-Feb-09 14:44
MemberMember 470880123-Feb-09 14:44 
GeneralRe: Ambiuous call to overloaded function Pin
Member 470880123-Feb-09 14:46
MemberMember 470880123-Feb-09 14:46 
GeneralRe: Ambiuous call to overloaded function Pin
Alphons van der Heijden9-Mar-09 3:36
professionalAlphons van der Heijden9-Mar-09 3:36 
GeneralRe: Ambiuous call to overloaded function Pin
jsaneill3-Mar-10 4:14
Memberjsaneill3-Mar-10 4:14 
GeneralThanks to Steven De Toni Pin
wanglung2-Dec-08 15:57
Memberwanglung2-Dec-08 15:57 
Questiona few question about play wave file with DirectSound and display spectrum. Pin
jackyxinli4-Sep-08 21:28
Memberjackyxinli4-Sep-08 21:28 
Generalsoftware osiloscope link Pin
nejatkaraca29-Aug-08 23:32
Membernejatkaraca29-Aug-08 23:32 
Generalwhen i set sound player volumn to zero, the spectrum disappeared. Pin
jackyxinli12-Aug-08 0:47
Memberjackyxinli12-Aug-08 0:47 
QuestionSource? Pin
Member 254060511-Dec-07 11:54
MemberMember 254060511-Dec-07 11:54 
Questioncan u guide me i am new for mixer control Pin
rajneshmalik30-Aug-07 3:25
Memberrajneshmalik30-Aug-07 3:25 
AnswerRe: can u guide me i am new for mixer control Pin
zhanghwem22-May-08 19:35
Memberzhanghwem22-May-08 19:35 
QuestionError unable to set recording WAVEOUT device Pin
hanji_moti29-Aug-07 8:45
Memberhanji_moti29-Aug-07 8:45 
AnswerRe: Error unable to set recording WAVEOUT device Pin
Steven De Toni29-Aug-07 11:48
MemberSteven De Toni29-Aug-07 11:48 
AnswerRe: Error unable to set recording WAVEOUT device Pin
zhanghwem22-May-08 19:16
Memberzhanghwem22-May-08 19:16 
GeneralRe: Error unable to set recording WAVEOUT device Pin
AEN23-Dec-08 8:17
MemberAEN23-Dec-08 8:17 
Generaluse many cpu percenge Pin
shallopquan13-Aug-07 22:08
Membershallopquan13-Aug-07 22:08 
GeneralRe: use many cpu percenge Pin
Steven De Toni15-Aug-07 19:40
MemberSteven De Toni15-Aug-07 19:40 
QuestionSetInputDevice on Vista Pin
HouseSparrow7-Aug-07 0:06
MemberHouseSparrow7-Aug-07 0:06 
QuestionUsing You Code Pin
eng_bite28-Jun-07 8:34
Membereng_bite28-Jun-07 8:34 
QuestionSetInputDevice: hMixer Undefined Value. Please help. Pin
jeffhcliu11-Jun-07 20:56
Memberjeffhcliu11-Jun-07 20:56 

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.