Aufgrund einer Wartung wird GitLab am 28.09. zwischen 10:00 und 11:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 28.09. between 10:00 and 11:00 am.

VAUListener.cs 4.06 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 20 21 22

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

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

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

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

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

72 73 74 75 76
    void SetActiveReverbZones()
    {
        if (reverbZones == null)
            return;

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

100

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

106
        shadowReverbTime = actReverbTime;
107

108
        if (i > 0)
109
            actReverbTime /= i;
110 111 112 113

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

114
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
115 116 117

    }

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

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

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