Search Unity

Audio Tips. 2D Source with Distance, HighPassFilter Analogic to LowPassFilter Curve

Discussion in 'Audio & Video' started by mitaywalle, Sep 13, 2017.

?

Was helpfull?

  1. Yes

    100.0%
  2. No

    0 vote(s)
    0.0%
  1. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    - if you add Low Pass Filter to AudioSource, in curves editor of Source will appear new lilac curve, describing low pass effect by distance.



    - You can make analogic logic for High Pass Filter'a with this script.
    Invoke Set() once for one-shot sounds, or per Update for loop sounds.
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class HighPassDistanceFader : MonoBehaviour
    4. {
    5.   public AnimationCurve HighCurve;
    6.   public bool SetInUpdate;
    7.  
    8.   private AudioHighPassFilter highPass;
    9.   private Transform Listener;
    10.   private AudioSource source;
    11.  
    12.   private float sqrMinDist;
    13.   private float sqrMaxDist;
    14.  
    15.   void Start()
    16.   {
    17.     highPass = GetComponent<AudioHighPassFilter>();
    18.     Listener = FindObjectOfType<AudioListener>().transform;
    19.     source = GetComponent<AudioSource>();
    20.     if (!source) return;
    21.  
    22.     sqrMinDist = source.minDistance * source.minDistance;
    23.     sqrMaxDist = source.maxDistance * source.maxDistance;
    24.   }
    25.  
    26.   void Update()
    27.   {
    28.     if (SetInUpdate) Set();
    29.   }
    30.  
    31.   void Set()
    32.   {
    33.     if (!highPass || !Listener) return;
    34.     var t = (transform.position - Listener.position).sqrMagnitude;
    35.     t = (t  - sqrMinDist) / (sqrMaxDist - sqrMinDist);
    36.     var value = HighCurve.Evaluate(t);
    37.     highPass.enabled = value <= 0f;
    38.     highPass.cutoffFrequency = Mathf.Lerp(10f,22000f,value);
    39.   }
    40. }

    - or for SpatialBlend (2D => 3D), closer - mean more 2D.
    This gives you effect '2D AudioSource affect by min and max Distance, and still can change volume'
    Helpful in baked, complex ambience-scapes, separate rooms in door or lakes and wood in outdoor
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class SpatialFader : MonoBehaviour
    4. {
    5.   private Transform Listener;
    6.   private AudioSource source;
    7.  
    8.   private float sqrMinDist;
    9.   private float sqrMaxDist;
    10.  
    11.   void Start()
    12.   {
    13.     Listener = FindObjectOfType<AudioListener>().transform;
    14.     source = GetComponent<AudioSource>();
    15.     if (!source) return;
    16.  
    17.     sqrMinDist = source.minDistance * source.minDistance;
    18.     sqrMaxDist = source.maxDistance * source.maxDistance;
    19.   }
    20.  
    21.   void Update()
    22.   {
    23.     if (!source || !Listener) return;
    24.     var t = (transform.position - Listener.position).sqrMagnitude;
    25.     t = (t  - sqrMinDist) / (sqrMaxDist - sqrMinDist);
    26.     source.spatialBlend = Mathf.Lerp(10f,22000f,t);
    27.   }
    28. }

    You need more sense of 3D for some special sounds (gunshots, recochets, muzzles)?
    Choose "Oculus" in AudioManager'e as Spatialize Plugin.
    And on needed AudioSource mark Spatialize flag as true.
    Flag can be changed at runtime

    Sounds became a bit distorted, quieter, but it's position in 3D space will became more clear.
     
    Last edited: Oct 26, 2017
  2. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    New stuff. Early I've found this thread. And apply it to link audio source output volume to change Particle System settings. here is what I've got:


    here is a code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SoundToPS : MonoBehaviour
    4. {
    5.     public AudioSource AS;
    6.     public ParticleSystem PS;
    7.     [Header("Emit")]
    8.     public bool Emit = true;
    9.     public float EmitMultiplier = 200f;
    10.     public float CurrentEmit;
    11.  
    12.     [Header("Size")]
    13.     public bool Size = true;
    14.     public float MinSize = 1f;
    15.     public float MaxSize = 5f;
    16.     public float CurrentSize;
    17.     [Header("Speed")]
    18.     public bool Speed = true;
    19.     public float MinSpeed  = 1f;
    20.     public float MaxSpeed = 5f;
    21.     public float CurrentSpeed;
    22.     [Header("Alpha")]
    23.     public bool Alpha = true;
    24.     public float MinAlpha = 1f;
    25.     public float MaxAlpha = 5f;
    26.     public float CurrentAlpha;
    27.  
    28.  
    29.     [Range(0f, 1f)] public float MinRMS = .1f;
    30.     [Range(0f, 1f)] public float MaxRMS = .25f;
    31.     [Range(0f,1f)] public float CurrentRMS;
    32.     public bool DebugRMS = true;
    33.  
    34.     public int qSamples = 4096;
    35.     private float[] samples;
    36.  
    37.     ParticleSystem.EmissionModule em;
    38.     ParticleSystem.MainModule sz;
    39.     ParticleSystem.MinMaxGradient clr;
    40.  
    41.     private void Start()
    42.     {
    43.         em = PS.emission;
    44.         sz = PS.main;
    45.         clr = PS.main.startColor;
    46.         samples = new float[qSamples];
    47.     }
    48.  
    49.     private void Update()
    50.     {
    51.         var rms = GetRMS();
    52.  
    53.         if (AS && DebugRMS) CurrentRMS = rms;
    54.         if (!PS || !AS) return;
    55.         if (Emit) em.rateOverTimeMultiplier = CurrentEmit = Mathf.InverseLerp(MinRMS,MaxRMS , rms) * EmitMultiplier;
    56.  
    57.         if (Size) sz.startSizeMultiplier = CurrentSize = Mathf.Lerp(MinSize,MaxSize, Mathf.InverseLerp(MinRMS, MaxRMS, rms));
    58.         if (Speed) sz.startSpeedMultiplier = CurrentSpeed = Mathf.Lerp(MinSpeed, MaxSpeed, Mathf.InverseLerp(MinRMS, MaxRMS, rms));
    59.         if (Alpha)
    60.         {
    61.             var col = sz.startColor.color;
    62.             col.a = CurrentAlpha = Mathf.Lerp(MinAlpha, MaxAlpha, Mathf.InverseLerp(MinRMS, MaxRMS, rms));
    63.             sz.startColor = col;
    64.         }
    65.     }
    66.  
    67.     float GetRMS()
    68.     {
    69.         return GetRMSPerChannel(0) + GetRMSPerChannel(1);
    70.     }
    71.  
    72.     float GetRMSPerChannel(int channel)
    73.     {
    74.         if (!AS) return 0f;
    75.  
    76.         AS.GetOutputData(samples, channel);
    77.         float sum = 0;
    78.         foreach (float f in samples)
    79.         {
    80.             sum += f * f;
    81.         }
    82.         return Mathf.Sqrt(sum / qSamples);
    83.     }
    84. }
     
    Last edited: Oct 26, 2017