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;
using VA;
// 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";
private static int Port = 12340;
private static string DefaultHRIRPath = "$(DefaultHRIR)";
private static string DefaultDirectivityPath = "$(Trumpet)";
private static int defaulthrir;
private static int defaultdirectivity;
private static int _DefaultHRIRID;
private static VANet _VA = null;
// No public construction allowed
private VAUAdapter() {}
private VAUAdapter() { }
public static VANet VA
{
......@@ -41,41 +41,31 @@ public class VAUAdapter : MonoBehaviour {
{
_VA.Connect(HostIP, Port);
_VA.Reset();
defaulthrir = _VA.LoadHRIRDataset(DefaultHRIRPath, "DefaultHRIR");
defaultdirectivity = _VA.LoadDirectivity(DefaultDirectivityPath, "DefaultDirectivity");
_VA.SetGlobalAuralizationMode(GlobalAuralizationMode);
Debug.Log("[VAU]Connect to Server: " + HostIP);
}
else
Debug.Log("[VAU]Already connected.");
}
public static int DefaultHRIR
{
get
{
return defaulthrir;
_DefaultHRIRID = _VA.LoadHRIRDataset(DefaultHRIRPath, "UnityDefaultHRIR");
}
}
public static int DefaultDirectivity
public static int DefaultHRIRID
{
get
{
return defaultdirectivity;
return _DefaultHRIRID;
}
}
// Use this for initialization
void Awake()
{
if(_VA==null)
if (_VA==null)
_VA = new VANet();
Init();
DeactivateUnitySound();
// if (! (_VA.AddSearchPath(Application.dataPath + "/..")))
// Debug.Log("[VA] Could not add SearchPath:" + Application.dataPath + "/..");
// Add Asset folder
_VA.AddSearchPath(Application.dataPath);
}
void OnEnable()
......@@ -91,23 +81,22 @@ public class VAUAdapter : MonoBehaviour {
void OnDisable()
{
VAUSoundSource[] SoundSources = FindObjectsOfType<VAUSoundSource>();
VAUSoundSource[] SoundSources = FindObjectsOfType<VAUSoundSource>();
foreach (VAUSoundSource SoundSource in SoundSources)
{
Debug.Log("[VAU]SoundSouce " + SoundSource.ID + " deleted.");
_VA.DeleteSoundSource(SoundSource.ID);
}
VAUListener[] Listeners = FindObjectsOfType<VAUListener>();
foreach (VAUListener Listener in Listeners)
{
Debug.Log("[VAU]Listener " + Listener.ID + " deleted.");
_VA.DeleteListener(Listener.ID);
}
if (GetComponent<VAUAuralizationMode>() != null)
GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnGlobalAuralizationModeChanged;
_VA.Disconnect();
}
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 {
private Quaternion q;
private Transform t;
private AudioReverbZone[] reverbZones;
private float currentArtificialReverberationTime = 0f;
private float shadowReverbTime;
public delegate void ReverbTimeChangedDelegate(double reverbTime);
......@@ -35,8 +34,7 @@ public class VAUListener : MonoBehaviour {
Listener = _VA.CreateListener(listenername);
listenername += " " + Listener;
_VA.SetListenerAuralizationMode(Listener, "all");
Debug.Log("[VAU]" + listenername + " created");
_VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIR);
_VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIRID);
SetListenerPositionOrientation();
_VA.SetArtificalReverbarationTime(VAAudioRenderer, 0.3f);
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;
using System.Collections.Generic;
using VA;
public class VAUSoundSource : MonoBehaviour {
public string directivitypath = "";
private int directivity;
private int Gain = 1;
private string SignalSource = "";
public class VAUSoundSource : MonoBehaviour
{
public bool ContinuousGainObserver = false; // Activate this if you want to validate & change volume and mute/unmute state on every update loop
public VAUDirectivity Directivity;
public VAUSignalSource SignalSource;
private VANet _VA = null;
private int SoundSource;
private int _ID;
private Quaternion q;
private Transform t;
private bool islooping;
private bool ismuted;
private bool isplaying;
private string _AudiofileSignalSource;
public int ID
{
get
{
return SoundSource;
}
}
public string SignalSourceID
{
get
{
return SignalSource;
return _ID;
}
}
......@@ -36,55 +28,67 @@ public class VAUSoundSource : MonoBehaviour {
void Start()
{
_VA = VAUAdapter.VA;
SoundSource = _VA.CreateSoundSource(this.name, 0, Gain);
_VA.SetSoundSourceAuralizationMode(SoundSource, "all");
islooping = GetComponent<AudioSource>().loop;
ismuted = GetComponent<AudioSource>().mute;
isplaying = GetComponent<AudioSource>().isPlaying;
if (directivitypath == "")
directivity = VAUAdapter.DefaultDirectivity;
else
directivity = _VA.LoadDirectivity(directivitypath, "SoundSource " + SoundSource + " Directivity");
_VA.SetSoundSourceDirectivity(SoundSource, directivity);
if (GetComponent<AudioSource>() != null)
{
if (GetComponent<AudioSource>().clip != null)
// Add sound source
_ID = _VA.CreateSoundSource(this.name, 0, 1.0);
_VA.SetSoundSourceAuralizationMode(_ID, "all");
// Connect to directivity, if linked or attached
if (Directivity)
_VA.SetSoundSourceDirectivity(_ID, Directivity.ID);
else if (GetComponent<VAUDirectivity>())
_VA.SetSoundSourceDirectivity(_ID, GetComponent<VAUDirectivity>().ID);
// Connect to signal source, if linked or attached
if (SignalSource)
_VA.SetSoundSourceSignalSource(_ID, SignalSource.ID);
else if (GetComponent<AudioSource>())
{
// 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");
_VA.SetSoundSourceSignalSource(SoundSource, SignalSource);
Debug.Log("[VAU]" + SignalSource + " connected to SoundSource " + SoundSource);
_VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PLAYING");
_VA.SetAudiofileSignalSourceIsLooping(SignalSource, islooping);
string FilePath = GetComponent<AudioSource>().clip.name + ".wav";
string Name = this.name + "_signal";
bool IsLooping = GetComponent<AudioSource>().loop;
bool PlayOnAwake = GetComponent<AudioSource>().playOnAwake;
_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);
if (GetComponent<VAUAuralizationMode>() != null)
{
// Activate auralization mode, if available
if (GetComponent<VAUAuralizationMode>())
GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
}
// Initially, set the pose (otherwise rendering module can not spatialize)
SetSoundSourcePositionOrientation();
}
// Update is called once per frame
void Update()
{
SetSoundSourcePositionOrientation();
SetSoundSourceMuted();
SetSignalSourcePlaying();
//_VA.SetSoundSourceVolume(SoundSource, Volume); <- flaot ändern
if (ContinuousGainObserver)
{
_VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
_VA.SetSoundSourceVolume(_ID, GetComponent<AudioSource>().volume);
}
}
void SetSoundSourcePositionOrientation()
{
//position and oritentation -> VA
// Convert left-handed Unity transform matrix to VA positions and orientations (right-handed, OpenGL)
_VA = VAUAdapter.VA;
t = GetComponent<Transform>();
q = t.rotation;
......@@ -92,43 +96,22 @@ public class VAUSoundSource : MonoBehaviour {
Vector3 view = q * Vector3.forward;
Vector3 view_ogl = new Vector3(view.x, view.y, -view.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);
}
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;
}
_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 OnSoundSourceAuralizationModeChanged(string AuraMode)
{
_VA.SetSoundSourceAuralizationMode(SoundSource, AuraMode);
_VA.SetSoundSourceAuralizationMode(_ID, AuraMode);
}
public void OnActivation()
{
ismuted = false;
}
public void OnDisable()
{
if (GetComponent<VAUAuralizationMode>() != null)
if (GetComponent<VAUAuralizationMode>())
GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged;
ismuted = true;
}
void OnDestroy()
......@@ -136,7 +119,11 @@ public class VAUSoundSource : MonoBehaviour {
_VA = VAUAdapter.VA;
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