Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing Audio Mixer Group volume with UI slider

Discussion in 'Audio & Video' started by jsleek, Feb 13, 2015.

  1. jsleek

    jsleek

    Joined:
    Oct 3, 2014
    Posts:
    61
    Hi there,

    I'm looking to create a sophisticated volume control in Unity 5 through the use of audio mixer groups. If I can find a way to expose the attenuation parameter on each Audio Mixer Group in code and hook it up to a UI slider, then i can control the volume of all music sources contained within the specific Audio Mixer Group.

    I read this thread here (http://forum.unity3d.com/threads/audio-mixer-exposing-vu-levels.277216/#post-1830016) and they said it is possible to expose this parameter in code, but I can't figure out how.

    Can anyone help?

    Cheers!
     
    CuteCatsXXL and Necronomicron like this.
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Yep.

    Select your mixer, select your audio group, right click on the attenuation level in the inspector, click expose. Screen Shot 2015-02-13 at 16.30.53.png
     
  3. davem250

    davem250

    Joined:
    May 28, 2013
    Posts:
    186
    I agree with gregzo, also check out this tutorial from Unity themselves :D



    Happy Mixing ;)
     
  4. jsleek

    jsleek

    Joined:
    Oct 3, 2014
    Posts:
    61
    Works great, thanks davem!
     
  5. davem250

    davem250

    Joined:
    May 28, 2013
    Posts:
    186
    You're welcome jsleek :D
     
  6. v01pe_

    v01pe_

    Joined:
    Mar 25, 2015
    Posts:
    71
  7. Neesha_93

    Neesha_93

    Joined:
    Jan 20, 2015
    Posts:
    1
    1- Go to Assets -> Create -> Audio Mixer

    2- Rename it as MasterMixer and double click on it

    3- In the Groups section click on + and add a group. Rename it as Music

    4- Select your group(Music), right click on the free space of the Attenuation in the inspector, click Expose.

    5- Click on Exposed Parameters in the Audio Mixer panel. Rename parameter name as musicVol and press enter


    6- Create the Game Object and name it as AudioObj. Click Add Component in the inspector and add Audio Source

    7- Click on small circle in the right side of the AudioClip. Select your audio

    8- Click small circle in the right side of the Output. Select MasterMixer -> Master -> Music


    9- Go to Assets -> Create -> C# script

    10- Add this code. (Use SetFloat name as the parameter name you gave to the Exposed Parameters)

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Audio;

    public class AudioScript : MonoBehaviour {

    public AudioMixer masterMixer;

    public void SetSound(float soundLevel)
    {
    masterMixer.SetFloat ("musicVol", soundLevel);
    }
    }

    11- Create a Game Object name it as AudioScriptObj and drag the script to it

    12- Drag Audio Mixer from the project panel and drop to the masterMixer


    13- Go to Game Object -> UI -> Slider

    14- Click the slider. In the inspector click the + of the On Value Changed

    15- Drag the AudioScriptObj to the object and click on function list. select AudioScript -> SetSound (it's under Dynamic float)

    16- Click the slider and change the Min Value to -80 and Max Value to 0

    That’s it. Cheers!
     
    SynoJ, Keriban, Stormer2020 and 34 others like this.
  8. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533
    was just recently looking into doing his today.... great post, all the info I needed

    .. unlike unity useless help docs.. surprised this was only posted like a week ago.... no idea how long it would have taken to gather all the required info to do this otherwise.
     
  9. Duke64

    Duke64

    Joined:
    Dec 28, 2017
    Posts:
    2
    Exactly what I'm looking for, thanks man!
     
  10. Mirotocivi

    Mirotocivi

    Joined:
    Apr 29, 2018
    Posts:
    1
    I have problem whit this. Its work fine, but when I move slider to the left music volume start to drop, but when slider hit middle music is lower down to the point you can`t hear it. My question, why is music turn down at middle of slider and not in the end (far left) of slider. thank you
     
    EZaca likes this.
  11. SpaghettinoHieu

    SpaghettinoHieu

    Joined:
    May 3, 2018
    Posts:
    2
    Just set the slider min value to 0
     
  12. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    Hi, the problem is that the fader value (-80db - 0db) is a logarithmic scale and the slider value is linear. For example: half volume is actually about -10db, but if you connect it to a linear scale, like the slider, then half volume will end up being -40db! which is why it sounds like it's basically silent at that point.

    There's an easy way to fix it.

    Instead of setting the slider min / max values to -80 and 0 (like you would to match the slider), set them to min 0.001 and max 1.

    Then in the script to set the value of the exposed parameter, use this to convert the linear value to an attenuation level:

    Code (CSharp):
    1. masterMixer.SetFloat("musicVol", Mathf.Log(soundLevel) * 20);
    It's important to set the min value to 0.001, otherwise dropping it all the way to zero breaks the calculation and puts the volume up again.

    Hope that helps!

    John.
     
  13. chacozar

    chacozar

    Joined:
    Feb 11, 2016
    Posts:
    1
    Can I ask why exactly the * 20 after the Mathf.Log(soundLevel) ?? is this some specific value related to the decibels? I See that it works but I want to know why it works as well, even a link to where I can read up on it myself would be helpful....
     
    ProGameDevUser likes this.
  14. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    I will admit that I had to look this up, but my understanding is that it's to do with the difference between representing power and voltage in decibels, where power is equivalent to voltage squared.

    When calculating power in decibels you need to use 10Log(x) or Log(x) * 10. However because power is equivalent to voltage squared, to do the same thing with voltage the equation would be 10Log(x²) or 20Log(x) which would return the same result. Or, as in the answer above, Log(x) * 20 which would also return the same.

    The fader value in the mixer represents a signal voltage, so the latter applies which is where the 20 comes from.
     
  15. CNR-EpiTel

    CNR-EpiTel

    Joined:
    Aug 23, 2018
    Posts:
    12
    Hi John, I reaally was grateful to read your reply to Chacoazar.
    I am looking for help because my audio is completely GONE! I have no idea what caused it. I was not messing with any audio. I was using demo scenes with Weather Manager at the time. I thought maybe that was where the problem was . I removed it, and I re-installed a fresh version of it. Still no audio. I tried other apps. No audio. I thought, I'll reinstall unity. No audio. I tried reinstalling old versions of unity. And play only the standard assets. Still no audio.. I am checking and reinstalling sound drivers and looking if there's anything wrong anywhere. Everything works and is update, or refreshed as well.

    here are the pics of my system.

    Is there any way of modifying somethhing- I have no clue what- to get the volume audible now??
    This is freaking me out-
    I have been working so hard on getting the scenes -and now it is cmpletly impossible to believe that I cannot find the audio volume....even when it works on everything else on my MSI-laptop Windows 10 VTX1070 with hi-res speakers.

    Thank you for any suggestions/
     

    Attached Files:

  16. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,303
    - don't cross post
    - create a new thread with your issue - which is unrelated to both threads you posted so far
    - you have 2 audio listeners - as is clearly indicated in the console - in your scene - solve this FIRST
    - as far as I can see you have no AudioClip assigned on the AudioSource you posted the screenshot of (unless the sound is generated there's nothing to play)
    - don't reply here, thanks !
     
  17. CNR-EpiTel

    CNR-EpiTel

    Joined:
    Aug 23, 2018
    Posts:
    12
    As a new user, new developer, if one site gets no reply, one must ALWAYS assume it is perhaps not right and to add or write more.
    People will always try a new location to find an answer. It is wrong to correct them as your first priorty rather than to recognise the logic of the behaviour. Knowledge does not improve by shutting a person with a "do not do that". It's the basis of delay, not assistance.

    I appreciate your answer and in general, demoscenes already are configured to work.

    I never tell anyone do not do something.
    I listen to why groups or individuals do it.
    Then support them.
     
    Last edited: Sep 18, 2018
    Cyril_m likes this.
  18. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    There could be all kinds of reasons that the audio isn't working but even with your screenshots I'd be guessing which one it is.

    r618 is right however, you should put this in a new thread, if only because you'll get a much better response from more members of the community. I didn't even get a notification for this, I only saw it by chance when I logged in to read a different post.

    If you're still having issues. My only suggestion is to work backwards to debug it. e.g. is the system audio working, if yes then is a different Unity project audio working (try an empty project) and so on until you find the problem. But definitely remove the 2nd audio listener first as there shouldn't be two.

    There are other things that can cause the audio to drop out in Unity. For example the AudioListener has its own volume and can be paused. I've found that this can be persistent, not only outside of play mode but also between separate projects. See here for how to access those values: https://docs.unity3d.com/ScriptReference/AudioListener.html

    If you're still having issues, make a new thread to post this to the wider community.

    Good luck with it.
     
    r618 likes this.
  19. CNR-EpiTel

    CNR-EpiTel

    Joined:
    Aug 23, 2018
    Posts:
    12
    It was solved on the other site
     
  20. trevorobrecht

    trevorobrecht

    Joined:
    Jan 10, 2016
    Posts:
    1
    You're an absolute genius @JLF , sorry for bumping this thread but I just wanted to thank you, I doubt I ever would have figure that out myself you lost me at converting the voltage. I appreciate it very much!
     
    protopop likes this.
  21. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    Too kind! But thank you. Because it has been such a surprisingly common question I since wrote a whole article on this on my website here: https://johnleonardfrench.com/artic...slider-in-unity-using-logarithmic-conversion/
     
    Westland and protopop like this.
  22. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,557
    JLF likes this.
  23. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    Must be
    Mathf.Log10(float f)
    , because
    Mathf.Log(float f)
    is the natural logarithm in Unity.

    To 0.0001.
     
    Last edited: Jul 7, 2020
  24. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    139
    You are correct.
     
    Necronomicron likes this.
  25. siemthanh5

    siemthanh5

    Joined:
    Jul 9, 2020
    Posts:
    2
  26. ProgrammingWhileSleeping

    ProgrammingWhileSleeping

    Joined:
    Nov 10, 2017
    Posts:
    17

    Thank you for the script, I was having trouble with it and also wanted to know why the calculation was like that. I read your explanation and though it didn't make enough sense for me, thanks for this still! Have a great day everyone!
     
  27. SuperAlexanderr

    SuperAlexanderr

    Joined:
    Nov 17, 2020
    Posts:
    1
  28. arrcwood

    arrcwood

    Joined:
    Sep 4, 2019
    Posts:
    4
    Sorry I'm late to the game, but I found setting the min to 0.001 to be key for my project thanks to John. Although at first, Unity wouldn't allow me to set the value to 0.001. Every time I tried, it would reset it to 0. Because of that, it cause my music and/or SFX to go up in value rather than mute when the slider was moved all the way to the left. I just tried again, except instead of tabbing out of the min field after typing in 0.001, I clicked on the next slider and entered that value again. I rechecked both and they stayed at 0.001 instead of resetting to 0, and now the sliders mute the music and SFX as necessary. I'm not sure if anybody else has run into same problem, but in case they did, that's what I did. Thanks again John!
     
  29. DryreL

    DryreL

    Joined:
    Feb 23, 2020
    Posts:
    49
    Thank you!! You solved my problem :)
     
  30. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    https://forum.unity.com/threads/cha...up-volume-with-ui-slider.297884/#post-6063395
     
  31. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Actually, with Mathf.Log works kinda better than with Mathf.Log10. Don't ask me why.
     
  32. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    156
    I'd like to add to that, Mathf.Log works just fine :)
     
  33. DSivtsov

    DSivtsov

    Joined:
    Feb 20, 2019
    Posts:
    151
    Not fully correctly explanation.
    It's not related with that, But proposed solution will work ("made slider more comfortable").

    Initially slider used the decibel values, which graduated on slider at constant linear factor ((max - min) values / size of slider, in your e.g. 0 and -80 db).
    When you change slider values to [0.0001; 1] you change nothing, except you begin use for SetFloat the value Mathf.Log10(sliderValue) * 20. Therefore the volume values will be in range from 0 to -80db. (if you set slider values to [0.01; 1] the range will be from 0 to -40db)

    In this case you just change the scale of the slider from linear scale (values placed with one step) to a something in logarithmic scale.
    It can be "Mathf.Log10(sliderValue) * 20" or "* 30" or "*19". It's no important.
    To be honest the values on slider less than 0.01, doesn't have any sense because can't be set precisely values less than 0.01

    The main important point is select the demanded range of volume values which you want set by a slider.
    Commonly useful values in range from 0 to -40db (and have separated sliders for master volume and other volumes), in this case you can simply set slide min/max values like -40/0 and always will work the same as with "logarithmic scale".

    Note.
    If you use the masterMixer.SetFloat() with "Mathf.Log10(sliderValue) * 20" don't forget when you set initial values of slider by masterMixer.GetFloat() use the correct value for sliderValue = Mathf.Pow(10f, Volume / 20)
     
    Last edited: Dec 25, 2022
    mitaywalle likes this.
  34. DSivtsov

    DSivtsov

    Joined:
    Feb 20, 2019
    Posts:
    151
    Code (CSharp):
    1. // Summary:
    2. //     Returns the natural (base e) logarithm of a specified number.
    3. //
    4. public static extern double Log(double d);
    5.  
    6. // Summary:
    7. //     Returns the base 10 logarithm of a specified number.
    8. //
    9. public static extern double Log10(double d);
    Log(x) = Log(10) * Log10(x) ~ 2.3 * Log10(x) (difference only in multiplier)
    graph.jpg
     
    Last edited: Dec 25, 2022
  35. gliealonso

    gliealonso

    Joined:
    Oct 21, 2018
    Posts:
    117
    Thank you!
     
    assertor likes this.
  36. Social_lucky

    Social_lucky

    Joined:
    Nov 6, 2022
    Posts:
    1
    Thank you very much!
     
  37. Kalzouzal123

    Kalzouzal123

    Joined:
    Jul 15, 2020
    Posts:
    2
    That is great, but I found out thanks to this tutorial:
    that using Log10() instead of Log() yields more accurate results.
    So use Log10() instead.