Triggering Audio With Code!
After you have some Sound Groups and Playlists set up, let's see how to trigger them from code. You can usually trigger them without coding however.
There are several sections of code functionality (and basics), listed below. Click on the links below to read more about each.
- Your Class Files
- Playing Sounds
- The PlaySoundResult Object
- Chaining Sounds Together
- Controlling Sound Effects
- Controlling Playlists
- Playlist Controller Events
Your Class Files
There are a couple basics you should know about when writing a class file that will use the Master Audio API.-
Namespace: When using the API, at the top of the code file, add the following:
using DarkTonic.MasterAudio;
- Property Drawers: For your own code classes you may write, you can use these Property Drawers.
-
SoundGroupAttribute: use this to decorate public strings that are Sound Groups in your Inspectors like this:
[SoundGroupAttribute] public string laserSound;
-
MasterCustomEventAttribute: use this to decorate public strings that are Custom Events in your Inspectors like this:
[MasterCustomEventAttribute] public string laserCustomEvent;
-
PlaylistAttribute: use this to decorate public strings that are Playlists in your Inspectors like this:
[PlaylistAttribute] public string playlist;
-
SoundGroupAttribute: use this to decorate public strings that are Sound Groups in your Inspectors like this:
Playing Sounds
The following API commands are available for playing sounds:- If you need to trigger any Sound Groups during times other than those provided by the included scripts, you can use the following line of code:
MasterAudio.PlaySound(string soundGroupName, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
- volumePercentage: This lets you play a lower volume version (0-1 is the range, 1 is default).
- pitch: If specified, let you override the chosen Variation's pitch and random pitch and use the pitch parameter instead (default is 1).
- variationname: If specified, lets you play a specific Variation (or its clones created from Voices > 1) by name.
- delaySoundTime: Lets you schedule a sound to be played X seconds from now.
- Other variants of the
PlaySound
method for playing at specific positions and optional following. See Intelli-sense, code comments or API website for explanation of each parameter. These are the same asPlaySound
, but you are additionally passing in a Transform object or Vector3 so that the sound will trigger from its position. If you use theFollowTransform
methods, the chosen Sound Group's Variation will follow the caller until it is done playing.MasterAudio.PlaySoundAndForget(string sType, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
MasterAudio.PlaySound3DAtVector3(string sType, Vector3 sourcePosition, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
AtVector3
methods is useful for 2D games where the Z of the object making the sound might not want to be used. You can alter is and use this.MasterAudio.PlaySound3DAtVector3AndForget(string sType, Vector3 sourcePosition, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
MasterAudio.PlaySound3DAtTransform(string sType, Transform sourceTrans, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
MasterAudio.PlaySound3DAtTransformAndForget(string sType, Transform sourceTrans, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
MasterAudio.PlaySound3DFollowTransform(string sType, Transform sourceTrans, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
MasterAudio.PlaySound3DFollowTransformAndForget(string sType, Transform sourceTrans, float volumePercentage, float? pitch, float delaySoundTime, string variationName);
The PlaySoundResult object
Note: TheAndForget
methods above do not return a PlaySoundResult, and the rest do. It's used to subscribe to events and manipulate the sound after it has been started playing. If you don't need it, it is better for performance to not generate it (0 bytes GC Allocation without it). A PlaySoundResult object has the following properties:
- SoundPlayed (boolean): True unless the sound was scheduled (below) or failed to play because all Variations were busy, Bus voice limit reached, etc.
- SoundScheduled (boolean): False unless you scheduled a sound with the delaySoundTime field.
- ActingVariation (SoundGroupVariation): This will give you access to the actual Variation used, if a sound was played. You can use
ActingVariation.audio
to access the properties of the Audio Source for the Variation used. You should *never* set volume this way though, as it will not take into account all the other Master Audio calculations for volume (Sound Group / Bus / Variation / Master Mixer Volume). If you do, it will appear that Master Audio is not working correctly. Instead, useActingVariation.AdjustVolume
for this so the calculations will be correct.
- Get notified of when a sound is finished playing so you can do something, like this (always check if it's null first!):
- The PlaySoundResult can also be used to fade a clip out early, like this:
Chaining Sounds Together
You can also write a Coroutine to "chain" several sounds together, like this:Controlling Sound Effects
There are dozens of control methods for Sound Groups. Here's a few:MasterAudio.StopAllOfSound(string soundGroupName);
MasterAudio.FadeOutAllOfSound(string soundGroupName, float fadeTime);
-
MasterAudio.MasterVolumeLevel
-
MasterAudio.GetGroupVolume(string soundType);
-
MasterAudio.SetGroupVolume(string soundType, float volume);
-
MasterAudio.MuteGroup(string soundType);
-
MasterAudio.UnmuteGroup(string soundType);
-
MasterAudio.SoloGroup(string soundType);
-
MasterAudio.UnsoloGroup(string soundType);
-
MasterAudio.GrabGroup(string soundType);
-
MasterAudio.SetBusVolumeByName(float volume, string busName);
-
MasterAudio.GrabBusByName(string busName);
Controlling Playlists
There are many control methods for Playlist Controllers. Here's a few:MasterAudio.TriggerPlaylistClip(string clipName);
MasterAudio.ChangePlaylistByName(string playlistName, bool playFirstClip);
MasterAudio.StartPlaylist(string playlistName);
MasterAudio.StopPlaylist();
MasterAudio.QueuePlaylistClip(string clipName);
MasterAudio.GrabPlaylist(string playlistName);
GrabPlaylist
) work if you only have a single Playlist Controller in the Scene. If you have more than one, there are overloaded methods of each method that take the Playlist Controller name as a parameter as well. Some have an "all Playlist Controllers" method as well, so you can "pause all music" etc.
Playlist Controller Events
You can subscribe to the events in the Playlist Controller through code. These are a some of the supported events:-
SongChanged
: This event in the Playlist Controller class can notify you when the song changes (when crossfading starts if using crossfading). Sample code looks like this: -
SongEnded
: This event can be hooked up to with the same type of code and notifies you when a song stops playing (different that SongChanged when using crossfading).
PlaylistFade
: You can execute a method when a gradual fade you asked for is completed as well. That code looks like this: