Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Access Audibility in Runtime?

Discussion in 'Audio & Video' started by DeathPro, Nov 7, 2023.

  1. DeathPro

    DeathPro

    Joined:
    Jul 28, 2018
    Posts:
    92
    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?

    image (1).png

    EDIT-1:

    I found something like this:

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

    I think the "Audibility" only works inside the engine :(
     
    Last edited: Nov 8, 2023
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    430
    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.
     
  3. DeathPro

    DeathPro

    Joined:
    Jul 28, 2018
    Posts:
    92

    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.

    Code (CSharp):
    1.        
    2.         private void CheckAudibility(AudioEntity audioEntity)
    3.         {
    4.             if (audioEntity.GetIsAudioSource3D())
    5.             {
    6.                 Vector3 distanceVector3 = _audioListenerTransform.position - audioEntity.transform.position;
    7.                 float distanceFromAudioListener = distanceVector3.sqrMagnitude;
    8.  
    9.                 if (distanceFromAudioListener <= audioEntity.GetAudioSourceMaxDistanceSqr())
    10.                     audioEntity.SetIsAudioSourceAudible(true);
    11.                 else
    12.                     audioEntity.SetIsAudioSourceAudible(false);
    13.             }
    14.         }
    15.  
    16.         public bool GetIsAudioSource3D() => _audioSource.spatialBlend > 0.5f;
    17.  
    18.         public float GetAudioSourceMaxDistanceSqr()
    19.         {
    20.             float maxDistance = _audioSource.maxDistance;
    21.          
    22.             return maxDistance * maxDistance;
    23.         }
    24.  
    25.         public void SetIsAudioSourceAudible(bool isAudible)
    26.         {
    27.             switch (isAudible)
    28.             {
    29.                 case false:
    30.                     _audioSource.Pause();
    31.                     break;
    32.                 case true:
    33.                     _audioSource.UnPause();
    34.                     break;
    35.             }
    36.         }
     
  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    430
    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 :eek:
     
    DeathPro likes this.