Delay when looping music

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
viking badger
Prole
Posts: 19
Joined: Sun Jul 08, 2012 1:00 am
Location: Denton, TX

Delay when looping music

Post by viking badger » Sun Jul 08, 2012 1:19 am

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.
Last edited by viking badger on Sun Jul 08, 2012 5:58 pm, edited 1 time in total.

User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Delay when looping music

Post by Inny » Sun Jul 08, 2012 5:16 am

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.

User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Delay when looping music

Post by schme16 » Sun Jul 08, 2012 5:45 am

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.
My Development Diary - http://shanegadsby.info

User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Delay when looping music

Post by dreadkillz » Sun Jul 08, 2012 8:14 am

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. :(

User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Delay when looping music

Post by dreadkillz » Sun Jul 08, 2012 9:04 am

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
Last edited by dreadkillz on Sun Jul 08, 2012 8:34 pm, edited 1 time in total.

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Delay when looping music

Post by bartbes » Sun Jul 08, 2012 9:19 am

That, or use anything but wav files.

User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Delay when looping music

Post by Boolsheet » Sun Jul 08, 2012 4:40 pm

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
Last edited by Boolsheet on Sun Jul 08, 2012 6:21 pm, edited 1 time in total.
Shallow indentations.

User avatar
viking badger
Prole
Posts: 19
Joined: Sun Jul 08, 2012 1:00 am
Location: Denton, TX

Re: Delay when looping music

Post by viking badger » Sun Jul 08, 2012 5:57 pm

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!

User avatar
viking badger
Prole
Posts: 19
Joined: Sun Jul 08, 2012 1:00 am
Location: Denton, TX

Re: Delay when looping music

Post by viking badger » Sun Jul 08, 2012 6:34 pm

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!

User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Delay when looping music

Post by dreadkillz » Sun Jul 08, 2012 8:57 pm

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.
Last edited by dreadkillz on Sun Jul 08, 2012 9:28 pm, edited 2 times in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests