Click here to Skip to main content
11,634,796 members (67,797 online)
Click here to Skip to main content

Multimedia PeakMeter Control

, 4 Sep 2008 CPOL 172.2K 15.1K 206
Rate this:
Please Sign up or sign in to vote.
Multimedia PeakMeter control - .NET version
PeakMeter Application

soundsmfc.jpg

soundsmfc.jpg

Introduction

A while ago, 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 a 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 produces some amplitudes on nearby bin frequencies.

Now all we have left to do is find 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 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 a 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 is that 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 the .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 background 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 the 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) are:

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

References

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)

Share

About the Author

Ernest Laurentin
Software Developer (Senior)
United States United States
Ernest is a multi-discipline software engineer.
Skilled at software design and development for all Windows platforms.
-
MCSD (C#, .NET)
Interests: User Interface, GDI/GDI+, Scripting, Android, iOS, Windows Mobile.
Programming Skills: C/C++, C#, Java (Android), VB and ASP.NET.

I hope you will enjoy my contributions.

You may also be interested in...

Comments and Discussions

 
Questionerror message on 64bit Windows7: An invalid parameter was passed to a system function. Pin
Hamish Ahern15-Jan-14 0:27
memberHamish Ahern15-Jan-14 0:27 
QuestionTrying to add the control the toolbox VS2012. help! Pin
Shannen Londres2-Oct-13 16:18
memberShannen Londres2-Oct-13 16:18 
GeneralMy vote of 5 Pin
wardensprite26-May-13 22:20
memberwardensprite26-May-13 22:20 
GeneralMy vote of 1 Pin
Abdullatif M. Abu Al Rub16-Mar-13 23:08
memberAbdullatif M. Abu Al Rub16-Mar-13 23:08 
QuestionHow can I get samples data of 32 bits audio files? Pin
flash-mmb12-Mar-13 11:42
memberflash-mmb12-Mar-13 11:42 
QuestionHow Can I Use The Voice-Input Device? Pin
yoli79948016530-Oct-12 16:08
memberyoli79948016530-Oct-12 16:08 
GeneralMy vote of 5 Pin
shengcheng_jin5-Jun-12 22:56
membershengcheng_jin5-Jun-12 22:56 
This function is very good ,thanks.
This idear is very good for open sources.
BugException raised when SetupMixer Pin
Code Artist20-Apr-12 5:07
memberCode Artist20-Apr-12 5:07 
BugDemo bug Pin
gwf_25sz27-Mar-12 17:43
membergwf_25sz27-Mar-12 17:43 
GeneralMy vote of 5 Pin
toann11-Jan-12 4:33
membertoann11-Jan-12 4:33 
QuestionDigital Output from a Sound track Pin
GPuthige4-Nov-11 20:30
memberGPuthige4-Nov-11 20:30 
QuestionHow to get sound directly from mic to speakers [modified] Pin
fer.dexter.fer21-Sep-11 3:20
memberfer.dexter.fer21-Sep-11 3:20 
AnswerRe: How to get sound directly from mic to speakers [modified] Pin
Ernest Laurentin28-Sep-11 10:04
memberErnest Laurentin28-Sep-11 10:04 
QuestionHow can I get sample directly from sound card? Pin
peteryu15-Sep-11 19:04
memberpeteryu15-Sep-11 19:04 
AnswerRe: How can I get sample directly from sound card? Pin
Ernest Laurentin15-Sep-11 19:37
memberErnest Laurentin15-Sep-11 19:37 
Questionpeakmeter without the sound of MediaPlayer Pin
Member 822966712-Sep-11 9:04
memberMember 822966712-Sep-11 9:04 
AnswerRe: peakmeter without the sound of MediaPlayer Pin
Ernest Laurentin15-Sep-11 19:35
memberErnest Laurentin15-Sep-11 19:35 
GeneralMy vote of 5 Pin
hattonjohn9-Sep-11 5:36
memberhattonjohn9-Sep-11 5:36 
GeneralMuddy Sound Pin
billiam90429-May-11 15:05
memberbilliam90429-May-11 15:05 
GeneralRe: Muddy Sound Pin
Ernest Laurentin30-May-11 6:03
memberErnest Laurentin30-May-11 6:03 
QuestionA question about the SoundStudio mfc Pin
kspliusa27-May-11 2:04
memberkspliusa27-May-11 2:04 
AnswerRe: A question about the SoundStudio mfc Pin
Ernest Laurentin30-May-11 5:54
memberErnest Laurentin30-May-11 5:54 
GeneralRe: A question about the SoundStudio mfc Pin
kspliusa31-May-11 3:39
memberkspliusa31-May-11 3:39 
GeneralMy vote of 5 Pin
kspliusa26-May-11 14:34
memberkspliusa26-May-11 14:34 
GeneralHow do I get a sample from a microphone? Pin
bartdivanov8-May-11 10:08
memberbartdivanov8-May-11 10:08 
GeneralRe: How do I get a sample from a microphone? Pin
Ernest Laurentin12-May-11 5:55
memberErnest Laurentin12-May-11 5:55 
GeneralRe: How do I get a sample from a microphone? Pin
bartdivanov14-May-11 10:12
memberbartdivanov14-May-11 10:12 
GeneralRe: How do I get a sample from a microphone? Pin
Shannen Londres2-Oct-13 14:00
memberShannen Londres2-Oct-13 14:00 
GeneralHow to Open the Projects in Microsoft Visual Studio 2010? [modified] Pin
bartdivanov8-May-11 9:57
memberbartdivanov8-May-11 9:57 
GeneralRe: How to Open the Projects in Microsoft Visual Studio 2010? Pin
Ernest Laurentin12-May-11 5:52
memberErnest Laurentin12-May-11 5:52 
GeneralRe: How to Open the Projects in Microsoft Visual Studio 2010? Pin
bartdivanov14-May-11 10:20
memberbartdivanov14-May-11 10:20 
Generalmono stereo Pin
steve23491-Dec-10 3:07
membersteve23491-Dec-10 3:07 
GeneralMy vote of 5 Pin
mikkojay22-Oct-10 21:07
membermikkojay22-Oct-10 21:07 
GeneralNewbe question Pin
Alex1971_rm20-Jul-10 4:11
memberAlex1971_rm20-Jul-10 4:11 
GeneralUse general Windows sound stream Pin
Max Stähr28-Jun-10 19:14
memberMax Stähr28-Jun-10 19:14 
GeneralRe: Use general Windows sound stream Pin
Ernest Laurentin28-Jun-10 19:18
memberErnest Laurentin28-Jun-10 19:18 
GeneralRe: Use general Windows sound stream Pin
Max Stähr19-Jul-10 6:42
memberMax Stähr19-Jul-10 6:42 
GeneralNo go in Win7 64 Pin
hhtbfsfdfasdfasd2-Jun-10 19:36
memberhhtbfsfdfasdfasd2-Jun-10 19:36 
GeneralRe: No go in Win7 64 Pin
Ernest Laurentin2-Jun-10 19:45
memberErnest Laurentin2-Jun-10 19:45 
GeneralRe: No go in Win7 64 Pin
element24-Jun-10 21:31
memberelement24-Jun-10 21:31 
GeneralRe: No go in Win7 64 Pin
bartdivanov8-May-11 9:41
memberbartdivanov8-May-11 9:41 
GeneralRe: No go in Win7 64 Pin
Ernest Laurentin12-May-11 5:57
memberErnest Laurentin12-May-11 5:57 
QuestionCan i use this in fft? or other algo? Pin
hugsnbaby10-May-10 16:28
memberhugsnbaby10-May-10 16:28 
AnswerRe: Can i use this in fft? or other algo? Pin
Ernest Laurentin11-May-10 13:55
memberErnest Laurentin11-May-10 13:55 
QuestionMissing cdecl on PeakFrequency* declarations? Pin
Ross Crawford23-Apr-10 13:00
memberRoss Crawford23-Apr-10 13:00 
AnswerRe: Missing cdecl on PeakFrequency* declarations? Pin
Ernest Laurentin23-Apr-10 14:21
memberErnest Laurentin23-Apr-10 14:21 
GeneralRe: Missing cdecl on PeakFrequency* declarations? Pin
Ross Crawford23-Apr-10 20:33
memberRoss Crawford23-Apr-10 20:33 
GeneralGreat work! Pin
zorou11-Mar-10 10:16
memberzorou11-Mar-10 10:16 
GeneralThank you very much Pin
DRIFTman12-Oct-09 8:30
memberDRIFTman12-Oct-09 8:30 
Generaloh,good job, thanks Pin
shihongwei29-Sep-09 18:20
membershihongwei29-Sep-09 18:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 4 Sep 2008
Article Copyright 2008 by Ernest Laurentin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid