Solving Problems with Unity's Audio Mixer

Posted On: 2020-03-30

By Mark

This past week, I started taking an inventory of audio issues in my current project. While many of them are unique to my project, there were a few that are generally applicable to anyone using Unity. While researching how others resolve such issues, I found that many of them could be resolved through the use of Unity's Audio Mixer feature. As such I thought it would be good to share my findings, in case it helps someone else.

Avoiding Tedium

The first issue I encountered with the audio was a large amount of repetitive work setting up audio sources*. Each audio source is ordinarily used to play a single audio clip**, so that leads to quite a few audio sources. What's more, playing the same clip without stopping the previous one also require multiple audio sources, so one can easily wind up with an unmanageable number of sources.

Fortunately, Unity provides a convenient way to play any clip using a single source, albeit with some pretty big limitations. To do so, one uses the PlayOneShot method to tell a source to play a particular clip. Playing a "one-shot" is a fire and forget approach: once it starts, there is no way to modify what is being played; no way to pause, stop, or change the volume. Unfortunately, this leads to a number of other, larger problems that one has to address.

Adjusting Volume

By playing a clip as a "one-shot", one sacrifices the ability to change the volume as it is playing. Yet there are many situations that would benefit from being able to adjust the volume in real-time. One such example is giving the player the ability to adjust the volume*. Whether that is by moving a slider in a configuration menu or by pressing dedicated volume up (or down) keys, hearing the change immediately is an essential part of providing feedback to the player.

Fortunately, if the audio source that plays a "one-shot" is connected to an audio mixer, this becomes less of an issue. Developers can add groups to a mixer (such as "SFX" or "Ambiance") in order to organize their sounds. Every group can have its volume adjusted separately from the others, and audio sources can be assigned to a particular group. Lastly, even if the audio is played as a "one-shot", the mixer will still modify the value of the sound. Thus, if one wants the player to be able to adjust the volume of (for example) the ambiance, then one could create a group "Ambiance" and give the player a slider (or button) to increase or decrease the volume of that group.

Capping Volume

There is a particular quirk about game audio that can cause sudden jumps in volume if the designer didn't account for it. Specifically, this occurs if one sound starts playing multiple times on the same frame*. In such a situation, the sound waves line up perfectly, so instead of hearing two separate sounds, the player hears one sound that is twice as loud. This can be jarring or distracting for a player, so it is important for the developer to take steps to prevent this situation.

While it is possible to avoid this through careful scripting, a quick fix is available using Unity's Audio Mixer. One can make use of mixer effects, such as a Compressor or Duck Volume*, both of which can be used to limit the volume of a group. Of course, the effect will apply to everything in the group, so using it for this purpose might push one to have more groups than otherwise**.

Ducking Volume

One final (and quite important) situation where one might want to adjust real-time volume of sounds is when one sound is much more important than the others. This is most common with voiced dialogue (in which case the voice is more important than any other sound), but it may also occur if there are audio cues that the player needs to listen for. This is typically called ducking, and Unity's mixer provides an effect specifically for that purpose.

Conclusion

As you can see, the Audio Mixer is a valuable tool for working around many of the limitations to controlling one-shot sounds. What's more, it's designed with many of these problems in mind, making it much simpler to use than trying to script solutions by hand. Hopefully you've found this to be an informative introduction to the benefits of the Audio Mixer. As always, if you have any thoughts or feedback, please let me know.