Sound fade in and fade out (SOLVED)

Hello all.

So i was looking the sound api to see how i could do a fadeout

I see i need to create it myzelf with a loop that degrees the gain over time.

I was thinking.
Would i be posible to make a api for it.
Sound.play_fadein(URL, duration)
Sound.stop_fadeout(URL, duration)

But i really dont know where to start to contribute this.

Can someone point me in the right direction?

1 Like

You can just use go.animate to increase the gain property:

local url = "#my_sound"
local duration = 2
sound.play(url)
sound.set_gain(url, 0)
go.animate(url, "gain", go.PLAYBACK_ONCE_FORWARD, 1, go.EASING_INOUTSINE, duration)
6 Likes

Also, check out the example:

If you are thinking about contributing and adding a new API - at first create an issue on Github and discuss it with Defold team :wink:

I think there should rather be something like:

sound.animate() to animate properties like “pan”, “gain” or “speed” maybe?

Or a cursor exposed as for sprites.

2 Likes

Thanks to the both of you.

I will have a look into this.

We are very reluctant to add these types of API functions when the same can be achieved using go.animate(). Every additional API function increases the engine size a little bit. Each API function also means more to maintain. More to document. More to consider when making changes.

A generic go.animate() to animate component properties is almost as good or perhaps even better than specific functions. Less code. A single API to learn.

The same goes for go.get() and go.set() compared to specific functions.

6 Likes

Thanks

The realization that go.animate also is for components inside the go is kind of a wow that is great.

And yeh knowing this an API wont add any value

1 Like

Yeah, go.animate() can be used on any script or component property, as well as on the transform of a game object. Very versatile!

1 Like

@britzl @Pawel @Halfstar

Thanks

I got it working now.
I do need to find a better sound

local function engine (a, self)
	--print(self.esound)
	if a == "on" and self.esound == false then
		local url = "sounds#engine"
		local duration = 0.1
		go.set(url, "gain", 0.3)
		sound.play(url)
		--go.animate(url, "gain", go.PLAYBACK_ONCE_FORWARD, 0.3, go.EASING_INOUTSINE, duration)
		self.esound = true 
	end
	if a == "off" and self.esound == true then
		local url = "sounds#engine"
		local duration = 0.5
		go.animate(url, "gain", go.PLAYBACK_ONCE_FORWARD, 0.0, go.EASING_INOUTSINE, duration)
		timer.delay(0.5, false,function() self.esound = false, sound.stop("/sounds#engine") end) 
	end

2 Likes