VAUSoundSource.cs 4.46 KB
Newer Older
1 2 3 4 5 6 7
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using VA;

public class VAUSoundSource : MonoBehaviour {
8
    
9 10 11
    public string directivitypath = "";
    private int directivity;
    private int Gain = 1;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
12
    private string SignalSource = "";
13 14
    private VANet _VA = null;
    private int SoundSource;
15 16
    private Quaternion q;
    private Transform t;
17 18
    private bool islooping;
    private bool ismuted;
19
    private bool isplaying;
20

21 22 23 24 25 26 27
    public int ID
    {
        get
        {
            return SoundSource;
        }
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
28 29 30 31 32 33 34
    public string SignalSourceID
    {
        get
        {
            return SignalSource;
        }
    }
35

36 37 38
    // Use this for initialization
    void Start()
    {
39
        _VA = VAUAdapter.VA;
40 41
        SoundSource = _VA.CreateSoundSource(this.name, 0, Gain);
        _VA.SetSoundSourceAuralizationMode(SoundSource, "all");
42

43
        islooping = GetComponent<AudioSource>().loop;
44 45 46
        ismuted = GetComponent<AudioSource>().mute;
        isplaying = GetComponent<AudioSource>().isPlaying;

47 48 49 50
        if (directivitypath == "")
            directivity = VAUAdapter.DefaultDirectivity;
        else
            directivity = _VA.LoadDirectivity(directivitypath, "SoundSource " + SoundSource + " Directivity");
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
51
        _VA.SetSoundSourceDirectivity(SoundSource, directivity);
52

53
        if (GetComponent<AudioSource>() != null)
54 55 56 57 58
        { 
            if (GetComponent<AudioSource>().clip != null)
            {
                SignalSource = _VA.CreateAudiofileSignalSource(AssetDatabase.GetAssetPath(GetComponent<AudioSource>().clip), this.name + "_signal");
                _VA.SetSoundSourceSignalSource(SoundSource, SignalSource);
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
59
                Debug.Log("[VAU]" + SignalSource + " connected to SoundSource " + SoundSource);
60 61 62 63
                _VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PLAYING");
                _VA.SetAudiofileSignalSourceIsLooping(SignalSource, islooping);
            }
            else
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
64
                Debug.Log("[VAU]No clip on AudioSource of GameObject: " + gameObject.name);
65
        }
66
        else
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
67
            Debug.Log("[VAU]No AudioSource on GameObject: " + gameObject.name);
68 69 70 71
        if (GetComponent<VAUAuralizationMode>() != null)
        {
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
        }
72
        SetSoundSourcePositionOrientation();
73 74 75 76
    }

    // Update is called once per frame
    void Update()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
77 78
    {

79
        SetSoundSourcePositionOrientation();
80 81
        SetSoundSourceMuted();
        SetSignalSourcePlaying();
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
82 83 84 85
        //_VA.SetSoundSourceVolume(SoundSource, Volume); <- flaot ändern
        
    }

86
    void SetSoundSourcePositionOrientation()
87 88
    {
        //position and oritentation -> VA
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
89
        _VA = VAUAdapter.VA;
90 91
        t = GetComponent<Transform>();
        q = t.rotation;
92 93
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
94 95
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
96
        _VA.SetSoundSourcePositionOrientationVU(SoundSource, t.transform.position.x, t.transform.position.y, -t.transform.position.z, view_ogl.x, view_ogl.y, view_ogl.z, up_ogl.x, up_ogl.y, up_ogl.z);
97
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
98

99 100 101 102 103 104 105 106 107 108 109 110
    void SetSignalSourcePlaying()
    {
        if(GetComponent<AudioSource>().isPlaying != isplaying)
        {
            isplaying = GetComponent<AudioSource>().isPlaying;
            if (isplaying)
                _VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PLAYING");
            else
                _VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PAUSED");
        }
    }
    void SetSoundSourceMuted()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
111
    {
112 113 114 115 116
        if (ismuted != GetComponent<AudioSource>().mute)
        {
            _VA.SetSoundSourceMuted(SoundSource, GetComponent<AudioSource>().mute);
            ismuted = GetComponent<AudioSource>().mute;
        }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
117 118
    }

119 120 121 122
    void OnSoundSourceAuralizationModeChanged(string AuraMode)
    {
        _VA.SetSoundSourceAuralizationMode(SoundSource, AuraMode);
    }
123

124 125 126 127
    public void OnActivation()
    {
        ismuted = false;
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
128
    public void OnDisable()
129 130 131
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged;
132
        ismuted = true;
133
    }
134

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
135 136
    void OnDestroy()
    {
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
137
        _VA = VAUAdapter.VA;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
138 139 140 141 142
        if (_VA.IsConnected())
        {
            _VA.DeleteSoundSource(SoundSource);
        }
    }
143
}