Delay (approx 200 ms) in streamed audio playback

限于喜欢 提交于 2019-12-04 10:34:37

There is always latency introduced with audio playback. For WaveOut, you can specify the number of buffers and the overall desired latency. You can also specify buffer sizes for the other driver models too. For most playback scenarios, two buffers of 100ms each is ideal as it is reasonably responsive and will not stutter except under extreme load. However, you can go lower if you need to, but run the risk of it not being able to fill the next buffer in time. Don't expect as low latencies as you could get in a DAW (e.g. 5ms) with NAudio, it is not so highly optimised, and the .NET framework is not particularly well suited for this kind of application anyway due to the garbage collector.

Here's an example of setting the output latency for WaveOut:

var waveOut = new WaveOut();
waveout.DesiredLatency = 50; // 50ms latency

I also develop audio streaming application using NAudio. We also have latency issue. It reaches 300 ms.

The capture happens 10 times per second (once a 100 ms).

Using the advice of Vikram.exe to use DirectSoundOut instead of WaveOut helped a bit. The latency decreased by 50 or 100 ms, but only if I set Desired Latency to 50 ms.

new DirectSoundOut(guid, 50);

One more trick has lowered the latency by 100 or 200 ms. We check if there is a sound being played and skip new frames if it is.

if (s_WaveProvider.BufferedDuration <= 100)
    s_WaveProvider.AddSamples(s_Samples, 0, size);

There is still some work to be done in regards of sound smoothness, but generally we have no latency now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!