Aufgrund eines Versionsupdates wird GitLab am 01.04. zwischen 9:00 und 9:30 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to a version upgrade, GitLab won't be accessible at 01.04. between 9:00 and 9:30 a.m.

VAUAdapter.cs 3.05 KB
Newer Older
1 2 3 4
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
5
using System.Runtime.InteropServices;
6 7 8
using VA;

// Adapter to VA (singleton)
9 10
public class VAUAdapter : MonoBehaviour
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
11 12
    public static string Server = "localhost";
    public static int Port = 12340;
13

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
14 15 16
    public static string AdditionalSearchPath = ""; // Provide further paths where VA should look for audio files, directivities and HRIRs.
    public static bool ResetOnAwake = true;
    public static bool DeactivateUnitySoundOnAwake = true;
17

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
18
    public static string DefaultHRIRPath = "$(DefaultHRIR)";
19

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
20
    private static int _DefaultHRIRID = -1;
21
    private static VANet _VA = null;
22 23

    // No public construction allowed
24
    private VAUAdapter() { }
25 26 27 28 29

    public static VANet VA
    {
        get
        {
30
            if (_VA == null)
31
            {
32
                _VA = new VANet();
33 34 35 36 37 38 39 40
                Init();
            }
            return _VA;
        }
    }

    private static void Init()
    {
41
        if (!_VA.IsConnected())
42
        {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            if (!_VA.Connect(Server, Port))
            {
                Debug.LogError("Could not connect to VA server on " + Server + " using port " + Port);
                return;
            }


            // Add Asset folder as search path for VA (only works if VA is running on same host PC)
            if (!_VA.AddSearchPath(Application.dataPath))
                Debug.LogError("Could not add application assets folder to VA search path, VA server running on remote host?");

            if (AdditionalSearchPath.Length > 0)
            {
                _VA.AddSearchPath(AdditionalSearchPath);
                // ... append more if required.
            }

            if (ResetOnAwake)
                _VA.Reset();

            if (DefaultHRIRPath.Length > 0)
                _DefaultHRIRID = _VA.LoadHRIRDataset(DefaultHRIRPath, "UnityDefaultHRIR");
65 66 67
        }
    }

68
    public static int DefaultHRIRID
69 70 71
    {
        get
        {
72
            return _DefaultHRIRID;
73 74
        }
    }
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
75

76 77 78
    // Use this for initialization
    void Awake()
    {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
79
        if (_VA == null)
80
            _VA = new VANet();
81

82
        Init();
83

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
84 85
        if (DeactivateUnitySoundOnAwake)
            DeactivateUnitySound();
86 87
    }

88 89
    void OnEnable()
    {
90 91
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnGlobalAuralizationModeChanged;
92 93 94 95 96 97 98
    }

    void OnGlobalAuralizationModeChanged(string AuraMode)
    {
        _VA.SetGlobalAuralizationMode(AuraMode);
    }

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
99
    void OnDestroy()
100 101
    {
        _VA.Disconnect();
102 103 104 105 106 107 108 109 110 111 112 113 114 115
    }

    void DeactivateUnitySound()
    {
        AudioSource[] audioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
        foreach (AudioSource audioSource in audioSources)
        {
            audioSource.enabled = false;
        }
        AudioListener[] audioListeners = FindObjectsOfType(typeof(AudioListener)) as AudioListener[];
        foreach (AudioListener audioListener in audioListeners)
        {
            audioListener.enabled = false;
        }
116 117
    }
}