5,699,997 members and growing! (24,398 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Multimedia     Beginner License: The Code Project Open License (CPOL)

Multimedia PeakMeter control

By Ernest Laurentin

Multimedia PeakMeter control - .NET version
C++ (VC8.0, C++, VC9.0), C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows, .NET CF, .NET, Win32, MFC, GDI+, Dev, Design

Posted: 25 May 2008
Updated: 4 Sep 2008
Views: 20,049
Bookmarked: 81 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
26 votes for this Article.
Popularity: 6.20 Rating: 4.38 out of 5
2 votes, 7.7%
1
2 votes, 7.7%
2
1 vote, 3.8%
3
2 votes, 7.7%
4
19 votes, 73.1%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
PeakMeter Application

soundsmfc.jpg

Introduction

A while back, I released the .NET version of the Peak Meter control but one thing I quickly discovered is that many developers would not know how to use it in their own application. This is the reason for this update. I will present the various components that are involved in order to show audio meter in real-time.

Description

Real-time Peak Meter processing is composed of three blocks.
Mic processing

  • The source: source of audio data, this can be a microphone, a digital sound file (CD audio) or even a video if you consider grabbing the audio part of it. Whatever your source the principle is the same, you will have to convert the data to a format that is suitable for Signal Processing.
  • DSP Processing: what I'm calling DSP processing block here is the component that is responsible to convert the audio data to peak amplitude format.
  • Peak Meter rendering: this is basically the rendering control (just like the Peak Meter control of this article). The rendering engine doesn't have to be all fancy like this control. A progress control can be used to show Peak audio. In fact if you look around Windows control panel, this is exactly what is being used.

Audio Source

The microphone is the most basic audio source that you can think of.
Basically the previous figure illustrates how your computer receives digital samples of the sound that is produced from the microphone. The way the whole process works is that you specify the format that you wish to receive the data, mostly PCM data (Pulse Code Modulation) and the audio capture device will collect the samples based on sampling frequency. PCM data and WAVE chunks are common terms to describe the data that is obtained from the ADC (Analog to Digital Converter).
For example to digitize audio samples from a microphone at 22050 Hz single channel audio (mono) and 16 bits resolution; and let's say you want to receive samples every 100 ms, you will have to provide a buffer of 4410 bytes (22050*(16/8)*0.1).

I mentioned previously about file source. A file source is simply an audio data storage that gives you quick access to your samples. Audio formats like: MP3, WMA and Ogg Vorbis; all they do is compressing the data while trying to maintain the audio quality.

DSP Processing

DSP processing is very interesting subject to learn and work with. This block receives digital samples from the source. It approximates the original waveform and finds its peak magnitudes.
Since I would not be able to go in details about how FFT (Fast Fourier Transform) works in this article, I recommend the interested reader to visit some of the links in the reference section to increase his/her knowledge about this process.

FFT plays an important role in signal processing and is probably one of the most written subjects in software engineering. When dealing with digital system, the ADC (Analog to Digital Converter) gives us a set of digital audio samples (discrete signal). The theory behind it tells us that when we perform a DFT (Discrete Fourier Transform) on a discrete signal, we find its composant frequencies including their phase and amplitude. I would like to direct the advanced reader that I'm not talking only about pure sine wave where only the fundamental is visible but performing a DFT (or FFT) on audio signal generally produce some amplitudes on nearby bin frequencies.

Now all we have left to do is finding the range of frequencies for our analysis. According to the sampling theorem we can approximate the maximum audio frequency in our signal. The Nyquist theorem states that the baseband Fs > 2B, which means the sampling frequency (digital) must be at least twice than any frequency in the range of B frequencies (analog) in order to reconstruct the original signal and prevent aliasing. Aliasing is the effect that causes different signal to be indistinguishable (or aliases of one another) when sampled. Thus, if we sample at 44.1KHz (audio CD quality), we could capture the entire range of sound (roughly 20KHz).

Note that the sampling frequency needs to be at least twice our max frequency, using 44.1KHz to digitize a 20KHz sine waves improves our spectrum analysis by getting a few more samples than necessary.

Ideal FFT
Ideal FFT

Peak Meter Rendering

Peak Meter is just a "piece of cake" since the DSP block takes all the burden of what has to be done. What we do next, we select a group of frequencies and display the amplitude of their closest bin frequencies. Peak Meter control can be as simple as using a progress control. But I guess we like to be fancy from time to time! The PeakMeter control presented here does just that.
It is best to choose frequencies in the range below because they are typically found in normal conversation and music.
Audio Frequency map

Using PeakMeter in .NET

You can use .NET version of this control in WinForms applications. Just add it to your toolbox and drag and drop it to your WinForm canvas. Here's a summary of the properties for this control

PeakMeterControl Properties


Property Description
MeterStyle Show Meter bands in Horizontal (PMS_Horizontal) or Vertical (PMS_Vertical)
ShowGrid Show meter background grid
ColoredGrid Show meter backgroung grid using color schemes
GridColor Background grid color (when ColoredGrid property is false)
ColorNormal Low range color
ColorNormalBack Low range color Background (ColoredGrid=1)
ColorMedium Medium range color
ColorMediumBack Medium range color Background (ColoredGrid=1)
ColorHigh High range color
ColorHighBack High range color Background (ColoredGrid=1)
BandsCount Number of bands
LEDCount Number of LED per bands (when 1, band is smooth)
FalloffSpeed Falloff effect speed (1=fast, 10=normal, 100=slow)
FalloffEffect Enable falloff effect, call Start() to run
FalloffColor Falloff line color


PeakMeterControl Methods


Name Description
bool Start(int delay) Start animation (delay in ms). Can be called from non-UI thread.
bool Stop() Stop animation. Can be called from non-UI thread.
void SetMeterBands(int bands, int led) Set meter bands properties and number of LED in one call
void SetRange(int min, int med, int max) Change meter control default range
bool SetData(int[] values, int offset, int size) Set peak meter data. Can be called from non-UI thread.

Using the PeakMeter

Using this control is very straightforward, add it to your toolbox and drop it to your form. You can use Start method to animate the falloff effect. The demo fills the data with random numbers.

     private void FillMeterData()
     {
         int[] meters1 = new int[NumLEDS];
         Random rand = new Random();
         for (int i = 0; i < meters1.Length; i++)
         {
             meters1[i] = rand.Next(0, 100);
         }
         // fill meter data
         this.peakMeterCtrl1.SetData(meters1, 0, meters1.Length);
     }

SoundStudio Demo

SoundStudio Application is a simple sound player application capable of playing various audio files (.wav, .mp3. and .wma). It uses the WindowsMedia .NET library to parse the audio.
SoundStudioCS (C# version) can be built using the unmanaged DLL (FFTLib.dll). Simply add USE_FFTLIB to your project build settings (Build->General tab).

Some of the features of WindowsMedia .NET (Release)

  • Microsoft WAVE device support (waveInOpen, waveOutOpen)
  • DirectShow Filters, including sample grabber filter
  • Microsoft Multimedia Streaming Interfaces

References

Discrete Fourier Tranform
Discrete Fourier Transform (Math)
FFT
Nyquist Theorem

History

09/04/2008: Updated all samples (+WindowsMedia release)
08/13/2008: SoundStudio Release (include WindowsMedia pre-release)
05/24/2008: First revision for .NET

License

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

About the Author

Ernest Laurentin


Ernest is a multi-discipline software engineer.
Skilled at software design and development for all Windows platforms (starting Windows 3.11),
Windows CE and cross-platform UI development.
-
MCSD (C#, .NET)
Interests: User Interface, GDI/GDI+, Scripting, Windows CE .NET.
Programming Skills: C/C++, C#, Java, VB and ASP.NET.

I hope you will enjoy my contributions.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Audio and Video articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralwaveOutXXX & DirectSoundmemberjacky_zz17:39 9 Nov '08  
QuestionHow to convert microphone signals (wave) to a stream of musical notes?memberMember 44745217:42 31 Oct '08  
Questionis it possible?memberjacky_zz21:36 11 Sep '08  
GeneralOpen source managed DSPmembergnjunge1:12 5 Sep '08  
Generala few question about play wave file with DirectSound and display spectrum.memberjacky_zz20:01 3 Sep '08  
AnswerRe: a few question about play wave file with DirectSound and display spectrum.memberErnest Laurentin16:50 4 Sep '08  
GeneralCan you tell me how to get the audio feed from the sound card/audio out instead of from an audio file?memberDeepak Paul John20:05 28 Aug '08  
GeneralRe: Can you tell me how to get the audio feed from the sound card/audio out instead of from an audio file?memberErnest Laurentin4:35 29 Aug '08  
GeneralRe: Can you tell me how to get the audio feed from the sound card/audio out instead of from an audio file?memberDaveyM695:46 29 Aug '08  
GeneralExcellentmemberTe-Deum13:26 27 Aug '08  
GeneralRe: ExcellentmemberErnest Laurentin16:58 27 Aug '08  
GeneralRe: ExcellentmemberTe-Deum23:08 27 Aug '08  
GeneralRe: ExcellentmemberErnest Laurentin4:27 28 Aug '08  
GeneralRe: ExcellentmemberTe-Deum8:35 28 Aug '08  
Generali upload kj dsp source code, what difference process PCM data between your work and it?memberjacky_zz0:08 27 Aug '08  
Generalcan you introduce me an open source sound library like bass?memberjacky_zz17:35 26 Aug '08  
GeneralRe: can you introduce me an open source sound library like bass?memberErnest Laurentin18:14 26 Aug '08  
GeneralRe: can you introduce me an open source sound library like bass?memberjacky_zz18:26 26 Aug '08  
Questioni got two questiones about function CSoundStudioDlg::GetAudioDatamemberjacky_zz22:41 25 Aug '08  
AnswerRe: i got two questiones about function CSoundStudioDlg::GetAudioDatamemberErnest Laurentin4:30 26 Aug '08  
GeneralA questionmemberjacky_zz15:28 14 Aug '08  
AnswerRe: A questionmemberErnest Laurentin16:03 14 Aug '08  
GeneralRe: A questionmemberjacky_zz16:45 14 Aug '08  
GeneralThis control in Silverlight?memberjones_m2:20 1 Aug '08  
GeneralRe: This control in Silverlight?memberErnest Laurentin14:40 1 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Sep 2008
Editor:
Copyright 2008 by Ernest Laurentin
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project