Cleaning receiver and re-adding listener as a special receiver

parent f7013dd8
using UnityEngine;
using System.Linq;
using System.Collections;
using VA;
[Tooltip("A human listener that can receive sound and will be rendered by an audio renderer")]
public class VAUListener : MonoBehaviour {
[Tooltip("Descriptive name")]
public string Name = "HumanListener";
[Tooltip("Number of reverbzones used for determining the current reverb.")]
public int NumMaxReverbZones = 2;
public string VAAudioRenderer = "MyBinauralArtificialReverb";
[Tooltip("Add custom VAUHRIR dataset.")]
public VAUDirectivity HRIR = null;
[Tooltip("Anthropometric head width, for HRIR individualization or generic binaural cues")]
public double HeadWidth = 0.12;
[Tooltip("Anthropometric head height, for HRIR individualization or generic binaural cues")]
public double HeadHeight = 0.10;
[Tooltip("Anthropometric head depth, for HRIR individualization or generic binaural cues")]
public double HeadDepth = 0.15;
private double ShadowHeadWidth;
private double ShadowHeadHeight;
private double ShadowHeadDepth;
private int _ID;
private VANet _VA = null;
private Quaternion q;
private Transform t;
private AudioReverbZone[] reverbZones;
private float shadowReverbTime;
public delegate void ReverbTimeChangedDelegate(double reverbTime);
public event ReverbTimeChangedDelegate ReverbTimeChanged;
public int ID
{
get
{
return _ID;
}
}
void Start()
{
_VA = VAUAdapter.VA;
_ID = _VA.CreateSoundReceiver(Name);
_VA.SetSoundReceiverAuralizationMode(_ID, "all");
if (HRIR)
_VA.SetSoundReceiverDirectivity(_ID, HRIR.ID);
else if( VAUAdapter.DefaultHRIRID != -1 )
_VA.SetSoundReceiverDirectivity(_ID, VAUAdapter.DefaultHRIRID);
_VA.SetSoundReceiverAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
ShadowHeadWidth = HeadWidth;
ShadowHeadHeight = HeadHeight;
ShadowHeadDepth = HeadDepth;
SetSoundReceiverPositionOrientation();
_VA.SetArtificialReverberationTime(VAAudioRenderer, 0.3f);
reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
}
void OnEnable()
{
if (GetComponent<VAUAuralizationMode>() != null)
GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundReceiverAuralizationModeChanged;
}
void Update ()
{
SetSoundReceiverPositionOrientation();
SetActiveReverbZones();
if( HeadWidth != ShadowHeadWidth || HeadHeight != ShadowHeadHeight || HeadDepth != ShadowHeadDepth )
{
_VA.SetSoundReceiverAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
ShadowHeadWidth = HeadWidth;
ShadowHeadHeight = HeadHeight;
ShadowHeadDepth = HeadDepth;
}
}
// Uses the View- and Up-Vector to transmit the position of the listener to VA
void SetSoundReceiverPositionOrientation()
{
_VA = VAUAdapter.VA;
t = GetComponent<Transform>();
q = t.rotation;
Vector3 up = q * Vector3.up;
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.SetSoundReceiverPosition(_ID, new VAVec3( t.transform.position.x, t.transform.position.y, -t.transform.position.z ));
_VA.SetSoundReceiverOrientationVU(_ID, new VAVec3( view_ogl.x, view_ogl.y, view_ogl.z ), new VAVec3( up_ogl.x, up_ogl.y, up_ogl.z ));
}
void SetActiveReverbZones()
{
if (reverbZones == null)
return;
float actReverbTime = 0f;
int i = 0;
foreach (AudioReverbZone reverbZone in reverbZones)
{
if (i >= NumMaxReverbZones)
break;
float actDistance = Vector3.Distance(reverbZone.transform.position, gameObject.transform.position);
if ((actDistance < reverbZone.maxDistance))
{
if (actDistance > reverbZone.minDistance)
{
actReverbTime += reverbZone.decayTime * (reverbZone.maxDistance - actDistance) / (reverbZone.maxDistance - reverbZone.minDistance);
}
else
{
actReverbTime += reverbZone.decayTime;
}
i++;
}
}
if (shadowReverbTime == actReverbTime)
return;
if (actReverbTime < 0.3f)
_VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", true);
else
_VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", false);
shadowReverbTime = actReverbTime;
if (i > 0)
actReverbTime /= i;
if (ReverbTimeChanged != null)
ReverbTimeChanged(actReverbTime);
_VA.SetArtificialReverberationTime(VAAudioRenderer, actReverbTime);
}
void OnSoundReceiverAuralizationModeChanged(string AuraMode)
{
_VA.SetSoundReceiverAuralizationMode(_ID, AuraMode);
}
private void OnDestroy()
{
if (GetComponent<VAUAuralizationMode>() != null)
GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundReceiverAuralizationModeChanged;
_VA.DeleteSoundReceiver(_ID);
}
}
......@@ -4,37 +4,23 @@ using System.Collections;
using VA;
[Tooltip("Entity that can receive sound and will be rendered by an audio renderer")]
public class VAUSoundReceiver : MonoBehaviour {
[Tooltip("Descriptive name")]
public string Name = "SoundReceiver";
[Tooltip("Number of reverbzones used for determining the current reverb.")]
public int NumMaxReverbZones = 2;
public string VAAudioRenderer = "MyBinauralArtificialReverb";
[Tooltip("Add custom VAUHRIR dataset.")]
public VAUDirectivity HRIR = null;
[Tooltip("Set an explicit renderer for this receiver")]
public VAUAudioRenderer Renderer = null;
[Tooltip("Anthropometric data, for HRIR individualization or generic binaural cues")]
public double HeadWidth = 0.12;
[Tooltip("Anthropometric data, for HRIR individualization or generic binaural cues")]
public double HeadHeight = 0.10;
[Tooltip("Anthropometric data, for HRIR individualization or generic binaural cues")]
public double HeadDepth = 0.15;
private double ShadowHeadWidth;
private double ShadowHeadHeight;
private double ShadowHeadDepth;
[Tooltip("Connect an directivity to this receiver")]
public VAUDirectivity Directivity = null;
private int _ID;
private VANet _VA = null;
private Quaternion q;
private Transform t;
private AudioReverbZone[] reverbZones;
private float shadowReverbTime;
public delegate void ReverbTimeChangedDelegate(double reverbTime);
public event ReverbTimeChangedDelegate ReverbTimeChanged;
public int ID
{
......@@ -54,15 +40,7 @@ public class VAUSoundReceiver : MonoBehaviour {
else if( VAUAdapter.DefaultHRIRID != -1 )
_VA.SetSoundReceiverDirectivity(_ID, VAUAdapter.DefaultHRIRID);
_VA.SetSoundReceiverAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
ShadowHeadWidth = HeadWidth;
ShadowHeadHeight = HeadHeight;
ShadowHeadDepth = HeadDepth;
SetSoundReceiverPositionOrientation();
_VA.SetArtificialReverberationTime(VAAudioRenderer, 0.3f);
reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
}
void OnEnable()
......@@ -74,15 +52,6 @@ public class VAUSoundReceiver : MonoBehaviour {
void Update ()
{
SetSoundReceiverPositionOrientation();
SetActiveReverbZones();
if( HeadWidth != ShadowHeadWidth || HeadHeight != ShadowHeadHeight || HeadDepth != ShadowHeadDepth )
{
_VA.SetSoundReceiverAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
ShadowHeadWidth = HeadWidth;
ShadowHeadHeight = HeadHeight;
ShadowHeadDepth = HeadDepth;
}
}
// Uses the View- and Up-Vector to transmit the position of the listener to VA
......@@ -99,52 +68,6 @@ public class VAUSoundReceiver : MonoBehaviour {
_VA.SetSoundReceiverOrientationVU(_ID, new VAVec3( view_ogl.x, view_ogl.y, view_ogl.z ), new VAVec3( up_ogl.x, up_ogl.y, up_ogl.z ));
}
void SetActiveReverbZones()
{
if (reverbZones == null)
return;
float actReverbTime = 0f;
int i = 0;
foreach (AudioReverbZone reverbZone in reverbZones)
{
if (i >= NumMaxReverbZones)
break;
float actDistance = Vector3.Distance(reverbZone.transform.position, gameObject.transform.position);
if ((actDistance < reverbZone.maxDistance))
{
if (actDistance > reverbZone.minDistance)
{
actReverbTime += reverbZone.decayTime * (reverbZone.maxDistance - actDistance) / (reverbZone.maxDistance - reverbZone.minDistance);
}
else
{
actReverbTime += reverbZone.decayTime;
}
i++;
}
}
if (shadowReverbTime == actReverbTime)
return;
if (actReverbTime < 0.3f)
_VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", true);
else
_VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", false);
shadowReverbTime = actReverbTime;
if (i > 0)
actReverbTime /= i;
if (ReverbTimeChanged != null)
ReverbTimeChanged(actReverbTime);
_VA.SetArtificialReverberationTime(VAAudioRenderer, actReverbTime);
}
void OnSoundReceiverAuralizationModeChanged(string AuraMode)
{
_VA.SetSoundReceiverAuralizationMode(_ID, AuraMode);
......
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