Music Looping

I’m now going to ask a question that future me is going to thank me for:
Is there a way to only a certain section of music?

Because, you know, some musics have an intro which then has a looping part in it that does not include re-looping the intro, which would make no sense.
Would there be a way in Defold to be able to loop a certain part of music?

I would just use audacity to copy and trim the song to the loop you want.

2 Likes

Btw I do this more with soundeffects. For some reason many sound effect sites sell or host SFX that have repeated sounds, padded space, or over long play.

Example: On my shoot sound I downloaded it was 2 seconds long- trimming dead space down reduced to 0.7 seconds.

Try using FMOD to do this. It supports many features beyond what builtin Defold audio can currently do.

That’s cool and all, but I think I will stick to… another type of music looping using the “sound_done” function in Defold.
The problem is, I don’t really understand the entire complete_function part of sound.play
Can someone explain that to me?

The complete_function should be either an anonymous function or a reference to another function within the scope of the current script.

An anonymous function is like

function(self, message_id, message, sender)
  -- do something
end

This defines some code without yet running it. You’ll see it as useful concept in many other areas of the API.

So with play sound you can do (and note that it’s complete_function not complete_function() which would run the code immediately upon running the sound.play)

local function complete_function(self, message_id, message, sender)
 -- do something when sound ends
end

sound.play("#sound", { delay = 1, gain = 0.5, pan = -1.0 } , complete_function)

or

sound.play("#sound", { delay = 1, gain = 0.5, pan = -1.0 } , function (self, message_id, message, sender)
 -- do something when sound ends
end)

And then in either case that code can be ran after the sound completes playing.

You have another option to check the on_message for the script which called the sound.play() and there you should get the sound_done message when the sound stops playing.

5 Likes