Endless Random Background Music with Seamless Cross-Fade

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
IrisColt
Newbie
Posts: 8
Joined: Tue Apr 09, 2024 8:11 am
Contact:

Endless Random Background Music with Seamless Cross-Fade

#1 Post 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!

IrisColt
Newbie
Posts: 8
Joined: Tue Apr 09, 2024 8:11 am
Contact:

Re: Endless Random Background Music with Seamless Cross-Fade

#2 Post 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

IrisColt
Newbie
Posts: 8
Joined: Tue Apr 09, 2024 8:11 am
Contact:

Re: Endless Random Background Music with Seamless Cross-Fade

#3 Post 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.
Attachments
stuttering.zip
(2.77 MiB) Downloaded 4 times

IrisColt
Newbie
Posts: 8
Joined: Tue Apr 09, 2024 8:11 am
Contact:

Re: Endless Random Background Music with Seamless Cross-Fade

#4 Post 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.

Post Reply

Who is online

Users browsing this forum: BBN_VN