[solved] How to get the volume of an audio channel as a value to use in a screen

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
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

[solved] How to get the volume of an audio channel as a value to use in a screen

#1 Post by Imperf3kt »

I'm trying to get the volume of the music, sound and voice channels as text in my screen, but I'm not sure how.
Currently I have this:

Code: Select all

default mus_vol = _preferences.get_volume("music")

            if config.has_music:
                label _("Music Volume")

                vbox:
                    bar value Preference("music volume") tooltip _("Change the music volume")
                    text _("[mus_vol]%")
This works and places the value as text on the screen (see image below), but it doesn't update when I adjust the slider (even after restarting the game) and it also displays it as 1.0 instead of a percentage (100)
I've also tried using python substitution

Code: Select all

default mus_vol = _preferences.get_volume("music")

            if config.has_music:
                label _("Music Volume")

                vbox:
                    bar value Preference("music volume") tooltip _("Change the music volume")
                    text _("%s") %mus_vol
but this just gives me the same results (except using this method I cannot work out how to add a percent symbol at the end of the text)


How can I get this as a value that I can use and convert it to a 100, 99, 98, etc instead of 1.0, 0.99, 0.98 etc.
Image
Last edited by Imperf3kt on Thu Oct 22, 2020 2:29 pm, edited 1 time in total.
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

User avatar
SypherZent
Veteran
Posts: 362
Joined: Fri Sep 02, 2016 3:14 am
Completed: Multiverse Heroes, Space Hamster in Turmoil
Location: Puerto Rico
Contact:

Re: How to get the volume of an audio channel as a value to use in a screen

#2 Post by SypherZent »

I'm actually happy to finally be able to help you with something, since you've helped me so many times in the past Imperf3kt.

The problem you're experiencing is happening because Ren'Py will not automatically update values defined with "default" within a screen unless they are being modified by screen actions such as SetScreenVariable, ToggleScreenVariable, etc.

There are 2 possible solutions:

1) Use "$" instead of "default":

Code: Select all

$ mus_vol = _preferences.get_volume("music")

            if config.has_music:
                label _("Music Volume")

                vbox:
                    bar value Preference("music volume") tooltip _("Change the music volume")
                    text _("[mus_vol]%")

2) Call the value directly from text:

Code: Select all

            if config.has_music:
                label _("Music Volume")

                vbox:
                    bar value Preference("music volume") tooltip _("Change the music volume")
                    text _(str(_preferences.get_volume("music")))

Note that you can also create a custom function that returns the value configured to display as a percentage, instead of calling _preferences.get_volume("music") from the screen itself. Something like this:

Code: Select all

$ mus_vol = Get_Music_Volume("music")

            if config.has_music:
                label _("Music Volume")

                vbox:
                    bar value Preference("music volume") tooltip _("Change the music volume")
                    text _("[mus_vol]%")
Where "Get_Music_Volume" is a function you have defined somewhere else that returns _preferences.get_volume("music") as a percentage and string.

Also note, it's best to use str(val) than "[val]" in this case, as the variable is not being handled by "default" anymore.


Hope this helps. ^^

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: How to get the volume of an audio channel as a value to use in a screen

#3 Post by RicharDann »

You can also use .format() interpolation to convert and display the value, like this:

Code: Select all

# Use str.format to interpolate the result of mus_vol * 100, with no decimal places this gives you the rounded percent 
text _( "{:.0f}%".format(mus_vol * 100) ) 
The most important step is always the next one.

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

Re: How to get the volume of an audio channel as a value to use in a screen

#4 Post by Imperf3kt »

Perfect, thanks.
I was trying to make a function, but forgot how, so these suggestions help a lot.
I used a python block instead of $ though.
SypherZent wrote: Thu Oct 22, 2020 8:43 am I'm actually happy to finally be able to help you with something, since you've helped me so many times in the past Imperf3kt.
I'm glad I was able to help out and thank you in return :)

One small issue that I'm not sure what's causing it, is when I duplicate it for sound and voice, voice works and displays a percentage, but sound just shows 0% no matter where the bar is set.
https://i.imgur.com/lK19moq.png
Will need to look into this further.


It seems that the sound channel isn't called "sound", but "sfx".
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

Post Reply

Who is online

Users browsing this forum: Tony_Tan