Buns with sound in renpy

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
User avatar
Posts: 340
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia

Buns with sound in renpy

#1 Post by Andredron » Thu Mar 14, 2019 5:45 pm

1) Music on pause(automatic announcement of sounds)

Code: Select all

init python:
     # run the melody on the channel
     def mplay (fn, chan = "music", fin = 1.0, fout = 1.0):
         renpy.play (fn + ".mp3", channel = chan, loop = True, fadein = fin, fadeout = fout)
     # channel pause
     def mpause (channel = "music"):
         c = renpy.audio.audio.get_channel (channel)
         c.pause ()
     # remove from pause
     def munpause (channel = "music"):
         c = renpy.audio.audio.get_channel (channel)
         c.unpause ()
     # stop the melody
     def mstop (chan = "music", fout = 1.0):
         renpy.music.stop (channel = chan, fadeout = fout)
# test
label start:
     $ mplay ("mus")
     pause (2.0)
     $ mpause ()
     pause (2.0)
     $ munpause ()
     pause (2.0)
     $ mstop ()
     pause (2.0)
This function can also be tied to a button

KeyAction = renpy.curry(function_name)

2) The music on the menu depends on the time of day

Download http://renpyfordummies.blogspot.com/201 ... t.html?m=1

Code: Select all

init python:
    # game window - in the center of the screen
    import os
    os.environ ['SDL_VIDEO_CENTERED'] = '1'

    # the function translates the current time into the name of the time of day
    import datetime
    def get_t ():
        h = int (datetime.datetime.now (). strftime ("% H"))
        res = "night" # default night
        # borders of any time of day can be changed
        if (h> 6) and (h <11):
            res = "morning"
        if (h> = 11) and (h <= 18):
            res = "day"
        if (h> 18) and (h <23):
            res = "evening"
        return res
    last_t = None
    # function changes music and lighting in the menu
    # depending on the time of day
    def change_mus ():
        global last_t
        if last_t! = get_t ():
            last_t = get_t ()
            # restart menu drawing
            renpy.restart_interaction ()
            # changing the melody in the main menu
            config.main_menu_music = last_t + ".ogg"
            if renpy.music.get_playing ()! = last_t + ".ogg":
                renpy.music.play (last_t + ".ogg")
    # function - in action
    ChangeMus = renpy.curry (change_mus)
    # picture for the main menu background
    style.mm_root.background = "mm.jpg"

    # in main_menu after style "mm_root":
    # timer .05 repeat True action ChangeMus ()
    # if last_t:
        # add last_t

    # filters for lighting
    image morning = "# 8404"
    image day = "# 0000"
    image evening = "# 0484"
    image night = "# 000b"

# The game starts here.
label start:
    "You have created a new Ren'Py game."

3) Play 2 sounds on channel 1 (for example, forest and melody, channel music)

Image code:
https://pp.userapi.com/c850016/v850016 ... CKrB8.jpg

Mixer="sfx" - channel sound

Mixer="music" - channel music

Code: Select all

init python:
    renpy.music.register_channel("music_two", mixer="music", loop=True, stop_on_mute=True, tight=True, buffer_queue=True)
label start:
    play music "mm.mp3"
    play music_two "forest.mp3"
    "text to the sound of 2 melodies"
4) Anim bar sound

Download https://yadi.sk/d/govNVgHZiEDA2

5)Until the music ends don't skip the scene
Url- https://pastebin.com/LCLqsyFA

Code: Select all

init python:
    class __MusicInteract (renpy.Displayable):
        __author__ = "Vladya"
        def __init __ (self, filenames, channel = "music", * music_ar, ** music_kw):
            super (__ MusicInteract, self) .__ init __ ()
            self.channel = channel
            self.music_data = {
                "args": [filenames, channel] + list (music_ar),
                "kwargs": music_kw
            self .__ start_interact = False
        def start_play (self):
            renpy.music.play (
                * self.music_data ["args"],
                ** self.music_data ["kwargs"]
            self .__ start_interact = True
            return ui.interact ()
        def render (self, width, height, st, at):
            if self .__ start_interact:
                if not renpy.music.is_playing (self.channel):
                    renpy.end_interaction (st)
            renpy.redraw (self, 0)
            return renpy.Render (1, 1)
    def play_interact (* args, ** kwargs):
        u "" "
       Pass arguments as usual 'renpy.music.play'.
       Control will be returned to the player as soon as the audio file ends.
       "" "
        _music_object = __MusicInteract (* args, ** kwargs)
        renpy.show ("musicInteractObject", what = _music_object)
        play_time = _music_object.start_play ()
        renpy.hide ("musicInteractObject")
        return play_time
label start:
    $ play_interact ("sound / file123.ogg", loop = False)
    "The end of the song."
6) How to set a special melody when skipping.

In the screen.rpy file, find the skip screen and add the following:

Code: Select all

screen skip_indicator ():
    on 'show' action Play ('music', 'faster.mp3')
    on 'hide' action Play ('music', 'normal.mp3')
7) How to repeat the voice only if the voice was lost.

Make a button (textbutton, imagebutton ...) and prescribe the action VoiceReplay () to it.
Usually it is better to shove it either in the quick menu or in the dialog box, and so that it does not interfere, we need to set a condition. As a result, we get

Code: Select all

if voice_can_replay():
        idle "images / gui / other / log_voice.webp" 
        hover "images / gui / other / log_voice.webp" 
        xalign 0.91 yalign 0.99
        action VoiceReplay()
8 ) Add music in screen

Code: Select all

screen galereya():
        renpy.music.play("menu.mp3", fadeout=10.0, fadein=15.0)

9) in voice mode settings

Code: Select all

define eva = Character(_("Girl"), voice_tag="female")

define dshon = Character(_("Boy"), voice_tag="boy")

screen preferences:

        textbutton _(" Disable voices for female characters") action ToggleVoiceMute("female")

        textbutton _("Disable voices for boy characters") action ToggleVoiceMute("boy")
label start:
    voice "f01.ogg"
    eva "bla bla"
    voice "b01.ogg"
    dshon "bla bla"
10) How to change the volume of sounds and music

Code: Select all

label start:
    play music "horror.mp3"
    $ renpy.music.set_volume (0.2)
    "I'm getting closer to the room."
    $ renpy.music.set_volume (0.5)
    "I practically open the door."
    $ renpy.music.set_volume (1.0)
    "I'm in the room."

I know, I'm writing terribly in English.

I'm writing a Renpy textbook (in Russian). https://yadi.sk/d/ZX_DonP63USRru Update 22.06.18

Honest Critique

User avatar
Posts: 340
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia

Re: Buns with sound in renpy

#2 Post by Andredron » Sun Apr 28, 2019 9:49 am

11) sound check in bar volume


Code: Select all


# In any file.
init python:
     persistent.last_check = _preferences.get_volume ("sfx")
     def play_sound_check ():
         if persistent.last_check! = _preferences.get_volume ("sfx"):
             persistent.last_check = _preferences.get_volume ("sfx")
             renpy.sound.play ("sound_check.mp3", "sound")
# In screens.rpy find the line:
# bar value Preference ("sound volume")
# And add:
bar value Preference ("sound volume") changed play_sound_check ()

I know, I'm writing terribly in English.

I'm writing a Renpy textbook (in Russian). https://yadi.sk/d/ZX_DonP63USRru Update 22.06.18

Honest Critique

Post Reply

Who is online

Users browsing this forum: No registered users