Click here to Skip to main content
Click here to Skip to main content

LED Style Volume Meter Using DirectX

By , 21 Sep 2007
Rate this:
Please Sign up or sign in to vote.

Screenshot - AnalogSignalMeterSample.jpg

Introduction

This article serves as a usage manual for a UserControl I created called AnalogSignalMeter. This control uses Direct3D to paint the control, and DirectSound to sample the audio signal.

Background

I am a sound tech at my church, and I am currently developing some software. One of the items I needed was a signal level meter. I decided to create my own, and this is the result.

Demo

The demo program is a basic form that demonstrates the control. There are also controls that let you modify the properties of the control.

AnalogSignalMeter Public Properties

The following properties are available to you for modification:

  • BackgroundColor
  • LowLedColor
  • MidLedColor
  • HighLedColor
  • LowLedCount
  • MidLedCount
  • HighLedCount
  • LedHeight
  • LedWidth
  • LeftMargin
  • RightMargin
  • BottomMargin
  • TopMargin
  • LeftRightSeperation
  • UpDownSeperation
  • DisplayOrientation
  • AudioDisplayMode
  • MeterMode
  • SampleFrequencyMilliseconds
  • AutoStart
  • ClearMeterOnStop

BackgroundColor and the three LedColor properties are pretty straightforward. They control the colors of the meter. The three LedCount properties control the maximum number of each different LED level. LedHeight and LedWidth control the sizing of each individual LED. These properties are relative to the Orientation of the control. The four Margin properties control the spacing between the LEDs and the four edges of the control. They too are relative to the Orientation of the control. LeftRightSeperation and UpDownSeperation control the spacing between each LED. These properties are also relative to the Orientation of the control.

DisplayOrientation has four possibilities: Horizontal, HorizontalReverse, Vertical, and VerticalReverse. Horizontal draws the LEDs from least to greatest as left to right. HorizontalReverse is the opposite of this. Vertical draws the LEDs from least to greatest as top to bottom. VerticalReverse is the opposite of this. The orientation pictured at the top of the article is Horizontal. The most common arrangement to emulate a sound board meter is VerticalReverse.

AudioDisplayMode has two possibilities: Stereo and Mono. Stereo samples a unique level for the left and right channels. Mono samples the left channel for both left and right levels.

MeterMode has three possible values: AudioSignal, RandomLevels, and SpecificLevels. AudioSignal tells the meter to run in standard mode, capturing levels from the audio buffer. RandomLevels tells the meter to report randomly generated levels. SpecificLevels tells the meter to report the same level constantly.

SampleFrequencyMilliseconds controls how often the control samples the signal level, and is measured in milliseconds, thousandths of a second. The default, and probably the best value, is 100. However, feel free to experiment to your own liking.

AutoStart tells the control whether or not to start when the application loads. This is set to false, by default. This still needs a bit of tweaking on my part, and will be rectified in the next release.

ClearMeterOnStop tells the control whether or not to reset the meter to zero when stopped.

The following are read-only properties that you can access.

  • MeterRunning
  • TotalLedCount
  • LeftLevel
  • RightLevel

MeterRunning simply tells you whether or not the meter is running. TotalLedCount returns the total maximum number of LEDs. LeftLevel and RightLevel allow you to read the last sampled volume level.

AnalogSignalMeter Public Methods

There are four methods available for your use:

  • StartMeter()
  • StartRandomMeter()
  • StartSpecificMeter(int leftLevel, int rightLevel)
  • StopMeter()

StartMeter will start the meter and begin sampling the signal from the sound card. StartRandomMeter will start the meter, and begin feeding random levels to it. StartSpecificMeter will start the meter, and feed it a constant static level repeatedly. StopMeter will stop the meter, and if ClearMeterOnStop is true, then the levels will be reset to zero.

You may never need the last two start methods, but they are handy for troubleshooting the meters behavior if you start noticing odd behavior.

AnalogSignalMeter Events

There are three events that the control will trigger:

  • ReportLevel
  • LeftSignalPeak
  • RightSignalPeak

ReportLevel is triggered whenever a new sample is acquired. It has its own EventArgs, which contain both levels, args.LeftLevel, and args.RightLevel.

LeftSignalPeak and RightSignalPeak are triggered any time the sample level equals the total maximum LEDs. This is handy because this means that the audio signal is likely distorted, meaning a loss of quality, and it would be a signal to reduce the gain of the incoming signal. This event has its own EventArgs, which contains the Channel that peaked, args.Channel, and also a message, args.Message.

AnalogSignalMeter Tips

As mentioned above, AutoStart needs tweaking. If you wish for the meter to auto start, set AutoStart to true in the design stage, and then have your form check the property, and call the StartMeter method accordingly.

If you don't like the look of the meter but want the functionality, there is an alternative, which you can view in the demo application. You can set the control visibility to false, and in your ReportLevel handler, you can use the levels to set a progress bar value. Set the progress bar maximum to the TotalLedCount value, and then have the ReportLevel event handler set the progress bar value to the args.LeftLevel or args.RightLevel value. You can make a separate progress bar for each channel if you like. There is a nice gradient style progress bar available from someone on CodeProject. It can be used to mimic the separate color values for lower or higher signal levels.

History

September 21, 2007 - New Release Posted

I have uploaded a new release of both BuckSoft.DirectSound, and AnalogSignalMeterDemo.

BuckSoft.DirectSound

  • Added a new public property, MeterMode, along with an enumeration of values for it. This property tells the meter to run in either audio signal mode, random levels mode, or specific levels mode. The lack of this property is why the meter would always restart back into random mode when changing other properties with the demo.

AnalogSignalMeterDemo

  • Fixed the "Always restarts in random mode" bug.
  • Progress bar updates and level labels updates on the demo were handled in an unsafe thread call, this has been fixed.
  • Fixed the bug that caused a crash when LedCounts were increased

September 17, 2007 - Complete Source Code Added

On my initial post, I neglected to post the complete source code for the control. Now it is available.

September 14, 2007 - Initial Posting

Feel free to make suggestions for additions or improvements. I am still fresh to C# and DirectX, and welcome any insight that anyone may have to offer.

Disclaimer

This control is free to use and redistribute as you please. It is a work in progress. Use at your own risk.

License

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

About the Author

stebo0728

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 249972410-Dec-12 20:34 
Generalvu meter at playing Pinmembersteve234922-Nov-10 3:51 
GeneralMy vote of 4 Pinmembermikkojay22-Oct-10 20:31 
GeneralAwesome work! PinmemberJinx1013-Jan-10 11:54 
GeneralRe: Awesome work! PinmemberJinx1016-Jan-10 6:28 
GeneralQuestion Pinmemberalenet44413-Aug-09 3:35 
GeneralWorks fine in XP but not on Vista x64 Pinmembersilviu2k723-Mar-08 7:31 
Generalit doesn work!! PinmemberCARTAVIO11-Feb-08 11:53 
GeneralRe: it doesn work!! Pinmemberxtomiii28-Jul-08 12:17 
GeneralRe: it doesn work!! PinmemberMikeCam27-May-09 0:51 
GeneralError in code PinmemberGreg Osborne20-Nov-07 9:32 
QuestionOutOfVideoMemoryException Pinmemberisepise8-Nov-07 2:46 
GeneralInvalidCallException Pinmemberkgoodrich23-Oct-07 11:47 
QuestionHow to use this? Pinmemberalmere10918-Sep-07 11:39 
AnswerRe: How to use this? Pinmemberstebo072818-Sep-07 16:55 
GeneralRe: How to use this? Pinmemberalmere10918-Sep-07 20:42 
GeneralRe: How to use this? Pinmemberalmere10919-Sep-07 11:43 
GeneralRe: How to use this? Pinmembersjcardinale20-Sep-07 10:48 
GeneralRe: How to use this? Pinmemberalmere10920-Sep-07 20:36 
GeneralRe: How to use this? Pinmemberstebo072821-Sep-07 3:17 
GeneralGDI based control Pinmember_elli_15-Sep-07 6:03 
GeneralRe: GDI based control Pinmemberstebo072815-Sep-07 10:27 
GeneralRe: GDI based control Pinmember_elli_16-Sep-07 2:56 
GeneralRe: GDI based control Pinmemberalmere10919-Sep-07 11:45 
GeneralRe: GDI based control Pinmemberstebo072821-Sep-07 3:14 

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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 21 Sep 2007
Article Copyright 2007 by stebo0728
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid