I am trying to get audio input and send the output directly with less delay possible with C #.
I use the NAudio library , which supports ASIO for better latency.
In particular, I use an AsioOut object for recording, and another for playback, initialized with a BufferedWaveProvider , which is populated with a callback function: OnAudioAvailable , which allows me to use ASIO buffers.
The problem is that I hear sound with various glitches and with a bit of delay . I think the problem is the OnAudioAvailable function, where the buffer is filled with data taken as input from the sound card.
Ads:
NAudio.Wave.AsioOut playAsio;
NAudio.Wave.AsioOut recAsio;
NAudio.Wave.BufferedWaveProvider buffer;
Playback Order:
if (sourceList.SelectedItems.Count == 0) return;
int deviceNumber = sourceList.SelectedItems[0].Index;
recAsio = new NAudio.Wave.AsioOut(deviceNumber);
recAsio.InitRecordAndPlayback(null, 2, 44100);
NAudio.Wave.WaveFormat formato = new NAudio.Wave.WaveFormat();
buffer = new NAudio.Wave.BufferedWaveProvider(formato);
recAsio.AudioAvailable += new EventHandler<NAudio.Wave.AsioAudioAvailableEventArgs>(OnAudioAvailable);
playAsio = new NAudio.Wave.AsioOut(deviceNumber);
playAsio.Init(buffer);
recAsio.Play();
playAsio.Play();
OnAudioAvailable ():
private unsafe void OnAudioAvailable(object sender, NAudio.Wave.AsioAudioAvailableEventArgs e)
{
byte[] buf = new byte[e.SamplesPerBuffer];
for (int i = 0; i < e.InputBuffers.Length; i++)
{
Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer);
buffer.AddSamples(buf, 0, buf.Length);
}
}
AsioAudioAvailableEventArgs definition:
public AsioAudioAvailableEventArgs(IntPtr[] inputBuffers, int samplesPerBuffer, AsioSampleType asioSampleType);
public float[] GetAsInterleavedSamples();
Does anyone know how to fix this? Thanks to everyone.