discordrb
discordrb copied to clipboard
Voice timing improvements
For the first time in a while I used discordrb myself to make a voice bot (lol) and I noticed a bunch of things that could be improved, mainly relating to timing:
- Previously, only the nanosecond part of the wall clock time was used to compute the voice send delay time, which might have led to a skip whenever the start and end of the encode-send process would fall on different seconds. I'm not sure if this effect was actually noticeable on its own but it was certainly something that shouldn't have been this way, so I changed it to use the whole timestamp. (It might even be a good idea to use a monotonic time source instead of the wall clock time in the future, to avoid the dreaded "incorrect audio timing during leap seconds" bug, but as far as I can tell Ruby doesn't really have a system-agnostic monotonic time source yet...)
- More importantly, I've observed that Ruby's
sleep
is not accurate enough to achieve adequate audio timing; on my systems, it has a systemic deviation upwards ranging between 0 to ~0.2 ms. I've changed it so that it observes how long the lastsleep
call actually took wall-clock-wise and uses the difference to correct the nextsleep
call. This makes it slightly more precise and a lot more accurate, which leads to much improved audio timing. - I've ripped out all the
adjust_
stuff as it was essentially nothing more than a placebo.
With these changes, audio playback becomes butter smooth on both systems I've tried, although it would be nice if other people tried it as well!
Apart from these changes, I've fixed a minor bug which might cause stream_time
to have incorrect values, and changed it so that voice bots can be used without opus installed if you only want to play back DCA files.