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

11
    [Tooltip("Number of reverbzones used for determining the current reverb.")]
12
    public int NumMaxReverbZones = 2;
13
    public string VAAudioRenderer = "MyBinauralArtificialReverb";
14
    [Tooltip("Add custom VAUHRIR dataset.")]
15
    public VAUHRIRDataset HRIR = null;
16
    
17 18 19 20 21 22
    [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;
23 24 25 26

    private double ShadowHeadWidth;
    private double ShadowHeadHeight;
    private double ShadowHeadDepth;
27 28

    private int _ID;
29
    private VANet _VA = null;
30

31 32
    private Quaternion q;
    private Transform t;
33
    private AudioReverbZone[] reverbZones;
34
    private float shadowReverbTime;
35 36 37 38

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

39 40 41 42
    public int ID
    {
        get
        {
43
            return _ID;
44 45
        }
    }
46
    
47 48
    void Start()
    {
49
        _VA = VAUAdapter.VA;
50 51 52 53 54 55 56
        _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);

57 58 59 60 61
        _VA.SetListenerAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
        ShadowHeadWidth = HeadWidth;
        ShadowHeadHeight = HeadHeight;
        ShadowHeadDepth = HeadDepth;

62
        SetListenerPositionOrientation();
63

Dipl.-Ing. Jonas Stienen's avatar
Fixes  
Dipl.-Ing. Jonas Stienen committed
64
        _VA.SetArtificialReverberationTime(VAAudioRenderer, 0.3f);
65
        reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
66 67 68 69
    }

    void OnEnable()
    {
70 71
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnListenerAuralizationModeChanged;
72
    }
73 74

    void Update ()
75 76
    {
        SetListenerPositionOrientation();
77
        SetActiveReverbZones();
78 79 80 81 82 83 84 85

        if( HeadWidth != ShadowHeadWidth || HeadHeight != ShadowHeadHeight || HeadDepth != ShadowHeadDepth )
        {
            _VA.SetListenerAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
            ShadowHeadWidth = HeadWidth;
            ShadowHeadHeight = HeadHeight;
            ShadowHeadDepth = HeadDepth;
        }
86
    }
87

88
    // Uses the View- and Up-Vector to transmit the position of the listener to VA
89
    void SetListenerPositionOrientation()
90
    {
91
        _VA = VAUAdapter.VA;
92 93
        t = GetComponent<Transform>();
        q = t.rotation;
94 95
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
96 97
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
98 99
        _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);
100
    }
101

102 103 104 105 106
    void SetActiveReverbZones()
    {
        if (reverbZones == null)
            return;

107
        float actReverbTime = 0f;
108 109 110
        int i = 0;
        foreach (AudioReverbZone reverbZone in reverbZones)
        {
111
            if (i >= NumMaxReverbZones)
112 113 114
                break;
            float actDistance = Vector3.Distance(reverbZone.transform.position, gameObject.transform.position);
            if ((actDistance < reverbZone.maxDistance))
115
            {
116
                if (actDistance > reverbZone.minDistance)
117
                {
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
118
                    actReverbTime += reverbZone.decayTime * (reverbZone.maxDistance - actDistance) / (reverbZone.maxDistance - reverbZone.minDistance);
119
                }
120
                else
121
                {
122
                    actReverbTime += reverbZone.decayTime;
123
                }
124
                i++;
125 126
            }
        }
127 128 129
        if (shadowReverbTime == actReverbTime)
            return;

130

131
        if (actReverbTime < 0.3f)
132
            _VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", true);
133
        else
134
            _VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", false);
135

136
        shadowReverbTime = actReverbTime;
137

138
        if (i > 0)
139
            actReverbTime /= i;
140 141 142 143

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

Dipl.-Ing. Jonas Stienen's avatar
Fixes  
Dipl.-Ing. Jonas Stienen committed
144
		_VA.SetArtificialReverberationTime(VAAudioRenderer, actReverbTime);
145 146 147

    }

148 149
    void OnListenerAuralizationModeChanged(string AuraMode)
    {
150
        _VA.SetListenerAuralizationMode(_ID, AuraMode);
151
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
152

153
    private void OnDestroy()
154 155 156
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnListenerAuralizationModeChanged;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
157

158
        _VA.DeleteListener(_ID);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
159
    }
160
}