stop updating audio on dispose

cosmonaut 2023-12-13 13:57:53 -08:00
parent a9dd3dc13f
commit 97dd112e7a
1 changed files with 16 additions and 11 deletions

View File

@ -25,7 +25,7 @@ namespace MoonWorks.Audio
public float DopplerScale = 1f;
public float SpeedOfSound = 343.5f;
private readonly HashSet<GCHandle> resources = new HashSet<GCHandle>();
private readonly HashSet<GCHandle> resourceHandles = new HashSet<GCHandle>();
private readonly HashSet<UpdatingSourceVoice> updatingSourceVoices = new HashSet<UpdatingSourceVoice>();
private AudioTweenManager AudioTweenManager;
@ -265,7 +265,7 @@ namespace MoonWorks.Audio
{
lock (StateLock)
{
resources.Add(resourceReference);
resourceHandles.Add(resourceReference);
if (resourceReference.Target is UpdatingSourceVoice updatableVoice)
{
@ -278,7 +278,12 @@ namespace MoonWorks.Audio
{
lock (StateLock)
{
resources.Remove(resourceReference);
resourceHandles.Remove(resourceReference);
if (resourceReference.Target is UpdatingSourceVoice updatableVoice)
{
updatingSourceVoices.Remove(updatableVoice);
}
}
}
@ -293,18 +298,18 @@ namespace MoonWorks.Audio
Thread.Join();
// dispose all source voices first
foreach (var resource in resources)
foreach (var handle in resourceHandles)
{
if (resource.Target is SourceVoice voice)
if (handle.Target is SourceVoice voice)
{
voice.Dispose();
}
}
// dispose all submix voices except the faux mastering voice
foreach (var resource in resources)
foreach (var handle in resourceHandles)
{
if (resource.Target is SubmixVoice voice && voice != fauxMasteringVoice)
if (handle.Target is SubmixVoice voice && voice != fauxMasteringVoice)
{
voice.Dispose();
}
@ -317,15 +322,15 @@ namespace MoonWorks.Audio
FAudio.FAudioVoice_DestroyVoice(trueMasteringVoice);
// destroy all other audio resources
foreach (var resource in resources)
foreach (var handle in resourceHandles)
{
if (resource.Target is IDisposable disposable)
if (handle.Target is AudioResource resource)
{
disposable.Dispose();
resource.Dispose();
}
}
resources.Clear();
resourceHandles.Clear();
}
FAudio.FAudio_Release(Handle);