restructure audio cleanup

cosmonaut 2023-12-08 16:33:52 -08:00
parent 450b08cbd8
commit 385783a846
8 changed files with 63 additions and 58 deletions

View File

@ -64,12 +64,16 @@ namespace MoonWorks.Audio
}; };
} }
protected override unsafe void DisposeUnmanagedState() protected override unsafe void Dispose(bool disposing)
{
if (!IsDisposed)
{ {
if (OwnsBufferData) if (OwnsBufferData)
{ {
NativeMemory.Free((void*) BufferDataPtr); NativeMemory.Free((void*) BufferDataPtr);
} }
} }
base.Dispose(disposing);
}
} }
} }

View File

@ -90,9 +90,16 @@ namespace MoonWorks.Audio
/// <summary> /// <summary>
/// Unloads the Ogg data, freeing resources. /// Unloads the Ogg data, freeing resources.
/// </summary> /// </summary>
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;
}
} }
/// <summary> /// <summary>
@ -136,17 +143,5 @@ namespace MoonWorks.Audio
(uint) lengthInBytes, (uint) lengthInBytes,
true); true);
} }
protected override unsafe void DisposeUnmanagedState()
{
if (Loaded)
{
FAudio.stb_vorbis_close(VorbisHandle);
NativeMemory.Free((void*) FileDataPtr);
VorbisHandle = IntPtr.Zero;
FileDataPtr = IntPtr.Zero;
}
}
} }
} }

View File

@ -99,9 +99,16 @@ namespace MoonWorks.Audio
/// <summary> /// <summary>
/// Unloads the qoa data, freeing resources. /// Unloads the qoa data, freeing resources.
/// </summary> /// </summary>
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;
}
} }
/// <summary> /// <summary>
@ -153,17 +160,5 @@ namespace MoonWorks.Audio
((UInt64)(bytes[4]) << 24) | ((UInt64)(bytes[5]) << 16) | ((UInt64)(bytes[4]) << 24) | ((UInt64)(bytes[5]) << 16) |
((UInt64)(bytes[6]) << 8) | ((UInt64)(bytes[7]) << 0); ((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;
}
}
} }
} }

View File

@ -36,5 +36,14 @@ namespace MoonWorks.Audio
/// <param name="filledLengthInBytes">How much data was actually filled in by the decode.</param> /// <param name="filledLengthInBytes">How much data was actually filled in by the decode.</param>
/// <param name="reachedEnd">Whether the end of the data was reached on this decode.</param> /// <param name="reachedEnd">Whether the end of the data was reached on this decode.</param>
public abstract unsafe void Decode(void* buffer, int bufferLengthInBytes, out int filledLengthInBytes, out bool reachedEnd); 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);
}
} }
} }

View File

@ -19,23 +19,16 @@ namespace MoonWorks.Audio
Device.AddResourceReference(SelfReference); Device.AddResourceReference(SelfReference);
} }
protected virtual void DisposeManagedState() { } protected virtual void Dispose(bool disposing)
protected virtual void DisposeUnmanagedState() { }
protected void Dispose(bool disposing)
{ {
if (!IsDisposed) if (!IsDisposed)
{ {
if (disposing) if (disposing)
{ {
DisposeManagedState();
Device.RemoveResourceReference(SelfReference); Device.RemoveResourceReference(SelfReference);
SelfReference.Free(); SelfReference.Free();
} }
DisposeUnmanagedState();
IsDisposed = true; IsDisposed = true;
} }
} }
@ -43,18 +36,11 @@ namespace MoonWorks.Audio
~AudioResource() ~AudioResource()
{ {
#if DEBUG #if DEBUG
// If the graphics device associated with this resource was already disposed, we assume // If you see this log message, you leaked an audio resource without disposing it!
// that your game is in the middle of shutting down. // We can't clean it up for you because this can cause catastrophic issues.
if (!IsDisposed && Device != null && !Device.IsDisposed) // You should really fix this when it happens.
{
// 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."); Logger.LogWarn($"A resource of type {GetType().Name} was not Disposed.");
}
#endif #endif
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
} }
public void Dispose() public void Dispose()

View File

@ -214,10 +214,5 @@ namespace MoonWorks.Audio
PlaybackInitiated = false; PlaybackInitiated = false;
base.Reset(); base.Reset();
} }
protected override void DisposeManagedState()
{
Stop();
}
} }
} }

View File

@ -145,5 +145,22 @@ namespace MoonWorks.Audio
buffers[i] = (IntPtr) NativeMemory.Alloc(BufferSize); 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);
}
} }
} }

View File

@ -565,10 +565,14 @@ namespace MoonWorks.Audio
); );
} }
protected override void DisposeUnmanagedState() protected override unsafe void Dispose(bool disposing)
{
if (!IsDisposed)
{ {
NativeMemory.Free(pMatrixCoefficients); NativeMemory.Free(pMatrixCoefficients);
FAudio.FAudioVoice_DestroyVoice(Handle); FAudio.FAudioVoice_DestroyVoice(Handle);
} }
base.Dispose(disposing);
}
} }
} }