Search Unity

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:
    43
    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!
     
  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:
    43
    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:
    47
    sand_lantern likes this.
  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!
     
    briosh, kamathaj, TheBaldGuy and 4 others like this.
  8. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,390
    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
     
  11. Spaghetti-sama

    Spaghetti-sama

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

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    60
    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....
     
  14. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    60
    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:
    794
    - 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
  18. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    60
    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. CaliberBeats

    CaliberBeats

    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!
     
  21. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    60
    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/