From 385783a846cf4b45d66971b9b7856cd69c171723 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 8 Dec 2023 16:33:52 -0800 Subject: [PATCH] restructure audio cleanup --- src/Audio/AudioBuffer.cs | 10 +++++++--- src/Audio/AudioDataOgg.cs | 23 +++++++++-------------- src/Audio/AudioDataQoa.cs | 23 +++++++++-------------- src/Audio/AudioDataStreamable.cs | 9 +++++++++ src/Audio/AudioResource.cs | 24 +++++------------------- src/Audio/SourceVoice.cs | 5 ----- src/Audio/StreamingVoice.cs | 17 +++++++++++++++++ src/Audio/Voice.cs | 10 +++++++--- 8 files changed, 63 insertions(+), 58 deletions(-) diff --git a/src/Audio/AudioBuffer.cs b/src/Audio/AudioBuffer.cs index 78fe19d..38b8dc9 100644 --- a/src/Audio/AudioBuffer.cs +++ b/src/Audio/AudioBuffer.cs @@ -64,12 +64,16 @@ namespace MoonWorks.Audio }; } - protected override unsafe void DisposeUnmanagedState() + protected override unsafe void Dispose(bool disposing) { - if (OwnsBufferData) + if (!IsDisposed) { - NativeMemory.Free((void*) BufferDataPtr); + if (OwnsBufferData) + { + NativeMemory.Free((void*) BufferDataPtr); + } } + base.Dispose(disposing); } } } diff --git a/src/Audio/AudioDataOgg.cs b/src/Audio/AudioDataOgg.cs index 565ce49..9df5a6f 100644 --- a/src/Audio/AudioDataOgg.cs +++ b/src/Audio/AudioDataOgg.cs @@ -90,9 +90,16 @@ namespace MoonWorks.Audio /// /// Unloads the Ogg data, freeing resources. /// - public override void Unload() + public override unsafe void Unload() { - DisposeUnmanagedState(); + if (Loaded) + { + FAudio.stb_vorbis_close(VorbisHandle); + NativeMemory.Free((void*) FileDataPtr); + + VorbisHandle = IntPtr.Zero; + FileDataPtr = IntPtr.Zero; + } } /// @@ -136,17 +143,5 @@ namespace MoonWorks.Audio (uint) lengthInBytes, true); } - - protected override unsafe void DisposeUnmanagedState() - { - if (Loaded) - { - FAudio.stb_vorbis_close(VorbisHandle); - NativeMemory.Free((void*) FileDataPtr); - - VorbisHandle = IntPtr.Zero; - FileDataPtr = IntPtr.Zero; - } - } } } diff --git a/src/Audio/AudioDataQoa.cs b/src/Audio/AudioDataQoa.cs index 0adbe5a..2ee075f 100644 --- a/src/Audio/AudioDataQoa.cs +++ b/src/Audio/AudioDataQoa.cs @@ -99,9 +99,16 @@ namespace MoonWorks.Audio /// /// Unloads the qoa data, freeing resources. /// - public override void Unload() + public override unsafe void Unload() { - DisposeUnmanagedState(); + if (Loaded) + { + FAudio.qoa_close(QoaHandle); + NativeMemory.Free((void*) FileDataPtr); + + QoaHandle = IntPtr.Zero; + FileDataPtr = IntPtr.Zero; + } } /// @@ -153,17 +160,5 @@ namespace MoonWorks.Audio ((UInt64)(bytes[4]) << 24) | ((UInt64)(bytes[5]) << 16) | ((UInt64)(bytes[6]) << 8) | ((UInt64)(bytes[7]) << 0); } - - protected override unsafe void DisposeUnmanagedState() - { - if (Loaded) - { - FAudio.qoa_close(QoaHandle); - NativeMemory.Free((void*) FileDataPtr); - - QoaHandle = IntPtr.Zero; - FileDataPtr = IntPtr.Zero; - } - } } } diff --git a/src/Audio/AudioDataStreamable.cs b/src/Audio/AudioDataStreamable.cs index 7559bec..3c4ec01 100644 --- a/src/Audio/AudioDataStreamable.cs +++ b/src/Audio/AudioDataStreamable.cs @@ -36,5 +36,14 @@ namespace MoonWorks.Audio /// How much data was actually filled in by the decode. /// Whether the end of the data was reached on this decode. public abstract unsafe void Decode(void* buffer, int bufferLengthInBytes, out int filledLengthInBytes, out bool reachedEnd); + + protected override void Dispose(bool disposing) + { + if (!IsDisposed) + { + Unload(); + } + base.Dispose(disposing); + } } } diff --git a/src/Audio/AudioResource.cs b/src/Audio/AudioResource.cs index 3f6b4d0..bc7cab7 100644 --- a/src/Audio/AudioResource.cs +++ b/src/Audio/AudioResource.cs @@ -19,23 +19,16 @@ namespace MoonWorks.Audio Device.AddResourceReference(SelfReference); } - protected virtual void DisposeManagedState() { } - protected virtual void DisposeUnmanagedState() { } - - protected void Dispose(bool disposing) + protected virtual void Dispose(bool disposing) { if (!IsDisposed) { if (disposing) { - DisposeManagedState(); - Device.RemoveResourceReference(SelfReference); SelfReference.Free(); } - DisposeUnmanagedState(); - IsDisposed = true; } } @@ -43,18 +36,11 @@ namespace MoonWorks.Audio ~AudioResource() { #if DEBUG - // If the graphics device associated with this resource was already disposed, we assume - // that your game is in the middle of shutting down. - if (!IsDisposed && Device != null && !Device.IsDisposed) - { - // If you see this log message, you leaked a graphics resource without disposing it! - // This means your game may eventually run out of native memory for mysterious reasons. - Logger.LogWarn($"A resource of type {GetType().Name} was not Disposed."); - } + // If you see this log message, you leaked an audio resource without disposing it! + // We can't clean it up for you because this can cause catastrophic issues. + // You should really fix this when it happens. + Logger.LogWarn($"A resource of type {GetType().Name} was not Disposed."); #endif - - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(disposing: false); } public void Dispose() diff --git a/src/Audio/SourceVoice.cs b/src/Audio/SourceVoice.cs index d1a8626..bfef460 100644 --- a/src/Audio/SourceVoice.cs +++ b/src/Audio/SourceVoice.cs @@ -214,10 +214,5 @@ namespace MoonWorks.Audio PlaybackInitiated = false; base.Reset(); } - - protected override void DisposeManagedState() - { - Stop(); - } } } diff --git a/src/Audio/StreamingVoice.cs b/src/Audio/StreamingVoice.cs index 2e8b061..528e7ec 100644 --- a/src/Audio/StreamingVoice.cs +++ b/src/Audio/StreamingVoice.cs @@ -145,5 +145,22 @@ namespace MoonWorks.Audio buffers[i] = (IntPtr) NativeMemory.Alloc(BufferSize); } } + + protected override unsafe void Dispose(bool disposing) + { + if (!IsDisposed) + { + Stop(); + + for (int i = 0; i < BUFFER_COUNT; i += 1) + { + if (buffers[i] != IntPtr.Zero) + { + NativeMemory.Free((void*) buffers[i]); + } + } + } + base.Dispose(disposing); + } } } diff --git a/src/Audio/Voice.cs b/src/Audio/Voice.cs index d1739c9..3c9ee82 100644 --- a/src/Audio/Voice.cs +++ b/src/Audio/Voice.cs @@ -565,10 +565,14 @@ namespace MoonWorks.Audio ); } - protected override void DisposeUnmanagedState() + protected override unsafe void Dispose(bool disposing) { - NativeMemory.Free(pMatrixCoefficients); - FAudio.FAudioVoice_DestroyVoice(Handle); + if (!IsDisposed) + { + NativeMemory.Free(pMatrixCoefficients); + FAudio.FAudioVoice_DestroyVoice(Handle); + } + base.Dispose(disposing); } } }