11,716,285 members (86,733 online)

# Simple Audio Out Oscilloscope and Spectrum Analyzer

, 26 Jul 2006 CPOL 270.7K 22.4K 181
 Rate this:
An example of how to access the Audio Mix, sample audio output, and a simple DSP processing.

## 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.

## Share

 New Zealand
No Biography provided

## You may also be interested in...

 First PrevNext
 Sound output? stingray276-Feb-14 5:13 stingray27 6-Feb-14 5:13
 Not working on Windows 7 Stéphane Lenclud8-Jan-14 7:17 Stéphane Lenclud 8-Jan-14 7:17
 Thank you Steven De Toni :) chaiein8-May-12 21:30 chaiein 8-May-12 21:30
 Convert for c#?? uelfox9-Nov-09 10:48 uelfox 9-Nov-09 10:48
 can u help me about writing such a program? sahand1984wolf28-Jun-09 11:01 sahand1984wolf 28-Jun-09 11:01
 Ambiuous call to overloaded function Member 470880123-Feb-09 13:44 Member 4708801 23-Feb-09 13:44
 Re: Ambiuous call to overloaded function Member 470880123-Feb-09 13:46 Member 4708801 23-Feb-09 13:46
 Re: Ambiuous call to overloaded function alphons9-Mar-09 2:36 alphons 9-Mar-09 2:36
 Re: Ambiuous call to overloaded function jsaneill3-Mar-10 3:14 jsaneill 3-Mar-10 3:14
 Thanks to Steven De Toni wanglung2-Dec-08 14:57 wanglung 2-Dec-08 14:57
 a few question about play wave file with DirectSound and display spectrum. jacky_zz4-Sep-08 20:28 jacky_zz 4-Sep-08 20:28
 software osiloscope link nejatkaraca29-Aug-08 22:32 nejatkaraca 29-Aug-08 22:32
 Source? Member 254060511-Dec-07 10:54 Member 2540605 11-Dec-07 10:54
 can u guide me i am new for mixer control rajneshmalik30-Aug-07 2:25 rajneshmalik 30-Aug-07 2:25
 Re: can u guide me i am new for mixer control zhanghwem22-May-08 18:35 zhanghwem 22-May-08 18:35
 Error unable to set recording WAVEOUT device hanji_moti29-Aug-07 7:45 hanji_moti 29-Aug-07 7:45
 Re: Error unable to set recording WAVEOUT device Steven De Toni29-Aug-07 10:48 Steven De Toni 29-Aug-07 10:48
 Re: Error unable to set recording WAVEOUT device zhanghwem22-May-08 18:16 zhanghwem 22-May-08 18:16
 Re: Error unable to set recording WAVEOUT device AEN23-Dec-08 7:17 AEN 23-Dec-08 7:17
 23Dec2008 Greetings. I have two questions. 1) I really have no idea how to recompile the source code into an exe after editing out the "Break" line. Might someone have a moment to tell me how to do that? 2) There is now a version 4 release of this audio scope. (See the posting below about new version at SourceForge.) Might someone know how to resolve this same error message with the version 4 release? The v4 release seemss quite good. I have three notebooks. One is 10 years old, and I am trying to make it as multimedia capable as possible. This 10 year old notebook gives me the "Error unable to set recording WAVEOUT device" error message. Any replies appreciated. Thank you. Regards, AEN Æ
 use many cpu percenge shallopquan13-Aug-07 21:08 shallopquan 13-Aug-07 21:08
 Re: use many cpu percenge Steven De Toni15-Aug-07 18:40 Steven De Toni 15-Aug-07 18:40
 SetInputDevice on Vista HouseSparrow6-Aug-07 23:06 HouseSparrow 6-Aug-07 23:06
 Using You Code eng_bite28-Jun-07 7:34 eng_bite 28-Jun-07 7:34
 Help in Converting "Callback Scope". Thanks. jeffhcliu7-Jun-07 8:40 jeffhcliu 7-Jun-07 8:40
 Re: Help in Converting "Callback Scope". Thanks. Steven De Toni7-Jun-07 13:37 Steven De Toni 7-Jun-07 13:37
 Re: Help in Converting "Callback Scope". Thanks. jeffhcliu7-Jun-07 14:25 jeffhcliu 7-Jun-07 14:25
 More Advanced Version of Audio Scope Steven De Toni26-Mar-07 18:21 Steven De Toni 26-Mar-07 18:21
 Any flowchart or psuedo code available for this project Vidyadhar_India26-Mar-07 16:58 Vidyadhar_India 26-Mar-07 16:58
 Re: Any flowchart or psuedo code available for this project Steven De Toni26-Mar-07 18:18 Steven De Toni 26-Mar-07 18:18
 how to get amplitude and frequency lowkt2-Mar-07 3:34 lowkt 2-Mar-07 3:34
 Strange behaviour and Equalizer GianniGP29-Jan-07 0:09 GianniGP 29-Jan-07 0:09
 Re: Strange behaviour and Equalizer GianniGP31-Jan-07 3:45 GianniGP 31-Jan-07 3:45
 Re: Strange behaviour and Equalizer jeffhcliu9-Jun-07 10:17 jeffhcliu 9-Jun-07 10:17
 Want to convert this in C# Indian Ocean7-Jan-07 3:13 Indian Ocean 7-Jan-07 3:13
 Re: Want to convert this in C# Steven De Toni10-Jan-07 16:01 Steven De Toni 10-Jan-07 16:01
 Re: Want to convert this in C# Tauhid Shaikh23-Sep-07 19:58 Tauhid Shaikh 23-Sep-07 19:58
 Re: Want to convert this in C# osirisgothra7-Nov-07 2:37 osirisgothra 7-Nov-07 2:37
 Small bug for spectrum calculation mcanti2-Dec-06 1:19 mcanti 2-Dec-06 1:19
 Compile ERROR in VC 2003? [modified] descartes5-Nov-06 23:33 descartes 5-Nov-06 23:33
 Re: Compile ERROR in VC 2003? Estrayk11-Feb-07 7:13 Estrayk 11-Feb-07 7:13
 Re: Compile ERROR in VC 2003? CarEnMDC11-Apr-07 18:03 CarEnMDC 11-Apr-07 18:03
 Just a bit of help needed morfyh10-Oct-06 6:05 morfyh 10-Oct-06 6:05
 Re: Just a bit of help needed Steven De Toni10-Oct-06 10:04 Steven De Toni 10-Oct-06 10:04
 Re: Just a bit of help needed morfyh10-Oct-06 21:23 morfyh 10-Oct-06 21:23
 Modifications/improvement Irlande7818-Sep-06 3:17 Irlande78 18-Sep-06 3:17
 Last Visit: 31-Dec-99 18:00     Last Update: 2-Sep-15 20:53 Refresh 12 Next »