Search Unity

Volume Animation Scratches

Discussion in 'Audio & Video' started by tokejepsen, Dec 1, 2014.

  1. tokejepsen

    tokejepsen

    Joined:
    Mar 14, 2013
    Posts:
    6
    So I have used the animator to fade an audio source components volume in and out.

    When the audio source fades in, it sounds like it jumps and scratches.

    Anybody experienced the same?
     
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Please post some code and an example sample if possible.
     
  3. tokejepsen

    tokejepsen

    Joined:
    Mar 14, 2013
    Posts:
    6
  4. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi tokejepsen,

    I've never tried using animators for tweening volume. Is your fade in curve too steep? The fact that it seems performance related points in that direction. Did you try in a build, instead of in the editor? The audio thread can act strange in the editor...

    Also, try animating the fade in code, see if it makes a difference. Simply Lerp the value, or use HOTween to do the deed.

    Last resort, use OnAudioFilterRead to fade in audio data at buffer level: that would make your fades 100% frame rate independent.

    Cheers,

    Gregzo
     
  5. tokejepsen

    tokejepsen

    Joined:
    Mar 14, 2013
    Posts:
    6
    Hey gregzo,

    I initially resorted to this animators option cause I couldn't get the code to work. I now got a solution working, and it sounds great:) No jumps or scratches, so I wouldn't recommend using animators for tweening volume.

    I'll post the finished code when I'm done.
     
  6. tokejepsen

    tokejepsen

    Joined:
    Mar 14, 2013
    Posts:
    6
    Heres the finished code;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Music : MonoBehaviour {
    5.  
    6.     float oldVolume;
    7.     float newVolume;
    8.     public float masterVolume = 1f;
    9.     float startTime;
    10.     public float fadeDuration = 10f;
    11.     public string[] offScenes;
    12.     public string[] onScenes;
    13.     string previousScene = "";
    14.     bool previousZone = true;
    15.     bool zone = true;
    16.  
    17.     // Use this for initialization
    18.     void Awake ()
    19.     {
    20.         DontDestroyOnLoad(transform.gameObject);
    21.     }
    22.     void Start ()
    23.     {
    24.         startTime = Time.realtimeSinceStartup;
    25.         oldVolume = audio.volume;
    26.         newVolume = audio.volume;
    27.     }
    28.    
    29.     void Update ()
    30.     {
    31.         string sceneName = Application.loadedLevelName;
    32.  
    33.         //has scene changed?
    34.         if (previousScene != sceneName)
    35.         {
    36.             //if no no/off scenes is specified, always on
    37.             if (onScenes.Length == 0 && offScenes.Length == 0)
    38.             {
    39.                 zone = true;
    40.             }
    41.             //if off scenes specified, off scenes decide on/off state
    42.             if (onScenes.Length == 0 && offScenes.Length != 0)
    43.             {
    44.                 zone = !Contains(offScenes, sceneName);
    45.             }
    46.             //if on scenes specified, on scenes decide on/off state
    47.             if (onScenes.Length != 0 && offScenes.Length == 0)
    48.             {
    49.                 zone = Contains(onScenes, sceneName);
    50.             }
    51.             //should maybe implement what happens when both on and off
    52.             //scenes are specified
    53.             previousScene = sceneName;
    54.         }
    55.  
    56.         //has zone changed?
    57.         if (previousZone != zone)
    58.         {
    59.             startTime = Time.realtimeSinceStartup;
    60.             if (zone)
    61.             {
    62.                 oldVolume = audio.volume;
    63.                 newVolume = masterVolume;
    64.             }else
    65.             {
    66.                 oldVolume = audio.volume;
    67.                 newVolume = 0f;
    68.             }
    69.             previousZone = zone;
    70.         }
    71.         Fade ();
    72.     }
    73.  
    74.     bool Contains(string[] array, string key)
    75.     {
    76.         bool result = false;
    77.         foreach (string x in array)
    78.         {
    79.             if (x == key)
    80.             {
    81.                 result = true;
    82.             }
    83.         }
    84.         return result;
    85.     }
    86.  
    87.     void Fade()
    88.     {
    89.         if (((Time.realtimeSinceStartup - startTime) / fadeDuration) < 1f)
    90.         {
    91.             audio.volume = Mathf.Lerp(oldVolume, newVolume, (Time.realtimeSinceStartup - startTime) / fadeDuration);
    92.         }
    93.     }
    94. }
    95.  
    I can specify the scenes I want the music on and off, and it'll fade with the set duration. Works for my needs:)
     
  7. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Good news, thanks for sharing the code!

    Gregzo