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

6 7 8 9 10 11 12
public class VAUSoundSource : MonoBehaviour
{

    public bool ContinuousGainObserver = false; // Activate this if you want to validate & change volume and mute/unmute state on every update loop
    public VAUDirectivity Directivity;
    public VAUSignalSource SignalSource;

13
    private VANet _VA = null;
14
    private int _ID;
15 16
    private Quaternion q;
    private Transform t;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
17
    private string _AudiofileSignalSource = null;
18

19 20 21 22
    public int ID
    {
        get
        {
23
            return _ID;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
24 25
        }
    }
26

27 28
    void Start()
    {
29
        _VA = VAUAdapter.VA;
30

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
31 32 33
        if (!_VA.IsConnected())
            return;

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        // Add sound source
        _ID = _VA.CreateSoundSource(this.name, 0, 1.0);
        _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)
55
            {
56 57 58 59 60 61
                string FilePath = GetComponent<AudioSource>().clip.name + ".wav";
                string Name = this.name + "_signal";
                bool IsLooping = GetComponent<AudioSource>().loop;
                bool PlayOnAwake = GetComponent<AudioSource>().playOnAwake;

                _AudiofileSignalSource = _VA.CreateAudiofileSignalSource(FilePath, Name);
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
62
                Debug.Assert(_AudiofileSignalSource.Length > 0, "Could not create integrated audio file signal source '" + Name + "' file from path " + FilePath);
63 64 65 66 67
                _VA.SetAudiofileSignalSourceIsLooping(_AudiofileSignalSource, IsLooping);
                if (PlayOnAwake)
                    _VA.SetAudiofileSignalSourcePlaybackAction(_AudiofileSignalSource, "play");

                _VA.SetSoundSourceSignalSource(_ID, _AudiofileSignalSource);
68
            }
69
        }
70 71 72

        // Activate auralization mode, if available
        if (GetComponent<VAUAuralizationMode>())
73
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundSourceAuralizationModeChanged;
74 75

        // Initially, set the pose (otherwise rendering module can not spatialize)
76
        SetSoundSourcePositionOrientation();
77 78 79 80
    }

    // Update is called once per frame
    void Update()
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
81
    {
82
        SetSoundSourcePositionOrientation();
83 84 85 86 87 88

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

91
    void SetSoundSourcePositionOrientation()
92
    {
93
        // Convert left-handed Unity transform matrix to VA positions and orientations (right-handed, OpenGL)
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
94
        _VA = VAUAdapter.VA;
95 96
        t = GetComponent<Transform>();
        q = t.rotation;
97 98
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
99 100
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
101
        _VA.SetSoundSourcePositionOrientationVU(_ID, 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);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
102 103
    }

104 105
    void OnSoundSourceAuralizationModeChanged(string AuraMode)
    {
106
        _VA.SetSoundSourceAuralizationMode(_ID, AuraMode);
107
    }
108

109 110 111
    public void OnActivation()
    {
    }
112

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
113
    public void OnDisable()
114
    {
115
        if (GetComponent<VAUAuralizationMode>())
116 117
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundSourceAuralizationModeChanged;
    }
118

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
119 120
    void OnDestroy()
    {
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
121
        _VA = VAUAdapter.VA;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
122 123
        if (_VA.IsConnected())
        {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
124
            _VA.SetSoundSourceSignalSource(_ID, "");
125 126 127
            _VA.DeleteSoundSource(_ID);

            // Temptative signal source deletion
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
128
            if (_AudiofileSignalSource != null)
129
                _VA.DeleteSignalSource(_AudiofileSignalSource);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
130 131
        }
    }
132
}