Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Robust.Client/Audio/AudioManager.Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ public AudioStream LoadAudioOggVorbis(Stream stream, string? name = null)

_checkAlError();

var handle = new ClydeHandle(_audioSampleBuffers.Count);
_audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer));
var length = TimeSpan.FromSeconds(vorbis.TotalSamples / (double) vorbis.SampleRate);
var handle = RegisterBuffer(buffer);
return new AudioStream(this, buffer, handle, length, (int) vorbis.Channels, name, vorbis.Title, vorbis.Artist);
}

Expand Down Expand Up @@ -208,9 +207,8 @@ public AudioStream LoadAudioWav(Stream stream, string? name = null)

_checkAlError();

var handle = new ClydeHandle(_audioSampleBuffers.Count);
_audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer));
var length = TimeSpan.FromSeconds(wav.Data.Length / (double) wav.BlockAlign / wav.SampleRate);
var handle = RegisterBuffer(buffer);
return new AudioStream(this, buffer, handle, length, wav.NumChannels, name);
}

Expand Down Expand Up @@ -238,9 +236,8 @@ public AudioStream LoadAudioRaw(ReadOnlySpan<short> samples, int channels, int s

_checkAlError();

var handle = new ClydeHandle(_audioSampleBuffers.Count);
var length = TimeSpan.FromSeconds((double) samples.Length / channels / sampleRate);
_audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer));
var handle = RegisterBuffer(buffer);
return new AudioStream(this, buffer, handle, length, channels, name);
}

Expand Down Expand Up @@ -331,6 +328,12 @@ internal void RemoveBufferedAudioSource(int handle)

public IAudioSource? CreateAudioSource(AudioStream stream)
{
if (!_audioSampleBuffers.TryGetValue(stream.BufferId, out var sample))
{
OpenALSawmill.Error($"Audio stream '{stream.Name}' has no backing buffer, skipping.");
return null;
}

var source = AL.GenSource();

if (!AL.IsSource(source))
Expand All @@ -339,9 +342,7 @@ internal void RemoveBufferedAudioSource(int handle)
return null;
}

// ReSharper disable once PossibleInvalidOperationException
// TODO: This really shouldn't be indexing based on the ClydeHandle...
AL.Source(source, ALSourcei.Buffer, _audioSampleBuffers[stream.BufferId].BufferHandle);
AL.Source(source, ALSourcei.Buffer, sample.BufferHandle);

Comment thread
Rinary1 marked this conversation as resolved.
var audioSource = new AudioSource(this, source, stream);
_audioSources.Add(source, new WeakReference<BaseAudioSource>(audioSource));
Expand Down
10 changes: 10 additions & 0 deletions Robust.Client/Audio/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal sealed partial class AudioManager : IAudioInternal
private float _masterFadeElapsed = MasterFadeDuration;
private float _masterFadeStartGain = 1f;
private float _masterFadeTargetGain = 1f;
private int _nextClydeHandle;

public bool HasAlDeviceExtension(string extension) => _alcDeviceExtensions.Contains(extension);
public bool HasAlContextExtension(string extension) => _alContextExtensions.Contains(extension);
Expand Down Expand Up @@ -303,6 +304,15 @@ internal bool IsMainThread()
return Thread.CurrentThread == _gameThread;
}

private ClydeHandle RegisterBuffer(int buffer)
{
if (buffer == 0)
throw new InvalidOperationException("AL.GenBuffer returned 0 - no current OpenAL context.");

_audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer));
return new ClydeHandle(Interlocked.Increment(ref _nextClydeHandle));
}

private static void RemoveEfx((int sourceHandle, int filterHandle) handles)
{
if (handles.filterHandle != 0)
Expand Down
Loading