Access Audibility in Runtime?

I want to manage that if any sound’s Audibility is below a certain dB, I will stop and clear its AudioSource. I found Audibility in Profiler Window. Is it possible to access Audibility in Runtime?

EDIT-1:

I found something like this:

9455402--1328306--upload_2023-11-7_17-36-27.png

I think the “Audibility” only works inside the engine :frowning:

You’re right, this function is only internal, and that’s suboptimal, to say the least…

As a workaround, you could have function calculating the current loudness of the sound by analyzing the buffer got from AudioSource.GetOutputData. If you get a RMS value below a certain threshold for a certain number of frame, then you could flag that AudioSource as candidate for clean-up.

I found a solution but do not know if it is the best approach.

I have an AudioManager that iterates all playing AudioSources in one Update. Then I checked Audibility by AudioSource distance with AudioListener positions. I did this distance check with sqrMagnitude to get better performance calculating distance. If AudioSource is so far away then pause it.

        private void CheckAudibility(AudioEntity audioEntity)
        {
            if (audioEntity.GetIsAudioSource3D())
            {
                Vector3 distanceVector3 = _audioListenerTransform.position - audioEntity.transform.position;
                float distanceFromAudioListener = distanceVector3.sqrMagnitude;

                if (distanceFromAudioListener <= audioEntity.GetAudioSourceMaxDistanceSqr())
                    audioEntity.SetIsAudioSourceAudible(true);
                else
                    audioEntity.SetIsAudioSourceAudible(false);
            }
        }

        public bool GetIsAudioSource3D() => _audioSource.spatialBlend > 0.5f;

        public float GetAudioSourceMaxDistanceSqr()
        {
            float maxDistance = _audioSource.maxDistance;
          
            return maxDistance * maxDistance;
        }

        public void SetIsAudioSourceAudible(bool isAudible)
        {
            switch (isAudible)
            {
                case false:
                    _audioSource.Pause();
                    break;
                case true:
                    _audioSource.UnPause();
                    break;
            }
        }

I think this is a good system and that it surfaces a relevant data point, really useful to optimize you resources. Good job!

The only nitpicky remark I would make here is that audibility should involve how loud is the sound at that exact moment, as you could have a very dim sound close less audible than an very loud distant sound…

…but that would make me the asshole as I know that Unity doesn’t provide that “instantaneous loudness” information, nor does it make it easy to retrieve :hushed:

1 Like