libsoundio icon indicating copy to clipboard operation
libsoundio copied to clipboard

Huge latency on Windows

Open liquidev opened this issue 6 years ago • 6 comments
trafficstars

I'm having a bit of an issue with libsoundio. For some reason, on Linux, the library works perfectly OK—I set the latency to a very small value (like 0.001), and as expected, it works perfectly. However, when I try to set the latency on Windows, the effective latency is huge (up to 8s), and this was reproduced by multiple users on multiple different machines.

Here's my bit of code where I instantiate the device, set the write callback, etc.

https://github.com/liquid600pgm/rapid/blob/master/src/rapid/audio/device.nim

Is there anything I'm doing wrong, or is this an issue with soundio?

liquidev avatar Oct 01 '19 20:10 liquidev

It seems the library defaults to 4 second buffer time on wasapi with shared device. I remember there was spec stating that on backend that support variable length buffering, the min_frame and max_frame in the callback is set to 0 and maximum value the backend buffer can hold.

Modifying the library source code can result in shorter maximum buffer size, but it may be better to restrict your own rendering to use whatever size you want. The shared stream typically has a os audio engine refresh time of 10ms, thus anything less than 10ms making it do extra callback every time is not really a good idea (you may look at exclusive device if you want less latency).

Also, it seems that from windows 10, the sdk exposes the IAudioClient3 interface where you can query the update modes available. Also there's support for event based shared stream, so there can be less to do on the audio thread event loop.

(I was looking at the docs recently and trying to add a separate code path where when IAudioClient3 interface is available, use that extension for shared rendering. Don't know when there will be a pull request or if it would be merged... Currently things seems to work fine on my specific setup currently.)

shangjiaxuan avatar Feb 14 '20 17:02 shangjiaxuan

Just created a pr here (#231), you can see if the patch works out for you (only supporting lower latency output for now). (Use it and modify it any way you like, but I'm not absolutely sure if it will work on all circumstances.)

For a much cleaner fix, just change the 4 second to 20ms. This should work well on most cases (10ms will have dropouts probably due to padding needed by the os).

shangjiaxuan avatar Feb 15 '20 05:02 shangjiaxuan

Will the original author add collaborators? There are so many forks and open pull requests.

wegylexy avatar Jul 22 '20 07:07 wegylexy

CC @andrewrk. Yeah, it's a bit sad seeing all this great work bottlenecked by a lack of maintainers. I understand that there are other priorities, and that you can't accept things willy-nilly. Hopefully this can be delegated to someone else. Maybe collaborators like @cgutman or the others want to step up.

I'm just evaluating the pros and cons of using it, but it's something worth discussing. I think.

Swyter avatar Mar 06 '21 14:03 Swyter

The dev is too busy focused on zig https://github.com/ziglang/zig these days to care about this library anymore. I would suggest using https://github.com/mozilla/cubeb as an alternative.

mdsitton avatar Jun 18 '21 16:06 mdsitton

Never mind, we decided to migrate to .NET 6.0 and use NAudio.

wegylexy avatar Nov 13 '21 05:11 wegylexy