Skip to content

Commit

Permalink
Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Jan 17, 2025
1 parent 2bf16ac commit 3c88cfb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions Robust.Client/Audio/AudioManager.Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -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);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -224,7 +224,7 @@ public AudioStream LoadAudioRaw(ReadOnlySpan<short> 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)
Expand Down Expand Up @@ -383,5 +383,12 @@ public void DisposeAllAudio()
}

_bufferedAudioSources.Clear();

foreach (var buffer in _audioSampleBuffers.Values)
{
DeleteAudioBufferOnMainThread(buffer.BufferHandle);
}

_audioSampleBuffers.Clear();
}
}
12 changes: 10 additions & 2 deletions Robust.Client/Audio/AudioStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ namespace Robust.Client.Audio;
/// <summary>
/// Has the metadata for a particular audio stream as well as the relevant internal handle to it.
/// </summary>
public sealed class AudioStream
public sealed class AudioStream : IDisposable
{
private IAudioInternal _audio;

/// <summary>
/// Buffer ID for this audio in AL.
/// </summary>
Expand All @@ -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;
Expand All @@ -30,4 +33,9 @@ internal AudioStream(int bufferId, IClydeHandle? handle, TimeSpan length, int ch
Title = title;
Artist = artist;
}

public void Dispose()
{
_audio.Remove(this);
}
}

0 comments on commit 3c88cfb

Please sign in to comment.