This project has moved and is read-only. For the latest updates, please go here.

Wavein vs WaveinEvent?

Jul 24, 2013 at 3:45 AM
Edited Jul 24, 2013 at 3:52 AM
From what i understand, Wavein will run on the main thread, and WaveinEvent will run on a separate thread.

But i noticed, that if i use Waveinevent, i need to increase the buffer from 4-5ms to 11-12ms.
Which increases the latency alot in the total buffer (got a waveprovider also).

And i don´t see the reason for it, if Waveinevent is run on a separate thread, shouldn´t it be able to do the same as Wavein, or perhaps even better as it´s not limited by the GUI thread?

  void Sending(object sender, NAudio.Wave.WaveInEventArgs e)
           if (connect == true && MuteMic.Checked == false)
               udpClient.Send(e.Buffer, e.BytesRecorded, otherPartyIP.Address.ToString(), 1500);
              // waveWriterRam.Write(e.Buffer, 0, e.BytesRecorded);

                SendStream = new WaveInEvent();
                SendStream.BufferMilliseconds = 4;
                SendStream.WaveFormat = new WaveFormat(48000, 16, 2);
                SendStream.DeviceNumber = waveInDevice;
                SendStream.DataAvailable += Sending;
Runs vey bad (need to set 11ms).
                sourceStream.DataAvailable -= new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
                sourceStream = new WaveIn();
                sourceStream.BufferMilliseconds = 4;
                sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(48000, 16, 2);
                sourceStream.DeviceNumber = waveInDevice;

         void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)

              if (connect == true && MuteMic.Checked == false)
                udpClient.Send(e.Buffer, e.BytesRecorded, otherPartyIP.Address.ToString(), 1500);
This runs much much better.

Only thing is that if i move the GUI(Windows Form) it will stop for a while, then continue, as it runs on the GUI Thrad.
Jul 24, 2013 at 9:29 AM
I'm surprised you managed to get the latency that low at all with either type of WaveIn. It's not really a low latency audio API. The issue might be that when the event is set, perhaps the thread context switch isn't happening in time. I suppose you could always try your own WaveInEvent with a thread set to high priority.
Jul 24, 2013 at 9:37 AM
Oh, well whatt am i supposed to use then, i only know, Wavein, Wasapi and ASIO, and ASIO is of course the fastest, but i can´t use it.?

Though, i wonder, is it possible to use 2 ASIO drivers at the same time?
My built in for audio playback, and ASIO4ALL for my microphone?

i guess it could be possible, but not really sure. Would be awesome if it is.

Sady i am not that good at programming, so i have no idea how to change that event to high priority.

But is the GUI stop thing, caused by wavein being run on the main Thread?
Jul 24, 2013 at 1:54 PM
Not sure. It might be that the message queue is not getting serviced, or it might be that the garbage collector is running
Jul 24, 2013 at 1:58 PM
What can be done about that?
As it goes away with WaveinEvent, but that causes a big latency change weird enough.

My program is probably very unoptimized though, but i really have not clue here.

Is it possible to just put Wavein on a separate thread?
Jul 26, 2013 at 1:23 PM
that's what WaveInEvent is.
Jul 26, 2013 at 4:54 PM
Edited Jul 26, 2013 at 5:10 PM
well i know, but, is it possible to set the priority to high or something, as you mentioned earlier?
as perhaps that will solve the problem i am having.

Also what does number of buffer do?
how does it affect latency?