Looping Music

Hey Folks! Not a lot of progress today, unfortunately. I spent the entire day debugging a music looping issue, and in the end, had to hack my way around it.

The problem happens when the looping wilderness music reaches the end of the track, and instead of restarting, just stops. This is a fairly rare occurrence in the game, since there is only a chance any track is going to be the looping one, the tracks only play every 10ish minutes, and this bug only happens if the portion of the track played starts near the end and overlaps the loop point.

But it's annoying when it happens, because the music just cuts short.

As far as I can tell, the game is punching through a few layers of confusing code to eventually start an OpenAL audio source, with an offset to signal where it should start playing. But instead of triggering the code that loops back to the beginning, it just hits a wall at the end. It looks like the code to calculate the time left in the track is wrong, as it returns a negative number. And if I muck with it to fix it, it at least loops.

However, even then, the loop starts at the end of the track (at the original offset) instead of returning to the beginning. I can set that to an offset of 0 when the loop triggers, and we get a lot closer. It restarts as expected, but there's an audible gap in the loop.

Apart from the audible gap, this approach makes me nervous that I'm breaking something else. It's really low-level stuff in the engine, and I don't want to introduce a new bug just to get a so-so fix to this one.

I tried a few other methods of fixing the loop from outside. Including writing my own timer based on track remaining to restart the loop manually. I still get a gap. And even if I trim the timer to activate a frame sooner, there's still an audible glitch/pop.

In the end, I decided to hack the problem away by avoiding the loop point entirely. Now, the game just chooses a chunk of the looping track to play that doesn't overlap the loop point. E.g. if the track is 10s long, and the game plays a 5s chunk, the chunk starts anywhere from 0-4s into the track, so it has a chance to finish before reaching the track's end.

In practice, this is slightly less variety in the looped track, but probably way less noticeable of a bug. Especially since the track is fairly ambient in nature.

This has probably been fixed in more recent releases of Haxe/OpenFL, but I'm not upgrading now. No way :) Too risky!

Anyway, I think we're done with all known bugs now. The next step is to continue testing to find some more. I also fired-up the Mac today to start prepping it for a Mac build. Let's see if I can get other platforms to compile.

Hopefully, barring any new bugs, a new build is coming next week!