Skip to content

Commit b9b3b8e

Browse files
authored
Fixed FMOD volume sync going max volume if source distance is out of range (#1537)
* Fixed seamoth going max volume if out of range * Clamped other fmod volume sounds between 0 and 1 as well
1 parent 26ab6a3 commit b9b3b8e

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

NitroxClient/Communication/Packets/Processors/PlayFMODAssetProcessor.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using NitroxClient.Communication.Packets.Processors.Abstract;
44
using NitroxModel.Packets;
55
using NitroxModel_Subnautica.DataStructures;
6+
using UnityEngine;
7+
68
#pragma warning disable 618
79

810
namespace NitroxClient.Communication.Packets.Processors
@@ -14,7 +16,9 @@ public override void Process(PlayFMODAsset packet)
1416
EventInstance instance = FMODUWE.GetEvent(packet.AssetPath);
1517
instance.setProperty(EVENT_PROPERTY.MINIMUM_DISTANCE, 1f);
1618
instance.setProperty(EVENT_PROPERTY.MAXIMUM_DISTANCE, packet.Radius);
17-
instance.setVolume(packet.Volume);
19+
// Volume is a scalar, is should be limited to 0 and we don't need more than 100% volume (i.e. 1.0).
20+
// See docs: https://fmod.com/resources/documentation-api?version=2.00&page=studio-api-eventinstance.html#studio_eventinstance_setvolume
21+
instance.setVolume(Mathf.Clamp01(packet.Volume));
1822
instance.set3DAttributes(packet.Position.ToUnity().To3DAttributes());
1923
instance.start();
2024
instance.release();

NitroxClient/Debuggers/SoundDebugger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SoundDebugger : BaseDebugger
2525

2626
public SoundDebugger(FMODSystem fmodSystem) : base(700, null, KeyCode.S, true, false, true, GUISkinCreationOptions.DERIVEDCOPY)
2727
{
28-
assetList = fmodSystem.GetSoundDataList();
28+
assetList = fmodSystem.SoundDataList;
2929
ActiveTab = AddTab("Sounds", RenderTabAllSounds);
3030
}
3131

NitroxClient/GameLogic/FMOD/FMODSystem.cs

+10-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Globalization;
44
using NitroxClient.Communication.Abstract;
5+
using NitroxClient.Properties;
56
using NitroxModel.DataStructures;
67
using NitroxModel.DataStructures.GameLogic;
78
using NitroxModel.Logger;
@@ -11,19 +12,16 @@ namespace NitroxClient.GameLogic.FMOD
1112
{
1213
public class FMODSystem
1314
{
14-
private readonly Dictionary<string, SoundData> assetWhitelist = new Dictionary<string, SoundData>();
15-
15+
private readonly Dictionary<string, SoundData> assetWhitelist = new();
1616
private readonly IPacketSender packetSender;
1717

1818
public FMODSystem(IPacketSender packetSender)
1919
{
2020
this.packetSender = packetSender;
21-
22-
string soundsWhitelist = Properties.Resources.soundsWhitelist;
23-
21+
string soundsWhitelist = Resources.soundsWhitelist;
2422
if (string.IsNullOrWhiteSpace(soundsWhitelist))
2523
{
26-
Log.Error(new NullReferenceException(), "[FMODSystem]: soundsWhitelist.csv is null or whitespace");
24+
Log.Error("[FMODSystem]: soundsWhitelist.csv is null or whitespace");
2725
}
2826

2927
foreach (string entry in soundsWhitelist.Split('\n'))
@@ -47,6 +45,11 @@ public FMODSystem(IPacketSender packetSender)
4745
}
4846
}
4947

48+
public static FMODSuppressor SuppressSounds()
49+
{
50+
return new();
51+
}
52+
5053
public bool IsWhitelisted(string path)
5154
{
5255
return assetWhitelist.TryGetValue(path, out SoundData soundData) && soundData.IsWhitelisted;
@@ -66,7 +69,6 @@ public bool IsWhitelisted(string path, out bool isGlobal, out float radius)
6669
return false;
6770
}
6871

69-
7072
public void PlayAsset(string path, NitroxVector3 position, float volume, float radius, bool isGlobal)
7173
{
7274
packetSender.Send(new PlayFMODAsset(path, position, volume, radius, isGlobal));
@@ -87,11 +89,6 @@ public void PlayStudioEmitter(NitroxId id, string assetPath, bool play, bool all
8789
packetSender.Send(new PlayFMODStudioEmitter(id, assetPath, play, allowFadeout));
8890
}
8991

90-
public Dictionary<string, SoundData> GetSoundDataList() => assetWhitelist;
91-
92-
public static FMODSuppressor SuppressSounds()
93-
{
94-
return new FMODSuppressor();
95-
}
92+
public Dictionary<string, SoundData> SoundDataList => assetWhitelist;
9693
}
9794
}

NitroxClient/MonoBehaviours/MultiplayerSeaMoth.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ protected override void Awake()
2424

2525
protected void Update()
2626
{
27+
// Clamp volume between 0 and 1 (nothing or max). Going below 0 turns up volume to max.
2728
float distance = Vector3.Distance(Player.main.transform.position, transform.position);
28-
rpmSound.GetEventInstance().setVolume(1 - distance / radiusRpmSound);
29-
revSound.GetEventInstance().setVolume(1 - distance / radiusRevSound);
29+
rpmSound.GetEventInstance().setVolume(Mathf.Clamp01(1 - distance / radiusRpmSound));
30+
revSound.GetEventInstance().setVolume(Mathf.Clamp01(1 - distance / radiusRevSound));
3031

3132
if (lastThrottle)
3233
{

NitroxModel/Helper/Mathf.cs

+32
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ public static float Pow(float p1, float p2)
2828
return (float)Math.Pow(p1, p2);
2929
}
3030

31+
/// <summary>
32+
/// Clamps the given value between 0 and 1.
33+
/// </summary>
34+
/// <param name="value"></param>
35+
/// <returns></returns>
36+
public static float Clamp01(float value)
37+
{
38+
// Not using Clamp as an optimization.
39+
if (value < 0)
40+
{
41+
return 0;
42+
}
43+
if (value > 1)
44+
{
45+
return 1;
46+
}
47+
return value;
48+
}
49+
50+
public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
51+
{
52+
if (val.CompareTo(min) < 0)
53+
{
54+
return min;
55+
}
56+
if (val.CompareTo(max) > 0)
57+
{
58+
return max;
59+
}
60+
return val;
61+
}
62+
3163
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="digits" /> is less than 0 or greater than 15.</exception>
3264
public static float Round(float value, int digits = 0)
3365
{

NitroxServer/Communication/Packets/Processors/DefaultServerPacketProcessor.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class DefaultServerPacketProcessor : AuthenticatedPacketProcessor<Packet>
1111
{
1212
private readonly PlayerManager playerManager;
1313

14-
private readonly HashSet<Type> loggingPacketBlackList = new HashSet<Type> {
14+
private readonly HashSet<Type> loggingPacketBlackList = new()
15+
{
1516
typeof(AnimationChangeEvent),
1617
typeof(Movement),
1718
typeof(VehicleMovement),

0 commit comments

Comments
 (0)