Page 1 of 1

Endless Random Background Music with Seamless Cross-Fade

Posted: Tue Apr 09, 2024 8:37 am
by IrisColt
Could someone please assist me with setting up background music for my Ren'Py game?

In my game, I require background music that transitions smoothly between tracks. The music setup entails playing a song and seamlessly transitioning to the next song (cross-fade) before the current one ends. The next song should be randomly selected from a pool of available songs. I aim to establish the background music logic to operate autonomously in the background without requiring constant intervention. This means the cross-fade to the next song should occur automatically and independently from the ongoing events in the game.

Do you have any tips or strategies for accomplishing my goals? Thanks in advance for any assistance!

Re: Endless Random Background Music with Seamless Cross-Fade

Posted: Wed Apr 10, 2024 10:35 am
by IrisColt
I stumbled upon a code snippet for random sound playback in Ren'Py in viewtopic.php?p=453546#p453546.

However, I've encountered an unexpected hiccup: audio stuttering, particularly noticeable when dealing with larger sound files.

As it stands, due to the severe audio stuttering issue, Ren'Py proves ineffective for my project goals. Is this a known issue?

Code: Select all

define sounds = ['sounds/explosion.wav', 'sounds/glass.wav', 'sounds/shot.wav']

init python:
    def s_callback():
        if s_callback.queue_silence:
            silence = "<silence {0}>".format(renpy.random.random() * 2 + 0.5)
            renpy.sound.queue(silence)
        else:
            renpy.sound.set_volume(renpy.random.random())
            renpy.sound.queue(renpy.random.choice(sounds))
        s_callback.queue_silence = not s_callback.queue_silence
        
    s_callback.queue_silence = False
    
    renpy.sound.set_queue_empty_callback(s_callback)
    

label start:
    "Just sit here and listen to the sounds"
    return

Re: Endless Random Background Music with Seamless Cross-Fade

Posted: Wed Apr 10, 2024 10:40 am
by IrisColt
I attach a recording of the audio stuttering for reference. The audio stuttering becomes more noticeable, especially during the third sound, the explosion.

Re: Endless Random Background Music with Seamless Cross-Fade

Posted: Wed Apr 10, 2024 11:09 am
by IrisColt
The code below does not stutter.

Code: Select all

init python:
    sounds = ["audio_split_p48/part{}.wav".format(i) for i in range(1, 48)]

init python:
    def s_callback():
        renpy.sound.queue(renpy.random.choice(sounds))    
    
    renpy.sound.set_queue_empty_callback(s_callback)  

label start:
    "Just sit here and listen to the sounds"
    
    return
I couldn't tell you why my version seems to function differently, but it's curious that I didn't even require the original "silence generator" in the process.

Now, onto the realm of randomness, as I tackle the task of infusing some unpredictability into the song selector.