VAUSoundSource.cs 4.44 KB
Newer Older
1
using UnityEngine;
2
using System.IO;
3 4 5 6
using System.Collections.Generic;
using VA;

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

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

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

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

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

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

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

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

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

98 99 100 101 102 103 104 105 106 107 108 109
    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
110
    {
111 112 113 114 115
        if (ismuted != GetComponent<AudioSource>().mute)
        {
            _VA.SetSoundSourceMuted(SoundSource, GetComponent<AudioSource>().mute);
            ismuted = GetComponent<AudioSource>().mute;
        }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
116 117
    }

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

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

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