Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / MFC
Article

Simple Audio Out Oscilloscope and Spectrum Analyzer

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

Sample Image

Introduction

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.

Background

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.

History

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

License

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


Written By
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionModifications/improvement Pin
Irlande7818-Sep-06 3:17
Irlande7818-Sep-06 3:17 
NewsRe: Modifications/improvement Pin
MacGadger13-Oct-06 17:16
MacGadger13-Oct-06 17:16 
QuestionRe: Modifications/improvement Pin
Irlande7813-Oct-06 23:27
Irlande7813-Oct-06 23:27 
AnswerRe: Modifications/improvement Pin
Steven De Toni13-Oct-06 23:57
Steven De Toni13-Oct-06 23:57 
GeneralRe: Modifications/improvement Pin
Steven De Toni13-Oct-06 23:41
Steven De Toni13-Oct-06 23:41 
QuestionRe: Modifications/improvement Pin
Irlande7814-Oct-06 0:09
Irlande7814-Oct-06 0:09 
QuestionHow to select another mixer ? Pin
MacGadger1-Sep-06 8:43
MacGadger1-Sep-06 8:43 
AnswerRe: How to select another mixer ? Pin
Steven De Toni2-Sep-06 23:19
Steven De Toni2-Sep-06 23:19 
QuestionHow to get Average Value not the Peak ? Pin
MacGadger10-Aug-06 23:25
MacGadger10-Aug-06 23:25 
AnswerRe: How to get Average Value not the Peak ? Pin
Steven De Toni10-Oct-06 12:02
Steven De Toni10-Oct-06 12:02 
QuestionHow can I get sound from speaker and mic? Pin
Guto Garcia28-Jul-06 2:52
Guto Garcia28-Jul-06 2:52 
AnswerRe: How can I get sound from speaker and mic? Pin
Steven De Toni28-Jul-06 23:09
Steven De Toni28-Jul-06 23:09 
GeneralGreat! Pin
Jörg Anslik27-Jul-06 11:38
Jörg Anslik27-Jul-06 11:38 
GeneralBug Fix For SampleIter Class [modified] Pin
Steven De Toni26-Jul-06 10:28
Steven De Toni26-Jul-06 10:28 
GeneralThanks for the contrib... Pin
doug435026-Jul-06 9:26
doug435026-Jul-06 9:26 
QuestionNeed Help ?? Pin
MacGadger25-Jul-06 9:03
MacGadger25-Jul-06 9:03 
AnswerRe: Need Help ?? [modified] Pin
Steven De Toni26-Jul-06 13:12
Steven De Toni26-Jul-06 13:12 
GeneralRe: Need Help ?? Pin
MacGadger27-Jul-06 7:28
MacGadger27-Jul-06 7:28 
GeneralVery nice, very useful :) Pin
Kochise22-Jul-06 11:15
Kochise22-Jul-06 11:15 
GeneralSin/Cos Speedup [modified] Pin
Steven De Toni20-Jul-06 10:52
Steven De Toni20-Jul-06 10:52 
Generalim soo thnnfullllllllllllllll.... Pin
MacGadger20-Jul-06 5:27
MacGadger20-Jul-06 5:27 
GeneralRe: im soo thnnfullllllllllllllll.... Pin
Colin Angus Mackay20-Jul-06 5:46
Colin Angus Mackay20-Jul-06 5:46 

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.