VAUListener.cs 4.16 KB
Newer Older
1
using UnityEngine;
2
using System.Linq;
3 4 5 6 7 8
using System.Collections;
using VA;


public class VAUListener : MonoBehaviour {

9
    public string listenername = "Listener";
10 11
    public int maxreverbZones = 2;
    public string VAAudioRenderer = "MyBinauralArtificialReverb";
12 13
    private int Listener;
    private VANet _VA = null;
14 15
    private Quaternion q;
    private Transform t;
16
    private AudioReverbZone[] reverbZones;
17 18
    private float currentArtificialReverberationTime = 0f;
    private float shadowReverbTime;
19
    private VAUAuralizationMode globalAuraMode;
20 21 22 23

    public delegate void ReverbTimeChangedDelegate(double reverbTime);
    public event ReverbTimeChangedDelegate ReverbTimeChanged;

24 25 26 27 28 29 30 31
    public int ID
    {
        get
        {
            return Listener;
        }
    }

32 33 34
    // Use this for initialization
    void Start()
    {
35
        _VA = VAUAdapter.VA;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
36
        Listener = _VA.CreateListener(listenername);
37
        listenername += " " + Listener;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
38
        _VA.SetListenerAuralizationMode(Listener, "all");
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
39
        Debug.Log("[VAU]" + listenername + " created");
40
        _VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIR);
41
        SetListenerPositionOrientation();
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
42
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, 0.3f);
43
        reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
44
        globalAuraMode = FindObjectOfType<VAUAdapter>().gameObject.GetComponent<VAUAuralizationMode>();
45 46 47 48
    }

    void OnEnable()
    {
49 50
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnListenerAuralizationModeChanged;
51 52 53 54
    }
	
	// Update is called once per frame
	void Update ()
55 56
    {
        SetListenerPositionOrientation();
57
        SetActiveReverbZones();
58
    }
59

60
    //uses the View- and Up-Vector to transmit the position of the listener to VA
61
    void SetListenerPositionOrientation()
62
    {
63
        _VA = VAUAdapter.VA;
64 65
        t = GetComponent<Transform>();
        q = t.rotation;
66 67
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
68 69
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
70
        _VA.SetListenerPosition(Listener, t.transform.position.x, t.transform.position.y, -t.transform.position.z);
71
        _VA.SetListenerOrientationVU(Listener, view_ogl.x, view_ogl.y, view_ogl.z, up_ogl.x, up_ogl.y, up_ogl.z);
72
    }
73

74 75 76 77 78
    void SetActiveReverbZones()
    {
        if (reverbZones == null)
            return;

79
        float actReverbTime = 0f;
80 81 82
        int i = 0;
        foreach (AudioReverbZone reverbZone in reverbZones)
        {
83 84 85 86
            if (i >= maxreverbZones)
                break;
            float actDistance = Vector3.Distance(reverbZone.transform.position, gameObject.transform.position);
            if ((actDistance < reverbZone.maxDistance))
87
            {
88
                if (actDistance > reverbZone.minDistance)
89
                {
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
90
                    actReverbTime += reverbZone.decayTime * (reverbZone.maxDistance - actDistance) / (reverbZone.maxDistance - reverbZone.minDistance);
91
                }
92
                else
93
                {
94
                    actReverbTime += reverbZone.decayTime;
95
                }
96
                i++;
97 98
            }
        }
99 100 101
        if (shadowReverbTime == actReverbTime)
            return;

102 103 104 105 106 107
        
        if (actReverbTime < 0.3f)
            globalAuraMode.DiffuseDecay = false;
        else
            globalAuraMode.DiffuseDecay = true;

108
        shadowReverbTime = actReverbTime;
109

110
        if (i > 0)
111
            actReverbTime /= i;
112 113 114 115

        if (ReverbTimeChanged != null)
            ReverbTimeChanged(actReverbTime);

116
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
117 118 119

    }

120 121 122 123
    void OnListenerAuralizationModeChanged(string AuraMode)
    {
        _VA.SetListenerAuralizationMode(Listener, AuraMode);
    }
124

125 126 127 128 129
    void OnDisable()
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnListenerAuralizationModeChanged;
    }
130

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
131 132 133 134 135 136 137
    void OnDestroy()
    {
        if (_VA.IsConnected())
        {
            _VA.DeleteListener(Listener);
        }
    }
138
}