[solved]play random sounds for a period of time

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
Cristiander
Regular
Posts: 33
Joined: Sat Mar 04, 2017 8:19 am
Contact:

[solved]play random sounds for a period of time

#1 Post by Cristiander »

I have a couple of sounds and I want them to be played randomly for the duration of a few minutes in my game.
They each take about 10-12 seconds so in order to fill a 5-7 minute mark I can't use queues.

I need them to function independently from the text boxes. How could I do that?
Is there a way to set a function to work in parallel with the text boxes?
Last edited by Cristiander on Sun Apr 02, 2017 2:30 pm, edited 1 time in total.

User avatar
indoneko
Miko-Class Veteran
Posts: 528
Joined: Sat Sep 03, 2016 4:00 am
Contact:

Re: play random sounds for a period of time

#2 Post by indoneko »

I'm not sure how you want to "randomize" the music play... but if "shuffle" is fine, then you can call a screen that play play a music list created with MusicRoom class, and jump back to continue the dialogue (or whatever your game was doing at that time). The problem with this is that I can't no longer control what that screen is doing once I jumped. This is just a mock up.... perhaps someone else could make something better than this...

Code: Select all

screen musicbutton:    
    button:
        text "Hit me!" size 35
        background "#211"
        xpos .5
        ypos .5
        xysize(100, 100)
        action [mr.Play(), Jump("continue")]   
        
init python:
    mr = MusicRoom(fadeout=1.0, loop=False, shuffle=True)  #shuffle = random, right? :D
    mr.add("concerto.mp3", always_unlocked=True)
    mr.add("act2_theme.mp3", always_unlocked=True)
    mr.add("Love Song.mp3", always_unlocked=True)
    mr.add("05 Landscaper.mp3", always_unlocked=True)
    mr.add("09 Rainbow.mp3", always_unlocked=True)
 
label start:
        
    "It's so quiet here. How about some music..?"
    call screen musicbutton
    
label continue:

    "Ah... this is better..."    
My avatar is courtesy of Mellanthe

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

Re: play random sounds for a period of time

#3 Post by Imperf3kt »

Posting as a way to remind myself to supply an answer when I put my computer back together.

Here's what I would try:
You can use the playlist function to stack several sound files to play, one after the other.
Modify that to take a renpy.random value instead.

Define your audio for easy calling

Elsewhere (probably an init-1 block), define the renpy.random function.

I posted a similar function in the cookbook for multiple, random menu sounds. Perhaps it can be tweaked for your specific use.
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

Cristiander
Regular
Posts: 33
Joined: Sat Mar 04, 2017 8:19 am
Contact:

Re: play random sounds for a period of time

#4 Post by Cristiander »

I did some experimentation and came up with this code in the end:

Code: Select all

init:
    python:
        ##this function creates a playlist of sounds
        def sounds(songlist, lenght, channel_c, initial):
            playlist = ["sounds/pause_1s.mp3"]
            if initial == True:
                for song in songlist:
                    playlist.append(song)
                lenght -= 9

            i = 0
            while i < lenght:
                for song in songlist:
                    ii = renpy.random.randint(0, 8)
                    if ii == 0:
                        playlist.append(song)
                        i += 1

            return renpy.music.play(playlist, channel=channel_c)

Post Reply

Who is online

Users browsing this forum: No registered users