restructure audio cleanup

pull/53/head
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)
{
NativeMemory.Free((void*) BufferDataPtr);
}
}
base.Dispose(disposing);
}
}
}

View File

@ -90,9 +90,16 @@ namespace MoonWorks.Audio
/// <summary>
/// Unloads the Ogg data, freeing resources.
/// </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>
@ -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;
}
}
}
}

View File

@ -99,9 +99,16 @@ namespace MoonWorks.Audio
/// <summary>
/// Unloads the qoa data, freeing resources.
/// </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>
@ -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;
}
}
}
}

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="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);
protected override void Dispose(bool disposing)
{
if (!IsDisposed)
{
Unload();
}
base.Dispose(disposing);
}
}
}

View File

@ -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.
// 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()

View File

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

View File

@ -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);
}
}
}

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);
FAudio.FAudioVoice_DestroyVoice(Handle);
}
base.Dispose(disposing);
}
}
}