Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed FMOD volume sync going max volume if source distance is out of range #1537

Merged
merged 3 commits into from
Jul 24, 2021
Merged
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
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));
Measurity marked this conversation as resolved.
Show resolved Hide resolved
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