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

	[Tooltip("Set an explicit renderer for this source [todo]")]
21 22 23 24 25 26 27
	public VAUAudioRenderer Renderer = null;

	[Tooltip("Descriptive name")]
	public string Name = "SoundReceiver";

	[Tooltip("Sound power in Watts (default is 31mW)")]
	public double SoundPower = 0.0031;
28

29
    private VANet _VA = null;
30
    private int _ID;
31 32
    private Quaternion q;
    private Transform t;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
33
    private string _AudiofileSignalSource = null;
34

35 36 37 38
    public int ID
    {
        get
        {
39
            return _ID;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
40 41
        }
    }
42

43 44 45
	void OnEnable()
	{
		_VA = VAUAdapter.VA;
46

47 48
        if (!_VA.IsConnected())
		{
49
			Debug.LogError( "Could not create sound source, not connected to VA" );
50 51
            return;
		}
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
52

53 54 55
		// Add sound source
		_ID = _VA.CreateSoundSource(this.name);
		_VA.SetSoundSourceAuralizationMode(_ID, "all");
56
		_VA.SetSoundSourceSoundPower (_ID, SoundPower);
57
	}
58

59 60
    void Start()
    {
61 62 63 64 65 66 67
        // 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
68 69 70 71 72
		if (SignalSource)
		{
			_VA.SetSoundSourceSignalSource (_ID, SignalSource.ID);
		}
		else if (GetComponent<AudioSource>())
73 74 75
        {
            // Set gain control once
            _VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
76
			_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
77 78 79

            // Create and connect audio signal source
            if (GetComponent<AudioSource>().clip)
80
            {
81 82 83 84 85
                string FilePath = GetComponent<AudioSource>().clip.name + ".wav";
                string Name = this.name + "_signal";
                bool IsLooping = GetComponent<AudioSource>().loop;
                bool PlayOnAwake = GetComponent<AudioSource>().playOnAwake;

86
				_AudiofileSignalSource = _VA.CreateSignalSourceBufferFromFile(FilePath, Name);
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
87
                Debug.Assert(_AudiofileSignalSource.Length > 0, "Could not create integrated audio file signal source '" + Name + "' file from path " + FilePath);
88
				_VA.SetSignalSourceBufferLooping(_AudiofileSignalSource, IsLooping);
89
                if (PlayOnAwake)
90
					_VA.SetSignalSourceBufferPlaybackAction(_AudiofileSignalSource, "play");
91 92

                _VA.SetSoundSourceSignalSource(_ID, _AudiofileSignalSource);
93
            }
94
        }
95 96 97

        // Activate auralization mode, if available
        if (GetComponent<VAUAuralizationMode>())
98
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
99 100

        // Initially, set the pose (otherwise rendering module can not spatialize)
101
        SetSoundSourcePositionOrientation();
102 103 104 105
    }

    // Update is called once per frame
    void Update()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
106
    {
107
        SetSoundSourcePositionOrientation();
108 109 110 111

        if (ContinuousGainObserver)
        {
            _VA.SetSoundSourceMuted(_ID, GetComponent<AudioSource>().mute);
112
			_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
113
        }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
114 115
    }

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

130 131
    void OnSoundSourceAuralizationModeChanged(string AuraMode)
    {
132
        _VA.SetSoundSourceAuralizationMode(_ID, AuraMode);
133
    }
134

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
135
    public void OnDisable()
136
    {
137
        if (GetComponent<VAUAuralizationMode>())
138
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged;
139 140 141 142 143 144 145

		if (_AudiofileSignalSource != null) {
			if (stopOnDisable)
				_VA.SetSignalSourceBufferPlaybackAction (_AudiofileSignalSource, "stop");
			else
				_VA.SetSignalSourceBufferPlaybackAction (_AudiofileSignalSource, "pause");
		}
146
    }
147

148
    private void OnDestroy()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
149
    {
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
150
        _VA = VAUAdapter.VA;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
151 152
        if (_VA.IsConnected())
        {
153 154
			_VA.SetSoundSourceSignalSource(_ID, "");
            _VA.DeleteSoundSource(ID);
155 156

            // Temptative signal source deletion
157 158
			if (_AudiofileSignalSource != null)
				_VA.DeleteSignalSource (_AudiofileSignalSource);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
159 160
        }
    }
161
}