Issues creating an Audio Fade system

4.4

I have recently started trying to implement audio into my project however while attempting to create a fading effect this code causes my program to stop responding. I believe I am probably doing something wrong with the arrays as this is my first time using them in godot at least.

This code is for fading in but i also hope to use a similar method to fade out

Any help would be greatly appreciated thanks!

This code:

    for i in fadequeue:
        fetchsong(fadequeue[i])

In that, i is an array element, not an index. So if fadequeue is an array of AudioStreamPlayer, i is an AudioStreamPlayer. You probably want:

    for i in fadequeue: # 'i' is an array element
        fetchsong(i)

Or:

    for i in fadequeue.size(): # 'i' is an index
        fetchsong(fadequeue[i])
1 Like

Thanks
this code extends a node 2d with audio stream players as children fetchsong() takes an integer which corresponds to a song and sets fetched song to the audio stream player for that song for example fetchsong(5) would set fetchedsong to $intro. ‘i’ is meant to be set as the node fetched from fetchsong()

I have updated my code to this

however I get this error

image

I marked over the numbers because I don’t know what they mean and long numbers are scary to put on the internet

fadequeue is an array, and should be indexed with integers. i is whatever was in that cell of the array, which in this case is an AudioStreamPlayer, so you’re trying to dereference an array with something that isn’t an integer, and that’s what the error says. Arrays are integer-indexed only.

Try:

func _process(_delta: float) -> void:
    if fading:
        for i: int in fadequeue.size():
            fadequeue[i].volume_db = lerpf(fadequeue[i].volume_db, apprachvolume[i], 0.5)

You’re going to get some kind of weird results from that, though. volume_db is logarithmic, and your fixed timevalue lerpf() is also going to be logarithmic, so your fade speed is going to be strange.

You probably want something more like:

const FADE_TIME: float =    2.0 # 2 seconds
const MIN_VOL:   float = -100.0 # Decibels are logarithmic.
const MAX_VOL:   float =    0.0

var fade_counter: float # Set this to 0.0 at fade start.

func _process(delta: float) -> void:
    if fading:
        fade_counter = clampf(fade_counter + delta, 0.0, FADE_TIME)
        var fade_level: float = fade_counter / FADE_TIME
        for snd: AudioStreamPlayer in fadequeue:
            snd.volume_db = lerpf(0.0, -100.0, fade_level)

That will give you predictable framerate-independent fades. You might want to consider switching to linear volume as well, depending on how comfortable you are with decibels.

1 Like

Thank you so much!
I modified it a bit to allow for many different fades happening at once and it seems all good!


This was the final code which allows for many different fades in or out

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.