Click here to Skip to main content
12,630,940 members (31,307 online)
Click here to Skip to main content
Add your own
alternative version


183 bookmarked

Simple Audio Out Oscilloscope and Spectrum Analyzer

, 27 Jul 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...


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
alphons9-Mar-09 3:36
memberalphons9-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
jacky_zz4-Sep-08 21:28
memberjacky_zz4-Sep-08 21:28 
Generalsoftware osiloscope link Pin
nejatkaraca29-Aug-08 23:32
membernejatkaraca29-Aug-08 23:32 
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 
Well its not hard to guess why, so I'll visit it for you:

1) Winamp does not sample from the audio card, the sample information is
store in the compressed audio stream, so there is no hardware API to the audio hardware.

2) They have probably used some optimised C/assembly routine to crank out the FFT for the audio data.

3) Its little video spectrum graphics are so tiny that video hardware can update this quickly.

Now looking at my example, its clear I've written it to show how to do it,
and how to do simply.

Most of the CPU/work is actually done on the graphic output, and again its done to keep things simple so you can understand it, which means using slow Windows GDI.

Look here for more better example of turning this simple example into something more demanding:[^]

And yes, it also does consume CPU. Again, mainly due to the graphics output and Windows GDI, of course your video hardware may vary.

Having said that the FFT and sampling routines are quite quick, even if they are not overly optimised.

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 
AnswerRe: SetInputDevice: hMixer Undefined Value. Please help. Pin
Steven De Toni12-Jun-07 11:47
memberSteven De Toni12-Jun-07 11:47 
QuestionRe: SetInputDevice: hMixer Undefined Value. Please help. [modified] Pin
jeffhcliu14-Jun-07 19:29
memberjeffhcliu14-Jun-07 19:29 
QuestionCould not download source file Pin
david_taipei7-Jun-07 22:56
memberdavid_taipei7-Jun-07 22:56 
QuestionHelp in Converting "Callback Scope". Thanks. Pin
jeffhcliu7-Jun-07 9:40
memberjeffhcliu7-Jun-07 9:40 
AnswerRe: Help in Converting "Callback Scope". Thanks. Pin
Steven De Toni7-Jun-07 14:37
memberSteven De Toni7-Jun-07 14:37 
GeneralRe: Help in Converting "Callback Scope". Thanks. Pin
jeffhcliu7-Jun-07 15:25
memberjeffhcliu7-Jun-07 15:25 
Generalsound card with 3 mixers Pin
kenadams11129-Mar-07 4:15
memberkenadams11129-Mar-07 4:15 
GeneralMore Advanced Version of Audio Scope Pin
Steven De Toni26-Mar-07 19:21
memberSteven De Toni26-Mar-07 19:21 
QuestionAny flowchart or psuedo code available for this project Pin
Vidyadhar_India26-Mar-07 17:58
memberVidyadhar_India26-Mar-07 17:58 
AnswerRe: Any flowchart or psuedo code available for this project Pin
Steven De Toni26-Mar-07 19:18
memberSteven De Toni26-Mar-07 19:18 
Questionhow to get amplitude and frequency Pin
lowkt2-Mar-07 4:34
memberlowkt2-Mar-07 4:34 
QuestionStrange behaviour and Equalizer Pin
GianniGP29-Jan-07 1:09
memberGianniGP29-Jan-07 1:09 
AnswerRe: Strange behaviour and Equalizer Pin
GianniGP31-Jan-07 4:45
memberGianniGP31-Jan-07 4:45 
QuestionRe: Strange behaviour and Equalizer Pin
jeffhcliu9-Jun-07 11:17
memberjeffhcliu9-Jun-07 11:17 
GeneralWant to convert this in C# Pin
Indian Ocean7-Jan-07 4:13
memberIndian Ocean7-Jan-07 4:13 
GeneralRe: Want to convert this in C# Pin
Steven De Toni10-Jan-07 17:01
memberSteven De Toni10-Jan-07 17:01 
GeneralRe: Want to convert this in C# Pin
Tauhid Shaikh23-Sep-07 20:58
memberTauhid Shaikh23-Sep-07 20:58 
GeneralRe: Want to convert this in C# Pin
osirisgothra7-Nov-07 3:37
memberosirisgothra7-Nov-07 3:37 
GeneralSmall bug for spectrum calculation Pin
mcanti2-Dec-06 2:19
membermcanti2-Dec-06 2:19 
QuestionCompile ERROR in VC 2003? [modified] Pin
descartes6-Nov-06 0:33
memberdescartes6-Nov-06 0:33 
AnswerRe: Compile ERROR in VC 2003? Pin
Estrayk11-Feb-07 8:13
memberEstrayk11-Feb-07 8:13 
GeneralRe: Compile ERROR in VC 2003? Pin
CarEnMDC11-Apr-07 19:03
memberCarEnMDC11-Apr-07 19:03 
QuestionJust a bit of help needed Pin
morfyh10-Oct-06 7:05
membermorfyh10-Oct-06 7:05 
AnswerRe: Just a bit of help needed Pin
Steven De Toni10-Oct-06 11:04
memberSteven De Toni10-Oct-06 11:04 
GeneralRe: Just a bit of help needed Pin
morfyh10-Oct-06 22:23
membermorfyh10-Oct-06 22:23 
QuestionModifications/improvement Pin
Irlande7818-Sep-06 4:17
memberIrlande7818-Sep-06 4:17 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 27 Jul 2006
Article Copyright 2006 by Steven De Toni
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid