Skip to content

Commit

Permalink
Fix NaN gain audio
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth committed Mar 10, 2025
1 parent d48f7ec commit d4aecd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Robust.Shared/Audio/AudioParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;

namespace Robust.Shared.Audio
{
Expand All @@ -29,11 +32,25 @@ public enum Attenuation : int
[DataDefinition]
public partial struct AudioParams
{
private float _volume = Default.Volume;

/// <summary>
/// Base volume to play the audio at, in dB.
/// </summary>
[DataField]
public float Volume { get; set; } = Default.Volume;
public float Volume
{
get => _volume;
set
{
if (float.IsNaN(value))
{
value = float.NegativeInfinity;
}

_volume = value;
}
}

/// <summary>
/// Scale for the audio pitch.
Expand Down
7 changes: 7 additions & 0 deletions Robust.Shared/Audio/Systems/SharedAudioSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ public void SetVolume(EntityUid? entity, float value, AudioComponent? component
if (component.Params.Volume.Equals(value))
return;

// Not a log error for now because if something has a negative infinity volume (i.e. 0 gain) then subtracting from it can
// easily cause this and making callers deal with it everywhere is quite annoying.
if (float.IsNaN(value))
{
value = float.NegativeInfinity;
}

component.Params.Volume = value;
component.Volume = value;
DirtyField(entity.Value, component, nameof(AudioComponent.Params));
Expand Down

0 comments on commit d4aecd1

Please sign in to comment.