How to play a sound every time a letter is shown when displaying text with cps?

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
simba975
Newbie
Posts: 22
Joined: Sun Jul 23, 2023 12:05 pm
Contact:

How to play a sound every time a letter is shown when displaying text with cps?

#1 Post by simba975 »

I'm trying to do that thing where when a text is displayed with cps a sound is played every time a letter appears. I found how to do it in the renpy documentation but I can't make it work.

Here is what I have at the beginning in my scipt (I also tried putting it on screens and gui):

Code: Select all

init python:
    def beepy_voice(event, interact=True, **kwargs):
        if not interact:
            return

        if event == "show_done":
            renpy.sound.play("audio/SFX/boop.ogg")
        elif event == "slow_done":
            renpy.sound.stop()

define Emir = Character("Emir", callback=beepy_voice)

Then in a label:

Code: Select all

scene black
Emir "Sorry for being late, teacher."

And I have an audio called boop.ogg in a folder called SFX that's in the audio folder.

Any idea why it is not working or another methode to make it work?

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

Re: How to play a sound every time a letter is shown when displaying text with cps?

#2 Post by m_from_space »

I'm not sure it's possible using text-tags, since they only replace strings and return them.

But I came up with a solution (since this is a fun little thing). Here is a function that does it all for you. Just put your beepwords into <THIS>. This function uses an audiofile called "beep.ogg" by the way, you may want to change that. Normal text tags still work by the way.

Code: Select all

init python:
    import re
    def beepsay(who, what, cps=3):
        # store what was said so far
        past = ""
        # get a list of all substrings not beeping
        nobeeps = re.split("<[^>]+>", what)
        # go through every beepword <BEEP> we can find
        for i, beepword in enumerate(re.findall("<([^>]+)>", what)):
            # say what was said so far (fast) and the current nobeep string normally
            renpy.say(who, f"{past}{{fast}}{nobeeps[i]}{{nw}}")
            past += nobeeps[i]
            for letter in beepword:
                renpy.say(who, f"{past}{{fast}}{{cps={cps}}}{letter}{{/cps}}{{nw}}")
                renpy.play("audio/beep.ogg")
                past += letter
        # check if there are still things to say
        end = nobeeps[i+1] if i < len(nobeeps)-1 else ""
        renpy.say(who, f"{past}{{fast}}{end}")
        return

label start:
    $ beepsay(eileen, "Hey, I am a <ROBOT> and my name is <EILEEN>.")
    $ beepsay(None, "And I am...{w=0.4} uhm...{w=0.4} a very slow <ROBOT-NARRATOR>...", cps=1)
    return

Post Reply

Who is online

Users browsing this forum: No registered users