naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.52k stars 1.1k forks source link

MasterPeakValue works for speakers but not for a microphone. #347

Open gitlabisbetterthangithub opened 6 years ago

gitlabisbetterthangithub commented 6 years ago

Hello,

First off, I'd like to thank Mark for this library.

I tried following the tutorial on this repository for implementing a recording level meter however a lot of it was confusing from a beginners point of view.

I followed this tutorial (https://www.youtube.com/watch?v=OVkd6xIWHDc) and it works well for seeing the audio level for speakers however it doesn't work for a microphone without opening up the recording devices window.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.CoreAudioApi;

namespace projectname
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
            var devices = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
            comboBox1.Items.AddRange(devices.ToArray());

        }

        private void timerAudioInput_Tick(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                var device = (MMDevice) comboBox1.SelectedItem;
                device.AudioEndpointVolume.Mute = false;
                richTextBox1.Text = device.AudioMeterInformation.MasterPeakValue.ToString();
                progressBar1.Value = (int)(Math.Round(device.AudioMeterInformation.MasterPeakValue * 100));

            }
        }
    }
}

The microphone audio meter will work when I open up recording devices Window (see this image for the program I mean https://www.wikihow.com/images/thumb/7/76/Record-Audio-on-a-PC-Step-1-Version-2.jpg/aid7017679-v4-728px-Record-Audio-on-a-PC-Step-1-Version-2.jpg)

I am using Windows 10 and have installed the library through NuGet

How can I make the code work for microphones, not just speakers?

Thank you very much for your help.

markheath commented 6 years ago

This is a known issue with this particular API - it only reports levels while you're actually capturing audio. You'd need to use WasapiCapture to start capturing audio (you can just ignore the DataAvailable callbacks) and I suspect this would start working.

gitlabisbetterthangithub commented 6 years ago

Thank you Mark for your help.

I added a WaveIn recorder and it works now. I'm just not too sure where in the code I should call recorder.StartRecording();. It won't take up storage by recording, correct?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using NAudio.Wave;

namespace projectname
{
    public partial class Form1 : Form
    {
        private WaveIn recorder;
        public Form1()
        {
            InitializeComponent();
            recorder = new WaveIn();
            recorder.StartRecording();
            MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
            var devices = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
            comboBox1.Items.AddRange(devices.ToArray());

        }

        private void timerAudioInput_Tick(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                var device = (MMDevice) comboBox1.SelectedItem;
                device.AudioEndpointVolume.Mute = false;
                richTextBox1.Text = device.AudioMeterInformation.MasterPeakValue.ToString();
                progressBar1.Value = (int)(Math.Round(device.AudioMeterInformation.MasterPeakValue * 100));

            }
        }
    }
}
markheath commented 6 years ago

No, "recording" just means that we're monitoring the sound card, so nothing is getting written to disk