set_queue_empty_callback behavior

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:

set_queue_empty_callback behavior

#1 Post by IrisColt »

Hello, with the code below I'm hearing random music abruptly starting and stopping repeatedly (note fadein=10).

Code: Select all

init python:

    renpy.music.register_channel("music1", mixer="music", loop=False, stop_on_mute=True, tight=True, file_prefix='', file_suffix='', buffer_queue=True) 
    sounds = ["audio/audio{}.wav".format(i) for i in range(1, 3)]

    def m1_callback():
        renpy.music.play(renpy.random.choice(sounds), channel="music1", fadein=10)
    
    renpy.music.set_queue_empty_callback(m1_callback)

label start:
    "Just sit here and listen to the sounds"
    return
However the code below works correctly, that is, the random tracks fadeout for 10 seconds in succession (starting at full volume). The only change is "fadeout=10".

Code: Select all

init python:

    renpy.music.register_channel("music1", mixer="music", loop=False, stop_on_mute=True, tight=True, file_prefix='', file_suffix='', buffer_queue=True) 
    sounds = ["audio/audio{}.wav".format(i) for i in range(1, 3)]

    def m1_callback():
        renpy.music.play(renpy.random.choice(sounds), channel="music1", fadeout=10)
    
    renpy.music.set_queue_empty_callback(m1_callback)

label start:
    "Just sit here and listen to the sounds"
    return
Any help will be very much appreciated!

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2410
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: set_queue_empty_callback behavior

#2 Post by Ocelot »

Note that empty queue callback is called when the queue becomes empty, i. e. when the last song starts playing, not when it finishes.
https://www.renpy.org/doc/html/audio.ht ... y_callback
renpy.music.set_queue_empty_callback(callback, channel='music')

This sets a callback that is called when the queue is empty. This callback is called when the queue first becomes empty, and at least once per interaction while the queue is empty.

The callback is called with no parameters. It can queue sounds by calling renpy.music.queue with the appropriate arguments. Please note that the callback may be called while a sound is playing, as long as a queue slot is empty.
In your first example you immideately start playing a single track (with fadein) so queue becomes empty instantly and empty queue callback is called again.

In your second example currently playing track is set to stop (with fadeout) and a new track is placed in queue. When fadeout finishes, new track starts playing, queue becomes empty and callback is called, causing fadeout etc.
< < insert Rick Cook quote here > >

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

Re: set_queue_empty_callback behavior

#3 Post by IrisColt »

Ocelot wrote: Mon Apr 15, 2024 2:01 pm In your first example you immideately start playing a single track (with fadein) so queue becomes empty instantly and empty queue callback is called again.

In your second example currently playing track is set to stop (with fadeout) and a new track is placed in queue. When fadeout finishes, new track starts playing, queue becomes empty and callback is called, causing fadeout etc.
Thanks for your invaluable insight!

How do I manage to smoothly handle fading in a track, playing it for several seconds, fading it out, and then repeating this process again and again? I'm at a loss.

Thanks again!

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2410
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: set_queue_empty_callback behavior

#4 Post by Ocelot »

YOu almost got it, but instead of playing sound in callback, queue it to play right after current sound instead.
< < insert Rick Cook quote here > >

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

Re: set_queue_empty_callback behavior

#5 Post by IrisColt »

Ocelot wrote: Tue Apr 16, 2024 2:04 pm YOu almost got it, but instead of playing sound in callback, queue it to play right after current sound instead.
Thanks for your answer. Accordingly, my new approach is pasted below. Now music fades in, new music is queued, but the music never fades out (it plays for the configured time and then the music is quickly interrupted instead of fading out, just before playing the next file in the queue).

What am I doing wrong? I am truly desperate.

Thanks in advance.

Code: Select all

init python:

    renpy.music.register_channel("music1", mixer="music", loop=False, stop_on_mute=True, tight=True, file_prefix='', file_suffix='', buffer_queue=True) 
    sounds = ["audio/audio{}.wav".format(i) for i in range(1, 3)]

    def init_music1():
        renpy.music.play("<from 0 to 12>" + renpy.random.choice(sounds), channel='music1', loop=False, fadeout=5, fadein=5) 
        renpy.music.queue("<from 0 to 12>" + renpy.random.choice(sounds), channel='music1', loop=False, fadein=5)
        
    def m1_callback():
        renpy.music.queue("<from 0 to 12>" + renpy.random.choice(sounds), channel='music1', loop=False, fadein=5)
    
    renpy.music.set_queue_empty_callback(m1_callback, channel="music1")

label start:

    python:
        init_music1()

    "Just sit here and listen to the sounds"
    return

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2410
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: set_queue_empty_callback behavior

#6 Post by Ocelot »

YOu don't set any fadeout on any tracks aside from the first one.
< < insert Rick Cook quote here > >

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

Re: set_queue_empty_callback behavior

#7 Post by IrisColt »

Ocelot wrote: Sun Apr 21, 2024 7:10 am YOu don't set any fadeout on any tracks aside from the first one.
Initially, I anticipated finding a 'fadeout' parameter within renpy.music.queue. However, upon reviewing the documentation, I discovered that such a parameter was not present. https://www.renpy.org/doc/html/audio.ht ... usic.queue

There is only 'fadein' there. Did I understand your point accurately?

Code: Select all

renpy.music.queue(filenames, channel='music', loop=None, clear_queue=True, fadein=0, tight=None, relative_volume=1.0)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], White Phantom Games