Skip to content

Commit

Permalink
Added choice for starting FMOD event
Browse files Browse the repository at this point in the history
  • Loading branch information
eetteri committed Feb 7, 2021
1 parent cb13ffd commit 4bea3ca
Showing 8 changed files with 158 additions and 5 deletions.
14 changes: 14 additions & 0 deletions Runtime/AudioControlEventStarter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnityEngine;

public class AudioControlEventStarter : MonoBehaviour
{
public string signalName;

void OnCollisionEnter(Collision col) {
EventSignaling.TriggerEvent(signalName);
}

void OnTriggerEnter(Collider col) {
EventSignaling.TriggerEvent(signalName);
}
}
11 changes: 11 additions & 0 deletions Runtime/AudioControlEventStarter.cs.meta

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

24 changes: 22 additions & 2 deletions Runtime/AudioControlPointController.cs
Original file line number Diff line number Diff line change
@@ -25,16 +25,22 @@ public class AudioControlPointController : MonoBehaviour
[HideInInspector]
public bool m_connectionFoldout = true;
[HideInInspector]
public bool m_startModeFoldout = true;
[HideInInspector]
public bool m_controlPointsFoldout = true;
[HideInInspector]
public List<AudioControlPoint> controlPoints = new List<AudioControlPoint>();
[HideInInspector]
public Animator animator;
[HideInInspector]
public int fps;
public int fps = 25;
[HideInInspector]
public StartMode m_startMode;
[HideInInspector]
public string m_startSignalName;

private float _lastExecTime = 0f;
private float _timeThreshold = 0.02f; //50fps
private float _timeThreshold;
private float _fmodExectime;
private OscClient _client;

@@ -49,12 +55,17 @@ private void Awake()
animator = GetComponent<Animator>();
}

private void OnEnable() {
EventSignaling.StartListening(m_startSignalName, StartFMOD);
}

private void Start()
{
if (!String.IsNullOrEmpty(fmodEvent))
{
//need to have shared instance pointing to fmod event between possible multiple audiopointcontroller
_instance = FMODEventInstancer.instance.GetFmodEventInstance(fmodEvent);
if(m_startMode == StartMode.Automatic) { StartFMOD(); }
}

if (!String.IsNullOrEmpty(oscAddress) && oscPort > 1000) //FIXME: port checking is just wrong
@@ -71,6 +82,14 @@ private void Start()
}
}

private void StartFMOD() {
FMOD.Studio.PLAYBACK_STATE ps;
_instance.getPlaybackState(out ps);
if(ps != FMOD.Studio.PLAYBACK_STATE.PLAYING) {
_instance.start();
}
}

void UpdateValueHumanoid(AudioControlPoint acp)
{
Vector3 currentPos = animator.GetBoneTransform(acp.m_bonePoint).position;
@@ -341,6 +360,7 @@ private void OnDrawGizmos()

private void OnDisable()
{
EventSignaling.StopListening(m_startSignalName, StartFMOD);
FMODEventInstancer.instance.ReleaseFmodInstance(fmodEvent); // This is not okay, need to build some sort of retaining relation from instances hooking into fmod and only release the instance when there is no one left
}
}
5 changes: 5 additions & 0 deletions Runtime/ControlDataUtility.cs
Original file line number Diff line number Diff line change
@@ -37,4 +37,9 @@ public enum Axis
x,
y,
z
}

public enum StartMode {
Automatic,
Triggered
}
19 changes: 18 additions & 1 deletion Runtime/Editor/AudioPointControllerEditor.cs
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ public override void OnInspectorGUI()
EditorGUILayout.Space(8);
DrawControlPointHeader();
DrawConnectionSetups();
DrawStartSetup();
//EditorGUILayout.Space(8);
m_target.m_controlPointsFoldout = EditorGUILayout.Foldout(m_target.m_controlPointsFoldout, "Control points");
if (m_target.m_controlPointsFoldout)
@@ -60,14 +61,30 @@ void DrawConnectionSetups()
{
EditorGUILayout.BeginVertical();
m_target.fmodEvent = (string)EditorGUILayout.TextField("FMOD event path:", m_target.fmodEvent);
m_target.oscAddress =(string)EditorGUILayout.TextField("OSC ip address:", m_target.oscAddress);
m_target.oscAddress = (string)EditorGUILayout.TextField("OSC ip address:", m_target.oscAddress);
m_target.oscPort = (int)EditorGUILayout.IntField("OSC port:", m_target.oscPort);
m_target.m_outputChoice = (OutputChoice)EditorGUILayout.EnumPopup("Value if same targets: ", m_target.m_outputChoice);
EditorGUILayout.EndVertical();
m_target.fps = (int)EditorGUILayout.IntField("fps for data: ", m_target.fps);
}
}

void DrawStartSetup()
{
m_target.m_startModeFoldout = EditorGUILayout.Foldout(m_target.m_startModeFoldout, "Event start settings");
using (new EditorGUI.IndentLevelScope())
if (m_target.m_startModeFoldout)
{
EditorGUILayout.BeginVertical();
m_target.m_startMode = (StartMode)EditorGUILayout.EnumPopup("Start type: ", m_target.m_startMode);
if (m_target.m_startMode == StartMode.Triggered)
{
m_target.m_startSignalName = (string)EditorGUILayout.TextField("Start signal name:", m_target.m_startSignalName);
}
EditorGUILayout.EndVertical();
}
}

void DrawAddControlPointButton()
{
GUI.backgroundColor = Color.green;
75 changes: 75 additions & 0 deletions Runtime/EventSignaling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
using System.Collections.Generic;

public class EventSignaling : MonoBehaviour {

private Dictionary <string, UnityEvent> eventDictionary;

private static EventSignaling eventSignaling;

public static EventSignaling instance
{
get
{
if (!eventSignaling)
{
eventSignaling = FindObjectOfType (typeof (EventSignaling)) as EventSignaling;

if (!eventSignaling)
{
Debug.LogError ("There needs to be one active EventSignaling script on a GameObject in your scene.");
}
else
{
eventSignaling.Init ();
}
}

return eventSignaling;
}
}

void Init ()
{
if (eventDictionary == null)
{
eventDictionary = new Dictionary<string, UnityEvent>();
}
}

public static void StartListening (string eventName, UnityAction listener)
{
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
{
thisEvent.AddListener (listener);
}
else
{
thisEvent = new UnityEvent ();
thisEvent.AddListener (listener);
instance.eventDictionary.Add (eventName, thisEvent);
}
}

public static void StopListening (string eventName, UnityAction listener)
{
if (eventSignaling == null) return;
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
{
thisEvent.RemoveListener (listener);
}
}

public static void TriggerEvent (string eventName)
{
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
{
thisEvent.Invoke ();
}
}
}
11 changes: 11 additions & 0 deletions Runtime/EventSignaling.cs.meta

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

4 changes: 2 additions & 2 deletions Runtime/FMODEventInstancer.cs
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@

public sealed class FMODEventInstancer
{
//we store fmod event instances into dict to pass out reference to same instance between different control points
private Dictionary<String, EventInstance> _instances;


private static FMODEventInstancer _instance = null;
// mutex lock for thread safety
private static readonly object mutex = new object();
@@ -43,7 +43,7 @@ public EventInstance GetFmodEventInstance(string eventName)
else
{
var eventInstance = FMODUnity.RuntimeManager.CreateInstance(eventName);
eventInstance.start();
//eventInstance.start();
_instances.Add(eventName, eventInstance);
return eventInstance;
}

0 comments on commit 4bea3ca

Please sign in to comment.