PIC


Audio Controller for Unity Game Engine.

THE AUDIO CONTROLLER IS A WELL OPTIMIZED SOLUTION FOR THE UNITY GAME ENGINE. AIMED FOR SMOOTH AUDIO CHANGES EVEN UNDER HARD CONDITIONS. AMBIENT SOUND SOURCES ARE CONVENIENT TO USE AND SHOWN IN AN INTELLIGENT WAY.

Last changed: June, 2019

Company: metadesc, Helmut Kleber

Author: Helmut Kleber

Contents

List of Figures
1 Introduction
2 Components
 2.1 Sub-controller of the audio controller
  2.1.1 Music controller
  2.1.2 Sound effect controller
  2.1.3 Ambient sound controller
 2.2 Sound Effect
 2.3 Additional Options
 2.4 Ducking
 2.5 Detail Options
 2.6 Additional components
 2.7 Testing the configured audio entries
  2.7.1 Some examples how to use it in source code
 2.8 Dynamic changes (Volume, Pitch)
  2.8.1 Set the pitch eg. for a faster moving engine
 2.9 Check the audio source is running
  2.9.1 Check directly the audio source
  2.9.2 Check the defined audio source fully finished
  2.9.3 Forcibly ended
 2.10 Frocibly end an audio source
3 The ambient source activator
4 The event based audio triggerer
5 Outlook

List of Figures

Music Controller
Sound effect controller
Ambient sound controller
Sound effect controller
Ambient sound controller
Ambient sound controller
Ducking
Filter manager
IPool
10 Clock
11 Timer manager
12 Audio Tester
13 Ambient source activator
14 Event based audio triggerer

1 Introduction

The audio controller is made for the purpose to support all audio domains for game development. It comes with a pre-configured audio controller, which covers the main features for game development. The audio sources, the images are all created by myself and can be used without any copyright violations. Use the tool-tips/header text, if available, for attribute documentation. This document will not go in detail to explain each attributes. The audio controller is a fast, simple, but it can be also very detailed. The Unity components like the mixer, the filter are embedded and sometimes enhanced (filter can be attached dynamically to the audio source, mixer can also be set for an audio controller and must not be set for each audio source). The ambient sound handling on the top is the best one on the Unity asset store. The audio sources are visible in the scene and can be moved around easily.

2 Components

The different sound controller made with the focus to smoothly act on sound changes. Even ducking can have a smooth start and end. The music controller is also able to fade in/out or mixing the end of one clip and the start of the next clip. The ambient sound sources, which are visible by gizmo, can be moved around with the mouse. The ambient sound sources also fade in/out in a smooth way. The SFX controller additionally supports looping sound effects, eg. for looping engine clip, and allows changing the pitch to simulate faster/slower sound effects.
The number ambient/music/sound effect controllers is not limited. This freedom may help you organizing the audio clips in a better way. In the example project I defined a second music controller to add effects to the music player controller. The second music player ducks away the first music controller and plays the effect in this way over the currently playing clip.
The Unity components, like the mixer and the filter are also used. The advantage is, that the mixer must not be set for each audio source manually. This can be set once on the top level. The filter also can be set for different levels (on controller/clip group/clip).
The pooling for filter and audio sources speeds up the audio controller.

2.1 Sub-controller of the audio controller

2.1.1 Music controller

The music controller is used to play background audio clips. It has a player audio list (eg. for background music with cross-fade switching) and a dynamic audio list for switching the current running audio clip to a specific audio clip. Eg. switch a soft background song to a war or danger song.


PIC

Figure 1: Music Controller


2.1.2 Sound effect controller

Use this controller for looping audio sounds like engine, or for single audio clips, like explosion sound. The Play(...) methods returns the AudioSource, that can be directly modified by the user. The main purpose of sound effects are to follow objects (eg. engine) or to play the audio clip in the certain position (eg. explosion).


PIC

Figure 2: Sound effect controller


2.1.3 Ambient sound controller

The ambient sound is the main purpose for this audio controller. The ambient sound effective area is visible in the scene, so you will have an overview for ambient audio sources in your scene. I found this very important. You can click on the visual audio spheres and you can move them in the scene. To differentiate the ambient sounds a color can be assigned to them. The ambient sounds will be activated, if the listener is inside the ambient sound range. For this feature the AmbientSoundActivator must be attached to the listener. The range is defined by a fixed range, that is not the range of the audio source. In this way the ambient sound source can have a simple volume ramp. This is needed to shut down ambient sources to avoid playing multiple clips, which are not anymore recognizable. However, if the listener enters the ambient sound source, the volume will fade in. This also enables more dynamic clip changes, suppresses noises from other clips and can fade in a recognizable volume.


PIC

Figure 3: Ambient sound controller


2.2 Sound Effect

The sound effect can be referenced by its GameObejct name. It has also ’Repeat settings’, eg. it can be ’once’ or ’loop’. ’loop’ eg. for a looping engine, ’once’ for an explosion.

Between Min/Max Repeat Time the repeat time will be rolled influenced by the ’Repeat Weight’.

’Max Instances’ Defines how many instances can be exists.

’Sound Entries’ contains the variations of the effects. Each variation has a clip, one of the clips will be selected randomly.

It is also possible to see the sound effect by a sphere with the defined radius for its lifetime.


PIC

Figure 4: Sound effect controller


2.3 Additional Options

The ’Additional Options’ are similar to the other controller entries. In this case the ’Additional Options’ for music entries are shown.

With ’Override Volume’/’Override Pitch’ these settings can be overridden for each clip entries. It is also possible to override these settings for the controller and in the highest level, on the ’Global Settings’. The ’Audio Source Template’ refrences with a string a GameObject with an AudioSource component on it, in the pool manager IPool. When using the referenced Unity AudioSource component the values in ’options’ will be used to setup the AudioSource. A Unity mixer can be attached to the options for a single clip. Also a filter can be attached, which is managed by the FilterManager. The reference to the filter is also a string. The “Override Filter” overrides the settings in the higher level definitions and the ’Use Filter’ is for disable/enable the filter usage. Also ’Ducking’ can be overridden for a single clip.


PIC

Figure 5: Ambient sound controller


2.4 Ducking

Ducking is to decrease the volume on other controllers to make the clip more present. This can be eg. a mission description. More controllers can be selected. In this example the MusicControler ’MC’ and the AmbientSoundController ’AMB’ is ducked to 0.4 (40%) volume strength. The ducking can also be used to play effects over the continuously playing song, eg. danger signalizing drum. The ’Fade In Time’ defines the time until the ducked controller reaches the defined volume. The volume will be changed by linear interpolation. The ’Fade Out Time’ works in the same way, but at the end. This is needed to avoid mixing clips instantly, that mostly sounds non professional. This can be compared with terrain textures with no smoothing, with hard edges. The ducking can be defined for the entire controller, for the clip group and for each clips separately.


PIC

Figure 6: Ambient sound controller


2.5 Detail Options

The ’Detail Options’ has mostly the attributes for the Unity AudioSource. These settings will be set to setup the AudioSource of the pooled, re-used AudioSource based GameObject. You have always activate override, if you use the SoundSource in different configuration. I will not explain the Unity AudioSource based attributes, they can be read in the Unity documentation. The “Skip Beginning” and the “Skip Ending” are not Unity AudioSource settings.
The “Skip Beginning” skips the clip start phase. This makes sense, if the first part of the clip simply should not be played, maybe too boring, or too wild. You can also use this feature to play from a multiple effect clip one effect. “Skip Ending” cuts the last part of the clip for different reasons. With “Skip Beginning” and “Skip Ending” a certain part of the clip can be played. This feature makes no changes to the clips.


PIC

Figure 7: Ducking


2.6 Additional components

2.7 Testing the configured audio entries

The audio tester is a component, which can have more entries for playing audio entries. With this component you can test the background music cross fading, the ducking, and all the other features. You don’t have to wait in your game until a specific event happens. Here you can also blend together the background, the sound effects, the background overlay and the ambient sounds to adjust the volume, ducking for the controller or for specific audio entries. Don’t forget to disable the AmbientSoundActivator on the audio listener to test the ambient sounds in the audio tester. If the AmbientSoundActivator is enabled, then you can still move the ambient sources to the listener in the scene view. The audio tester can just be used in the running scene, not in the editor, because the different complex components around the audio controller, eg. the Unity mixer and filter.


PIC

Figure 12: Audio Tester


2.7.1 Some examples how to use it in source code

2.8 Dynamic changes (Volume, Pitch)

How to dynamically change the volume and the pitch for a looping sound source (for SFX, eg. looping engine)

SoundSource sr = Metadesc.AudioController.I.Sc("SFX").Play("Engine", transform);
// The volume will be used to fade the audio sources, for this reason it cannot be set
// directly on "sr.AS.volume = 0.5f", but you can set it in this way:
sr.CurrentVolume = 0.5f;

2.8.1 Set the pitch eg. for a faster moving engine

Keep in mind that the dynamic pitch changes for running audio sources changes the duration of the audio clips. If you dynamically set the pitch for a loop, that will work, but for a non looped audio source this can have different issues. If the pitch is set to a higher value the audio clip ends faster, so fade out can be skipped. If this is set to a lower value, the fade out part will have a longer duration.

Hint: Just change dynamically the pitch for looping audio sources.
SoundSource sr = Metadesc.AudioController.I.Sc("SFX").Play("Engine", transform);
// This can be set directly on the audio source:
sr.AR.pitch = 0.5f;

2.9 Check the audio source is running

2.9.1 Check directly the audio source

if (sr.AR != null && SR.AR.volume) {
    // The audio source is not playing, if we end it at this point, no strange things happens.
}

2.9.2 Check the defined audio source fully finished

if (sr.AR != null) {
    // sr.AR will be set to null at the very end. At this point the audio source finished fully with its defined end delay.
}

2.9.3 Forcibly ended

if (sr.AR != null && sr.fadeOutAudioSrc == true) {
    // The audio source is not playing, if we end it at this point, no strange things happens.
}

2.10 Frocibly end an audio source

Sometimes it makes sense to forcibly end a running audio source. If you would simply add an audio source instantly, then strange sounds can happen. For this purpose you can set in the result the "fadeOutAudioSrc" variable to true. This will signalize the defined audio source to fall through on each state, but play the fade out part to avoid strange sound transition and strange noises.

sr.AR.fadeOutAudioSrc = true;

3 The ambient source activator

The class AmbientSourceActivator must be attached on the listener, usually on the main camera. This component blends in and out the ambient sound sources, depends on the ambient sound source distance. On this component the min and max update distance can be set. A randomized value will be the next update distance time, that maybe helps to avoid load spikes, by do not causing load exactly in each periods.


PIC

Figure 13: Ambient source activator


4 The event based audio triggerer

The EventBasedAudioTriggerer class provides playing audio clips for certain callbacks. Callbacks like Start(), OnEnable(). This can be used eg. to play a destruction clip at OnDestruction, without deeper coding complexity. The ’Trigger Type’ defines the trigger event. The “Controller Type” defines the audio controller type, in the image the “SFX” controller has been used. The name of the controller is also “SFX”. The name of the sound source on the “SFX” controller is “Explosion”. Just in case of the music controller type the “Dyn” (Dynamic) has an impact. The music controller has usually looping music clips, but for certain events in games the looping music must be dynamically changed, eg. playing danger music. The GameObject on that the clip is played can be set by the “AttachOnThis” Transform typed field. The clip can be played also on a certain position. You can add more entries with different or with the same events. This component is just a helper component, which is can be enough for prototyping or simple game projects.


PIC

Figure 14: Event based audio triggerer


5 Outlook

This audio solution is a well rounded project, in my opinion the best audio controller at this time on the asset store. It is simple to use, well adapted in Unity, allows performance optimizations. In the future I will keep updating this project and I’m open for new features or new ideas.


PIC