[Solved] Issue with renpy.music.get_duration() and syncing lip flaps

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
filthsama
Newbie
Posts: 18
Joined: Tue Jan 05, 2021 4:11 am
Completed: 0
Projects: A dating sim with some Persona mechanics
Contact:

[Solved] Issue with renpy.music.get_duration() and syncing lip flaps

#1 Post by filthsama »

I'm trying to make a shop screen with a character on the side that randomly talks to you, and his lips will flap when a voice clip is playing. Problem is that I can't seem to get the duration of the voice clip within the screen. I've been doing it like this:

Code: Select all

screen shop:
	## just using a key to test if this idea would work, which it doesn't
        key "o" action [SetVariable('side_speaking',True)]

        if side_speaking == True:
            $ renpy.play('voiceclip.wav', channel='voice')

            $ line_duration = renpy.music.get_duration(channel="voice")
            
            add "shop guy speaking"
            timer line_duration action SetVariable('side_speaking',False)
        else:
            add "shop guy"
Apparently, Renpy is just returning 0.0 for the duration which is... odd. If I set the channel to "music", it works perfectly fine with the BGM I already have playing. I've also checked if it's the voice file itself, which it isn't.
Last edited by filthsama on Wed Jul 06, 2022 12:51 am, edited 1 time in total.

User avatar
m_from_space
Miko-Class Veteran
Posts: 957
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Issue with renpy.music.get_duration() and syncing lip flaps

#2 Post by m_from_space »

I tested it in multiple ways and this seems to be a timing problem of some sort, but with a weird glitch.

One thing first though: If you have python code inside your screen, make sure to understand that the code might get executed multiple times on random occations (when Renpy deems necessary). So you should put actions into screen objects like timers, that don't repeat otherwise the sound might just play again from the beginning.

As a fix for your screen I suggest:

Code: Select all

screen shop:
        # create a screen variable
	default line_duration = None

	## just using a key to test if this idea would work, which it doesn't
        key "o" action [SetVariable('side_speaking',True)]

        if side_speaking:
            # renpy will put all audio files found in folder "audio" into the audio object and strip the file extension
            timer 0.01 action Play("voice", audio.voiceclip)
            # in python this is not true for 0.0 by the way
            if not line_duration:
                timer 0.01 repeat True action SetScreenVariable("line_duration", renpy.music.get_duration("voice"))
            else:
                timer line_duration action SetVariable('side_speaking',False),SetScreenVariable("line_duration", None)
            add "shop guy speaking"
            
        else:
            add "shop guy"

User avatar
filthsama
Newbie
Posts: 18
Joined: Tue Jan 05, 2021 4:11 am
Completed: 0
Projects: A dating sim with some Persona mechanics
Contact:

Re: Issue with renpy.music.get_duration() and syncing lip flaps

#3 Post by filthsama »

Hi, sorry for late response! This worked like a charm, thank you so much. Also thank you for the advice! I didn't realize timers could be so useful!

Post Reply

Who is online

Users browsing this forum: AWizardWithWords