A small python function for crossfading music
Sometimes you want to change the time of day or move from one location to the next and smooothly switch music, but you don’t want to force the player to go through specified transition durations. Chances are, faster readers will skip through transitions before your specified music fades can complete, resulting in a slightly jarring experience where fading out music gets interrupted.
Here’s a crossfade for switching music that won’t be interrupted if the player reads too fast.
## example script play music happy \$ cf(sad_intro, sad_loop, 10, 30) "someone is happy!" "now someone will be sad in a few seconds" ## end exampmle # the `music` channel is already registered by default. We # just need a second music channel to be able to crossfade # between renpy.music.register_channel("music2", mixer="music", loop=True, stop_on_mute=True, tight=False, file_prefix='', file_suffix='', buffer_queue=True) def cf(track_new, track_new_loop=None, curr_time=4.2,new_time=4.2): curr_track = "music" x_fade_track = "music2" do_loop = True if track_new_loop is not None: do_loop = False if not renpy.music.is_playing("music"): x_fade_track = "music" curr_track = "music2" renpy.music.stop(curr_track,fadeout=curr_time) renpy.music.play(track_new,channel=x_fade_track,fadein=new_time,loop=do_loop) if not do_loop: renpy.music.queue(track_new_loop,channel=x_fade_track,loop=True)
We use two tracks to fade between:
music2. Most of the time, we want to fade between a loop and an [intro + loop], which is why
cf() can take two tracks as arguments. If we only want to fade from a [loop] to a [track], then we can do that too.
And the corresponding script in the example:
play music town_day_loop pause 6 $ cf(prairie_day_intro,prairie_day_loop,10,20)
Questions or Comments?
Feel free to send in any AG-related questions! Our Ask Box is always open.
Thanks so much for all of your amazing support, and stay safe out there!