|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis project is a live volume meter control for monitoring audio levels from a sound card. It is written entirely in C#, and uses Microsoft's Managed DirectX (June release). You can drag-and-drop the control into any Windows Forms application. You may select the input, and tweak the appearance of the animation through various parameters. The idea behind this project is to maintain a very short buffer of audio and constantly record into it. The control is on a timer, and periodically queries the buffer to see what volume level the most recent sample has, and then sets a progress bar's value accordingly. There are commercial controls that achieve the same purpose, but I wanted to dive in and get my hands dirty with DirectX (albeit the managed version :) ). I hope others will benefit from this work. Using the codeThis project consists of four DLLs: volumemeter.dll, progressbargradient.dll, Microsoft.DirectX.dll, and Microsoft.DirectX.DirectSound.dll. To use this control in a project, right-click in the toolbox, select "Choose Controls", and browse for volumemeter.dll. You don't need to manually reference any of the other DLLs, but they do need to be present. The control should now be present in your toolbox, so drag it onto a form to add it to your project. The control is active even while in the Visual Studio designer, so you can see it in action without having to run the project. The following methods are available to you:
The following properties are available to you:
The following events are available to you:
Points of InterestThis control uses GDI+ to run all the animation, so if you size the control to full-screen, you will most likely see a very high CPU usage. You can lower that usage by increasing the The progress bars in this project are custom controls I built. They let you use a gradient fill rather than a plain old solid color. If you want to use them independent of this project, just add the "ProgressBarGradient.dll" to your toolbox. I might post the ProgressBarGradient project online if/when I get around to it. If you're interested in how to use managed DirectX, here's the code for starting the audio capture buffer: // initialize the capture buffer and start the animation thread
Capture cap = new Capture(audioDevices[deviceIndex].DriverGuid);
CaptureBufferDescription desc = new CaptureBufferDescription();
WaveFormat wf = new WaveFormat();
wf.BitsPerSample = 16;
wf.SamplesPerSecond = 44100;
wf.Channels = 2;
wf.BlockAlign = (short)(wf.Channels * wf.BitsPerSample / 8);
wf.AverageBytesPerSecond = wf.BlockAlign * wf.SamplesPerSecond;
wf.FormatTag = WaveFormatTag.Pcm;
desc.Format = wf;
desc.BufferBytes = SAMPLES * wf.BlockAlign;
buffer = new Microsoft.DirectX.DirectSound.CaptureBuffer(desc, cap);
buffer.Start(true);
liveVolumeThread = new Thread(new ThreadStart(updateProgress));
liveVolumeThread.Priority = ThreadPriority.Lowest;
liveVolumeThread.Start();
And here's how you read values from the buffer: private const int SAMPLES = 8;
private static int[] SAMPLE_FORMAT_ARRAY = {SAMPLES, 2, 1};
...
Array samples = buffer.Read(0, typeof(Int16), LockFlag.FromWriteCursor,
SAMPLE_FORMAT_ARRAY);
Etc.I have tested and used this control on only a few different machines, so no guarantees about how well it will run for you. The usual disclaimers apply - run this code at your own risk, I am not responsible if you lose your firstborn because of some bug in the code :). That said, I have not encountered any problems using this control in my own projects. Feel free to contact me with any bugs or suggestions. Enjoy! History
|
||||||||||||||||||||||