Wasapi audio quality

2k Views Asked by At

Im new to using Wasapi in Naudio and Im having n issue with the sound quality. About 1/10 times the audio will sound perfect when I record and the other 9 times it will be fuzzy. I was wondering if there is any reason for this.

Here is my code i'm using to record the audio:

    public void CaptureAudio(String Name)
    {
        capture = new WasapiLoopbackCapture();
        capture.Initialize();
        w = new WaveWriter(Name, capture.WaveFormat);
        capture.DataAvailable += (s, capData) =>
        {
            w.Write(capData.Data, capData.Offset, capData.ByteCount);
        };

        capture.Start();

    }

    public void StartRecording(String Name)
    {
        new Thread(delegate(){CaptureAudio(Name); }).Start();
    }

    public void StopCapture()
    {
        capture.Stop();
        capture.Dispose();
        w.Dispose();
    }
3

There are 3 best solutions below

0
On BEST ANSWER

The problem turned out to be xbox music or windows media player running in the background, apparently they hog all the sound cards resources.

0
On

A few comments on your code:

First, have you modified WasapiLoopbackCapture in some way? The WaveInEventArgs on DataAvailable does not have the properties shown in your code. I'd expect you have some kind of block alignment error going on, so that your fuzzy sound is not reading on exact sample boundaries. Also NAudio does not have a class called WaveWriter - it's WaveFileWriter. Are you sure you are using NAudio?

Second, there is no need to start a new thread in StartRecording. WasapiLoopbackCapture will be using a background thread already.

0
On

First of all. As Mark already said, your code does not look like NAudio. It looks like CSCore. If you are using CSCore please create a new console application and copy paste the following code (I've modified your code). I just tried out that code on three different systems without any bugs and all 20 files were ok without beeing fuzzy.

        private static void Main(string[] args)
        {
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine(i);
                Capture(i);
            }
        }

        private static void Capture(int index)
        {
            string Name = String.Format("dump-{0}.wav", index);

            using (WasapiCapture capture = new WasapiLoopbackCapture())
            {
                capture.Initialize();
                using (var w = new WaveWriter(Name, capture.WaveFormat))
                {
                    capture.DataAvailable += (s, capData) => w.Write(capData.Data, capData.Offset, capData.ByteCount);
                    capture.Start();

                    Thread.Sleep(10000);

                    capture.Stop();
                }
            }
        }