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

6 7
public class VAUSoundSource : MonoBehaviour
{
8 9
    [Tooltip("Activate this if you want to validate & change volume and mute/unmute state on every update loop.")]
    public bool ContinuousGainObserver = false; 
10

11 12 13 14
    [Tooltip("Activate this if you dont want to pause the playback while the SoundSource is disabled but rather stop it.")]
    public bool stopOnDisable = false;

    [Tooltip("Insert a custom VAUDirectivity-Script.")] 
15
    public VAUDirectivity Directivity;
16 17

    [Tooltip("Insert a custom VAUSignalSource-Script.")]
18
    public VAUSignalSource SignalSource;
19
    
20

21
    private VANet _VA = null;
22
    private int _ID;
23 24
    private Quaternion q;
    private Transform t;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
25
    private string _AudiofileSignalSource = null;
26
    private bool enable = false;
27

28 29 30 31
    public int ID
    {
        get
        {
32
            return _ID;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
33 34
        }
    }
35

36 37
    void Start()
    {
38
        _VA = VAUAdapter.VA;
39

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
40 41 42
        if (!_VA.IsConnected())
            return;

43
        // Add sound source
44
        _ID = _VA.CreateSoundSource(this.name);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        _VA.SetSoundSourceAuralizationMode(_ID, "all");

        // Connect to directivity, if linked or attached
        if (Directivity)
            _VA.SetSoundSourceDirectivity(_ID, Directivity.ID);
        else if (GetComponent<VAUDirectivity>())
            _VA.SetSoundSourceDirectivity(_ID, GetComponent<VAUDirectivity>().ID);

        // Connect to signal source, if linked or attached
        if (SignalSource)
            _VA.SetSoundSourceSignalSource(_ID, SignalSource.ID);
        else if (GetComponent<AudioSource>())
        {
            // Set gain control once
            _VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
            _VA.SetSoundSourceVolume(_ID, GetComponent<AudioSource>().volume);

            // Create and connect audio signal source
            if (GetComponent<AudioSource>().clip)
64
            {
65 66 67 68 69
                string FilePath = GetComponent<AudioSource>().clip.name + ".wav";
                string Name = this.name + "_signal";
                bool IsLooping = GetComponent<AudioSource>().loop;
                bool PlayOnAwake = GetComponent<AudioSource>().playOnAwake;

70
				_AudiofileSignalSource = _VA.CreateSignalSourceBufferFromFile(FilePath, Name);
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
71
                Debug.Assert(_AudiofileSignalSource.Length > 0, "Could not create integrated audio file signal source '" + Name + "' file from path " + FilePath);
72
				_VA.SetSignalSourceBufferLooping(_AudiofileSignalSource, IsLooping);
73
                if (PlayOnAwake)
74
					_VA.SetSignalSourceBufferPlaybackAction(_AudiofileSignalSource, "play");
75 76

                _VA.SetSoundSourceSignalSource(_ID, _AudiofileSignalSource);
77
            }
78
            enable = true;
79
        }
80 81 82

        // Activate auralization mode, if available
        if (GetComponent<VAUAuralizationMode>())
83
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
84 85

        // Initially, set the pose (otherwise rendering module can not spatialize)
86
        SetSoundSourcePositionOrientation();
87 88 89 90
    }

    // Update is called once per frame
    void Update()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
91
    {
92
        SetSoundSourcePositionOrientation();
93 94 95 96 97 98

        if (ContinuousGainObserver)
        {
            _VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
            _VA.SetSoundSourceVolume(_ID, GetComponent<AudioSource>().volume);
        }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
99 100
    }

101
    void SetSoundSourcePositionOrientation()
102
    {
103
        // Convert left-handed Unity transform matrix to VA positions and orientations (right-handed, OpenGL)
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
104
        _VA = VAUAdapter.VA;
105 106
        t = GetComponent<Transform>();
        q = t.rotation;
107 108
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
109 110
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
111 112
		_VA.SetSoundSourcePosition (_ID, new VAVec3 (t.transform.position.x, t.transform.position.y, -t.transform.position.z));
		_VA.SetSoundSourceOrientationVU(_ID, new VAVec3( view_ogl.x, view_ogl.y, view_ogl.z ), new VAVec3( up_ogl.x, up_ogl.y, up_ogl.z ));
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
113 114
    }

115 116
    void OnSoundSourceAuralizationModeChanged(string AuraMode)
    {
117
        _VA.SetSoundSourceAuralizationMode(_ID, AuraMode);
118
    }
119

120
    public void OnEnable()
121
    {
122
        if (enable)
123
			_VA.SetSignalSourceBufferPlaybackAction(_AudiofileSignalSource, "play");
124
    }
125

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
126
    public void OnDisable()
127
    {
128
        if (GetComponent<VAUAuralizationMode>())
129
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged;
130
        if (stopOnDisable)
131
            _VA.SetSignalSourceBufferPlaybackAction(_AudiofileSignalSource, "stop");
132
        else
133
			_VA.SetSignalSourceBufferPlaybackAction(_AudiofileSignalSource, "pause");
134
    }
135

136
    private void OnDestroy()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
137
    {
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
138
        _VA = VAUAdapter.VA;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
139 140
        if (_VA.IsConnected())
        {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
141
            _VA.SetSoundSourceSignalSource(_ID, "");
142 143 144
            _VA.DeleteSoundSource(_ID);

            // Temptative signal source deletion
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
145
            if (_AudiofileSignalSource != null)
146
                _VA.DeleteSignalSource(_AudiofileSignalSource);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
147 148
        }
    }
149
}