naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.38k stars 1.09k forks source link

WasapiLoopbackCapture is receiving silence when speakers are muted on Windows 10. #1045

Open forlayo opened 11 months ago

forlayo commented 11 months ago

Steps

  1. Create WasapiLoopbackCapture and add a listener on DataAvailable.
  2. Execute StartRecording() of WasapiLoopbackCapture.
  3. See that when mute speakers audio data received on DataAvailable is just silence.

Note: This is not happening in my other computers with Windows 11.

Expected behavior Getting audio from what's being played in the device, as with other devices, even when speakers are muted.

Device's details

forlayo commented 11 months ago

This is a code sample of how to reproduce the issue:

        var outputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NAudio");
        Directory.CreateDirectory(outputFolder);
        var outputFilePath = Path.Combine(outputFolder, "recorded.pcm");

        var loopback = new WasapiLoopbackCapture();
        var fileWriter = new WaveFileWriter(outputFilePath, loopback.WaveFormat);
        loopback.RecordingStopped += (s, a) => { fileWriter.Dispose(); };
        loopback.DataAvailable += (object? sender, WaveInEventArgs args) =>
        {
            fileWriter.Write(args.Buffer, 0, args.BytesRecorded);
        };

Then I play a video on youtube and mute speakers.

On most devices, this works well and file produced has the audio that's being played on the device no matter if speakers are muted or not.

But there are some devices in which when speakers are muted the audio received and hence the file content is just silence.

For reference, I posted the issue on stackoveflow as well.

markheath commented 11 months ago

There can be different ways of "muting" speakers. One is to zero out all the samples in the stream. But other ways include turning a volume control that is after the DAC. So it might be that this device has implemented muting differently from your other devices.