From 3c88cfb9b522859281dc18b6e487303c76ba117a Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Fri, 17 Jan 2025 15:58:05 +1100 Subject: [PATCH] Buffer --- Robust.Client/Audio/AudioManager.Public.cs | 13 ++++++++++--- Robust.Client/Audio/AudioStream.cs | 12 ++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Robust.Client/Audio/AudioManager.Public.cs b/Robust.Client/Audio/AudioManager.Public.cs index ca68e257aef..095c12acd91 100644 --- a/Robust.Client/Audio/AudioManager.Public.cs +++ b/Robust.Client/Audio/AudioManager.Public.cs @@ -135,7 +135,7 @@ public AudioStream LoadAudioOggVorbis(Stream stream, string? name = null) var handle = new ClydeHandle(_audioSampleBuffers.Count); _audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer)); var length = TimeSpan.FromSeconds(vorbis.TotalSamples / (double) vorbis.SampleRate); - return new AudioStream(buffer, handle, length, (int) vorbis.Channels, name, vorbis.Title, vorbis.Artist); + return new AudioStream(this, buffer, handle, length, (int) vorbis.Channels, name, vorbis.Title, vorbis.Artist); } /// @@ -194,7 +194,7 @@ public AudioStream LoadAudioWav(Stream stream, string? name = null) var handle = new ClydeHandle(_audioSampleBuffers.Count); _audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer)); var length = TimeSpan.FromSeconds(wav.Data.Length / (double) wav.BlockAlign / wav.SampleRate); - return new AudioStream(buffer, handle, length, wav.NumChannels, name); + return new AudioStream(this, buffer, handle, length, wav.NumChannels, name); } /// @@ -224,7 +224,7 @@ public AudioStream LoadAudioRaw(ReadOnlySpan samples, int channels, int s var handle = new ClydeHandle(_audioSampleBuffers.Count); var length = TimeSpan.FromSeconds((double) samples.Length / channels / sampleRate); _audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer)); - return new AudioStream(buffer, handle, length, channels, name); + return new AudioStream(this, buffer, handle, length, channels, name); } public void SetMasterGain(float newGain) @@ -383,5 +383,12 @@ public void DisposeAllAudio() } _bufferedAudioSources.Clear(); + + foreach (var buffer in _audioSampleBuffers.Values) + { + DeleteAudioBufferOnMainThread(buffer.BufferHandle); + } + + _audioSampleBuffers.Clear(); } } diff --git a/Robust.Client/Audio/AudioStream.cs b/Robust.Client/Audio/AudioStream.cs index 25c10b4e96e..47e326347ce 100644 --- a/Robust.Client/Audio/AudioStream.cs +++ b/Robust.Client/Audio/AudioStream.cs @@ -6,8 +6,10 @@ namespace Robust.Client.Audio; /// /// Has the metadata for a particular audio stream as well as the relevant internal handle to it. /// -public sealed class AudioStream +public sealed class AudioStream : IDisposable { + private IAudioInternal _audio; + /// /// Buffer ID for this audio in AL. /// @@ -20,8 +22,9 @@ public sealed class AudioStream public string? Artist { get; } public int ChannelCount { get; } - internal AudioStream(int bufferId, IClydeHandle? handle, TimeSpan length, int channelCount, string? name = null, string? title = null, string? artist = null) + internal AudioStream(IAudioInternal internalAudio, int bufferId, IClydeHandle? handle, TimeSpan length, int channelCount, string? name = null, string? title = null, string? artist = null) { + _audio = internalAudio; BufferId = bufferId; ClydeHandle = handle; Length = length; @@ -30,4 +33,9 @@ internal AudioStream(int bufferId, IClydeHandle? handle, TimeSpan length, int ch Title = title; Artist = artist; } + + public void Dispose() + { + _audio.Remove(this); + } }