Click here to Skip to main content
15,887,477 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 386.8K   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

 
GeneralRe: Want to convert this in C# Pin
Tauhid Shaikh23-Sep-07 19:58
Tauhid Shaikh23-Sep-07 19:58 
GeneralRe: Want to convert this in C# Pin
osirisgothra7-Nov-07 2:37
osirisgothra7-Nov-07 2:37 
GeneralSmall bug for spectrum calculation Pin
mcanti2-Dec-06 1:19
mcanti2-Dec-06 1:19 
QuestionCompile ERROR in VC 2003? [modified] Pin
descartes5-Nov-06 23:33
descartes5-Nov-06 23:33 
AnswerRe: Compile ERROR in VC 2003? Pin
Estrayk11-Feb-07 7:13
Estrayk11-Feb-07 7:13 
GeneralRe: Compile ERROR in VC 2003? Pin
CarEnMDC11-Apr-07 18:03
CarEnMDC11-Apr-07 18:03 
QuestionJust a bit of help needed Pin
morfyh10-Oct-06 6:05
morfyh10-Oct-06 6:05 
AnswerRe: Just a bit of help needed Pin
Steven De Toni10-Oct-06 10:04
Steven De Toni10-Oct-06 10:04 
The line drawing method uses a technique called double buffering to allow for flicker free updates. The program creates a memory device context (which is microsoft speak for a graphics device handle), and creates memory bitmap area (microsoft speak for drawing area). You then need to associate the memory bitmap to the memory device context. Once this is done, then the line drawing algortihm comes in.

Drawing starts from left to right, with a origin of (bitmapHeight/2) and increments in the x direction based upon the sample number, and y direction based upn sample resolution.

So if your graphic output is 320 pixels wide, and you sample is 1024, we divide that by 2 for stereo and then by 2 again for 16 bit values, you end up with 128 data items per channel for the original 1024 byte sample.

so bitmapWidth / sampleChannel number = increment value
or
320 / 128 = 2.5

Each plot of a sample if incremented 2.5 in the xDirection, the yDirection is calculated the same way.

If the height of the bitmap is 200 pixels, and the sample data is signed to -32766 to +32767 we need to scall the data to fit the bitmap.

for bitmapHeight / sampleResultion = reductionFactor or increaseFactor
or
200 / (0xffff / 2) = 0.0061036087586785687

so given a yPlot Start Position of 100 <bitmapheight 2=""> and a data item of -12000
its calculated the plot position as 100 + (-12000 * 0.0061036087586785687) = 26

and if the next sample value is 12450 (as audio data goes from positive to negative to positive etc ) its calculated as

100 + (12450 * 0.0061036087586785687) = 175

and after each plot we increment in the x direction x = x + 2.5; (given that x is a float value). Thus you can plot your data to any bitmap size.

so some pseudo code would be:

short data[128]; // if given data in an array or some other object

float xPos = 0;
float xInc = 320 / 128; // = 2.5;
float yStart = 200 / 2;
float yReduce = 200 / (0xffff / 2); // = 0.0061036087586785687

bool starting = true;

for (idx = 0; idx < 128; idx++)
{
float yPos = yStart + (data[idx] * yReduce);

if (starting != false)
{
moveTo (xPos, yPos);
starting = false;
}
else
{
lineTo (xPos, yPos);
}
xPos = xPos + xInc;
}

// Once drawing is completed, then block imagae transfer the memory bitmap into video memory
BitBlt (screenDC, 0, 0, 320, 200, myDc, 0, 0, SRCCOPY);


How the above helps ?
GeneralRe: Just a bit of help needed Pin
morfyh10-Oct-06 21:23
morfyh10-Oct-06 21:23 
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 

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.