From the morning wake up call of a buzzing alarm, to the silent rumble of an air conditioner, sound surrounds us. Audio can make or break a video game and can provide a fully immersive experience for the player when done correctly. The Audio Mixer is a great aid in helping you achieve that.
In this tutorial, you’ll learn how to do the following:
- Create an audio mixer.
- Create groups, views, and snapshots for the audio mixer.
- Route audio source signals to groups.
- Send and receive audio from one group to another.
- Add effects to groups.
- Expose parameters for scripting in C#
Getting Started
Before you dive into the world of Audio Mixers, Groups, and Sound Effects, it’s best to understand the basic types of sounds that you’ll see in games:
- Music
- Sound effects or SFX
- Dialogue
- Environment or Ambiance
Each of these categories can be broken down into several sub-categories. For this tutorial, you will focus on the above four categories.
Download the starter project here and extract it to a location of your choosing.
Open up the Starter Project in Unity. The assets are sorted inside several folders:
- Audio: Holds anything relating to Audio.
- Audio Manager: Holds assets needed to manage your audio in-game.
- Environment: Contains environmental audio clips.
- Music: Contains musical audio clips.
- SFX: Contains various sound clips.
- StarterAssets: Contains various assets that will help make this tutorial easy and fun! You will not have to go into this folder for this tutorial.
Open the MixerMayhem scene in the StarterAssets/Scenes folder.
Run the starter project. If everything went okay, you should be able to hover over each object, and they should each produce a unique sound when clicked on.
Creating an Audio Mixer
Right now, the audio in the scene is being played by Audio Sources on each of the objects. Jumping back and forth between all these individual Audio Sources to make changes or add effects to them can become quite a tedious process. Luckily, the Unity Audio Mixer is here to save the day!
To create an Audio Mixer, first make sure the Audio Mixer window is open. You will find it located under Window >> Audio Mixer or Ctrl + 8:
In the Audio Mixer window, create an Audio Mixer named MasterMixer by clicking the + located at the top right of the window.
Alternatively, you can create an Audio Mixer under Project Window\Create\Audio Mixer:
You should now have a blank Audio Mixer opened in your Audio Mixer window.
Here’s some explanation:
- Audio Mixers can be routed into an Audio Group of another Audio Mixer. All of your Audio Mixers are listed here.
- Snapshots contain the parameters of a specific state within the Audio Mixer.
- Groups are how the Audio Mixer knows to work its way up the hierarchy. Each group creates another bus in the Audio Group strip view.
- Views are visibility toggle sets that indicate what groups to show in the strip view.
- Edit in Play Mode will only appear during play mode, and will allow you to make changes in the Audio Mixer during play mode that will persist outside of play mode.
- Exposed Parameters allows any parameter within an Audio Mixer to be modified via code.
- Master Group is the default group within every Audio Mixer.
Creating Groups
An Audio Mixer Group is used by one or more Audio Sources to combine and modify their audio outputs before it reaches the Audio Listener in the scene. A Group will route its output to another Group if it’s a child of that former group. The Master Group will normally route its output to the Audio Listener only if its Audio Mixer doesn’t route into yet another Audio Mixer.
To create a new Audio Mixer Group, click the + located next to Groups in the Audio Mixer window. Name this group Music.
A new group named Music should appear in the Strip View. By default, each group has a few traits to go along with it:
- Volume slider lets you adjust the decibel or dB level of the sound.
- Mute button lets you mute all sound routed to that group.
- Solo button adds the group to a set of soloed groups.
- Bypass button ignores all effects on the group.
- Attenuation is the default effect on every group that controls volume.
- Add effects allows you to layer effects in any order that will alter the final output of the sound.
Add the following additional Groups:
- SFX
- Grenade
- Sink
- Radio
- Environment
SFX should be under Master, while Grenade, Sink, Radio and Environment should all be under SFX. If your groups ended up in a different order, you can re-order them by dragging and dropping, just as you do with GameObjects in the Hierarchy.
It’s a good habit to color-code your Groups for the ease of readability. Do that now by setting the Master to Red, Music to Orange and all of the SFX to Cyan.
To change the color of a Group, simply right-click the Group in the Strip View and choose a color.
When finished, your Groups should look like this:
Setting up Views
With your Groups in place, you can now set up Views, which are visibility toggle sets that indicate which groups to show in the Strip view. Views become quite useful when there are many Groups.
When you created the Audio Mixer, an initial View called View was created. Change the name of that view to Everything, as this will be your main view of all of the Groups. Then create two more Views by clicking the + next to Views and name them Music and SFX respectively.
With the Music View selected, toggle everything Cyan off.
Then select the SFX View and toggle everything Orange off.
You should now be able to jump between all three views by clicking on their names in the View window, and the appropriate Groups should appear in the Strip View.
Routing and Mixing
You have an Audio Mixer with a bunch of Groups set up, but no Audio Sources are outputting to any Groups. Currently, your Audio Sources are routed directly to the Audio Listener.
What you want is for the Audio Sources to output to an appropriate Group that will then output to the Audio Mixer, which will in turn output to the Audio Listener.
Locate the GameObject named Audio Objects in the Hierarchy window.
Now select the Trumpet child object of Audio Objects. Then jump over to the Inspector window, and scroll down until you see the Audio Source attached to the Trumpet. You should see an Output field underneath the AudioClip field. Click on the icon next to the Output field and select the Music group.
Awesome job — you just routed your first Audio Source! With the Audio Mixer window still open, run the game, and click on the Trumpet object. Notice when the Trumpet is playing, the Music group is showing Attenuation, as is the Master group, because the Music group is a child of the Master group.
Click the Edit in Play Mode button while the game is still running. You can adjust the Music volume slider to that of your liking, and the changes will persist outside of Play Mode.
So now you have one Audio Source currently routed, but you need to route all of the SFX sounds.
Go through the remaining children under the Audio Objects game object in the Hierarchy window, and set their Audio Source Output fields to an appropriate Group:
- AirConditioner: Environment
- Phone: SFX
- Grenades: Grenade
- FireAlarm: SFX
- AssaultRifle: SFX
- Cans: SFX (For both attached Audio Sources)
- Sink: Sink
- Radio: SFX (For the First/Top Audio Source)
- Radio: Radio (For the Second/Bottom Audio Source)
There’s also one more Audio Source on the AudioManager GameObject for a room tone sound. Set the Ouput field for that one to Environment.
Now run the project, and start clicking away. If you take a look at the Audio Mixer window, and you should start to see all of your Groups being put to use as you click the various objects in the scene.
Play around with the Solo and Mute buttons to see just exactly how they work with the Audio Mixer. For instance, muting the SFX group will mute all sound that routes through that group.
Effects
You can add various effects to Groups just like you can add effects to Audio Sources. To add an effect simply, select the Group you wish to add an effect to and click on Add Effect in the Inspector window.
Unity provides a bunch of audio effects right out of the box. You’ll be using the following for the remainder of this tutorial:
- SFX Reverb to make all the sound in the game feel like part of the room.
- Highpass to cutoff low frequency sounds.
- Lowpass to cutoff high frequency sounds.
- Compressor to balance the volume of sounds.
- Distortion to help simulate a radio transmission.
Groups can have as many effects as you’d like, with Attenuation being the default effect for every Group. Effects are executed from top to bottom, meaning the order of effects can impact the final output of the sound. You can re-order effects at anytime by simply moving them up or down in the Strip View.
A common practice in audio mixing is to create separate Groups for complicated or frequently used effects.
Create two additional Groups. Name one Reverb and the other Distortion. Mark them both as green for readability and move them up front next to the Master.
Reverb
Instead of adding a SFX Reverb to each Group, you can simply add it to the Reverb group. Do this now.
You’re probably wondering how this will do anything if your Audio Sources are not routed to the Reverb group. You’re right — it won’t. You will have to add another effect named Receive to your Reverb group, and make sure it is above the Reverb effect as order matters.
Then you can add the opposite effect named Send on the Music and SFX groups.
Select the Music group and in the Inspector window under the Send effect, connect the Receive field to the Reverb\Receive option, and set the Send Level to 0.00 dB.
Then do the same thing with the SFX group.
With all that sending and receiving set up, jump back over to your Reverb group. In the Inspector window, under SFX Reverb, change the following fields to the indicated value:
- Room: -1000.00 mB
- Room HF: -1200.00 mB
- Decay Time: 1.49 s
- Decay HF Ratio: 0.54
- Reflections: -370.00 mB
- Reflect Delay: 0.00
- Reverb: 1030.00 mB
- Reverb Delay: 0.01 s
- Density: 60.00 %
These settings will give your audio a nice empty-room effect, and will help unify the audio as if it’s all coming from the same room.
Save and run your project, and start making some noise! You should see the Reverb group in the Audio Mixer window picking up noise as you click on various objects.
Try starting and abruptly stopping the Trumpet sound. You should hear the Reverb echoing and decaying quite nicely.
You can also use the Bypass button to have the Reverb group bypass its effects, so you can hear the difference with and without the SFX Reverb.
For some finishing touches, add a Lowpass filter to the Grenade group and the Sink group. The default values should be just fine. This will help remove some of the high frequency signals being created from the Reverb group.
Distortion
With the Reverb working nicely, it’s time to add the next effect — Distortion.
Run the game and click the radio, then wait until you hear the phrase Ladies and Gentlemen.
Right now, that phrase doesn’t sound like it’s coming from a radio at all. To fix that, you’ll first need to add a Receive effect to the Distortion group, and then have the Radio group send to the newly created Receive channel.
Don’t forget to set the Send Level field to 0.00 dB under the Radio group in the Inspector window. Otherwise the Distortion group will have no effect.
Now it’s time to create the actual Distorting effect. To really get it sounding like a radio transmission, you’ll need to do a little bit of layering.
Add the following effects:
- Highpass to remove any low frequency room tone noises.
- Compressor to bring the decibel levels to an equilibrium.
- Distortion to simulate a radio transmission.
- Another Distortion to really help sell the effect.
- Lowpass to remove any high-pitched screeching created from the distortion.
Now select the Distortion group. Go to the Inspector window and set the values for all of the effects accordingly:
- Pitch: 100%
- Volume: -10dB
- Cutoff freq: 2909.00 Hz
- Resonance: 1.00
- Threshold: -9.80 dB
- Attack: 50.00 ms
- Release: 50.00 ms
- Make up gain: 0.00 dB
- Level: 0.96
- Level: 0.96
- Cutoff freq: 2064.00 Hz
- Resonance: 1.00
When finished, run the project again and click on the radio.
The Distortion effect should sound similar to this:
With all of the effects in place, you can pat yourself on the back for setting up your first Audio Mixer!
As a final touch, you can adjust some of the decibel levels. Music and Environment should be more of a background noise, so lower their decibel levels. Also, you don’t want to give your player a heart attack when the Grenade goes off so lower that a bit too. :]
In the next section you’ll learn how to change some of the Audio Mixer fields via scripting.
Audio Manager
With the Audio Mixer complete, it’s time to give the player some options in game that will let them make their own changes to the Audio Mixer.
In the Hierarchy window, there is a game object named AudioManager that has a child canvas object named AudioManagerCanvas. Under that, you should see another object that is currently inactive named SliderPanel. Enable this game object.
Exposing Parameters
In the Game window you should see some sliders and buttons. These will be the sliders to adjust the Music and SFX volumes.
In order to access the volume levels of your Music and SFX group, you need to expose those parameters. To do this, first select the Music group, then in the Inspector window, right click the Volume field and select the Expose ‘Volume (of Music)’ to script option.
Now select the SFX group and expose its Volume as well. Back in the Audio Mixer window, you should now see Exposed Parameters (2) at the top right.
Click on this and rename the newly exposed parameters respectively to musicVol and sfxVol. These are the names you’ll use to access them in scripts.
Select the AudioManager game object. In the Inspector window, navigate to the Audio Manager script. This contains an array of Audio Settings
.
Each element within the array has three values. The first two are for the sliders functionality, but the third is the exposed parameter that the slider influences. Type in the exposed parameter names where musicVol should be under the element containing the MusicSlider. sfxVol should be under the SFXSlider.
With that done, open up the AudioManager.cs script. The first thing you need to add is a reference to your Audio Mixer
. At the top of the script, underneath public static AudioManager instance;
add the following line:
public AudioMixer mixer;
Save the script, then return to the Unity Editor. A new field named Mixer should be on the Audio Manager component. Drag your MasterMixer from the Project window into this empty variable. This will give you a reference to your Audio Mixer so you can change its exposed parameters.
Open AudioManager.cs again, scroll down to the AudioSetting class, and just below the Initialize() method, add this new method:
public void SetExposedParam(float value) // 1
{
redX.SetActive(value <= slider.minValue); // 2
AudioManager.instance.mixer.SetFloat(exposedParam, value); // 3
PlayerPrefs.SetFloat(exposedParam, value); // 4
}
Here's what that code does:
- A public method that takes in a
float
for the new value. - A red X sprite that is set active when sound is completely disabled.
- The call to the
Audio Mixer
that sets the exposed parameter to the specified value. - A
PlayerPref
to remember the users volume choice.
In the main AudioManager
class, create the following two public methods that your sliders will hook to:
public void SetMusicVolume(float value)
{
audioSettings[(int)AudioGroups.Music].SetExposedParam(value);
}
public void SetSFXVolume(float value)
{
audioSettings[(int)AudioGroups.SFX].SetExposedParam(value);
}
Save the script. Head back to the Unity Editor and navigate to the MusicSlider game object.
Find the Slider component attached to the Music Slider object in the Inspector window. Then hook the AudioManager.SetMusicVolume
dynamically to the slider.
Then select the SFXSlider game object and do the same thing, but hook it dynamically to AudioManager.SetSFXVolume
.
Selecting Dynamic float passes in the current value of the slider to the method.
Save and run the project. You should now be able to adjust the sliders in-game to change the volume levels of the Music and SFX groups.
Congratulations! You just exposed and modified your first Audio Mixer fields.
Snapshots
Now it's time to give functionality to the two buttons that have been waiting patiently in the wings. These will be used to transition between Snapshots in your Audio Mixer.
The first thing you need is another Snapshot, which is basically a saved state of your Audio Mixer. For this example, you'll be creating a new Snapshot that focuses the audio levels to favor the Music group.
Click the + next to the Snapshots header, and name your new Snapshot MusicFocus, as the focus of the Snapshot will be on the Music group. Then rename the first Snapshot to Starting as it will be the default Snapshot that will be used, as indicated by the star icon to the right of the name.
With the MusicFocus Snapshot selected, adjust the Music group volume to be louder than the SFX group volume. You should now be able to jump back and forth between Snapshots, and the values for each Snapshot should be automatically saved.
With the setup done, you can dive back into the AudioManager
script, and add the following new instance variables to the top of the script.
private AudioMixerSnapshot startingSnapshot; // 1
private AudioMixerSnapshot musicFocusSnapshot; // 2
Here's what they do:
- A variable used to reference the starting snapshot.
- A variable used to reference the music focus snapshot.
At the top of the Start()
method, just before the for loop, add:
startingSnapshot = mixer.FindSnapshot("Starting"); // 1
musicFocusSnapshot = mixer.FindSnapshot("MusicFocus"); // 2
- Finds the Starting snapshot on the audio mixer and stores it in the
startingSnapshot
reference variable. - Finds the MusicFocus snapshot on the audio mixer and stores it in the
musicFocusSnapshot
reference variable.
Then create two new methods in the AudioManager
class that will be used to transition between each snapshot:
public void SnapshotStarting()
{
startingSnapshot.TransitionTo(.5f);
}
public void SnapshotMusic()
{
musicFocusSnapshot.TransitionTo(.5f);
}
TransitionTo
will interpolate from the current snapshot to the invoking snapshot over a specified time interval.
Save your script and return to the Unity Editor. Find the StartingSnapShot game object in the Hierarchy window.
In the Inspector window under the Button component, hook the button into the newly created SnapshotStarting()
method.
Then do the same with the MusicFocusSnapshot game object, but with the SnapshotMusic()
method.
Save and run your project. When you click the Music Focus button, you should see the volume of the Music group transition up, and the SFX group transition down. You can then transition back to the starting snapshot by clicking the Starting Snapshot button.
Where to Go From Here?
In case you missed anything, you can download the final project for this tutorial here.
That wraps up this introduction to the Unity Audio Mixer, and all of its major components.
I'd love to see the neat effects you come up with, so I encourage you to try out the other effects not covered by this tutorial! Also, have a go at using multiple Audio Mixers by routing one into another, for organizational and layering benefits.
If you have any questions or comments, please join in the forum discussion below!
The post Audio tutorial for Unity: the Audio Mixer appeared first on Ray Wenderlich.