love icon indicating copy to clipboard operation
love copied to clipboard

Source:tell() with looping streaming sources gets desynced.

Open NyakoFox opened this issue 2 years ago • 3 comments

My friend is working on a project where a sound effect plays every beat of a song, which works fine until the song loops. After the song loops, the sound starts being played slightly before where it should, seeming to get worse every loop.

This can't be an issue with our code since it uses source:tell() to get the position of the song. If said function was returning the right values, looping wouldn't affect the syncing at all.

NyakoFox avatar May 20 '22 19:05 NyakoFox

Can you upload a .love or audio file that demonstrates the issue?

slime73 avatar May 20 '22 20:05 slime73

You do need to note that what you consider the right values from :tell might be based on a false assumption; you'll never get a truly accurate value from that (for sync purposes) due to the granularity of that function (and the internals of the library that handles audio)... and even if it was infinitely precise, you probably would need to account for OpenALSoft's own latencies on top of things.

As a first assumption, looping offsets the internal buffer contents (or however it's implemented in OALS), hence the values returned from tell will drift after each loop.

If that is indeed the case, then it's not a bug, just your expectations being wrong about :tell.

However, a solution that i do recommend for games that need to sync audio with anything (gameplay or other audio or whatever) is to look into Queueable Sources, and use those with manual syncing and buffering; you'll get (samplepoint-) accurate playback, and you can sync stuff based on that.

(Also do join the löve discord, there people can help better with initial issues that may not even be löve bugs...)

zorggn avatar May 20 '22 20:05 zorggn

@zorggn Thanks for the information! I'll upload a testing .love anyway since requested but we'll definitely look into Queueable Sources.

Can you upload a .love or audio file that demonstrates the issue?

Here's soundtest.zip (GitHub doesn't like attaching .love files apparently)

Space to seek to 3 seconds before the end of the file, M to stop/start

You can hear after the first loop that it desyncs.

NyakoFox avatar May 20 '22 21:05 NyakoFox

Thanks! I added some time drift logging to that test .love which helped me narrow down the main cause and improve it.

slime73 avatar Dec 04 '22 23:12 slime73