Music Position

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
CoolerMudkip
Regular
Posts: 40
Joined: Tue Jul 24, 2018 9:15 pm
Completed: Memories~ Megami no hogo-sha [Demo]
Projects: Northward
Tumblr: CoolerMudkip
Github: ztc0611
itch: MorallyGay
Location: United States, East Coast
Discord: MorallyGay#5861
Contact:

Music Position

#1 Post by CoolerMudkip »

Is it possible to get the currently elapsed time into a track? For example, if the game is 45 seconds into the track, retrieve that information? I'm trying to use it in order to pause the music, play another short track, then resume the previous music where it left off. (An example of very similar behavior would be Ralsei's lullaby move from Deltarune.) I know of sounds, but I don't want them to both play at the same time.

A rough example of what I mean would be:

Code: Select all

play music "music1.ogg"
[some gameplay here]
$time = getmusictime
stop music
play music "music 2.ogg"
queue music "<from time>music1.ogg" 
Is this possible?
Any help would be appreciated, thanks!

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3791
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Music Position

#2 Post by Imperf3kt »

You could use renpy.music.get_pos()
https://www.renpy.org/doc/html/audio.ht ... ic.get_pos
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
CoolerMudkip
Regular
Posts: 40
Joined: Tue Jul 24, 2018 9:15 pm
Completed: Memories~ Megami no hogo-sha [Demo]
Projects: Northward
Tumblr: CoolerMudkip
Github: ztc0611
itch: MorallyGay
Location: United States, East Coast
Discord: MorallyGay#5861
Contact:

Re: Music Position

#3 Post by CoolerMudkip »

Imperf3kt wrote: Thu Mar 07, 2019 5:01 pm You could use renpy.music.get_pos()
https://www.renpy.org/doc/html/audio.ht ... ic.get_pos
Alright, how do I use that to resume the music then?
I did this:

Code: Select all

	$ battlemusic = renpy.music.get_pos(channel='music')
        play music "sounds/exp/jingle.ogg"
        pause 1.5
        play music "<from battlemusic>sounds/exp/random battle 1.ogg" loop

Code: Select all

play music "<from [battlemusic]>music.ogg" loop
and

Code: Select all

play music "<from battlemusic>music.ogg" loop
both error out.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3791
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Music Position

#4 Post by Imperf3kt »

I'm not sure. I'd have to look into it more to be able to help.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Music Position

#5 Post by Remix »

Would it not be better/nicer to define a second music channel, fade out and pause the main channel, play the second, then revert?

Code: Select all

init python:

    renpy.music.register_channel(name='music_2', mixer='music') # 2nd channel using same volume setting as music

label start:

    play music "music1.ogg"
    "..."
    $ renpy.music.set_volume(0.0, delay=0.5, channel='music') # 0.0 is based on the mixer volume
    $ renpy.music.set_pause(True, channel='music')
    play music_2 "sounds/exp/jingle.ogg"
    pause 3.0
    $ renpy.music.set_pause(False, channel='music')
    $ renpy.music.set_volume(1.0, delay=0.5, channel='music') # 1.0 is back to normal volume
    "..."
Frameworks & Scriptlets:

User avatar
CoolerMudkip
Regular
Posts: 40
Joined: Tue Jul 24, 2018 9:15 pm
Completed: Memories~ Megami no hogo-sha [Demo]
Projects: Northward
Tumblr: CoolerMudkip
Github: ztc0611
itch: MorallyGay
Location: United States, East Coast
Discord: MorallyGay#5861
Contact:

Re: Music Position

#6 Post by CoolerMudkip »

Remix wrote: Fri Mar 08, 2019 3:53 pm Would it not be better/nicer to define a second music channel, fade out and pause the main channel, play the second, then revert?

Code: Select all

init python:

    renpy.music.register_channel(name='music_2', mixer='music') # 2nd channel using same volume setting as music

label start:

    play music "music1.ogg"
    "..."
    $ renpy.music.set_volume(0.0, delay=0.5, channel='music') # 0.0 is based on the mixer volume
    $ renpy.music.set_pause(True, channel='music')
    play music_2 "sounds/exp/jingle.ogg"
    pause 3.0
    $ renpy.music.set_pause(False, channel='music')
    $ renpy.music.set_volume(1.0, delay=0.5, channel='music') # 1.0 is back to normal volume
    "..."

Will this lose the user set volume?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Music Position

#7 Post by Remix »

CoolerMudkip wrote: Fri Mar 08, 2019 4:11 pm Will this lose the user set volume?
Nope, the fade out and fade in value are as a float percentage of the mixer volume (as set in preferences), so 1.0 might be 30% if music is set to that and 0.0 will always be zero volume.

Alternatively, maybe just test it...
Frameworks & Scriptlets:

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: Music Position

#8 Post by IrinaLazareva »

CoolerMudkip wrote: Thu Mar 07, 2019 10:41 am Is it possible to get the currently elapsed time into a track? For example, if the game is 45 seconds into the track, retrieve that information? I'm trying to use it in order to pause the music, play another short track, then resume the previous music where it left off. (An example of very similar behavior would be Ralsei's lullaby move from Deltarune.) I know of sounds, but I don't want them to both play at the same time.

A rough example of what I mean would be:

Code: Select all

play music "music1.ogg"
[some gameplay here]
$time = getmusictime
stop music
play music "music2.ogg"
queue music "<from time>music1.ogg" 
Is this possible?
Any help would be appreciated, thanks!

Code: Select all

    play music 'music1.ogg'
    'some gameplay here'
    $ mytime = renpy.music.get_pos(channel='music')
    
    $ renpy.block_rollback()  # << this line is not necessary, but without it, there could be problems (If a player uses rollback)
    
    play music 'music 2.ogg'
    queue music "<from %d>music1.ogg" % mytime
https://renpy.org/doc/html/audio.html#partial-playback
https://docs.python.org/2/library/stdty ... operations
https://renpy.org/doc/html/save_load_ro ... k_rollback

Post Reply

Who is online

Users browsing this forum: Bing [Bot]