Search code examples
c#wpfnaudio

NAudio AudioMeterInformation works only if "control mmsys.cpl sounds" is open


I'm trying to capture the sound of MIC (DataFlow.Capture), but AudioMeterInformation.PeakValues only works if sound properties is open (control mmsys.cpl sounds)

Working example

Working

But when i close sound properties..

Not working

My code

    private void calculateChannels(Object source, ElapsedEventArgs e)
    {
        dev = devEnum.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);

        try
        {
            double currentLeftChannel = 100 - (dev.AudioMeterInformation.PeakValues[0] * 100);
            double currentRightChannel = 100 - (dev.AudioMeterInformation.PeakValues[1] * 100);

            // this function just "smooth" the progress bar
            this.leftChannel = round(this.leftChannel, currentLeftChannel);
            this.rightChannel = round(this.rightChannel, currentRightChannel);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

    }

I'm used (-100) on the result because my progress bar is inverted (black colors are actually the progress)

DataFlow.Render is working fine, even without properties open.

How can I fix this?


Solution

  • I found the solution. I had to put WaveInEvent on my constructor to enable the device.

    waveInStream = new WaveInEvent();
    waveInStream.WaveFormat = new WaveFormat(44100, 1);
    waveInStream.StartRecording();
    

    Now, It is working.