naudio / NAudio

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

Error when "Audio enhancement" is checked on windows #645

Open carlosmachel opened 4 years ago

carlosmachel commented 4 years ago

Hi there. First of all, I would like to thank you for this great library, saved my life.

I'm having some problems with the audio enhancement when I try to capture the sound with WasapiLoopbackCapture.

I instanciate the recorder like the code below.

recorder = new WasapiLoopbackCapture(WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice());

And in the code below I start the recording.

recorder.DataAvailable += Recorder_DataAvailable;
recorder.RecordingStopped += (s, a) => CapturarAsync();
recorder.StartRecording();

But on the StartRecording it gives an COMEXCEPTION like this one #453 .

Is it possible to make this work or should I ask the user to disable de "audio enhancement".

Thanks!

strawhat2010 commented 3 years ago

Also got this COMException '0x88890008' in .net 5 WPF application, but it's working in a Console application with the same code......😢 BTW, There's no "audio enhancement" on my dev PC..

The WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient in the WPF application:

{NAudio.CoreAudioApi.AudioClient}
    AudioCaptureClient: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioCaptureClient' threw an exception of type 'System.Runtime.InteropServices.COMException'
    AudioClockClient: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioClockClient' threw an exception of type 'System.Runtime.InteropServices.COMException'
    AudioRenderClient: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioRenderClient' threw an exception of type 'System.Runtime.InteropServices.COMException'
    AudioStreamVolume: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioStreamVolume' threw an exception of type 'System.Runtime.InteropServices.COMException'
    BufferSize: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.BufferSize' threw an exception of type 'System.Runtime.InteropServices.COMException'
    CurrentPadding: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.CurrentPadding' threw an exception of type 'System.Runtime.InteropServices.COMException'
    DefaultDevicePeriod: 100000
    MinimumDevicePeriod: 30000
    MixFormat: {32 bit PCM: 48kHz 8 channels wBitsPerSample:32 dwChannelMask:1599 subFormat:00000003-0000-0010-8000-00aa00389b71 extraSize:22}
    StreamLatency: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.StreamLatency' threw an exception of type 'System.Runtime.InteropServices.COMException'

and it's normal 2 channels in the Console application :

{NAudio.CoreAudioApi.AudioClient}
    AudioCaptureClient: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioCaptureClient' threw an exception of type 'System.Runtime.InteropServices.COMException'
    AudioClockClient: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioClockClient' threw an exception of type 'System.Runtime.InteropServices.COMException'
    AudioRenderClient: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioRenderClient' threw an exception of type 'System.Runtime.InteropServices.COMException'
    AudioStreamVolume: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.AudioStreamVolume' threw an exception of type 'System.Runtime.InteropServices.COMException'
    BufferSize: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.BufferSize' threw an exception of type 'System.Runtime.InteropServices.COMException'
    CurrentPadding: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.CurrentPadding' threw an exception of type 'System.Runtime.InteropServices.COMException'
    DefaultDevicePeriod: 100000
    MinimumDevicePeriod: 30000
    MixFormat: {32 bit PCM: 48kHz 2 channels wBitsPerSample:32 dwChannelMask:3 subFormat:00000003-0000-0010-8000-00aa00389b71 extraSize:22}
    StreamLatency: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.StreamLatency' threw an exception of type 'System.Runtime.InteropServices.COMException'
    StreamLatency: 'WasapiLoopbackCapture.GetDefaultLoopbackCaptureDevice().AudioClient.StreamLatency' threw an 

really weird...

So I tried this...:

    class HackedWasapiLoopbackCapture: WasapiCapture
    {
        public HackedWasapiLoopbackCapture() :
            this(GetDefaultLoopbackCaptureDevice())
        {
        }

        public HackedWasapiLoopbackCapture(MMDevice captureDevice) :
            base(captureDevice)
        {
        }

        public static MMDevice GetDefaultLoopbackCaptureDevice()
        {
            MMDeviceEnumerator devices = new MMDeviceEnumerator();
            return devices.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
        }

        public override WaveFormat WaveFormat
        {
            get { return base.WaveFormat; }
            set { base.WaveFormat = value; }
        }

        protected override AudioClientStreamFlags GetAudioClientStreamFlags()
        {
            return AudioClientStreamFlags.Loopback;
        }
    }

and then

            var capture = new HackedWasapiLoopbackCapture();
            capture.WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(48000, 2);

it's working now... but not sure if it will work on other PCs.