VAUListener.cs 3.73 KB
Newer Older
1 2 3 4 5 6
using UnityEngine;
using System.Linq;
using System.Collections;
using VA;


7 8
public class VAUListener : VAUSoundReceiver {
    
9 10
    [Tooltip("Number of reverbzones used for determining the current reverb.")]
    public int NumMaxReverbZones = 2;
11

12 13 14 15 16 17 18 19 20 21 22 23
    public string VAAudioRenderer = "MyBinauralArtificialReverb";
    
    [Tooltip("Anthropometric head width, for HRIR individualization or generic binaural cues")]
    public double HeadWidth = 0.12;
    [Tooltip("Anthropometric head height, for HRIR individualization or generic binaural cues")]
    public double HeadHeight = 0.10; 
    [Tooltip("Anthropometric head depth, for HRIR individualization or generic binaural cues")]
    public double HeadDepth = 0.15;

    private double ShadowHeadWidth;
    private double ShadowHeadHeight;
    private double ShadowHeadDepth;
24
    
25 26 27 28 29
    private AudioReverbZone[] reverbZones;
    private float shadowReverbTime;

    public delegate void ReverbTimeChangedDelegate(double reverbTime);
    public event ReverbTimeChangedDelegate ReverbTimeChanged;
30
        
31 32
    void Start()
    {
33 34 35 36
        _ID = VAUAdapter.VA.CreateSoundReceiver(Name);
        VAUAdapter.VA.SetSoundReceiverAuralizationMode(_ID, "all");
		if (Directivity)
			VAUAdapter.VA.SetSoundReceiverDirectivity(_ID, Directivity.ID);
37

38
        VAUAdapter.VA.SetSoundReceiverAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
39 40 41 42 43 44
        ShadowHeadWidth = HeadWidth;
        ShadowHeadHeight = HeadHeight;
        ShadowHeadDepth = HeadDepth;

        SetSoundReceiverPositionOrientation();

45
        VAUAdapter.VA.SetArtificialReverberationTime(VAAudioRenderer, 0.3f);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
    }

    void OnEnable()
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundReceiverAuralizationModeChanged;
    }

    void Update ()
    {
        SetSoundReceiverPositionOrientation();
        SetActiveReverbZones();

        if( HeadWidth != ShadowHeadWidth || HeadHeight != ShadowHeadHeight || HeadDepth != ShadowHeadDepth )
        {
62
            VAUAdapter.VA.SetSoundReceiverAnthropometricData(_ID, HeadWidth, HeadHeight, HeadDepth);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            ShadowHeadWidth = HeadWidth;
            ShadowHeadHeight = HeadHeight;
            ShadowHeadDepth = HeadDepth;
        }
    }

    void SetActiveReverbZones()
    {
        if (reverbZones == null)
            return;

        float actReverbTime = 0f;
        int i = 0;
        foreach (AudioReverbZone reverbZone in reverbZones)
        {
            if (i >= NumMaxReverbZones)
                break;
            float actDistance = Vector3.Distance(reverbZone.transform.position, gameObject.transform.position);
            if ((actDistance < reverbZone.maxDistance))
            {
                if (actDistance > reverbZone.minDistance)
                {
                    actReverbTime += reverbZone.decayTime * (reverbZone.maxDistance - actDistance) / (reverbZone.maxDistance - reverbZone.minDistance);
                }
                else
                {
                    actReverbTime += reverbZone.decayTime;
                }
                i++;
            }
        }
        if (shadowReverbTime == actReverbTime)
            return;


        if (actReverbTime < 0.3f)
99
            VAUAdapter.VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", true);
100
        else
101
            VAUAdapter.VA.SetRenderingModuleMuted("MyBinauralArtificialReverb", false);
102 103 104 105 106 107 108 109 110

        shadowReverbTime = actReverbTime;

        if (i > 0)
            actReverbTime /= i;

        if (ReverbTimeChanged != null)
            ReverbTimeChanged(actReverbTime);

111
        VAUAdapter.VA.SetArtificialReverberationTime(VAAudioRenderer, actReverbTime);
112 113 114

    }
}