Skip to content

Commit

Permalink
Version updated to 0.2.2.2.
Browse files Browse the repository at this point in the history
Resolved:
1. Large-scale refactoring. Since int was used as the key for dictionaries, which can act as an array index, all dictionaries in the EazySoundManager class have been replaced with the AudioList class, this will avoid the overhead associated with dictionaries and gives potential scope for optimization. AudioList, in turn, is an implementation of the standard List<Audio>, but internally it uses a regular array, all elements of which are static and will never be moved, as a result of which it is guaranteed that one unique int type id corresponds to one element at the same time.
2. Now in the EazySoundSystem class, when deleting Audio from AudioList, the queue is not used as it was with the dictionary, this avoids unnecessary costs.
3. Fixed an error when the GetAudioFast() method in the EazySoundManager class did not return an element with index 0.
4. Fixed an error when the sound did not continue after changing the scene when the corresponding setting was enabled.
5. Fixed an error when in the Audio class a clip for AudioSource was assigned immediately when creating an instance of the class.
6. The READMI has been updated.
  • Loading branch information
Linerichka committed Feb 23, 2024
1 parent e95ca66 commit 1c380d4
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 117 deletions.
19 changes: 5 additions & 14 deletions Assets/Lineri/SoundSystem/Eazy Sound Manager/Scripts/Audio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ public enum AudioType
UISound
}

private static int _audioCounter = int.MinValue;

private AudioClip _clip;
private float _volume;
private bool _loop;
Expand All @@ -232,19 +230,17 @@ public enum AudioType
private float _fadeInterpolater = 0f;
private float _onFadeStartVolume;

public Audio(in AudioType audioType, AudioClip clip, in bool loop, in bool persist, in float volume, in float fadeInValue,
public Audio(in int audioID, in AudioType audioType, AudioClip clip, in bool loop, in bool persist, in float volume, in float fadeInValue,
in float fadeOutValue, Transform sourceTransform, AudioSource audioSource, in bool overrideAudioSourceSettings = true)
{
// Set unique audio ID
AudioID = _audioCounter;
if (_audioCounter == int.MaxValue) ResetCounter();
else _audioCounter++;
AudioID = audioID;

/// Initialize values
/// Use private fields for setting to prevent parameters from being applied to the AudioSource
this.AudioSource = audioSource;
this.Type = audioType;
this.Clip = clip;
this._clip = clip;
this.SourceTransform = sourceTransform;
this._loop = loop;
this.Persist = persist;
Expand All @@ -268,7 +264,7 @@ private void SetValueAudioSource()
{
if (DeleteAudioSource)
{
AudioSource.clip = Clip;
AudioSource.clip = _clip;
AudioSource.loop = Loop;
AudioSource.mute = Mute;
AudioSource.volume = Volume;
Expand All @@ -286,7 +282,7 @@ private void SetValueAudioSource()
//uses the current audio source settings, except for some of them
else
{
AudioSource.clip = Clip;
AudioSource.clip = _clip;
AudioSource.loop = Loop;
AudioSource.volume = Volume;
AudioSource.pitch = Pitch;
Expand Down Expand Up @@ -488,10 +484,5 @@ public void Delete()
AudioSource = null;
Deleted = true;
}

public static void ResetCounter()
{
_audioCounter = int.MinValue;
}
}
}
261 changes: 261 additions & 0 deletions Assets/Lineri/SoundSystem/Eazy Sound Manager/Scripts/AudioList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Lineri.SoundSystem
{
public class ListAudio : ICollection, IEnumerable<Audio>
{
public int Count => _array.Length;
public bool IsSynchronized => _array.IsSynchronized;
public object SyncRoot => _array.SyncRoot;
public bool IsFixedSize => false;
public bool IsReadOnly => false;

private int _capacity = 64;
/// <summary>
/// Do not specify less than 8. Workability is not guaranteed if the number is less than 8.
/// </summary>
public int Capacity
{
get => _capacity;
set
{
_capacity = value;
RecreateArray();
}
}

private Audio[] _array;
private int _lastIndex = 0;

#region Constructors
public ListAudio()
{
_array = new Audio[_capacity];
}

public ListAudio(int capacity)
{
_array = new Audio[capacity];
}

public ListAudio(Audio[] array)
{
_array = (Audio[])array.Clone();
Capacity = array.Length < Capacity ? Capacity : array.Length;
}
#endregion

public Audio[] ToArray()
{
return (Audio[])_array.Clone();
}

public void CopyTo(Array array, int index)
{
_array.CopyTo(array, index);
}

#region Enumerators
IEnumerator IEnumerable.GetEnumerator()
{
return _array.GetEnumerator();
}

public IEnumerator<Audio> GetEnumerator()
{
int length = _array.Length;
for (int i = 0; i < length; i++)
{
if (_array[i] != null) yield return _array[i];
}
}
#endregion

public Audio this[int index]
{
get
{
return _array[index];
}
set
{
_array[index] = value;
}
}

public void Remove(int index)
{
_array[index] = null;
}

public void Clear()
{
for (int i = _array.Length - 1; i >= 0; i--)
{
_array[i] = null;
}
}

public bool Contains(int index)
{
if (index < 0 || index >= _array.Length) return false;
return _array[index] != null;
}

#region IndexOf
public int IndexOf(Audio value)
{
for (int i = _array.Length - 1; i >= 7; i -= 8)
{
if (_array[i] == value) return i;
else if (_array[i - 1] == value) return i - 1;
else if (_array[i - 2] == value) return i - 2;
else if (_array[i - 3] == value) return i - 3;
else if (_array[i - 4] == value) return i - 4;
else if (_array[i - 5] == value) return i - 5;
else if (_array[i - 6] == value) return i - 6;
else if (_array[i - 7] == value) return i - 7;
}

if (_array[0] == value) return 0;
else if (_array[1] == value) return 1;
else if (_array[2] == value) return 2;
else if (_array[3] == value) return 3;
else if (_array[4] == value) return 4;
else if (_array[5] == value) return 5;
else if (_array[6] == value) return 6;
else if (_array[7] == value) return 7;

return -1;
}

public int IndexOf(Predicate<Audio> predicate)
{
for (int i = _array.Length - 1; i >= 0; i--)
{
if (predicate(_array[i])) return i;
}

return -1;
}

public int IndexOf(AudioClip value)
{
for (int i = _array.Length - 1; i >= 0; i--)
{
if (_array[i]?.Clip == value) return i;
}

return -1;
}
#endregion

public int Add(Audio audio)
{
int index = GetFreeIndex();
_array[index] = audio;
return index;
}

public int GetFreeIndex(bool reset = true)
{
int length = _array.Length;

if (_lastIndex >= length - 1)
{
if (reset)
{
ResetIndex();
}
else
{
Capacity *= 2;
}
}

if (_array[_lastIndex] == null)
{
return _lastIndex;
}
else
{
_lastIndex++;

if (_array[_lastIndex] == null)
{
return _lastIndex;
}
}

for (int i = _lastIndex; i < length; i++)
{
if (_array[i] != null)
{
_lastIndex++;
}
else
{
return i;
}
}

return GetFreeIndex(false);
}

public int GetCountNoNull()
{
int r0 = 0;
int r1 = 0;
int r2 = 0;
int r3 = 0;
int r4 = 0;
int r5 = 0;
int r6 = 0;
int r7 = 0;

int i = _array.Length - 1;
for (; i >= 7; i -= 8)
{
if (_array[i] != null) r0++;
if (_array[i - 1] != null) r1++;
if (_array[i - 2] != null) r2++;
if (_array[i - 3] != null) r3++;
if (_array[i - 4] != null) r4++;
if (_array[i - 5] != null) r5++;
if (_array[i - 6] != null) r6++;
if (_array[i - 7] != null) r7++;
}

if (i > 0 && _array[0] != null) r0++;
if (i > 1 && _array[1] != null) r1++;
if (i > 2 && _array[2] != null) r2++;
if (i > 3 && _array[3] != null) r3++;
if (i > 4 && _array[4] != null) r4++;
if (i > 5 && _array[5] != null) r5++;
if (i > 6 && _array[6] != null) r6++;
if (i > 7 && _array[7] != null) r7++;

return r0+r1+r2+r3+r4+r5+r6+r7;
}

private void ResetIndex()
{
_lastIndex = 0;
}

private void RecreateArray()
{
Audio[] result = new Audio[_capacity];

for (int i = _array.Length - 1; i >= 0; i--)
{
result[i] = _array[i];
}

_array = result;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1c380d4

Please sign in to comment.