using System;
using System.Threading;
using NAudio.Wave;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var micManager = new MicManager();
while (true)
{
Console.WriteLine("Уровень сигнала микрофона: {0}%", micManager.GetMicVolume());
Thread.Sleep(1000);
}
}
}
class MicManager
{
private int _currentLevel;
public MicManager()
{
var waveIn = new WaveInEvent();
waveIn.DataAvailable += WaveOnDataAvailable;
waveIn.WaveFormat = new WaveFormat(8000, 1);
waveIn.StartRecording();
}
public int GetMicVolume()
{
return _currentLevel;
}
private void WaveOnDataAvailable(object sender, WaveInEventArgs e)
{
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
float amplitude = sample / 32768f;
_currentLevel = (int)(Math.Abs(amplitude) * 100);
}
}
}
}