示例#1
0
    /// <summary>
    /// Start this instance.
    /// </summary>
    void Start()
    {
#if VRC_CLIENT
        var source = GetComponent <AudioSource>();

        if (source != null && source.outputAudioMixerGroup != VRCAudioManager.GetAvatarGroup())
        {
            VRCAudioManager.ApplyGameAudioMixerSettings(source);
        }
#endif
    }
        public static IEnumerator EnforceAudioSourceLimitsEnumerator(GameObject currentAvatar, System.Action <AudioSource> onFound)
        {
            if (currentAvatar == null)
            {
                yield break;
            }

            Queue <GameObject> children = new Queue <GameObject>();

            if (currentAvatar != null)
            {
                children.Enqueue(currentAvatar.gameObject);
            }
            while (children.Count > 0)
            {
                GameObject child = children.Dequeue();
                if (child == null)
                {
                    continue;
                }

                int childCount = child.transform.childCount;
                for (int idx = 0; idx < child.transform.childCount; ++idx)
                {
                    children.Enqueue(child.transform.GetChild(idx).gameObject);
                }

#if VRC_CLIENT
                if (child.GetComponent <USpeaker>() != null)
                {
                    continue;
                }
#endif

                AudioSource[] sources = child.transform.GetComponents <AudioSource>();
                if (sources != null && sources.Length > 0)
                {
                    AudioSource au = sources[0];
                    if (au == null)
                    {
                        continue;
                    }

#if VRC_CLIENT
                    au.outputAudioMixerGroup = VRCAudioManager.GetAvatarGroup();
#endif

                    if (au.volume > 0.9f)
                    {
                        au.volume = 0.9f;
                    }

#if VRC_CLIENT
                    // someone mucked with the sdk forced settings, shame on them!
                    if (au.spatialize == false)
                    {
                        au.volume = 0;
                    }
#else
                    au.spatialize = true;
#endif

                    au.priority              = Mathf.Clamp(au.priority, 200, 255);
                    au.bypassEffects         = false;
                    au.bypassListenerEffects = false;
                    au.spatialBlend          = 1f;
                    au.spread = 0;

                    au.minDistance = Mathf.Clamp(au.minDistance, 0, 2);
                    au.maxDistance = Mathf.Clamp(au.maxDistance, 0, 30);

                    float range = au.maxDistance - au.minDistance;
                    float min   = au.minDistance;
                    float max   = au.maxDistance;
                    float mult  = 50.0f / range;

                    // setup a custom rolloff curve
                    Keyframe[] keys = new Keyframe[7];
                    keys[0] = new Keyframe(0, 1);
                    keys[1] = new Keyframe(min, 1, 0, -0.4f * mult);
                    keys[2] = new Keyframe(min + 0.022f * range, 0.668f, -0.2f * mult, -0.2f * mult);
                    keys[3] = new Keyframe(min + 0.078f * range, 0.359f, -0.05f * mult, -0.05f * mult);
                    keys[4] = new Keyframe(min + 0.292f * range, 0.102f, -0.01f * mult, -0.01f * mult);
                    keys[5] = new Keyframe(min + 0.625f * range, 0.025f, -0.002f * mult, -0.002f * mult);
                    keys[6] = new Keyframe(max, 0);
                    AnimationCurve curve = new AnimationCurve(keys);

                    au.rolloffMode = AudioRolloffMode.Custom;
                    au.SetCustomCurve(AudioSourceCurveType.CustomRolloff, curve);

                    // if we have an onsp component, also configure that
                    ONSPAudioSource oa = au.GetComponent <ONSPAudioSource>();
                    if (oa)
                    {
                        if (oa.Gain > 10f)
                        {
                            oa.Gain = 10f;
                        }
#if VRC_CLIENT
                        // someone mucked with the sdk forced settings, shame on them!
                        if (oa.enabled == false || oa.EnableSpatialization == false)
                        {
                            oa.Gain   = 0f;
                            au.volume = 0f;
                        }
#else
                        oa.enabled = true;
                        oa.EnableSpatialization = true;
#endif
                        oa.UseInvSqr = true; // This is the ENABLED value for OCULUS ATTENUATION
                        oa.EnableRfl = false;
                        if (oa.Near > 2f)
                        {
                            oa.Near = 2f;
                        }
                        if (oa.Far > 30f)
                        {
                            oa.Far = 30f;
                        }
                        oa.VolumetricRadius = 0f;
                    }

                    onFound(au);

                    if (sources.Length > 1)
                    {
                        Debug.LogError("Disabling extra AudioSources on GameObject(" + child.name + "). Only one is allowed per GameObject.");
                        for (int i = 1; i < sources.Length; i++)
                        {
                            if (sources[i] == null)
                            {
                                continue;
                            }

#if VRC_CLIENT
                            sources[i].enabled = false;
                            sources[i].clip    = null;
#else
                            Validation.RemoveComponent(sources[i]);
#endif
                        }
                    }
                }

                yield return(null);
            }
        }
        public static IEnumerator EnforceAudioSourceLimitsEnumerator(GameObject currentAvatar, System.Action <AudioSource> onFound)
        {
            if (currentAvatar == null)
            {
                yield break;
            }

            Queue <GameObject> children = new Queue <GameObject>();

            if (currentAvatar != null)
            {
                children.Enqueue(currentAvatar.gameObject);
            }

            while (children.Count > 0)
            {
                if (Time.frameCount > _enforceAudioSourcesFrameNumber)
                {
                    _enforceAudioSourcesFrameNumber        = Time.frameCount;
                    _enforceAudioSourcesProcessedThisFrame = 0;
                }

                if (_enforceAudioSourcesProcessedThisFrame > ENFORCE_AUDIO_SOURCE_GAMEOBJECTS_PER_FRAME)
                {
                    yield return(null);
                }

                Profiler.BeginSample("EnforceAudioSourceLimitsEnumerator");
                _enforceAudioSourcesProcessedThisFrame++;

                GameObject child = children.Dequeue();
                if (child == null)
                {
                    Profiler.EndSample();
                    continue;
                }

                int childCount = child.transform.childCount;
                for (int idx = 0; idx < childCount; ++idx)
                {
                    children.Enqueue(child.transform.GetChild(idx).gameObject);
                }

                #if VRC_CLIENT
                if (child.GetComponent <USpeaker>() != null)
                {
                    Profiler.EndSample();
                    continue;
                }
                #endif

                AudioSource[] sources = child.transform.GetComponents <AudioSource>();
                if (sources != null && sources.Length > 0)
                {
                    AudioSource audioSource = sources[0];
                    if (audioSource == null)
                    {
                        Profiler.EndSample();
                        continue;
                    }

                    VRC_SpatialAudioSource vrcSpatialAudioSource = audioSource.gameObject.GetComponent <VRC_SpatialAudioSource>();

                    #if VRC_CLIENT
                    audioSource.outputAudioMixerGroup = VRCAudioManager.GetAvatarGroup();
                    audioSource.priority = Mathf.Clamp(audioSource.priority, 200, 255);
                    if (vrcSpatialAudioSource != null)
                    {
                        // copy the values into the onsp component
                        var onspAudioSource = audioSource.gameObject.GetOrAddComponent <ONSPAudioSource>();
                        onspAudioSource.Gain                 = vrcSpatialAudioSource.Gain;
                        onspAudioSource.Near                 = vrcSpatialAudioSource.Near;
                        onspAudioSource.Far                  = vrcSpatialAudioSource.Far;
                        onspAudioSource.VolumetricRadius     = vrcSpatialAudioSource.VolumetricRadius;
                        onspAudioSource.EnableSpatialization = vrcSpatialAudioSource.EnableSpatialization;
                        onspAudioSource.UseInvSqr            = !vrcSpatialAudioSource.UseAudioSourceVolumeCurve;
                        if (!vrcSpatialAudioSource.EnableSpatialization)
                        {
                            audioSource.spatialize = false;
                        }
                    }
                    #else
                    // these are SDK only, we rely on AvatarAudioSourceLimiter to enforce
                    // values at runtime

                    #if SUPPORT_DEPRECATED_ONSP
                    ONSPAudioSource[] allOnsp = audioSource.gameObject.GetComponents <ONSPAudioSource>();
                    if (allOnsp != null && allOnsp.Length > 0)
                    {
                        ONSPAudioSource onsp = allOnsp[0];
                        if (vrcSpatialAudioSource == null)
                        {
                            vrcSpatialAudioSource = audioSource.gameObject.AddComponent <VRC_SpatialAudioSource>();
                        }

                        vrcSpatialAudioSource.Gain                      = Mathf.Min(onsp.Gain, VRCSDK2.AudioManagerSettings.AvatarAudioMaxGain);
                        vrcSpatialAudioSource.Far                       = Mathf.Min(onsp.Far, VRCSDK2.AudioManagerSettings.AvatarAudioMaxRange);
                        vrcSpatialAudioSource.VolumetricRadius          = Mathf.Min(onsp.Far, VRCSDK2.AudioManagerSettings.AvatarAudioMaxRange);
                        vrcSpatialAudioSource.Near                      = Mathf.Min(onsp.Near, onsp.Far);
                        vrcSpatialAudioSource.EnableSpatialization      = onsp.EnableSpatialization;
                        vrcSpatialAudioSource.UseAudioSourceVolumeCurve = !onsp.UseInvSqr;

                        Debug.LogWarningFormat("ONSPAudioSource found on {0}. converted to VRC_SpatialAudioSource.", child.name);
                        foreach (var o in allOnsp)
                        {
                            Object.DestroyImmediate(o, true);
                        }
                    }
                    #endif
                    if (vrcSpatialAudioSource == null)
                    {
                        // user has not yet added VRC_SpatialAudioSource (or ONSP)
                        // so set up some defaults
                        vrcSpatialAudioSource                      = audioSource.gameObject.AddComponent <VRC_SpatialAudioSource>();
                        vrcSpatialAudioSource.Gain                 = AudioManagerSettings.AvatarAudioMaxGain;
                        vrcSpatialAudioSource.Far                  = AudioManagerSettings.AvatarAudioMaxRange;
                        vrcSpatialAudioSource.Near                 = 0f;
                        vrcSpatialAudioSource.VolumetricRadius     = 0f;
                        vrcSpatialAudioSource.EnableSpatialization = true;
                        vrcSpatialAudioSource.enabled              = true;
                        audioSource.spatialize                     = true;
                        audioSource.priority              = Mathf.Clamp(audioSource.priority, 200, 255);
                        audioSource.bypassEffects         = false;
                        audioSource.bypassListenerEffects = false;
                        audioSource.spatialBlend          = 1f;
                        audioSource.spread = 0;

                        // user is allowed to change, but for now put a safe default
                        audioSource.maxDistance = AudioManagerSettings.AvatarAudioMaxRange;
                        audioSource.minDistance = audioSource.maxDistance / 500f;
                        audioSource.rolloffMode = AudioRolloffMode.Logarithmic;
                    }
                    #endif //!VRC_CLIENT

                    onFound(audioSource);

                    if (sources.Length > 1)
                    {
                        Debug.LogError("Disabling extra AudioSources on GameObject(" + child.name + "). Only one is allowed per GameObject.");
                        for (int i = 1; i < sources.Length; i++)
                        {
                            if (sources[i] == null)
                            {
                                Profiler.EndSample();
                                continue;
                            }

                            #if VRC_CLIENT
                            sources[i].enabled = false;
                            sources[i].clip    = null;
                            #else
                            ValidationUtils.RemoveComponent(sources[i]);
                            #endif //!VRC_CLIENT
                        }
                    }
                }

                Profiler.EndSample();
            }
        }
示例#4
0
        private static IEnumerator EnforceAudioSourceLimitsEnumerator(GameObject currentAvatar, System.Action <AudioSource> onFound)
        {
            if (currentAvatar == null)
            {
                yield break;
            }

            Queue <GameObject> children = new Queue <GameObject>();

            if (currentAvatar != null)
            {
                children.Enqueue(currentAvatar.gameObject);
            }

            while (children.Count > 0)
            {
                if (Time.frameCount > _enforceAudioSourcesFrameNumber)
                {
                    _enforceAudioSourcesFrameNumber        = Time.frameCount;
                    _enforceAudioSourcesProcessedThisFrame = 0;
                }

                if (_enforceAudioSourcesProcessedThisFrame > ENFORCE_AUDIO_SOURCE_GAMEOBJECTS_PER_FRAME)
                {
                    yield return(null);
                }

                Profiler.BeginSample("EnforceAudioSourceLimitsEnumerator");
                _enforceAudioSourcesProcessedThisFrame++;

                GameObject child = children.Dequeue();
                if (child == null)
                {
                    Profiler.EndSample();
                    continue;
                }

                int childCount = child.transform.childCount;
                for (int idx = 0; idx < childCount; ++idx)
                {
                    children.Enqueue(child.transform.GetChild(idx).gameObject);
                }

#if VRC_CLIENT
                if (child.GetComponent <USpeaker>() != null)
                {
                    Profiler.EndSample();
                    continue;
                }
#endif

                AudioSource[] sources = child.transform.GetComponents <AudioSource>();
                if (sources != null && sources.Length > 0)
                {
                    AudioSource audioSource = sources[0];
                    if (audioSource == null)
                    {
                        Profiler.EndSample();
                        continue;
                    }


#if VRC_CLIENT
                    audioSource.outputAudioMixerGroup = VRCAudioManager.GetAvatarGroup();
                    audioSource.priority = Mathf.Clamp(audioSource.priority, 200, 255);
#else
                    ProcessSpatialAudioSources(audioSource);
#endif //!VRC_CLIENT

                    onFound(audioSource);

                    if (sources.Length > 1)
                    {
                        Debug.LogError("Disabling extra AudioSources on GameObject(" + child.name + "). Only one is allowed per GameObject.");
                        for (int i = 1; i < sources.Length; i++)
                        {
                            if (sources[i] == null)
                            {
                                Profiler.EndSample();
                                continue;
                            }

#if VRC_CLIENT
                            sources[i].enabled = false;
                            sources[i].clip    = null;
#else
                            ValidationUtils.RemoveComponent(sources[i]);
#endif //!VRC_CLIENT
                        }
                    }
                }

                Profiler.EndSample();
            }
        }
        public static void EnforceAudioSourceLimits(GameObject currentAvatar)
        {
            using (_enforceAudioSourceLimitsProfilerMarker.Auto())
            {
                if (currentAvatar == null)
                {
                    return;
                }

                Queue <GameObject> children = new Queue <GameObject>();
                if (currentAvatar != null)
                {
                    children.Enqueue(currentAvatar.gameObject);
                }

                while (children.Count > 0)
                {
                    GameObject child = children.Dequeue();
                    if (child == null)
                    {
                        continue;
                    }

                    int childCount = child.transform.childCount;
                    for (int idx = 0; idx < childCount; ++idx)
                    {
                        children.Enqueue(child.transform.GetChild(idx).gameObject);
                    }

                    #if VRC_CLIENT
                    if (child.GetComponent <USpeaker>() != null)
                    {
                        continue;
                    }
                    #endif

                    AudioSource[] sources = child.transform.GetComponents <AudioSource>();
                    if (sources == null || sources.Length <= 0)
                    {
                        continue;
                    }

                    AudioSource audioSource = sources[0];
                    if (audioSource == null)
                    {
                        continue;
                    }


                    #if VRC_CLIENT
                    audioSource.outputAudioMixerGroup = VRCAudioManager.GetAvatarGroup();
                    audioSource.priority = Mathf.Clamp(audioSource.priority, 200, 255);
                    #else
                    ProcessSpatialAudioSources(audioSource);
                    #endif //!VRC_CLIENT

                    if (sources.Length <= 1)
                    {
                        continue;
                    }

                    Debug.LogError("Disabling extra AudioSources on GameObject(" + child.name + "). Only one is allowed per GameObject.");
                    for (int i = 1; i < sources.Length; i++)
                    {
                        if (sources[i] == null)
                        {
                            Profiler.EndSample();
                            continue;
                        }

                        #if VRC_CLIENT
                        sources[i].enabled = false;
                        sources[i].clip    = null;
                        #else
                        ValidationUtils.RemoveComponent(sources[i]);
                        #endif //!VRC_CLIENT
                    }
                }
            }
        }