Changing Audio Mixer Group volume with UI slider

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 (Audio Mixer - Exposing VU levels? - Unity Engine - Unity Discussions) and they said it is possible to expose this parameter in code, but I can’t figure out how.

Can anyone help?

Cheers!

2 Likes

Yep.

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

9 Likes

I agree with gregzo, also check out this tutorial from Unity themselves :smile:

Happy Mixing :wink:

10 Likes

Works great, thanks davem!

You’re welcome jsleek :smile:

If you don’t watch the whole video (what I did, and it’s good) and prefer a step by step explanation, I just posted one here: http://answers.unity3d.com/answers/1233447/view.html

4 Likes

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 MasterMixerMasterMusic

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 AudioScriptSetSound (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!

41 Likes

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.

Exactly what I’m looking for, thanks man!

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

2 Likes

Just set the slider min value to 0

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:

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.

37 Likes

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…

1 Like

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.

9 Likes

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/

3690928--303931--Speaker.PNG
3690823--303907--audiomanager settings.PNG
3690928--303937--Audio settings.PNG

  • 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 !

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.

1 Like

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.

1 Like

It was solved on the other site

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!

1 Like