Please upgrade to Libaudioverse 0.9-alpha6

11 views
Skip to first unread message

Austin Hicks

unread,
Aug 27, 2016, 2:19:27 PM8/27/16
to libaud...@camlorn.net
Hi,

The short version is you want to do this unless you enjoy some
machines having crackling audio on Windows 8 and later. This specific
problem may not be solved, so say something if you have issues that look
like they might be due to it and I'll give you a test program. The
specific behavior is that Libaudioverse will produce crackling if some
programs such as Chicken Nugget are not running. Specifically not
running; this isn't a typo. The long, technical info:


Firstly, this is a theory based on what fixed the problem and some
other information I have seen in the past. Emphasis on theory. this
could prove to be completely wrong, but it's what I came up with, and it
fits the available information on my end.


Windows provides a sleep function (called sleep), used indirectly
by the C++ standard library and most other languages when they wish to
sleep a thread. This function takes a time in MS. The accuracy of it
depends on a few things, but most notably the frequency of the timer
that Windows is using for anything that needs it. Audio_io pretended
that this function was accurate; this worked because I was testing with
Chicken Nugget running (I'll come back to this).


The difference between Windows 7 and Windows 8+ is that Windows
moved to a tickless kernel. before Windows 8, the kernel would wake up
and go through all the threads, etc, on a fixed period. I attempted to
reproduce this bug on Windows 7 and couldn't. I am assuming that the
accuracy of Sleep became less accurate overall when they made this
change. It's actually a good thing, as it increases battery life.


Some apps need to have precise timing. For this use case,
Microsoft provides a number of APIs. The one that matters here is a
function called timeSetPeriod, and the effect is to raise the frequency
of the timer on a system-wide basis. Windows uses the frequency
specified by the app that wants the most precision for all apps; because
timers are in hardware, it's not like we can allocate more of them. If
you are running an app that calls timeSetPeriod to increase the
accuracy, then the sleep function becomes accurate as well.


I do not call timeSetPeriod. This is bad if it can at all be
avoided, and we can. What I did do is tweak how the thread computes the
time for which it needs to sleep. If the problem still persists, I may
have to call timeSetPeriod temporarily and/or do some spin waiting.
This is yet another thing that can be fixed by throwing out and
rewriting 99% of audio_io. You can thank Microsoft for making the audio
API push-based but making the correct APIs you're supposed to use for
precise timers pull-based for the lack of a very definite fix here.


As for why Chicken Nugget calls timeSetPeriod, I don't know. You
can see who is requesting the most precise timer with the command
powercfg --energy at an administrator command prompt. On my system,
it's requesting a timer resolution of 1MS, as opposed to a default of
15MS. If you think you have this problem, running that command while
the offending app is running and looking for anything about timer
resolutions can tell you if it might be this bug recurring.

Reply all
Reply to author
Forward
0 new messages