Click here to Skip to main content
15,889,266 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 387K   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

 
QuestionUsing You Code Pin
eng_bite28-Jun-07 7:34
eng_bite28-Jun-07 7:34 
QuestionSetInputDevice: hMixer Undefined Value. Please help. Pin
jeffhcliu11-Jun-07 19:56
jeffhcliu11-Jun-07 19:56 
AnswerRe: SetInputDevice: hMixer Undefined Value. Please help. Pin
Steven De Toni12-Jun-07 10:47
Steven De Toni12-Jun-07 10:47 
QuestionRe: SetInputDevice: hMixer Undefined Value. Please help. [modified] Pin
jeffhcliu14-Jun-07 18:29
jeffhcliu14-Jun-07 18:29 
QuestionCould not download source file Pin
david_taipei7-Jun-07 21:56
david_taipei7-Jun-07 21:56 
QuestionHelp in Converting "Callback Scope". Thanks. Pin
jeffhcliu7-Jun-07 8:40
jeffhcliu7-Jun-07 8:40 
AnswerRe: Help in Converting "Callback Scope". Thanks. Pin
Steven De Toni7-Jun-07 13:37
Steven De Toni7-Jun-07 13:37 
GeneralRe: Help in Converting "Callback Scope". Thanks. Pin
jeffhcliu7-Jun-07 14:25
jeffhcliu7-Jun-07 14:25 
Hi Steven.

Thank you so much for your quick reply. Much appreciated. I guess I do superficially understand what the code does and have studied it several times but the problem (I think) I am having is that I can't see a way of utilizing it in my program (due to my lack of knowledge on programming).

I have my own program which does a bunch of things within an infinite loop. I would like to add the capabilities of your program to mine, i.e. being able to sample from a microphone and doing FFT analysis on it. In other words, as soon as I clicked on a button, I would want start the sound sampling (along with all the other stuff within that infinite loop) and perform FFT, etc. Then when I click on another button, I will break that loop as well as stopping the sampling.

Now, as your program creates a dialog box and is based on Windows Messages, I am not really sure how to translate that with what I know. In the high likelihood of sounding extremely stupid, I have changed the WM_COMMAND case into StartRecord and StopRecord "void" subroutines which will be called when certain buttons (on a Windows Form) are clicked. Now, I am searching for a way of translating the MM_WIM_DATA part into a subroutine (ProcessAudio), or a function callback as you have kindly suggested.

But I really don't know how to do this. I think I am facing two main problems at this point (based on my approach). If I don't use Windows Messages, what am I going to pass as the lParam value to ProcessAudio (as the MM_WIM_DATA case uses it heavily)? The other problem is that I do not want ProcessAudio to be processing all the time (i.e. "in an infinite loop of its own", in my words) as there will be other stuff that will need to be processed within my own main infinite loop, of which ProcessAudio will be only a part of. I have read up on multi-threading as well as callbacks but I still don't know how to proceed.

I hope you understand what I have just said and what I am trying to do and please forgive me if I am doing something very stupid as I really don't have that much programming experience.

A million thanks.

Regards,
Jeff
Generalsound card with 3 mixers Pin
kenadams11129-Mar-07 3:15
kenadams11129-Mar-07 3:15 
GeneralMore Advanced Version of Audio Scope Pin
Steven De Toni26-Mar-07 18:21
Steven De Toni26-Mar-07 18:21 
QuestionAny flowchart or psuedo code available for this project Pin
Vidyadhar_India26-Mar-07 16:58
Vidyadhar_India26-Mar-07 16:58 
AnswerRe: Any flowchart or psuedo code available for this project Pin
Steven De Toni26-Mar-07 18:18
Steven De Toni26-Mar-07 18:18 
Questionhow to get amplitude and frequency Pin
lowkt2-Mar-07 3:34
lowkt2-Mar-07 3:34 
QuestionStrange behaviour and Equalizer Pin
GianniGP29-Jan-07 0:09
GianniGP29-Jan-07 0:09 
AnswerRe: Strange behaviour and Equalizer Pin
GianniGP31-Jan-07 3:45
GianniGP31-Jan-07 3:45 
QuestionRe: Strange behaviour and Equalizer Pin
jeffhcliu9-Jun-07 10:17
jeffhcliu9-Jun-07 10:17 
GeneralWant to convert this in C# Pin
Indian Ocean7-Jan-07 3:13
Indian Ocean7-Jan-07 3:13 
GeneralRe: Want to convert this in C# Pin
Steven De Toni10-Jan-07 16:01
Steven De Toni10-Jan-07 16:01 
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 

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.