VAUListener.cs 3.9 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
    private float shadowReverbTime;
18 19 20 21

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

22 23 24 25 26 27 28 29
    public int ID
    {
        get
        {
            return Listener;
        }
    }

30 31 32
    // Use this for initialization
    void Start()
    {
33
        _VA = VAUAdapter.VA;
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
34
        Listener = _VA.CreateListener(listenername);
35
        listenername += " " + Listener;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
36
        _VA.SetListenerAuralizationMode(Listener, "all");
37
        _VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIRID);
38
        SetListenerPositionOrientation();
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
39
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, 0.3f);
40
        reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
41 42 43 44
    }

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

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
56
    //uses the View- and Up-Vector to transmit the position of the listener to VA
57
    void SetListenerPositionOrientation()
58
    {
59
        _VA = VAUAdapter.VA;
60 61
        t = GetComponent<Transform>();
        q = t.rotation;
62 63
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
64 65
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
66
        _VA.SetListenerPosition(Listener, t.transform.position.x, t.transform.position.y, -t.transform.position.z);
67
        _VA.SetListenerOrientationVU(Listener, view_ogl.x, view_ogl.y, view_ogl.z, up_ogl.x, up_ogl.y, up_ogl.z);
68
    }
69

70 71 72 73 74
    void SetActiveReverbZones()
    {
        if (reverbZones == null)
            return;

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

98

99
        if (actReverbTime < 0.3f)
100
            _VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", true);
101
        else
102
            _VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", false);
103

104
        shadowReverbTime = actReverbTime;
105

106
        if (i > 0)
107
            actReverbTime /= i;
108 109 110 111

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

112
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
113 114 115

    }

116 117 118 119
    void OnListenerAuralizationModeChanged(string AuraMode)
    {
        _VA.SetListenerAuralizationMode(Listener, AuraMode);
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
120

121 122 123 124 125
    void OnDisable()
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnListenerAuralizationModeChanged;
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
126

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
127 128
    void OnDestroy()
    {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
129
        _VA.DeleteListener(Listener);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
130
    }
131
}