Commit 84400381 authored by Dipl.-Ing. Jonas Stienen's avatar Dipl.-Ing. Jonas Stienen
Browse files

Refactoring a lot. Also introducing directivity, signal source super class and...

Refactoring a lot. Also introducing directivity, signal source super class and audiofile signal source
parent 01af3b80
...@@ -6,20 +6,20 @@ using System.Runtime.InteropServices; ...@@ -6,20 +6,20 @@ using System.Runtime.InteropServices;
using VA; using VA;
// Adapter to VA (singleton) // Adapter to VA (singleton)
public class VAUAdapter : MonoBehaviour { public class VAUAdapter : MonoBehaviour
{
private static string HostIP = "localhost"; public static string HostIP = "localhost";
public static string GlobalAuralizationMode = "all"; public static string GlobalAuralizationMode = "all";
private static int Port = 12340; private static int Port = 12340;
private static string DefaultHRIRPath = "$(DefaultHRIR)"; private static string DefaultHRIRPath = "$(DefaultHRIR)";
private static string DefaultDirectivityPath = "$(Trumpet)"; private static int _DefaultHRIRID;
private static int defaulthrir;
private static int defaultdirectivity;
private static VANet _VA = null; private static VANet _VA = null;
// No public construction allowed // No public construction allowed
private VAUAdapter() {} private VAUAdapter() { }
public static VANet VA public static VANet VA
{ {
...@@ -41,41 +41,31 @@ public class VAUAdapter : MonoBehaviour { ...@@ -41,41 +41,31 @@ public class VAUAdapter : MonoBehaviour {
{ {
_VA.Connect(HostIP, Port); _VA.Connect(HostIP, Port);
_VA.Reset(); _VA.Reset();
defaulthrir = _VA.LoadHRIRDataset(DefaultHRIRPath, "DefaultHRIR");
defaultdirectivity = _VA.LoadDirectivity(DefaultDirectivityPath, "DefaultDirectivity");
_VA.SetGlobalAuralizationMode(GlobalAuralizationMode); _VA.SetGlobalAuralizationMode(GlobalAuralizationMode);
Debug.Log("[VAU]Connect to Server: " + HostIP); _DefaultHRIRID = _VA.LoadHRIRDataset(DefaultHRIRPath, "UnityDefaultHRIR");
}
else
Debug.Log("[VAU]Already connected.");
}
public static int DefaultHRIR
{
get
{
return defaulthrir;
} }
} }
public static int DefaultDirectivity public static int DefaultHRIRID
{ {
get get
{ {
return defaultdirectivity; return _DefaultHRIRID;
} }
} }
// Use this for initialization // Use this for initialization
void Awake() void Awake()
{ {
if(_VA==null) if (_VA==null)
_VA = new VANet(); _VA = new VANet();
Init(); Init();
DeactivateUnitySound(); DeactivateUnitySound();
// if (! (_VA.AddSearchPath(Application.dataPath + "/..")))
// Debug.Log("[VA] Could not add SearchPath:" + Application.dataPath + "/.."); // Add Asset folder
_VA.AddSearchPath(Application.dataPath);
} }
void OnEnable() void OnEnable()
...@@ -91,23 +81,22 @@ public class VAUAdapter : MonoBehaviour { ...@@ -91,23 +81,22 @@ public class VAUAdapter : MonoBehaviour {
void OnDisable() void OnDisable()
{ {
VAUSoundSource[] SoundSources = FindObjectsOfType<VAUSoundSource>(); VAUSoundSource[] SoundSources = FindObjectsOfType<VAUSoundSource>();
foreach (VAUSoundSource SoundSource in SoundSources) foreach (VAUSoundSource SoundSource in SoundSources)
{ {
Debug.Log("[VAU]SoundSouce " + SoundSource.ID + " deleted.");
_VA.DeleteSoundSource(SoundSource.ID); _VA.DeleteSoundSource(SoundSource.ID);
} }
VAUListener[] Listeners = FindObjectsOfType<VAUListener>(); VAUListener[] Listeners = FindObjectsOfType<VAUListener>();
foreach (VAUListener Listener in Listeners) foreach (VAUListener Listener in Listeners)
{ {
Debug.Log("[VAU]Listener " + Listener.ID + " deleted.");
_VA.DeleteListener(Listener.ID); _VA.DeleteListener(Listener.ID);
} }
if (GetComponent<VAUAuralizationMode>() != null) if (GetComponent<VAUAuralizationMode>() != null)
GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnGlobalAuralizationModeChanged; GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnGlobalAuralizationModeChanged;
_VA.Disconnect(); _VA.Disconnect();
} }
void DeactivateUnitySound() void DeactivateUnitySound()
......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VAUAudiofileSignalSource : VAUSignalSource {
public string FilePath;
public string Name;
public bool IsLooping = true;
public bool PlayOnStart = true;
// Use this for initialization
void Awake()
{
_ID = VAUAdapter.VA.CreateAudiofileSignalSource(FilePath, Name);
Debug.Assert(_ID.Length > 0);
}
// Use this for initialization
void Start()
{
VAUAdapter.VA.SetAudiofileSignalSourceIsLooping(_ID, IsLooping);
if( PlayOnStart )
VAUAdapter.VA.SetAudiofileSignalSourcePlaybackAction(_ID, "PLAY");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VAUDirectivity : MonoBehaviour {
protected int _ID = -1;
public string Path = "";
public string Name = "";
public int ID
{
get
{
return _ID;
}
}
// Use this for initialization
void Start ()
{
if (Path.Length > 0)
_ID = VAUAdapter.VA.LoadDirectivity(Path, Name);
}
// Update is called once per frame
void Update () {
}
}
...@@ -14,7 +14,6 @@ public class VAUListener : MonoBehaviour { ...@@ -14,7 +14,6 @@ public class VAUListener : MonoBehaviour {
private Quaternion q; private Quaternion q;
private Transform t; private Transform t;
private AudioReverbZone[] reverbZones; private AudioReverbZone[] reverbZones;
private float currentArtificialReverberationTime = 0f;
private float shadowReverbTime; private float shadowReverbTime;
public delegate void ReverbTimeChangedDelegate(double reverbTime); public delegate void ReverbTimeChangedDelegate(double reverbTime);
...@@ -35,8 +34,7 @@ public class VAUListener : MonoBehaviour { ...@@ -35,8 +34,7 @@ public class VAUListener : MonoBehaviour {
Listener = _VA.CreateListener(listenername); Listener = _VA.CreateListener(listenername);
listenername += " " + Listener; listenername += " " + Listener;
_VA.SetListenerAuralizationMode(Listener, "all"); _VA.SetListenerAuralizationMode(Listener, "all");
Debug.Log("[VAU]" + listenername + " created"); _VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIRID);
_VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIR);
SetListenerPositionOrientation(); SetListenerPositionOrientation();
_VA.SetArtificalReverbarationTime(VAAudioRenderer, 0.3f); _VA.SetArtificalReverbarationTime(VAAudioRenderer, 0.3f);
reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[]; reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VAUSignalSource : MonoBehaviour {
protected string _ID = "";
public string ID
{
get
{
return _ID;
}
}
}
...@@ -3,32 +3,24 @@ using System.IO; ...@@ -3,32 +3,24 @@ using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using VA; using VA;
public class VAUSoundSource : MonoBehaviour { public class VAUSoundSource : MonoBehaviour
{
public string directivitypath = "";
private int directivity; public bool ContinuousGainObserver = false; // Activate this if you want to validate & change volume and mute/unmute state on every update loop
private int Gain = 1; public VAUDirectivity Directivity;
private string SignalSource = ""; public VAUSignalSource SignalSource;
private VANet _VA = null; private VANet _VA = null;
private int SoundSource; private int _ID;
private Quaternion q; private Quaternion q;
private Transform t; private Transform t;
private bool islooping; private string _AudiofileSignalSource;
private bool ismuted;
private bool isplaying;
public int ID public int ID
{ {
get get
{ {
return SoundSource; return _ID;
}
}
public string SignalSourceID
{
get
{
return SignalSource;
} }
} }
...@@ -36,55 +28,67 @@ public class VAUSoundSource : MonoBehaviour { ...@@ -36,55 +28,67 @@ public class VAUSoundSource : MonoBehaviour {
void Start() void Start()
{ {
_VA = VAUAdapter.VA; _VA = VAUAdapter.VA;
SoundSource = _VA.CreateSoundSource(this.name, 0, Gain);
_VA.SetSoundSourceAuralizationMode(SoundSource, "all"); // Add sound source
_ID = _VA.CreateSoundSource(this.name, 0, 1.0);
islooping = GetComponent<AudioSource>().loop; _VA.SetSoundSourceAuralizationMode(_ID, "all");
ismuted = GetComponent<AudioSource>().mute;
isplaying = GetComponent<AudioSource>().isPlaying; // Connect to directivity, if linked or attached
if (Directivity)
if (directivitypath == "") _VA.SetSoundSourceDirectivity(_ID, Directivity.ID);
directivity = VAUAdapter.DefaultDirectivity; else if (GetComponent<VAUDirectivity>())
else _VA.SetSoundSourceDirectivity(_ID, GetComponent<VAUDirectivity>().ID);
directivity = _VA.LoadDirectivity(directivitypath, "SoundSource " + SoundSource + " Directivity");
_VA.SetSoundSourceDirectivity(SoundSource, directivity); // Connect to signal source, if linked or attached
if (SignalSource)
if (GetComponent<AudioSource>() != null) _VA.SetSoundSourceSignalSource(_ID, SignalSource.ID);
{ else if (GetComponent<AudioSource>())
if (GetComponent<AudioSource>().clip != null) {
// Set gain control once
_VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
_VA.SetSoundSourceVolume(_ID, GetComponent<AudioSource>().volume);
// Create and connect audio signal source
if (GetComponent<AudioSource>().clip)
{ {
SignalSource = _VA.CreateAudiofileSignalSource( "Audiofiles/" + GetComponent<AudioSource>().clip.name + ".wav", this.name + "_signal"); string FilePath = GetComponent<AudioSource>().clip.name + ".wav";
_VA.SetSoundSourceSignalSource(SoundSource, SignalSource); string Name = this.name + "_signal";
Debug.Log("[VAU]" + SignalSource + " connected to SoundSource " + SoundSource); bool IsLooping = GetComponent<AudioSource>().loop;
_VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PLAYING"); bool PlayOnAwake = GetComponent<AudioSource>().playOnAwake;
_VA.SetAudiofileSignalSourceIsLooping(SignalSource, islooping);
_AudiofileSignalSource = _VA.CreateAudiofileSignalSource(FilePath, Name);
Debug.Assert(_AudiofileSignalSource.Length > 0);
_VA.SetAudiofileSignalSourceIsLooping(_AudiofileSignalSource, IsLooping);
if (PlayOnAwake)
_VA.SetAudiofileSignalSourcePlaybackAction(_AudiofileSignalSource, "play");
_VA.SetSoundSourceSignalSource(_ID, _AudiofileSignalSource);
} }
else
Debug.Log("[VAU]No clip on AudioSource of GameObject: " + gameObject.name);
} }
else
Debug.Log("[VAU]No AudioSource on GameObject: " + gameObject.name); // Activate auralization mode, if available
if (GetComponent<VAUAuralizationMode>() != null) if (GetComponent<VAUAuralizationMode>())
{
GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged; GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
}
// Initially, set the pose (otherwise rendering module can not spatialize)
SetSoundSourcePositionOrientation(); SetSoundSourcePositionOrientation();
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
SetSoundSourcePositionOrientation(); SetSoundSourcePositionOrientation();
SetSoundSourceMuted();
SetSignalSourcePlaying(); if (ContinuousGainObserver)
//_VA.SetSoundSourceVolume(SoundSource, Volume); <- flaot ändern {
_VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
_VA.SetSoundSourceVolume(_ID, GetComponent<AudioSource>().volume);
}
} }
void SetSoundSourcePositionOrientation() void SetSoundSourcePositionOrientation()
{ {
//position and oritentation -> VA // Convert left-handed Unity transform matrix to VA positions and orientations (right-handed, OpenGL)
_VA = VAUAdapter.VA; _VA = VAUAdapter.VA;
t = GetComponent<Transform>(); t = GetComponent<Transform>();
q = t.rotation; q = t.rotation;
...@@ -92,43 +96,22 @@ public class VAUSoundSource : MonoBehaviour { ...@@ -92,43 +96,22 @@ public class VAUSoundSource : MonoBehaviour {
Vector3 view = q * Vector3.forward; Vector3 view = q * Vector3.forward;
Vector3 view_ogl = new Vector3(view.x, view.y, -view.z); Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
Vector3 up_ogl = new Vector3(up.x, up.y, -up.z); Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
_VA.SetSoundSourcePositionOrientationVU(SoundSource, t.transform.position.x, t.transform.position.y, -t.transform.position.z, view_ogl.x, view_ogl.y, view_ogl.z, up_ogl.x, up_ogl.y, up_ogl.z); _VA.SetSoundSourcePositionOrientationVU(_ID, t.transform.position.x, t.transform.position.y, -t.transform.position.z, view_ogl.x, view_ogl.y, view_ogl.z, up_ogl.x, up_ogl.y, up_ogl.z);
}
void SetSignalSourcePlaying()
{
if(GetComponent<AudioSource>().isPlaying != isplaying)
{
isplaying = GetComponent<AudioSource>().isPlaying;
if (isplaying)
_VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PLAYING");
else
_VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PAUSED");
}
}
void SetSoundSourceMuted()
{
if (ismuted != GetComponent<AudioSource>().mute)
{
_VA.SetSoundSourceMuted(SoundSource, GetComponent<AudioSource>().mute);
ismuted = GetComponent<AudioSource>().mute;
}
} }
void OnSoundSourceAuralizationModeChanged(string AuraMode) void OnSoundSourceAuralizationModeChanged(string AuraMode)
{ {
_VA.SetSoundSourceAuralizationMode(SoundSource, AuraMode); _VA.SetSoundSourceAuralizationMode(_ID, AuraMode);
} }
public void OnActivation() public void OnActivation()
{ {
ismuted = false;
} }
public void OnDisable() public void OnDisable()
{ {
if (GetComponent<VAUAuralizationMode>() != null) if (GetComponent<VAUAuralizationMode>())
GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged; GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged;
ismuted = true;
} }
void OnDestroy() void OnDestroy()
...@@ -136,7 +119,11 @@ public class VAUSoundSource : MonoBehaviour { ...@@ -136,7 +119,11 @@ public class VAUSoundSource : MonoBehaviour {
_VA = VAUAdapter.VA; _VA = VAUAdapter.VA;
if (_VA.IsConnected()) if (_VA.IsConnected())
{ {
_VA.DeleteSoundSource(SoundSource); _VA.DeleteSoundSource(_ID);
// Temptative signal source deletion
if (_AudiofileSignalSource.Length > 0)
_VA.DeleteSignalSource(_AudiofileSignalSource);
} }
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment