Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Core.AudioStack Class Reference

A stack system for 2D audio sources. More...

+ Inheritance diagram for Hydrogen.Core.AudioStack:

Public Member Functions

string Add (AudioClip clip)
 Add/Load an AudioStackItem More...
 
string Add (AudioClip clip, bool createDuplicate)
 Add/Load an AudioStackItem More...
 
string Add (AudioStackItem item)
 Add/Load an AudioStackItem More...
 
string Add (AudioStackItem item, bool createDuplicate)
 Add/Load an AudioStackItem More...
 
bool IsLoaded (AudioClip clip)
 Determines whether an AudioClip is currently loaded and managed by the AudioStack. More...
 
bool IsLoaded (AudioStackItem item)
 Determines whether an AudioStackItem is currently loaded and managed by the AudioStack. More...
 
bool IsLoaded (string key)
 Determines whether there is something loaded in the AudioStack based on the reference key. More...
 
bool IsPlaying (AudioClip clip)
 Determines whether an AudioClip is playing. More...
 
bool IsPlaying (AudioStackItem item)
 Determines whether an AudioStackItem is playing. More...
 
bool IsPlaying (string key)
 Determines whether an AudioStackItem is playing by reference to the specified key. More...
 
void Remove (AudioClip clip)
 Removes the specified AudioClip and its associated AudioStackItem from the AudioStack. More...
 
void Remove (AudioStackItem item)
 Removes the specified AudioStackItem from the AudioStack. More...
 
void Remove (string key)
 Remove the AudioStackItem associated to the specified key in the AudioStack. More...
 

Public Attributes

int MaximumSources = 20
 Maximum number of sources to have in total (Stack + Playing). More...
 
int MinimumSources = 10
 Minimum number of sources to have in total (Stack + Playing). More...
 
bool UsePriorities
 Should the stack look at the priority of the existing Audio Stack Items playing, and if the stack is full stop the lowest and use it's newly available source to play the new item. More...
 

Protected Member Functions

virtual void Awake ()
 Unity's Awake Event More...
 
virtual void Update ()
 Unity's Update Event More...
 

Properties

Dictionary< string,
AudioStackItem
LoadedItems [get]
 Gets all currently loaded AudioStackItems. More...
 
int SourcesCount [get]
 The number of currently used AudioSources. More...
 

Detailed Description

A stack system for 2D audio sources.

This should NEVER be used for 3D audio sources.

Definition at line 40 of file AudioStack.cs.

Member Function Documentation

string Hydrogen.Core.AudioStack.Add ( AudioClip  clip)

Add/Load an AudioStackItem

Parameters
clipAn AudioClip to be used to create a new AudioStackItem from.

Definition at line 89 of file AudioStack.cs.

90  {
91  return Add (new AudioStackItem (clip), false);
92  }
string Add(AudioClip clip)
Add/Load an AudioStackItem
Definition: AudioStack.cs:89
string Hydrogen.Core.AudioStack.Add ( AudioClip  clip,
bool  createDuplicate 
)

Add/Load an AudioStackItem

Parameters
clipAn AudioClip to be used to create a new AudioStackItem from.
createDuplicateCreate a duplicate entry if need be.

Definition at line 99 of file AudioStack.cs.

100  {
101  return Add (new AudioStackItem (clip), createDuplicate);
102  }
string Add(AudioClip clip)
Add/Load an AudioStackItem
Definition: AudioStack.cs:89
string Hydrogen.Core.AudioStack.Add ( AudioStackItem  item)

Add/Load an AudioStackItem

Parameters
itemAn AudioStackItem.

Definition at line 108 of file AudioStack.cs.

109  {
110  return Add (item, false);
111  }
string Add(AudioClip clip)
Add/Load an AudioStackItem
Definition: AudioStack.cs:89
string Hydrogen.Core.AudioStack.Add ( AudioStackItem  item,
bool  createDuplicate 
)

Add/Load an AudioStackItem

Parameters
itemAn AudioStackItem.
createDuplicateCreate a duplicate entry if need be.

Definition at line 118 of file AudioStack.cs.

119  {
120  if (IsLoaded (item.Key) && !createDuplicate) {
121 
122  // Update any settings we need too
123  _loadedItems [item.Key].Fade = item.Fade;
124  _loadedItems [item.Key].Loop = item.Loop;
125  _loadedItems [item.Key].PlayOnLoad = item.PlayOnLoad;
126 
127  _loadedItems [item.Key].FadeInSpeed = item.FadeInSpeed;
128  _loadedItems [item.Key].FadeOutSpeed = item.FadeOutSpeed;
129  _loadedItems [item.Key].Persistant = item.Persistant;
130  _loadedItems [item.Key].TargetVolume = item.TargetVolume;
131  _loadedItems [item.Key].StartVolume = item.StartVolume;
132  _loadedItems [item.Key].RemoveAfterFadeOut = item.RemoveAfterFadeOut;
133  _loadedItems [item.Key].Priority = item.Priority;
134 
135  if (_loadedItems [item.Key].Source != null) {
136  _loadedItems [item.Key].Source.priority = item.Priority;
137  }
138 
139  } else {
140 
141  // If we do not have enough sources, we should make one, if we can.
142  if (_audioSources.Count <= 0 && _loadedItems.Count < MaximumSources) {
143  var source = _poolObject.AddComponent<AudioSource> ();
144 
145  // Make sure we dont have any fun little hickups
146  source.playOnAwake = false;
147 
148  _audioSources.Push (source);
149  }
150 
151  // Let's rock this playing of one
152  if (_audioSources.Count > 0) {
153 
154  // Create a slightly different key if need be
155  // You should never be starting the exact same sound twice in the same frame,
156  // thus we can just use the this simple addition
157  if (createDuplicate && IsLoaded (item.Key)) {
158  item.Key = item.Key + Time.time + item.GetHashCode ();
159  }
160 
161  // Use this stack for callback
162  // TODO: Don't like this
163  item.Stack = this;
164 
165  item.Source = _audioSources.Pop () as AudioSource;
166  // Update Our Source
167  item.Source.clip = item.Clip;
168  item.Source.volume = item.StartVolume;
169  item.Source.loop = item.Loop;
170  item.Source.priority = item.Priority;
171 
172  // Auto Play Stuff
173  if (item.PlayOnLoad) {
174  item.Source.Play ();
175  }
176 
177  _loadedItems.Add (item.Key, item);
178 
179  } else if (_audioSources.Count <= 0 && UsePriorities) {
180 
181  // Find our sucker to replace.
182  //TODO: Maybe we can search for how long a track has already played
183  AudioStackItem replaceItem = item;
184  foreach (string s in _loadedItems.Keys.ToList()) {
185  if (_loadedItems [s].Priority < replaceItem.Priority &&
186  !_loadedItems [s].Loop) {
187  replaceItem = _loadedItems [s];
188  }
189  }
190 
191  // Did any thing qualify?
192  if (replaceItem != item) {
193  Remove (replaceItem);
194  Add (item);
195  } else {
196  Debug.Log ("No available Audio Sources from the AudioStack to use, even when prioritized.");
197  }
198  } else {
199  Debug.Log ("No available Audio Sources from the AudioStack to use.");
200  }
201  }
202 
203  return item.Key;
204  }
void Remove(AudioClip clip)
Removes the specified AudioClip and its associated AudioStackItem from the AudioStack.
Definition: AudioStack.cs:271
int MaximumSources
Maximum number of sources to have in total (Stack + Playing).
Definition: AudioStack.cs:45
string Add(AudioClip clip)
Add/Load an AudioStackItem
Definition: AudioStack.cs:89
bool UsePriorities
Should the stack look at the priority of the existing Audio Stack Items playing, and if the stack is ...
Definition: AudioStack.cs:55
bool IsLoaded(AudioClip clip)
Determines whether an AudioClip is currently loaded and managed by the AudioStack.
Definition: AudioStack.cs:211
virtual void Hydrogen.Core.AudioStack.Awake ( )
protectedvirtual

Unity's Awake Event

Reimplemented in hAudioStack.

Definition at line 317 of file AudioStack.cs.

318  {
319  // Create our stack
320  _audioSources = new Stack (MinimumSources);
321  // Create our holder GameObject
322  _poolObject = new GameObject ();
323  // Create our lookup
324  _loadedItems = new Dictionary<string, AudioStackItem> ();
325 
326  // Assign our new friend as our child
327  _poolObject.transform.parent = gameObject.transform;
328  _poolObject.name = "Audio Pool";
329 
330  // Create the AudioSources and associate them to their respective arrays.
331  for (int x = 0; x < MinimumSources; x++) {
332 
333  var source = _poolObject.AddComponent<AudioSource> ();
334 
335  // Make sure we dont have any fun little hickups
336  source.playOnAwake = false;
337 
338  _audioSources.Push (source);
339  }
340  }
int MinimumSources
Minimum number of sources to have in total (Stack + Playing).
Definition: AudioStack.cs:49
bool Hydrogen.Core.AudioStack.IsLoaded ( AudioClip  clip)

Determines whether an AudioClip is currently loaded and managed by the AudioStack.

Returns
Is the AudioClip present in the AudioStack?
Parameters
clipAn AudioClip.

Definition at line 211 of file AudioStack.cs.

212  {
213  return clip != null && _loadedItems.ContainsKey (clip.name);
214  }
bool Hydrogen.Core.AudioStack.IsLoaded ( AudioStackItem  item)

Determines whether an AudioStackItem is currently loaded and managed by the AudioStack.

Returns
Is the AudioStackItem present in the AudioStack?
Parameters
itemAn AudioStackItem.

Definition at line 221 of file AudioStack.cs.

222  {
223  return item != null && IsLoaded (item.Key);
224  }
bool IsLoaded(AudioClip clip)
Determines whether an AudioClip is currently loaded and managed by the AudioStack.
Definition: AudioStack.cs:211
bool Hydrogen.Core.AudioStack.IsLoaded ( string  key)

Determines whether there is something loaded in the AudioStack based on the reference key.

Returns
Is something loaded and associate to the target key.
Parameters
keyTarget Key.

Definition at line 231 of file AudioStack.cs.

232  {
233  return key != null && _loadedItems.ContainsKey (key);
234  }
bool Hydrogen.Core.AudioStack.IsPlaying ( AudioClip  clip)

Determines whether an AudioClip is playing.

Returns
Is the AudioClip playing?

Must be playing through a managed AudioSource.

Parameters
clipAn AudioClip.

Definition at line 242 of file AudioStack.cs.

243  {
244  return IsLoaded (clip.name) && _loadedItems [clip.name].Source.isPlaying;
245  }
bool IsLoaded(AudioClip clip)
Determines whether an AudioClip is currently loaded and managed by the AudioStack.
Definition: AudioStack.cs:211
bool Hydrogen.Core.AudioStack.IsPlaying ( AudioStackItem  item)

Determines whether an AudioStackItem is playing.

Returns
Is the AudioStackItem playing?
Parameters
itemAn AudioStackItem.

Definition at line 252 of file AudioStack.cs.

253  {
254  return item != null && item.Source != null && item.Source.isPlaying;
255  }
bool Hydrogen.Core.AudioStack.IsPlaying ( string  key)

Determines whether an AudioStackItem is playing by reference to the specified key.

Returns
Is the AudioStackItem referenced by the specified key?
Parameters
keyTarget Key.

Definition at line 262 of file AudioStack.cs.

263  {
264  return key != null && IsLoaded (key) && _loadedItems [key].Source.isPlaying;
265  }
bool IsLoaded(AudioClip clip)
Determines whether an AudioClip is currently loaded and managed by the AudioStack.
Definition: AudioStack.cs:211
void Hydrogen.Core.AudioStack.Remove ( AudioClip  clip)

Removes the specified AudioClip and its associated AudioStackItem from the AudioStack.

Parameters
clipAn AudioClip.

Definition at line 271 of file AudioStack.cs.

272  {
273  if (IsLoaded (clip)) {
274  Remove (_loadedItems [clip.name]);
275  }
276  }
void Remove(AudioClip clip)
Removes the specified AudioClip and its associated AudioStackItem from the AudioStack.
Definition: AudioStack.cs:271
bool IsLoaded(AudioClip clip)
Determines whether an AudioClip is currently loaded and managed by the AudioStack.
Definition: AudioStack.cs:211
void Hydrogen.Core.AudioStack.Remove ( AudioStackItem  item)

Removes the specified AudioStackItem from the AudioStack.

Parameters
itemAn AudioStackItem.

Definition at line 282 of file AudioStack.cs.

283  {
284  if (!IsLoaded (item))
285  return;
286 
287  // Make sure the item is stopped
288  item.Source.Stop ();
289 
290  // Remove the clip association
291  item.Source.clip = null;
292 
293  // Add the source back to the stack
294  _audioSources.Push (item.Source);
295 
296  // Remove our item definition
297  _loadedItems.Remove (item.Key);
298 
299  // Free Item
300  item = null;
301  }
bool IsLoaded(AudioClip clip)
Determines whether an AudioClip is currently loaded and managed by the AudioStack.
Definition: AudioStack.cs:211
void Hydrogen.Core.AudioStack.Remove ( string  key)

Remove the AudioStackItem associated to the specified key in the AudioStack.

Parameters
keyTarget Key.

Definition at line 307 of file AudioStack.cs.

308  {
309  if (IsLoaded (key)) {
310  Remove (_loadedItems [key]);
311  }
312  }
void Remove(AudioClip clip)
Removes the specified AudioClip and its associated AudioStackItem from the AudioStack.
Definition: AudioStack.cs:271
bool IsLoaded(AudioClip clip)
Determines whether an AudioClip is currently loaded and managed by the AudioStack.
Definition: AudioStack.cs:211
virtual void Hydrogen.Core.AudioStack.Update ( )
protectedvirtual

Unity's Update Event

> This neeeds to be executed to handle processing the Audio Sources correctly.

Definition at line 348 of file AudioStack.cs.

349  {
350  foreach (string s in _loadedItems.Keys.ToList()) {
351  _loadedItems [s].Process ();
352  }
353  }

Member Data Documentation

int Hydrogen.Core.AudioStack.MaximumSources = 20

Maximum number of sources to have in total (Stack + Playing).

Definition at line 45 of file AudioStack.cs.

int Hydrogen.Core.AudioStack.MinimumSources = 10

Minimum number of sources to have in total (Stack + Playing).

Definition at line 49 of file AudioStack.cs.

bool Hydrogen.Core.AudioStack.UsePriorities

Should the stack look at the priority of the existing Audio Stack Items playing, and if the stack is full stop the lowest and use it's newly available source to play the new item.

This will ignore any playing sources that are set to loop.

Definition at line 55 of file AudioStack.cs.

Property Documentation

Dictionary<string, AudioStackItem> Hydrogen.Core.AudioStack.LoadedItems
get

Gets all currently loaded AudioStackItems.

The loaded AudioStackItems with their associated keys.

Definition at line 73 of file AudioStack.cs.

int Hydrogen.Core.AudioStack.SourcesCount
get

The number of currently used AudioSources.

Used AudioSource Count.

Definition at line 81 of file AudioStack.cs.