Skip to content

Commit

Permalink
Fixed FMOD volume sync going max volume if source distance is out of …
Browse files Browse the repository at this point in the history
…range (#1537)

* Fixed seamoth going max volume if out of range

* Clamped other fmod volume sounds between 0 and 1 as well
  • Loading branch information
Measurity authored Jul 24, 2021
1 parent 26ab6a3 commit b9b3b8e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxModel.Packets;
using NitroxModel_Subnautica.DataStructures;
using UnityEngine;

#pragma warning disable 618

namespace NitroxClient.Communication.Packets.Processors
Expand All @@ -14,7 +16,9 @@ public override void Process(PlayFMODAsset packet)
EventInstance instance = FMODUWE.GetEvent(packet.AssetPath);
instance.setProperty(EVENT_PROPERTY.MINIMUM_DISTANCE, 1f);
instance.setProperty(EVENT_PROPERTY.MAXIMUM_DISTANCE, packet.Radius);
instance.setVolume(packet.Volume);
// Volume is a scalar, is should be limited to 0 and we don't need more than 100% volume (i.e. 1.0).
// See docs: https://fmod.com/resources/documentation-api?version=2.00&page=studio-api-eventinstance.html#studio_eventinstance_setvolume
instance.setVolume(Mathf.Clamp01(packet.Volume));
instance.set3DAttributes(packet.Position.ToUnity().To3DAttributes());
instance.start();
instance.release();
Expand Down
2 changes: 1 addition & 1 deletion NitroxClient/Debuggers/SoundDebugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SoundDebugger : BaseDebugger

public SoundDebugger(FMODSystem fmodSystem) : base(700, null, KeyCode.S, true, false, true, GUISkinCreationOptions.DERIVEDCOPY)
{
assetList = fmodSystem.GetSoundDataList();
assetList = fmodSystem.SoundDataList;
ActiveTab = AddTab("Sounds", RenderTabAllSounds);
}

Expand Down
23 changes: 10 additions & 13 deletions NitroxClient/GameLogic/FMOD/FMODSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using NitroxClient.Communication.Abstract;
using NitroxClient.Properties;
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Logger;
Expand All @@ -11,19 +12,16 @@ namespace NitroxClient.GameLogic.FMOD
{
public class FMODSystem
{
private readonly Dictionary<string, SoundData> assetWhitelist = new Dictionary<string, SoundData>();

private readonly Dictionary<string, SoundData> assetWhitelist = new();
private readonly IPacketSender packetSender;

public FMODSystem(IPacketSender packetSender)
{
this.packetSender = packetSender;

string soundsWhitelist = Properties.Resources.soundsWhitelist;

string soundsWhitelist = Resources.soundsWhitelist;
if (string.IsNullOrWhiteSpace(soundsWhitelist))
{
Log.Error(new NullReferenceException(), "[FMODSystem]: soundsWhitelist.csv is null or whitespace");
Log.Error("[FMODSystem]: soundsWhitelist.csv is null or whitespace");
}

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

public static FMODSuppressor SuppressSounds()
{
return new();
}

public bool IsWhitelisted(string path)
{
return assetWhitelist.TryGetValue(path, out SoundData soundData) && soundData.IsWhitelisted;
Expand All @@ -66,7 +69,6 @@ public bool IsWhitelisted(string path, out bool isGlobal, out float radius)
return false;
}


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

public Dictionary<string, SoundData> GetSoundDataList() => assetWhitelist;

public static FMODSuppressor SuppressSounds()
{
return new FMODSuppressor();
}
public Dictionary<string, SoundData> SoundDataList => assetWhitelist;
}
}
5 changes: 3 additions & 2 deletions NitroxClient/MonoBehaviours/MultiplayerSeaMoth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ protected override void Awake()

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

if (lastThrottle)
{
Expand Down
32 changes: 32 additions & 0 deletions NitroxModel/Helper/Mathf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ public static float Pow(float p1, float p2)
return (float)Math.Pow(p1, p2);
}

/// <summary>
/// Clamps the given value between 0 and 1.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static float Clamp01(float value)
{
// Not using Clamp as an optimization.
if (value < 0)
{
return 0;
}
if (value > 1)
{
return 1;
}
return value;
}

public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0)
{
return min;
}
if (val.CompareTo(max) > 0)
{
return max;
}
return val;
}

/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="digits" /> is less than 0 or greater than 15.</exception>
public static float Round(float value, int digits = 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class DefaultServerPacketProcessor : AuthenticatedPacketProcessor<Packet>
{
private readonly PlayerManager playerManager;

private readonly HashSet<Type> loggingPacketBlackList = new HashSet<Type> {
private readonly HashSet<Type> loggingPacketBlackList = new()
{
typeof(AnimationChangeEvent),
typeof(Movement),
typeof(VehicleMovement),
Expand Down

0 comments on commit b9b3b8e

Please sign in to comment.