VAUSoundSource.cs 3.19 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

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
        islooping = GetComponent<AudioSource>().loop;
42
43
44
45
        if (directivitypath == "")
            directivity = VAUAdapter.DefaultDirectivity;
        else
            directivity = _VA.LoadDirectivity(directivitypath, "SoundSource " + SoundSource + " Directivity");
46
47
48
49
        if (GetComponent<AudioSource>() != null)
        {
            SignalSource = _VA.CreateAudiofileSignalSource( AssetDatabase.GetAssetPath(GetComponent<AudioSource>().clip), this.name + "_signal");
            _VA.SetSoundSourceSignalSource(SoundSource, SignalSource);
50
            Debug.Log(SignalSource + " connected to SoundSource " + SoundSource);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
51
            _VA.SetAudiofileSignalSourcePlaybackAction(SignalSource, "PLAYING");
52
            _VA.SetAudiofileSignalSourceIsLooping(SignalSource, islooping);
53
        }
54
55
56
57
        if (GetComponent<VAUAuralizationMode>() != null)
        {
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
        }
58
59
60
61
    }

    // Update is called once per frame
    void Update()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
62
63
64
65
66
67
68
69
70
    {

        SetSoundSourcePosition();
        SetSoundSourcePlaybackState();
        //_VA.SetSoundSourceVolume(SoundSource, Volume); <- flaot ändern
        
    }

    void SetSoundSourcePosition()
71
72
    {
        //position and oritentation -> VA
73
74
        t = GetComponent<Transform>();
        q = t.rotation;
75
76
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
77
78
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
79
        _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);
80
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
81
82
83
84
85
86

    void SetSoundSourcePlaybackState()
    {
        _VA.SetSoundSourceMuted(SoundSource, GetComponent<AudioSource>().mute);
    }

87
88
89
90
    void OnSoundSourceAuralizationModeChanged(string AuraMode)
    {
        _VA.SetSoundSourceAuralizationMode(SoundSource, AuraMode);
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
91
    public void OnDisable()
92
93
94
95
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged;
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
96
97
98
99
100
101
102
    void OnDestroy()
    {
        if (_VA.IsConnected())
        {
            _VA.DeleteSoundSource(SoundSource);
        }
    }
103
}