Page 1 of 1

[Bug] End of music is cut off (spoilers)

Posted: Thu Mar 03, 2016 8:20 am
by minmay
Remember when you took long enough to beat the Lindworm that the music looped? And there was an ugly off-tempo jump in the music, like the music file hadn't been seamlessly looped?
Here's the thing. That file is seamlessly looped - like every other music file in the game. But the game itself doesn't loop music correctly.
From experimentation, it appears that the music loops correctly if the number of samples is divisible by 512. Otherwise, it loops too early. Here are some ogg files that offer illustrative examples.
A 441Hz sine wave for 100 samples. The file is almost a mathematically perfect, perfectly looping, sine wave, and you can confirm this for yourself by playing it on loop in Audacity or whatnot. But if you try it in-game, it becomes a much rougher "sine-saw" wave because many of the samples are cut off.
A 344.53125Hz sine wave for 512 samples. Like the previous file, this is an almost-perfect looping sine wave, but this one loops correctly in-game, because it is 512 samples. I've tried other miscellaneous numbers divisible by 512 and they seem to loop seamlessly here.
A 344.53125Hz sine wave for 256 samples. Again, essentially a perfect, looping sine wave, but this one doesn't loop correctly in-game because 256 is not divisible by 512.
A demonstration of why this can be a big problem for music in practice. This is a sequence of 8 tones, and is a total of 1 second (44100 samples) long. The tones have a little bit of silence before them except for the first one, which makes the loop point highly audible. If you try it in-game, the last tone will be cut off because 44100 is not divisible by 512.
You can confirm this by playing the track in-game and playing it on loop in Audacity (or whatever program) at the same time: because the game is looping it too soon, the two repeating tone sequences will be different lengths, resulting in them gradually weaving in and out of sync.
For a more dramatic demonstration of why this is a problem in practice...well, just listen to boss_fight_lindworm again. There's a really bad pop when it loops, which isn't present in the actual music file. Here it is.

Perhaps this has to do with the audio buffer size? Anyway, I hope this can be fixed because not only is it very obnoxious for dungeon makers (good loops are hard enough already, and because of this bug, now the overwhelming majority of possible loop points are unusable), it affects most of the standard game's music files too!

Re: [Bug] End of music is cut off (spoilers)

Posted: Thu Mar 03, 2016 8:52 am
by petri
Many thanks for the bug report! For some reason this bug has completely escaped our radar. Added to bugs list.

Re: [Bug] End of music is cut off (spoilers)

Posted: Sat May 13, 2017 6:00 am
by minmay
Update: My original test for this was not rigorous enough. It appears that samples around the loop point - both on the start and end of the stream - get cut off regardless of the number of samples in the stream, even when they are powers of 2. Basically, as far as I can tell, Vorbis audio will never loop seamlessly in Grimrock 2 (Windows version at least, haven't tried the Mac version). So there doesn't seem to be much point in trying to work around it with precise sample counts.

RIFF WAV streams still loop fine.