[SOLVED] action PauseAudio with fadeout/fadein?

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
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

[SOLVED] action PauseAudio with fadeout/fadein?

#1 Post by Lezalith »

I hope you're all having a lovely morning/afternoon/evening.

I'm trying to achieve a fadein and fadeout when pausing audio.
I tried:

A) Making it work with PauseAudio, but that one doesn't have any fadein/fadeout options.

B) Make a custom function:

Code: Select all

    def musicStart():
        play main_screen "main_screen.ogg" fadein 1.0
        play ambience "ambience.mp3" fadein 1.0
        play noise "noise.mp3" fadein 1.0
       
    def musicStop():
        stop main_screen fadeout 1.0
        stop ambience fadeout 1.0
        stop noise fadeout 1.0
I thought this would work, but it gives me an "invalid syntax" on the very first line with "play".
Note: main_screen, ambience and noise are all registered channels.

Any ideas, please?
Last edited by Lezalith on Thu Apr 06, 2017 11:06 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2415
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: action PauseAudio with fadeout/fadein?

#2 Post by Ocelot »

Use Stop(channel, fadeout=1.0) instead of PauseAudio
< < insert Rick Cook quote here > >

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: action PauseAudio with fadeout/fadein?

#3 Post by Lezalith »

But that stops it. I want to only pause it, so that I can resume it later.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2415
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: action PauseAudio with fadeout/fadein?

#4 Post by Ocelot »

Pausing and resuming channel channel does not support fading. You might wait and hope that it will in next versions, or implement it yourself and send pull request to incorporate your changes to renpy code.

Another approach is: get the name of track playing (via renpy.music.get_playing function) and current time (via renpy.music.get_pos). Then stop channel with fadeout. When you want to resume, start previously gotten file from previously gotten position
< < insert Rick Cook quote here > >

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: action PauseAudio with fadeout/fadein?

#5 Post by Lezalith »

Alright, I see how I would do that... More or less.
The renpy.music.play doesn't have an argument that let's you play a file from a specified position, however.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2415
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: action PauseAudio with fadeout/fadein?

#6 Post by Ocelot »

The positional arguments go before file name:
Ren'Py supports partial of audio files. This is done by putting a playback specification, enclosed in angle brackets, at the start of the file. The partial playback specification should consist of alternating property name and value pairs, with every thing separated by spaces.
Example: "<from 5 to 15.5>waves.opus"

You will need to build string dynamically, of course.
< < insert Rick Cook quote here > >

User avatar
Lezalith
Regular
Posts: 82
Joined: Mon Dec 21, 2015 6:45 pm
Contact:

Re: action PauseAudio with fadeout/fadein?

#7 Post by Lezalith »

I did it!

At the beginning, all three variables are set to 0, so they can play from the beginning.

Code: Select all

    def musicStart():
        renpy.music.play("<from {0}>main_screen.ogg".format(returnVariableMain_screen), channel="main_screen", fadein=1.0)
        renpy.music.play("<from {0}>ambience.mp3".format(returnVariableAmbience), channel="ambience", fadein=1.0)
        renpy.music.play("<from {0}>noise.mp3".format(returnVariableNoise), channel="noise", fadein=1.0)
Then before stopping the music, value of those variables gets overwritten by the returns...

Code: Select all

    def musicReturnPosMain_screen():
        return renpy.music.get_pos(channel="main_screen")
    def musicReturnPosAmbience():
        return  renpy.music.get_pos(channel="ambience")
    def musicReturnPosNoise():
        return renpy.music.get_pos(channel="noise")

    def musicReturn():
        global returnVariableMain_screen
        global returnVariableAmbience
        global returnVariableNoise
        returnVariableMain_screen = musicReturnPosMain_screen()
        returnVariableAmbience = musicReturnPosAmbience()
        returnVariableNoise = musicReturnPosNoise()
And finally, it stops the music with fadeout.

Code: Select all

    def musicStop():
        renpy.music.stop(channel="main_screen", fadeout=1.0)
        renpy.music.stop(channel="ambience", fadeout=1.0)
        renpy.music.stop(channel="noise", fadeout=1.0)
musicStart() will then play it from where it left off.

I am so happy. When I was posting this, I didn't even know what Return actually does.
Thank you for the assistance Ocelot!

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: [SOLVED] action PauseAudio with fadeout/fadein?

#8 Post by Saltome »

Oh, snap. I was too slow.
And here I was so giddy with my success.

Hey man I made my own solution for your problem. Of course, credit for the idea goes to Ocelot. I just patched together a few code snippets. You are free to use it if you want...

Code:

Code: Select all

default pauseddict = {}
init python:
    def _insert(name, time):
        store.pauseddict[name] = time
            
    def fading_pause(channel = "music", pause = "toggle", fade = None, filename = None):
        if pause == "toggle":
            pause = renpy.music.get_playing(channel)
            
        
        if pause:
            _insert(renpy.music.get_playing(), renpy.music.get_pos())
            renpy.music.stop(channel, fade)
        else:
            if filename == None:
                name, time = store.pauseddict.popitem()
            else:
                name, time = store.pauseddict.pop(filename)
            fn = "<from {}>".format(time) + name
            renpy.music.play(fn, channel, fadein = fade)

Installation:
Paste the code at the top of your script file.

Use:

Code: Select all

label start:
    
    "Start"
    play music "music.mp3"
    "Click to pause."
    $ fading_pause()
    "Click to unpause."
    $ fading_pause()
    "End"
    return
And here's the beauty of it. You can pass a number of arguments to the fading_pause function...
You can specify the channel, pause, and fade time, and a filename.

Code: Select all

$ fading_pause(channel = "music", pause = True, fade = 1.0, filename = "Song.mp3")
Or, any one of them.
Or you could even use it as in my example, without any arguments.
The default behavior uses the music channel, if it's playing it pauses, if it isn't playing it starts playing, and fading is disabled, if you provide a filename it will resume playing that particular song.
It can not only fade out when you pause but fade in when you unpause.
You can, in theory even pause multiple files then unpause them.
Now this isn't entirely foolproof, but if you don't try anything crazy you shouldn't run into any problems.
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

Post Reply

Who is online

Users browsing this forum: thexerox123