How do I fade sound out (without plugin)

Get help using Construct 2

Post » Sat May 20, 2017 8:43 am

Hi to all !
I'm building an app in which each tile of a tile map triggers a sound when the user drags a sprite on it.
I' managed to trigger the sound and let it play as long as the sprite is over a given tile but I would need the sound to fade out and stop when the sprite leaves the tile.
How can I do that ?

My second goal is to have a single function to make this automatic, a single function for every tile.
Any idea on how to do that ?
Thanks !
Laurent

Here's a cap of the code I have so far
https://www.dropbox.com/s/6njx2wzt5dg1j ... .37.27.png
B
33
S
14
G
5
Posts: 232
Reputation: 5,557

Post » Sat May 20, 2017 10:43 am

@Laurent

I think I can help with the first part of your question; here's a capx based on your code:

https://www.dropbox.com/s/kwh2ci35iikao ... .capx?dl=0

I'm still considering the second part - do you mean that every tile type plays a different sound when the sprite moves over it? If so, should the sound continue to loop if the sprite moves over a group of the same tile type or start afresh for each tile in the group?
B
19
S
8
G
3
Posts: 423
Reputation: 3,964

Post » Sat May 20, 2017 10:52 am

@mekonbekon
Thanks !
This looks awesome !
Yes, the idea is this : every type style plays a different sound. Sound keeps playing the same when you go from a tile type to the same tile type and fades when you go to a different tile type. The loops I'm playing are about 30 s long (rain, wind in the trees...)
The point is to make a "sound landscape" that evolves when the player moves.
B
33
S
14
G
5
Posts: 232
Reputation: 5,557

Post » Sat May 20, 2017 4:09 pm

@Laurent - you're welcome :)

I've updated the capx to provide a solution for multiple tile sounds:

https://www.dropbox.com/s/kwh2ci35iikao ... .capx?dl=0

There's no function, instead I use an array:

There are 3 global volume variables. These control (1) the volume for the currently occupied tile type, (2) the volume for the previously occupied tile type, and (3) the volume for the type before that.

Each time the sprite walks onto a new tile type a) Sprite.onTile is set to the tileNo, and b) the tileNo is pushed to the back of the array. This means that in the array we have a record of the order that the tile types have been stepped on - we can use this to determine which sounds to start, fade in, fade out and stop. By counting back from the end of the array we can identify the current and last two sounds played and apply the corresponding volume variables.

The sound file is then called by name - each sound filename is suffixed with the corresponding tileNo so if you walk onto tile 1 for example, you can play "sound_1" by calling "sound_"&Sprite.onTile".

The current volumes are then shifted one along: 2 to 3, 1 to 2 and 0 to 1, then volume 0 is reset to -40. This is so they correspond to the new positions in the array of the currently playing sounds.

The loop volume for the currently stepped on tile types fades in from -40 to 0.
The loop volumes for the previously stepped on tile types are gradually faded out - if the tag of a loop is more than 2 behind the newest tag then the sound is stopped.
B
19
S
8
G
3
Posts: 423
Reputation: 3,964

Post » Sat May 20, 2017 9:26 pm

i like this example. thanks for sharing!
B
80
S
29
G
112
Posts: 195
Reputation: 62,035

Post » Sun May 21, 2017 7:05 am

@mekonbekon
That's a really smart way of achieving this !
Thanks so much !
I was wondering, instead of fading previous sound and starting new one once previous has stopped, how can we do a cross fade so the sound level heard would remain stable ?
What about "fade=0.5" variable ? Is it fading rate ?
B
33
S
14
G
5
Posts: 232
Reputation: 5,557

Post » Sun May 21, 2017 8:17 am

@Laurent
No problem :)

Yes, you can adjust the fade value to change the rate - a lower value results in a slower fade with more overlap. Note that you won't get the sound level remaining consistent with this system, but you should be able to get pretty close.

This system doesn't limit the number of sounds you can have, you just import a new sound, making sure the filename ends with the corresponding tileNo.
and it should work automatically without any code updates.

If you want to increase the number of loops that can play concurrently, add another "volume" global, stepping up the final number, and duplicate event 4, adjusting the values in the new event and the final event to account for the extra variable.

One way to get truly accurate environmental fades would be to add a "speaker" sprite to each of the different tile groups on the tilemap, tag it to the group using an instance variable and then start playing the sound for that tile group (using the "play at object" audio action) when the player enters the group. This solution would automatically adjust the volume based on the player's distance to the speaker object. This works okay if you build the maps in advance, but becomes trickier to implement if you are procedurally generating the tilemap, as you'd have to come up with a method for determining the approximate centre of a group of tiles. Not impossible though! ;)
B
19
S
8
G
3
Posts: 423
Reputation: 3,964

Post » Sun May 21, 2017 3:41 pm

@mekonbekon
Your pedagogical talents are astonishing !
Your last suggestions about the speaker option sounds very interesting and absolutely what I may need for my project . I can't really get how you would do that. I mean, I understand the logic but am not skilled enough to implement it. Any (additional) help would be great !
About this method, BTW, can we instead imagine the sprite being a Listener and activating sound of the 8 adjacent tiles around it ?
B
33
S
14
G
5
Posts: 232
Reputation: 5,557

Post » Sun May 21, 2017 5:56 pm

@Laurent
Very kind of you to say so :)

I've updated the capx to include an example of speaker objects:

https://www.dropbox.com/s/kwh2ci35iikao ... .capx?dl=0

I created a Speaker object and added one on top of each group of tiles.
On start, I set the Sprite to be a listener object, set each Speaker.TileNo to the tileNo the Speaker is sitting on. and being playing the corresponding loop at that object.
This means that if there are a lot of speakers in the level you could end up with loads of sounds triggering at the same time on start. Most of them should be playing quietly, but be warned!

The next thing I did was fiddle around with the audio object's "Positioned audio" properties; I still haven't quite got my head around how the listener Z height, reference distance, maximum distance and roll-off factor interrelate - have a play with them and see what shakes. It can be easier to test this with only two Speaker objects.

You can find out more info in this section in the manual:

https://www.scirra.com/manual/109/audio
B
19
S
8
G
3
Posts: 423
Reputation: 3,964

Post » Sun May 21, 2017 10:42 pm

@mekonbekon
I get it ! It seems to be working.
My first attempt with this soundscape thing was to use positioned audio. My scene had almost 30 different sound sources with a moveable listener but this (very well working) system needed to much ressources. I was thinking about starting to play the different sound sources when the listener sprite was entering their "sphere of influence", stoping sources that are to far to be heard anyway and thus preserving CPU ressources.
But I was not able to make this system to work.
B
33
S
14
G
5
Posts: 232
Reputation: 5,557

Next

Return to How do I....?

Who is online

Users browsing this forum: blue visionary and 34 guests