2

Based on this question made ​​by me: How to record and playback with NAudio using AsioOut

With this code:

[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
private static unsafe extern void MoveMemory(IntPtr dest, IntPtr src, int size);

private void OnAudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{
     for (int i = 0; i < e.InputBuffers.Length; i++)
     {
          MoveMemory(e.OutputBuffers[i], e.InputBuffers[i], e.SamplesPerBuffer * e.InputBuffers.Length);
     }

     e.WrittenToOutputBuffers = true;
}

Doing like this feels a bit latency and a bit of echo and I don't know how to solve them.

I searched the libraries on the web that allow me to eliminate the echo (AEC) but have not found anything useful for C#. Does anyone know how to help me?

EDIT 1: My AsioOut.PlaybackLatency is 2048ms. It's a bit too high..

EDIT 2: I tried with headphones and I noticed that the echo is only due to the proximity between the microphone and speakers, so it is not a problem although it would be interesting to have an AEC.

PROBLEM SOLVED: I solved the latency problem. It was a problem of my ASIO drivers, I uninstalled Cubase and I installed the ASIO4ALL driver and now it works fine. PS: I had to add a *2 in the last parameter of the MoveMemory. I don't know why, but without it you hear the sound in spurts.

Community
  • 1
  • 1
Shafa95
  • 201
  • 2
  • 14

1 Answers1

0

What buffer size are you using? Remember it is completely impossible to eliminate all latency if you are recording and playing back the audio. Most ASIO drivers allow you to go to quite low latencies (sub 10ms), but you also need to make sure that any direct monitoring feature of your soundcard is turned off, or you will hear an echo.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • for "buffer size" you mean `BufferedWaveProvider.BufferLength` ? if the answer is yes then this is my buffer size: 882000 – Shafa95 Mar 31 '14 at 18:11
  • no I mean ASIO buffer size. Should be visible on the ASIO control panel for your sound card – Mark Heath Apr 01 '14 at 08:50
  • Ok the size of the _input_ and _output buffers_ are **2048**, and the _Sync reference_ is set to _input_. While the size of the _audio buffer_ isn't defined and it's standard: 10ms.. I tried to change these values ​​and I got a slightly lower latency just seems a little far away from the idea of ​​low-latency ASIO (<10ms). it's also annoying that it can't be handled by the application, but only by the ASIO driver. But maybe I didn't understand. – Shafa95 Apr 01 '14 at 18:02
  • That's just the way ASIO works. By the way, what are you using BufferedWaveProvider for? Your code sample doesn't seem to be using that at all? – Mark Heath Apr 02 '14 at 19:56
  • I used the `BufferedWaveProvider` because you suggested me to use it in the post that I linked at the top.. It's only used to the define the number of output channels. – Shafa95 Apr 02 '14 at 20:15
  • Ah, OK. So there is no reason why the latency of your code above should be any longer than the length of the ASIO buffers – Mark Heath Apr 02 '14 at 20:17