Better audio logs for gain (#4789)

* Better audio logs for gain

Need to track down the -10 gain setter.

* Release checks
This commit is contained in:
metalgearsloth
2023-12-31 13:34:43 +11:00
committed by GitHub
parent 73357f022b
commit 517ae7f1bd
4 changed files with 58 additions and 3 deletions

View File

@@ -148,7 +148,7 @@ internal sealed partial class AudioManager : IAudioInternal
var error = AL.GetError();
if (error != ALError.NoError)
{
OpenALSawmill.Error("[{0}:{1}] AL error: {2}, {3}", callerMember, callerLineNumber, error, message);
OpenALSawmill.Error("[{0}:{1}] AL error: {2}, {3}. Stacktrace is {4}", callerMember, callerLineNumber, error, message, Environment.StackTrace);
}
}

View File

@@ -326,7 +326,7 @@ public sealed partial class AudioSystem : SharedAudioSystem
return;
}
var paramsGain = MathF.Pow(10, component.Params.Volume / 10);
var paramsGain = VolumeToGain(component.Params.Volume);
// Thought I'd never have to manually calculate gain again but this is the least
// unpleasant audio I could get at the moment.

View File

@@ -152,12 +152,24 @@ public abstract partial class SharedAudioSystem : EntitySystem
public static float GainToVolume(float value)
{
if (value < 0f)
{
throw new InvalidOperationException($"Tried to get volume calculation for gain of {value}.");
}
return 10f * MathF.Log10(value);
}
public static float VolumeToGain(float value)
{
return MathF.Pow(10, value / 10);
var result = MathF.Pow(10, value / 10);
if (result < 0f)
{
throw new InvalidOperationException($"Tried to get gain calculation that resulted in invalid value of {result}");
}
return result;
}
/// <summary>

View File

@@ -0,0 +1,43 @@
using NUnit.Framework;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Maths;
namespace Robust.UnitTesting.Shared.Audio;
[TestFixture]
public sealed class AudioGainVolume_Test
{
private static float[] _gainValues = new[]
{
1f,
0.5f,
0f,
};
private static float[] _volumeValues = new[]
{
-100f,
-3f,
0f,
1f,
100f,
};
[Test, TestCaseSource(nameof(_gainValues))]
public void GainCalculationTest(float value)
{
var volume = SharedAudioSystem.GainToVolume(value);
var gain = SharedAudioSystem.VolumeToGain(volume);
Assert.That(MathHelper.CloseTo(value, gain, 0.01f), $"Expected {value} and found {volume}");
}
[Test, TestCaseSource(nameof(_volumeValues))]
public void VolumeCalculationTest(float value)
{
var gain = SharedAudioSystem.VolumeToGain(value);
var volume = SharedAudioSystem.GainToVolume(gain);
Assert.That(MathHelper.CloseTo(value, volume, 0.01f), $"Expected {value} and found {volume}");
}
}