optimize audio resource tracking

pull/47/head
cosmonaut 2023-03-06 11:04:04 -08:00
parent 8aa9ce550d
commit 0d441ae07a
2 changed files with 36 additions and 28 deletions

View File

@ -27,8 +27,8 @@ namespace MoonWorks.Audio
}
}
private readonly List<WeakReference<AudioResource>> resources = new List<WeakReference<AudioResource>>();
private readonly List<StreamingSound> streamingSounds = new List<StreamingSound>();
private readonly HashSet<WeakReference> resources = new HashSet<WeakReference>();
private readonly HashSet<WeakReference> streamingSoundReferences = new HashSet<WeakReference>();
private AudioTweenPool AudioTweenPool = new AudioTweenPool();
private readonly List<AudioTween> AudioTweens = new List<AudioTween>();
@ -150,9 +150,17 @@ namespace MoonWorks.Audio
previousTickTime = TickStopwatch.Elapsed.Ticks;
float elapsedSeconds = (float) tickDelta / System.TimeSpan.TicksPerSecond;
for (var i = streamingSounds.Count - 1; i >= 0; i--)
foreach (var weakReference in streamingSoundReferences)
{
streamingSounds[i].Update();
var target = weakReference.Target;
if (target == null)
{
streamingSoundReferences.Remove(weakReference);
}
else
{
(target as StreamingSound).Update();
}
}
for (var i = AudioTweens.Count - 1; i >= 0; i--)
@ -252,21 +260,11 @@ namespace MoonWorks.Audio
WakeSignal.Set();
}
private void AddDynamicSoundInstance(StreamingSound instance)
{
streamingSounds.Add(instance);
}
private void RemoveDynamicSoundInstance(StreamingSound reference)
{
streamingSounds.Remove(reference);
}
internal void AddResourceReference(AudioResource resource, WeakReference<AudioResource> resourceReference)
internal void AddResourceReference(AudioResource resource)
{
lock (StateLock)
{
resources.Add(resourceReference);
resources.Add(resource.weakReference);
if (resource is StreamingSound streamingSound)
{
@ -275,11 +273,11 @@ namespace MoonWorks.Audio
}
}
internal void RemoveResourceReference(AudioResource resource, WeakReference<AudioResource> resourceReference)
internal void RemoveResourceReference(AudioResource resource)
{
lock (StateLock)
{
resources.Remove(resourceReference);
resources.Remove(resource.weakReference);
if (resource is StreamingSound streamingSound)
{
@ -288,6 +286,16 @@ namespace MoonWorks.Audio
}
}
private void AddDynamicSoundInstance(StreamingSound instance)
{
streamingSoundReferences.Add(instance.weakReference);
}
private void RemoveDynamicSoundInstance(StreamingSound instance)
{
streamingSoundReferences.Remove(instance.weakReference);
}
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
@ -296,13 +304,13 @@ namespace MoonWorks.Audio
{
if (disposing)
{
for (var i = resources.Count - 1; i >= 0; i--)
foreach (var weakReference in resources)
{
var weakReference = resources[i];
var target = weakReference.Target;
if (weakReference.TryGetTarget(out var resource))
if (target != null)
{
resource.Dispose();
(target as IDisposable).Dispose();
}
}
resources.Clear();

View File

@ -8,14 +8,14 @@ namespace MoonWorks.Audio
public bool IsDisposed { get; private set; }
private WeakReference<AudioResource> selfReference;
internal WeakReference weakReference;
public AudioResource(AudioDevice device)
{
Device = device;
selfReference = new WeakReference<AudioResource>(this);
Device.AddResourceReference(this, selfReference);
weakReference = new WeakReference(this);
Device.AddResourceReference(this);
}
protected abstract void Destroy();
@ -26,10 +26,10 @@ namespace MoonWorks.Audio
{
Destroy();
if (selfReference != null)
if (weakReference != null)
{
Device.RemoveResourceReference(this, selfReference);
selfReference = null;
Device.RemoveResourceReference(this);
weakReference = null;
}
IsDisposed = true;