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 3.83 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 62
        t = GetComponent<Transform>();
        q = t.rotation;
63 64
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
65 66
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
67
        _VA.SetListenerPosition(Listener, t.transform.position.x, t.transform.position.y, -t.transform.position.z);
68
        _VA.SetListenerOrientationVU(Listener, view_ogl.x, view_ogl.y, view_ogl.z, up_ogl.x, up_ogl.y, up_ogl.z);
69
    }
70

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

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

        shadowReverbTime = actReverbTime;
100

101
        if (i > 0)
102
            actReverbTime /= i;
103 104 105 106

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

107
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
108 109 110

    }

111 112 113 114
    void OnListenerAuralizationModeChanged(string AuraMode)
    {
        _VA.SetListenerAuralizationMode(Listener, AuraMode);
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
115

116 117 118 119 120
    void OnDisable()
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnListenerAuralizationModeChanged;
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
121

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
122 123 124 125 126 127 128
    void OnDestroy()
    {
        if (_VA.IsConnected())
        {
            _VA.DeleteListener(Listener);
        }
    }
129
}