VAUAdapter.cs 3.33 KB
Newer Older
1 2 3 4 5
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
6
using System.Runtime.InteropServices;
7 8 9 10 11 12
using VA;

// Adapter to VA (singleton)
public class VAUAdapter : MonoBehaviour {

    
13
    private static string HostIP = "localhost";
14
    public static string GlobalAuralizationMode = "all";
15
    private static int Port = 12340;
Maurice Tayeb Andreas's avatar
assert  
Maurice Tayeb Andreas committed
16
    private static string DefaultHRIRPath = "$(DefaultHRIR)";
17
    private static string DefaultDirectivityPath = "$(Trumpet)";
18
    private static int defaulthrir;
19
    private static int defaultdirectivity;
20
    private static VANet _VA = null;
21 22 23 24 25 26 27 28

    // No public construction allowed
    private VAUAdapter() {}

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

            return _VA;
        }
    }

    private static void Init()
    {
41
        if (!_VA.IsConnected())
42 43 44 45
        {
            _VA.Connect(HostIP, Port);
            _VA.Reset();
            defaulthrir = _VA.LoadHRIRDataset(DefaultHRIRPath, "DefaultHRIR");
46 47
            defaultdirectivity = _VA.LoadDirectivity(DefaultDirectivityPath, "DefaultDirectivity");
            _VA.SetGlobalAuralizationMode(GlobalAuralizationMode);
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
48
            Debug.Log("[VAU]Connect to Server: " + HostIP);
49
        }
50
        else
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
51
            Debug.Log("[VAU]Already connected.");
52 53 54 55 56 57 58 59 60 61
    }

    public static int DefaultHRIR
    {
        get
        {
            return defaulthrir;
        }
    }

62 63 64 65 66 67 68 69 70
    public static int DefaultDirectivity
    {
        get
        {
            return defaultdirectivity;
        }
    }


71 72 73
    // Use this for initialization
    void Awake()
    {
74 75
        if(_VA==null)
            _VA = new VANet();
76 77
        Init();
        DeactivateUnitySound();
78 79
    }

80 81
    void OnEnable()
    {
82 83
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnGlobalAuralizationModeChanged;
84 85 86 87 88 89 90
    }

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

91
    void OnDisable()
92
    {
93 94 95
        VAUSoundSource[] SoundSources = FindObjectsOfType<VAUSoundSource>(); 
        foreach (VAUSoundSource SoundSource in SoundSources)
        {
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
96
            Debug.Log("[VAU]SoundSouce " + SoundSource.ID + " deleted.");
97 98 99 100 101 102
            _VA.DeleteSoundSource(SoundSource.ID);
        }
        
        VAUListener[] Listeners = FindObjectsOfType<VAUListener>();
        foreach (VAUListener Listener in Listeners)
        {
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
103
            Debug.Log("[VAU]Listener " + Listener.ID + " deleted.");
104 105
            _VA.DeleteListener(Listener.ID);
        }
106 107
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnGlobalAuralizationModeChanged;
108
        _VA.Disconnect();
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        
    }

    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;
        }
124 125
    }
}