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.73 KB
Newer Older
1 2 3 4 5 6 7 8
using UnityEngine;
using System;
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 17 18 19
    private AudioReverbZone[] reverbZones;
    private AudioReverbZone[] shadowReverbZones;
    private double currentArtificialReverberationTime = 1;
    
20 21 22 23 24 25 26 27
    public int ID
    {
        get
        {
            return Listener;
        }
    }

28 29 30
    // Use this for initialization
    void Start()
    {
31
        _VA = VAUAdapter.VA;
32
        Listener = _VA.CreateListener(listenername, 0, VAUAdapter.DefaultHRIR);
33
        listenername += " " + Listener;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
34
        _VA.SetListenerAuralizationMode(Listener, "all");
35
        Debug.Log(listenername + " created");
36
        _VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIR);
37
        SetListenerPositionOrientation();
38 39 40 41 42
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, 2);
    }

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

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

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    void SetActiveReverbZones()
    {
        double actReverbTime = currentArtificialReverberationTime;
        reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
        if (reverbZones == shadowReverbZones)
            return;

        if (reverbZones == null)
        {
            if (actReverbTime != currentArtificialReverberationTime)
                _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
            return;
        }

        shadowReverbZones = reverbZones;
        int i = 0;
        foreach (AudioReverbZone reverbZone in reverbZones)
        {
            float actdistance = Vector3.Distance(reverbZone.transform.position, gameObject.transform.position);
            if (actdistance < reverbZone.maxDistance)
            {
                if (i <= maxreverbZones)
                {
                    actReverbTime += reverbZone.decayTime;
                    i++;
                }
                if (actdistance > reverbZone.minDistance)
                {

                }
            }
        }

        if ( i > 0)
            actReverbTime /= i;

        if( actReverbTime != currentArtificialReverberationTime)
            _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
    }

107 108 109 110
    void OnListenerAuralizationModeChanged(string AuraMode)
    {
        _VA.SetListenerAuralizationMode(Listener, AuraMode);
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
111

112 113 114 115 116
    void OnDisable()
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnListenerAuralizationModeChanged;
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
117

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
118 119 120 121 122 123 124
    void OnDestroy()
    {
        if (_VA.IsConnected())
        {
            _VA.DeleteListener(Listener);
        }
    }
125
}