'Mic Volume Meter

i'm currently working on a personal project to create a c# small program that let me know when people clap laugther (i just need to know the volume peak value)? Using Laptop Internal mic or input line (for external mic). I'm not much of an expert so i was wondering if someone can give me a guidance of how to use NAudio to achieve my goal.

Best Regards.



Solution 1:[1]

Please check this article I wrote for Coding4Fun about how to create a .NET Voice Recorder using NAudio. It includes a volume meter, and the source code is available as an open source project.

Solution 2:[2]

First download latest version of NAudio from dll-files.com

Then add NAudio to your project reference

Then insert this code given below in using group:

using NAudio;
using NAudio.CoreAudioApi;
using NAudio.Wave;

After adding code given above, add this void "For Mono Use" or if you want for stereo, go down

private void Mono_REC(ProgressBar pb)
{
    MMDeviceEnumerator MDE = new MMDeviceEnumerator();
    MMDevice MD = MDE.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
    WaveIn mic = new WaveIn();
    mic.StartRecording();
    var AMI = MD.AudioMeterInformation;
    pb.Value = AMI.MasterPeakValue * 100;
}

Stereo

private void Stereo_REC(ProgressBar left, ProgressBar right)
{
    MMDeviceEnumerator MDE = new MMDeviceEnumerator();
    MMDevice MD = MDE.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
    WaveIn mic = new WaveIn();
    mic.StartRecording();
    var AMI = MD.AudioMeterInformation;
    left.Value = AMI.PeakValues[0] * 100;
    right.Value = AMI.PeakValues[1] * 100;
}

If mic.StartRecording(); isn't working in any one or both voids, then remove:

WaveIn mic = new WaveIn();
mic.StartRecording();

And add: WaveIn mic = new WaveIn(); outside all voids and add mic.StartRecording(); in any 1 of following:

private void Form1_Load(Object sender, EventArgs e) {}
private void Button1_Click(Object sender, EventArgs e) {}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Heath
Solution 2 Peter Csala