Skip to content

Commit

Permalink
Adjust Spatialized Audio
Browse files Browse the repository at this point in the history
Spatialized max distance needs to be adjusted based on the `App.Scene.Pose.scale` to do so we introduce a new script `MultiplayerAudioSourcesManager` which set all of the remote users audioSources max distance whenever the scene scales up or down
  • Loading branch information
sbanca committed Jan 15, 2025
1 parent 83e8822 commit ff0fc39
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Assets/Resources/Multiplayer/Photon/SpeakerPrefab.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ AudioSource:
MinDistance: 1
MaxDistance: 500
Pan2D: 0
rolloffMode: 0
rolloffMode: 2
BypassEffects: 0
BypassListenerEffects: 0
BypassReverbZones: 0
Expand All @@ -66,21 +66,21 @@ AudioSource:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
inSlope: -0.7447439
outSlope: -0.7447439
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
inWeight: 0
outWeight: 0.32808557
- serializedVersion: 3
time: 1
value: 0
inSlope: 0
outSlope: 0
time: 0.99664307
value: 0.305542
inSlope: -0.7058288
outSlope: -0.7058288
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
inWeight: 0.12208462
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
Expand Down
13 changes: 13 additions & 0 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -15179,6 +15179,7 @@ GameObject:
- component: {fileID: 1052269837}
- component: {fileID: 1052269836}
- component: {fileID: 1052269835}
- component: {fileID: 1052269838}
m_Layer: 0
m_Name: MultiplayerManager
m_TagString: Untagged
Expand Down Expand Up @@ -15321,6 +15322,18 @@ MonoBehaviour:
m_SyncType: 0
numberOfCommandsExpected: 0
numberOfCommandsSent: 0
--- !u!114 &1052269838
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1052269830}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5e5dc8298e822c34698aa17ff24b2b77, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1057179852
GameObject:
m_ObjectHideFlags: 0
Expand Down
67 changes: 67 additions & 0 deletions Assets/Scripts/Multiplayer/MultiplayerAudioSourcesManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 The Open Brush Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Linq;
using TiltBrush;
using UnityEngine;

public class MultiplayerAudioSourcesManager : MonoBehaviour
{
public static MultiplayerAudioSourcesManager m_Instance;
private List<AudioSource> sources;
private float _previousScale;

public void AddAudioSource(AudioSource source)
{

sources.Append(source);

}

void Update()
{
float currentScale = App.Scene.Pose.scale;

if (!Mathf.Approximately(currentScale, _previousScale))
{
_previousScale = currentScale;
UpdateAudioSources(currentScale);
}
}

private void UpdateAudioSources(float sceneScale)
{
// Loop backward to remove invalid AudioSources
for (int i = sources.Count - 1; i >= 0; i--)
{
var source = sources[i];
if (source != null)
{
float adjustedMaxDistance = CalculateMaxDistance(sceneScale);
source.maxDistance = adjustedMaxDistance;
}
else sources.RemoveAt(i);
}
}

private float CalculateMaxDistance(float sceneScale)
{
// This is based on OpenBrush default scene max radius
// - At scale 0.1, the mountains diameter is 200 (close range).
// - At scale 1.0,the mountains diameter is 20000 (far range).
return Mathf.Lerp(200f, 20000f, Mathf.Clamp01(sceneScale));
}

}
11 changes: 11 additions & 0 deletions Assets/Scripts/Multiplayer/MultiplayerAudioSourcesManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Scripts/Multiplayer/MultiplayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ void OnRemotePlayerJoined(int id, ITransientData<PlayerRigData> playerData)
{
MultiplayerSceneSync.m_Instance.StartSyncronizationForUser(id);
}

}

public void OnRemoteVoiceConnected(int id, GameObject voicePrefab)
Expand Down Expand Up @@ -446,6 +447,14 @@ public void OnRemoteVoiceConnected(int id, GameObject voicePrefab)
{
Debug.LogWarning($"HeadTransform not found in {RemotePlayerGameObject.name}");
}

AudioSource audioSource = voicePrefab.GetComponent<AudioSource>();
if (audioSource == null)
{
Debug.LogWarning($"VoicePrefab with ID {id} lack AudioSource :S ");
return;
}
MultiplayerAudioSourcesManager.m_Instance.AddAudioSource(audioSource);
}

public void SendLargeDataToPlayer(int playerId, byte[] Data, int percentage)
Expand Down

0 comments on commit ff0fc39

Please sign in to comment.