Fade sound in game engine.

Hi,
I need a script that allow me to fade a sound( from 1-0) like 1-0.99-0.98 etc.
Is this possible to do it with logic bricks or py?

Controlling the volume with some variables would be just great!

Thank you!:yes:

Maybe try creating an empty with a 3D sound far away and have it move closer to the camera. But there is possibly a better way using python.

EDIT:

Create a delay sensor enable pulse triggering and set the frequency to 10 and the duration to 1100
and connect to this script.


import bge

cont = bge.logic.getCurrentController()

obj = cont.owner

sound = obj.actuators["Sound"]
sound.volume += 0.01

print (sound.volume)

On the same object add an always sensor and a sound actuator named “Sound” and set its volume to zero.

This script must be run on an always sensor with enabled true pulse or must be activated if the property has changed (property changed sensor).


# import GameLogic
import GameLogic
 
# get the controller
cont = GameLogic.getCurrentController()
 
# get owner
own = cont.owner
 
# get the actuator attached to the controller named slam
act = cont.actuators["slam"]
 
# set the volume with the property named volume
act.volume = own["volume"]

You need also a alwayssensor with enabled true pulse connected to a property actuator (ADD -0.01) to cout the property volume down.

Audaspace has a builtin functions…for fadein and out


fadein(start, length)
 Fades a factory in by raising the volume linearly in the given time interval.
     Parameters:

- <b>start</b> (<i>float</i>) – Time in seconds when the fading should start.
- <b>length</b> (<i>float</i>) – Time in seconds how long the fading should last.

   Returns:The created [Factory](http://www.blender.org/documentation/blender_python_api_2_59_0/aud.html#aud.Factory) object.
   Return type:[Factory](http://www.blender.org/documentation/blender_python_api_2_59_0/aud.html#aud.Factory)
       Before the fade starts it plays silence.
 
 fadeout(start, length)
 Fades a factory in by lowering the volume linearly in the given time interval.
     Parameters:

- <b>start</b> (<i>float</i>) – Time in seconds when the fading should start.
- <b>length</b> (<i>float</i>) – Time in seconds how long the fading should last.

   Returns:The created [Factory](http://www.blender.org/documentation/blender_python_api_2_59_0/aud.html#aud.Factory) object.
   Return type:[Factory](http://www.blender.org/documentation/blender_python_api_2_59_0/aud.html#aud.Factory)


A sample code would be (not tested)


import aud
device = aud.device()
s = aud.Factory(path_mp3)
s =s.fadein(3.6,10) # fadein(start,length)
dev = device.play(s)


minor demo of controlling realtime volume by myself:P
/uploads/default/original/3X/c/0/c08567e06e0f73e19e90e7ea42a4e6e41cc44ab3.blendd=1312561176

more info on http://www.blender.org/documentation/blender_python_api_2_59_0/aud.html

Thanks for scripts, but none of them worked…also, i am on 2.5

Every script is working and the are all running in the Blender 2.5. But this are example codes. You made no exact description how you like it. So we don’t know how you want to start, stop, fade in, fade out, with or without property and so on.

I think you have forgotten to activate the sound actuator with an always sensor. You can also extend JASGames code with cont.activate(act) and my code with cont.activate(act).

Also in both scripts are not catching values lower 0 and higher 1.
JASGames


if sound.volume &lt; 0.0
    sound.volume = 0.0
elif sound.volume &gt; 1.0
    sound.volume = 1.0

My


if own["volume"] &lt; 0.0
    own["volume"] = 0.0
elif own["volume"] &gt; 1.0
    own["volume"] = 1.0

If you want to pay again you must reset the volume to the start value.

For Karakasimov code you need to change path_mp3 to your path and filename and tweak the values a little bit.
s =s.fadein(0.0, 2.0) # for fade in
s =s.fadout(0.0, 2.0) # for fade out

Hi, I see this topic is old but I’m working on a “musicfader”, too.
What I know is with aud you can not pack soundfiles in your .blend, what might not be
that much of a problem, but it’s the reason why I decided to do it with Logikbricks+python.
It’s very easy to control the Sound Logicbrick via python but I’m having a hard time wraping my head around the python logic. what I have so far is a script that is controlled by a Indexnumber of a integer property in logiceditor. the idea behind this is to play a level music loop after colliding with a sensor and sending a message to the “music manager”.
right now I’m able to change music by changing property index ingame. the music fades in nicely and plays until I stop it or switch to another music. What have to be done is the fadeout of each music and that’s where Im stuck right now. I gues the scriptlogic must be rewitten for this purpose .
In case this helps somebody or anyone has ideas how to finish this scrip I drop the code below:

import bge
'''
DOC

you have one "music manager" in your level(if you use constant HUD put it there)
it can be any object
set up 3 properties:

SoundIndex  integer
last_index  integer
Volume      float

set an always sensor named "mySensor" and put it in a python controller with this
skript

combine the python controller with sound actuators named "music1" and "music2"

change property "SoundIndex"
'''
def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner
    
#### properties
    
    SI = own["SoundIndex"]
    LI = own["last_index"]
    VO = own["Volume"]

    
#### sensors

    sens = cont.sensors['mySensor']
    
#### actuators
    
    m1 = cont.actuators['music1']

    m2 = cont.actuators['music2']
    
#### DJ

    if SI == 1:
        
        if SI != LI:# make sure this happens only once
            own["Volume"] = 0# reset property for fadein/may be useless later
            cont.activate('music1')# activate actuator with soundfile
            m1.volume = VO# mute sound for fadein
            cont.deactivate('music2')
            own["last_index"] = 1
        elif VO <= 1:# rise volume to 1
            own["Volume"] += 0.01# set property(just a variable)
            m1.volume = VO# assign current volume


    elif SI == 2:
        if SI != LI:# make sure this happens only once
            own["Volume"] = 0# reset property for fadein/may be useless later
            cont.activate('music2')# activate actuator with soundfile
            m2.volume = VO# mute sound for fadein
            cont.deactivate('music1')
            own["last_index"] = 2
        elif VO <= 1:# rise volume to 1
            own["Volume"] += 0.01# set property(just a variable)
            m2.volume = VO# assign current volume

        
    elif SI == 0:
        if SI != LI:
            own["Volume"] = 0# reset property for fadein
            cont.deactivate('music1')
            cont.deactivate('music2')
            own["last_index"] = 0




main()

1 Like