How do I fade sound out (without plugin)

0 favourites
From the Asset Store
In this template the music plays without looping in your game
  • 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

  • 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?

  • 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.

  • Laurent - you're welcome <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    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.

  • i like this example. thanks for sharing!

  • 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 ?

  • 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!

  • 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 ?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Laurent

    Very kind of you to say so <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    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

  • 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.

  • Laurent

    I've updated the capx; now the speakers switch themselves off if the sprite is more than 300px away:

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

    I changed the audio tags so that they reference the UID of their speaker object instead of the tile - this prevents speakers playing the same sound from turning each other off.

    I added a distance parameter and on/off boolean to the speaker objects to control their state - this updates only every 0.1 sec to stop the checks eating up resources.

    If you run the capx in debug and click on the audio object you can follow the loops turning on and off as move towards/away from them.

  • mekonbekon

    You are amazing and so reactive ! .

    I discovered that Speaker objet part of the code is sufficient to make the soundscape engine to run. Tile map part of the code can be switched of. You achieved to create something wonderful with very few lines. I'm impressed

  • Laurent - you could still use the tilemap system for one-shot and non-fading loop effects, e.g. different environmental footstep sounds, rustling bushes as you pass through them etc - a combination of the two systems should create quite an immersive audioscape

  • mekonbekon

    Indeed !

    Sound scape is something I wanted to try for a long time.

    Thanks again for your kind help !

  • mekonbekon

    I was wondering about another way to design soundscape that could be an alteration of your first proposition. What about a hero sprite that is a listener surrounded by 4 speakers attached to it, moving close to it and emitting sound from the tiles they are on. That could help recreate panoramic sound. What do you think ?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)