VAUListener.cs 3.91 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
10
11
    public string Name = "Listener";

    public int NumMaxReverbZones = 2;
12
    public string VAAudioRenderer = "MyBinauralArtificialReverb";
13
14
15
    public VAUHRIRDataset HRIR = null;

    private int _ID;
16
    private VANet _VA = null;
17

18
19
    private Quaternion q;
    private Transform t;
20
    private AudioReverbZone[] reverbZones;
21
    private float shadowReverbTime;
22
23
24
25

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

26
27
28
29
    public int ID
    {
        get
        {
30
            return _ID;
31
32
        }
    }
33
    
34
35
    void Start()
    {
36
        _VA = VAUAdapter.VA;
37
38
39
40
41
42
43
        _ID = _VA.CreateListener(Name);
        _VA.SetListenerAuralizationMode(_ID, "all");
        if (HRIR)
            _VA.SetListenerHRIRDataset(_ID, HRIR.ID);
        else if( VAUAdapter.DefaultHRIRID != -1 )
            _VA.SetListenerHRIRDataset(_ID, VAUAdapter.DefaultHRIRID);

44
        SetListenerPositionOrientation();
45

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
46
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, 0.3f);
47
        reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
48
49
50
51
    }

    void OnEnable()
    {
52
53
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnListenerAuralizationModeChanged;
54
    }
55
56

    void Update ()
57
58
    {
        SetListenerPositionOrientation();
59
        SetActiveReverbZones();
60
    }
61

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

76
77
78
79
80
    void SetActiveReverbZones()
    {
        if (reverbZones == null)
            return;

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

104

105
        if (actReverbTime < 0.3f)
106
            _VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", true);
107
        else
108
            _VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", false);
109

110
        shadowReverbTime = actReverbTime;
111

112
        if (i > 0)
113
            actReverbTime /= i;
114
115
116
117

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

118
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
119
120
121

    }

122
123
    void OnListenerAuralizationModeChanged(string AuraMode)
    {
124
        _VA.SetListenerAuralizationMode(_ID, AuraMode);
125
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
126

127
128
129
130
131
    void OnDisable()
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnListenerAuralizationModeChanged;
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
132

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
133
134
    void OnDestroy()
    {
135
        _VA.DeleteListener(_ID);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
136
    }
137
}