Page 1 of 2

Delay when looping music

Posted: Sun Jul 08, 2012 1:19 am
by viking badger
Hi everyone, I've been working on little turn based game and I just recently got around to adding background music. The problem I'm running into is that there is a noticeable delay when the music ends and loops back to the beginning. It does this with LOVE's built in audio functions as well as with the TEsound library. Just to make sure something in my game wasn't causing the issue, I wrote up this little sandbox script:

Code: Select all

function love.load()
	require("tesound")
	
	TEsound.playLooping("music.mp3", "music")
	
	--music = love.audio.newSource("music.mp3", "stream")
	--music:setLooping(true)
	--love.audio.play(music)
	
	playing = true
end

function love.keypressed(key, unicode)
	if key == " " then
		if playing then
			TEsound.stop("music")
			--love.audio.stop(music)
			playing = false
		else
			TEsound.playLooping("music.mp3", "music")
			--love.audio.play(music)
			playing = true
		end
	end
end

function love.update(dt)
	TEsound.cleanup()
end
Unfortunately, the problem persists.

If anyone could shed some light on what might be causing this and possibly point me in the direction of a fix (or even a workaround), I would greatly appreciate it.

Thanks!

EDIT: Removed .love file. See Boolsheet's solution.

Re: Delay when looping music

Posted: Sun Jul 08, 2012 5:16 am
by Inny
Love uses libmodplug as it's audio backend. It's great, but the momentary delay at a loop point is common across everything that uses it.

Incidentally, the crew that maintains the Megazeux source code developed their own patches for libmodplug to get around that bug Link. modplug hasn't had any receptive maintainers in a long time, and you probably couldn't fix it yourself without building love from source and applying patches that may or may not work in your case.

I recommend ignoring it. Or if you compose your own music, account for whole rest you're being forced to take at the loop point.

Re: Delay when looping music

Posted: Sun Jul 08, 2012 5:45 am
by schme16
I too would like an aswer to this; I've been making a Pokemon Red clone and noticed that there is a delay when first starting and then when repeating.

Re: Delay when looping music

Posted: Sun Jul 08, 2012 8:14 am
by dreadkillz
It's a flaw in the the way LOVE handles audio. A proper player will loop gaplessly. I tested your sample in my foobar2000 player, and it loops perfectly. Unless you know how to mess with the source code, I don't think you can avoid this problem. :(

Re: Delay when looping music

Posted: Sun Jul 08, 2012 9:04 am
by dreadkillz
Sorry for double posting, but I will post a temporary 'solution' for looping music so future searchers will see this. You need to set up a check in update to allow manual looping. You will need the length of all your songs in seconds for this to work. You can incorporate this into TEsound if you're willing to mess with its code.

Code: Select all

function love.load()
	music = love.audio.newSource("music.mp3", "stream")
	music:play()
	t = 0
end

function love.update(dt)
	t = t + dt
	if t >= 56 then -- time passed since our 56 second long song
		music:play() -- force song to replay
		music:seek(t - 56,'seconds') -- jump to position
		t = t - 56
	end
end
removed .love due to licensing

Re: Delay when looping music

Posted: Sun Jul 08, 2012 9:19 am
by bartbes
That, or use anything but wav files.

Re: Delay when looping music

Posted: Sun Jul 08, 2012 4:40 pm
by Boolsheet
Let's not confuse viking badger. Here are the facts:
  • Yes, LÖVE uses libmodplug, but only for the module formats and WAVE.
  • Yes, libmodplug has a known problem of appending silence at the end of the decoded audio (but only for the module formats and WAVE).
  • No, LÖVE does not have a problem with handling audio (as long as the system is working as expected). foobar2000 does a crossfade of 200 ms or something.
  • Probing the playing state every frame is a bit clunky. Especially if you only have an approximate temporal resolution of 60 FPS/0.016 ms for the polling.
  • Yes, avoid .wav files for now.
OS X is showing some weird behaviour lately. We should hope it really isn't its OpenAL implementation. Can you tell us if you're getting the issue on OS X?

And now to the actual problem. Your mp3 has a few milliseconds of silence at the beginning and the end. It's very likely this is the delay you hear. Use an audio editor to check if it properly loops. Audacity is a simple and free editor that has a looping option.

Here is a .love with an edited mp3. Does this sound better or do you still hear it?
file deleted

Btw. if you think the stereo is not as pronounced as it should be (on Linux and Windows) that will be fixed in the next version of LÖVE. It has to do with the new OpenAL Soft version.

Edit: There come also licensing issues to mind if you distribute a file like this. I will delete my .love once this is solved. If you don't have a license, you should do it too just out of respect of the author. Disregard this if you are the author. :P

Re: Delay when looping music

Posted: Sun Jul 08, 2012 5:57 pm
by viking badger
Boolsheet wrote: Here is a .love with an edited mp3. Does this sound better or do you still hear it?
That sounds much better! I have a little experience with Audacity, so I'll play around with that. Fortunately, the license does allow modification to the sound file as long as I don't change the overall composition of the music. Thanks everyone for your help. I'll post later to let everyone know how Boolsheet's solution works.

According the the music license, I'm not supposed to distribute the music file, so I'll be removing the .love file now, and I would appreciate it If anyone who downloaded it could please dispose of the music file. Thanks again!

Re: Delay when looping music

Posted: Sun Jul 08, 2012 6:34 pm
by viking badger
Just got done chopping the ends of the sound file a little bit and it sounds much better now. Not flawless, yet, but with a little more tweaking, it definitely could be. Thanks Boolsheet!

Re: Delay when looping music

Posted: Sun Jul 08, 2012 8:57 pm
by dreadkillz
After browsing the Hydrogen Audio forum to read on gapless playback, the issue depends on how the music was encoded, and the decoder used in the playback software.

Lame mp3's have tags that tell how much padding a song has. A proper mp3 player will respect the tag and create a seamless loop. So mp3's are pretty much a nightmare because they have to be properly encoded with the proper tags and then the player has to be able to decode the mp3's properly with those tags. It gets even worse when different encoders with their own tagging conventions are used for the same format like Itune vs LAME mp3's.

EDIT: Ogg vorbis apparently encodes gaplessly without using a stupid hack (tagging system). That takes care of the encoding part; the onus is then on how the playback software handle song transitions and decode the file.