VAUSoundSource.cs 5.28 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 = null;
16 17

    [Tooltip("Insert a custom VAUSignalSource-Script.")]
18
    public VAUSignalSource SignalSource = null;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
19 20 21

	[Tooltip("Set an explicit renderer for this source [todo]")]
	public VAUAudioRenderer Renderer = null;    
22

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

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

37 38 39
	void OnEnable()
	{
		_VA = VAUAdapter.VA;
40

41 42
        if (!_VA.IsConnected())
		{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
43
			Debug.LogError( "Could not create sound source, not connected to VA" );
44 45
            return;
		}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
46

47 48 49 50
		// Add sound source
		_ID = _VA.CreateSoundSource(this.name);
		_VA.SetSoundSourceAuralizationMode(_ID, "all");
	}
51

52 53
    void Start()
    {
54 55 56 57 58 59 60
        // 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
61 62 63 64 65
		if (SignalSource)
		{
			_VA.SetSoundSourceSignalSource (_ID, SignalSource.ID);
		}
		else if (GetComponent<AudioSource>())
66 67 68
        {
            // Set gain control once
            _VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
69
			_VA.SetSoundSourceSoundPower( _ID, GetComponent< AudioSource >().volume * 31.69e-3 ); // Volume of 1.0 results in default sound power: 31.67 mW -> 94 dB SPL @ 1m
70 71 72

            // Create and connect audio signal source
            if (GetComponent<AudioSource>().clip)
73
            {
74 75 76 77 78
                string FilePath = GetComponent<AudioSource>().clip.name + ".wav";
                string Name = this.name + "_signal";
                bool IsLooping = GetComponent<AudioSource>().loop;
                bool PlayOnAwake = GetComponent<AudioSource>().playOnAwake;

79
				_AudiofileSignalSource = _VA.CreateSignalSourceBufferFromFile(FilePath, Name);
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
80
                Debug.Assert(_AudiofileSignalSource.Length > 0, "Could not create integrated audio file signal source '" + Name + "' file from path " + FilePath);
81
				_VA.SetSignalSourceBufferLooping(_AudiofileSignalSource, IsLooping);
82
                if (PlayOnAwake)
83
					_VA.SetSignalSourceBufferPlaybackAction(_AudiofileSignalSource, "play");
84 85

                _VA.SetSoundSourceSignalSource(_ID, _AudiofileSignalSource);
86
            }
87
        }
88 89 90

        // Activate auralization mode, if available
        if (GetComponent<VAUAuralizationMode>())
91
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
92 93

        // Initially, set the pose (otherwise rendering module can not spatialize)
94
        SetSoundSourcePositionOrientation();
95 96 97 98
    }

    // Update is called once per frame
    void Update()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
99
    {
100
        SetSoundSourcePositionOrientation();
101 102 103 104

        if (ContinuousGainObserver)
        {
            _VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
105
			_VA.SetSoundSourceSoundPower(_ID, GetComponent<AudioSource>().volume * 31.69e-3 ); // Volume of 1.0 results in default sound power: 31.67 mW -> 94 dB SPL @ 1m
106
        }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
107 108
    }

109
    void SetSoundSourcePositionOrientation()
110
    {
111
        // Convert left-handed Unity transform matrix to VA positions and orientations (right-handed, OpenGL)
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
112
        _VA = VAUAdapter.VA;
113 114
        t = GetComponent<Transform>();
        q = t.rotation;
115 116
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
117 118
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
119 120
		_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
121 122
    }

123 124
    void OnSoundSourceAuralizationModeChanged(string AuraMode)
    {
125
        _VA.SetSoundSourceAuralizationMode(_ID, AuraMode);
126
    }
127

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

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

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