Search code examples
c#asp.netwinformsnaudio

NAudio windows application form, has delay loopingback(Input to DirectSoundOut)


Problem:

As a part of school project, I attempt to build an application that provides a guitar AMP using the NAudio library.

When i plug in the guitar it recognizes it, and everything is working properly, but there is a huge delay between the input and the output from the speakers.

Here is my source code:

   private void button2_Click(object sender, EventArgs e)
        {
            if (sourceList.SelectedItems.Count == 0) return;

            int deviceNumber = sourceList.SelectedItems[0].Index;
            sourceStream = new WaveIn();
            sourceStream.DeviceNumber = deviceNumber;
            sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
            sourceStream.StartRecording();

            WaveInProvider waveIn = new WaveInProvider(sourceStream);

            waveOut = new DirectSoundOut();
            waveOut.Init(waveIn);
            waveOut.Play();
        }

in this code I catch an event of a button click that uses the selected input (microphone/guitar) and converts the sound it recieves to output. The delay between the input and the output is around ~1sec and it's a deal breaker.

How do I improve the delay, to make the application more responsive?


Solution

  • DirectSoundOut and WaveIn are not particularly low-latency audio APIs. For something like this, ASIO is preferable. AsioOut is unfortunately a bit more complicated to work with, but it should allow you to get much lower latencies.