# Fading sounds with go.animate, kidnaps the sound (SOLVED)

**URL:** <https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857>\
**Category:** Questions\
**Created:** [October 13, 2022, 1:38am UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857 "2022-10-13T01:38:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Musenik](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/musenik/32/20573_2.png) [@Musenik](https://forum.defold.com/u/Musenik)\
**Post date:** [October 13, 2022, 1:38am UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/1 "2022-10-13T01:38:49Z")

</div>

This is just weird. I’m playing a sound and at some point before it ends, the code fades out the gain with go.animate. Afterwards, the sound cannot be played again.

Here is minimal code that I think should work, but doesn’t.

```lua

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("@render:", "use_fixed_fit_projection", { near = -1, far = 1 })
	
	sound.play("/music#waggs away", {gain=0.3})
	timer.delay(10, false, FadeOut)
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		print("Play again!")
		
		sound.stop("/music#waggs away")
	    sound.play("/music#waggs away", {gain=0.3})
	end
end

function FadeOut(self)
    go.animate("/music#waggs away", "gain", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_LINEAR, 6, 0, CleanUpAnimate)
end

function CleanUpAnimate(self)
		go.cancel_animations("/music#waggs away", 'gain')
		print('Click to play sound again.')
end

```

NOTE: Do not click the mouse until the sound’s first play is fully complete.

Also, notice that when the animate call is made, the sound’s volume jumps louder before fading.

---

<div class="post-metadata">

**Author:** ![Musenik](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/musenik/32/20573_2.png) [@Musenik](https://forum.defold.com/u/Musenik)\
**Post date:** [October 13, 2022, 1:47am UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/2 "2022-10-13T01:47:06Z")

</div>

Here’s a zip of the project.

[Bed for Tests.zip](https://forum.defold.com/uploads/short-url/ajtmNNjCUTKpNEGuroefiwb9vTy.zip) (2.8 MB)

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [October 13, 2022, 6:36am UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/3 "2022-10-13T06:36:22Z")

</div>

You’re not setting the gain on the individual voice, but the sound component.

---

<div class="post-metadata">

**Author:** ![Musenik](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/musenik/32/20573_2.png) [@Musenik](https://forum.defold.com/u/Musenik)\
**Post date:** [October 13, 2022, 12:58pm UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/4 "2022-10-13T12:58:00Z")

</div>

How does one address an individual voice?

I tried changing the address to “/music”, like some examples. It seems to work but I got an error:

ERROR:SCRIPT: attempt to concatenate a function value  
stack traceback:  
[C]: - 1: in function animate  
main/main.script:21: in function \<main/main.script:20\>

---

<div class="post-metadata">

**Author:** ![Musenik](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/musenik/32/20573_2.png) [@Musenik](https://forum.defold.com/u/Musenik)\
**Post date:** [October 13, 2022, 1:14pm UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/5 "2022-10-13T13:14:57Z")

</div>

In this example, it talks about setting the component’s gain. I’m so confused.

> **[Fade In-Out](https://defold.com/examples/sound/fade_in_out/)**
>
> This example shows how to make Fade-In and fade Fade-Out music.

---

<div class="post-metadata">

**Author:** ![anon95708182](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@anon95708182](https://forum.defold.com/u/anon95708182)\
**Post date:** [October 13, 2022, 2:17pm UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/6 "2022-10-13T14:17:56Z")

</div>

I was just as puzzled until I read the section about gain here:

> **[Gain - Sound in Defold](https://defold.com/manuals/sound/#gain)**
>
> Defold’s sound implementation is simple but powerful. There are only two concepts that you need to be aware of: | This manual explains how you bring sounds into your Defold project, play back and control them.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [October 13, 2022, 6:58pm UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/7 "2022-10-13T18:58:53Z")

</div>

Yeah, we have no way of setting the individual voice properties other than at the ”sound\_play()” time.  
Think of the component as the parent of the voice. If you alter its properties, it will be used for its children too.

---

<div class="post-metadata">

**Author:** ![Musenik](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/musenik/32/20573_2.png) [@Musenik](https://forum.defold.com/u/Musenik)\
**Post date:** [October 14, 2022, 3:31am UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/8 "2022-10-14T03:31:30Z")

</div>

so it looks like the solution is to use go.set to reset the gain for the component.

```lua
        go.set("/music#waggs away", "gain', 0.3)
	sound.stop("/music#waggs away")
	 sound.play("/music#waggs away", {gain=0.3})

```

So, a big part of my confusion is the inconsistency of what a URL means. The same URL can mean two different things in this case. I hope Defold’s workers realize how confusing that can be. I understand, that under the hood, they are always talking about a component, but effectively they cause different results. Because sounds are created like a factory made them. Whereas static components are always individual entities. Context sensitive URLs are causing this problem.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [October 14, 2022, 4:21am UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/9 "2022-10-14T04:21:21Z")

</div>

> [@Musenik](#):
>
> Because sounds are created like a factory made them. Whereas static components are always individual entities. Context sensitive URLs are causing this problem.

Sounds are not exactly like factories. Sounds are “play and forget”. A call to sound.play() will “create and play an instance of the sound component” (ie a voice). The difference from a factory is that once you have called sound.play() you have no direct control over the individual voice. It will play with the gain that you specified in the call to sound.play().

This means that you can have multiple instances of the same sound playing at the same time. With different gains.

And like you saw from the documentation the final gain of the sound depends not only on the gain of the “play and forget” sound instance but also on the overall gain on the component, the group and the master.

So when you use go.set() or go.animate() you modify the gain of the component, which in turn impacts the gain of any playing sound instances.

To me this is logical and easy to reason about, but perhaps this needs to be clarified further?

---

<div class="post-metadata">

**Author:** ![Musenik](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/musenik/32/20573_2.png) [@Musenik](https://forum.defold.com/u/Musenik)\
**Post date:** [October 14, 2022, 2:57pm UTC](https://forum.defold.com/t/fading-sounds-with-go-animate-kidnaps-the-sound-solved/71857/10 "2022-10-14T14:57:06Z")

</div>

That’s the best description of what’s going on that I’ve read so for. Thanks, @britzl. When I first wrote that sentence I used the word ‘similar’ instead of like. Factories and sound objects both spawn instances. That’s what I meant. The evidence for misunderstanding is these two lines of code.

```lua
go.set("/music#waggs away", "gain', 0.3)
sound.play("/music#waggs away", {gain=0.3})

```

The url is the same, but what gets set is different. I misunderstood a url to refer to an instance, which seems pretty easy to do when you’re dealing with other elements of the engine.
