Pick Random Music Loop each time [Urgent]

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
User avatar
csky
Regular
Posts: 93
Joined: Sun Sep 14, 2014 3:46 pm
Completed: Perceptions of the Dead, Embers of Magic
Projects: Various Commissioned Artwork
Contact:

Pick Random Music Loop each time [Urgent]

#1 Post by csky »

Hi there, I have an urgent problem I've been trying to solve and I can't seem to figure it out.
I need this solved in about a few hours from now, but even if this thread is not replied by then it will still be useful.

Pretty much what I've got going is two music channels. The 'music' channel is playing a rhythmic beat that continuously plays and won't need to be touched. (This is working fine so it's not a problem)

On the other hand I have another channel "Chan1" needing to pick one of three loops randomly and play it, then pick another loop randomly and play it, and loop that forever ect.

I've been reading the documentation and past renpy questions about similar things, and I managed to find this code here that I tried.

Code: Select all

$ randomswamp = renpy.random.choice( ("music/swamp silence.ogg", "music/swamp pan1.ogg", "music/swamp pan2") )
$ renpy.music.play (randomswamp, channel="Chan1", if_changed=True, loop=True, fadein=0, tight=True)
The problem is, it selects one of the three tracks randomly and then ONLY loops that one track forever. It doesn't go back and re-pick another one randomly when it loops, which is what I want it to do.
To summarize, I want it so it picks a new random track every loop.

I know that with images you can define animation loops and use the 'repeat' command, but I can't figure out how to do this with music. My ability with code is very simple, and nothing in the renpy documentation music page seems to cover what I want to do.

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Pick Random Music Loop each time [Urgent]

#2 Post by Zetsubou »

What you want is renpy.music.set_queue_empty_callback
http://www.renpy.org/doc/html/audio.htm ... y_callback
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
csky
Regular
Posts: 93
Joined: Sun Sep 14, 2014 3:46 pm
Completed: Perceptions of the Dead, Embers of Magic
Projects: Various Commissioned Artwork
Contact:

Re: Pick Random Music Loop each time [Urgent]

#3 Post by csky »

Zetsubou wrote:What you want is renpy.music.set_queue_empty_callback
http://www.renpy.org/doc/html/audio.htm ... y_callback
Okay, that's a start, I suppose. I had a look, but I have no idea what to do with it. It sounds complex and I have no clue how it works or where to start.
It doesn't have any examples so I'm still clueless.

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Pick Random Music Loop each time [Urgent]

#4 Post by Zetsubou »

The idea is that set_queue_empty_callback will call a function whenever the queue for that channel is empty.
So using your code:

Code: Select all

init python:
    def queueChanCallback():
        randomswamp = renpy.random.choice( ("music/swamp silence.ogg", "music/swamp pan1.ogg", "music/swamp pan2") )
        renpy.music.play (randomswamp, channel="Chan1", if_changed=True, loop=False, fadein=0, tight=True)
    renpy.music.set_queue_empty_callback(queueChanCallback, channel='Chan1')

#... other code ...

label start:
    #game stuff...
    $ queueChanCallback()
Once queueChanCallback is called in the start label, it runs your code once.
Then, when the queue finishes, the callback says to run the function again, and thus runs the random choice method again.

^All untested. I've never used this function before. I'm just going by the docs. You may need to use queue instead of play.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
csky
Regular
Posts: 93
Joined: Sun Sep 14, 2014 3:46 pm
Completed: Perceptions of the Dead, Embers of Magic
Projects: Various Commissioned Artwork
Contact:

Re: Pick Random Music Loop each time [Urgent]

#5 Post by csky »

Thanks man, I managed to get it working now, I did have to swap for queue instead of play.
Last edited by csky on Thu Nov 05, 2015 8:11 pm, edited 1 time in total.

User avatar
csky
Regular
Posts: 93
Joined: Sun Sep 14, 2014 3:46 pm
Completed: Perceptions of the Dead, Embers of Magic
Projects: Various Commissioned Artwork
Contact:

Re: Pick Random Music Loop each time [Urgent]

#6 Post by csky »

Oh actually, I take that back. The loop is working fine, but it won't stop when I use stop Chan1 or renpy.music.stop(

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Pick Random Music Loop each time [Urgent]

#7 Post by Zetsubou »

Hm, you could probably get away with using a variable to check if it should loop.

Code: Select all

init python:
    continueLooping = True

    def queueChanCallback():
        if continueLooping:
            randomswamp = renpy.random.choice( ("music/swamp silence.ogg", "music/swamp pan1.ogg", "music/swamp pan2") )
            renpy.music.queue (randomswamp, channel="Chan1", if_changed=True, loop=False, fadein=0, tight=True)
    renpy.music.set_queue_empty_callback(queueChanCallback, channel='Chan1')

#... other code ...

label start:
    #start music
    $ queueChanCallback()
    
    #Now lets stop the music
    $ continueLooping = False
    stop music

    #start music again
    $ continueLooping = True
    $ queueChanCallback()
Once again, untested ;)
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
csky
Regular
Posts: 93
Joined: Sun Sep 14, 2014 3:46 pm
Completed: Perceptions of the Dead, Embers of Magic
Projects: Various Commissioned Artwork
Contact:

Re: Pick Random Music Loop each time [Urgent]

#8 Post by csky »

Thanks. I'll try again later. For now I did a temporary fix where I just made a second call queue and had all the files as 'silence' to replace the other one.

But I'll try figure out the proper way in the long term.

crimsonnight
Veteran
Posts: 298
Joined: Fri Apr 20, 2012 4:44 am
Contact:

Re: Pick Random Music Loop each time [Urgent]

#9 Post by crimsonnight »

Zetsubou wrote:Hm, you could probably get away with using a variable to check if it should loop.

Code: Select all

init python:
    continueLooping = True

    def queueChanCallback():
        if continueLooping:
            randomswamp = renpy.random.choice( ("music/swamp silence.ogg", "music/swamp pan1.ogg", "music/swamp pan2") )
            renpy.music.queue (randomswamp, channel="Chan1", if_changed=True, loop=False, fadein=0, tight=True)
    renpy.music.set_queue_empty_callback(queueChanCallback, channel='Chan1')

#... other code ...

label start:
    #start music
    $ queueChanCallback()
    
    #Now lets stop the music
    $ continueLooping = False
    stop music

    #start music again
    $ continueLooping = True
    $ queueChanCallback()
Once again, untested ;)
Thanks for the code. How can I play a track before executing it though? At the moment, the tracks play wherever I write the init code, making the 'play' command redundant.

PS 'stop music' doesn't work for me, but stopping the created channel does.

This is the code that works for me (tracks play on the title screen):

Code: Select all

init python:
    continueLooping = True

    def titlemusic():
        if continueLooping:
            randomswamp = renpy.random.choice( ("music/2.ogg", "music/3.ogg", "music/4.ogg", "music/5.ogg") )
            renpy.music.queue (randomswamp, channel="titletrack", loop=False, fadein=0, tight=True)
    renpy.music.set_queue_empty_callback(titlemusic, channel='titletrack')

# The game starts here.

label start:

    #Now lets stop the music
    $ continueLooping = False
    stop titletrack
   
and I want to play 'music/1.ogg' before the others start looping.
alwaysthesamebluesky.com

Post Reply

Who is online

Users browsing this forum: Ocelot, VESTED