Volume Animation Scratches

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?

Please post some code and an example sample if possible.

I’ve a fiddling around, and it seems to be something to do with having multiple audio sources playing at the same.

Here a link to a example; Dropbox - Error - Simplify your life

The jumps increases a lot when rendering something as well, so I thinking it might be something with the performance?

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

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.

Heres the finished code;

using UnityEngine;
using System.Collections;

public class Music : MonoBehaviour {

    float oldVolume;
    float newVolume;
    public float masterVolume = 1f;
    float startTime;
    public float fadeDuration = 10f;
    public string[] offScenes;
    public string[] onScenes;
    string previousScene = "";
    bool previousZone = true;
    bool zone = true;

    // Use this for initialization
    void Awake ()
    {
        DontDestroyOnLoad(transform.gameObject);
    }
    void Start ()
    {
        startTime = Time.realtimeSinceStartup;
        oldVolume = audio.volume;
        newVolume = audio.volume;
    }
   
    void Update ()
    {
        string sceneName = Application.loadedLevelName;

        //has scene changed?
        if (previousScene != sceneName)
        {
            //if no no/off scenes is specified, always on
            if (onScenes.Length == 0 && offScenes.Length == 0)
            {
                zone = true;
            }
            //if off scenes specified, off scenes decide on/off state
            if (onScenes.Length == 0 && offScenes.Length != 0)
            {
                zone = !Contains(offScenes, sceneName);
            }
            //if on scenes specified, on scenes decide on/off state
            if (onScenes.Length != 0 && offScenes.Length == 0)
            {
                zone = Contains(onScenes, sceneName);
            }
            //should maybe implement what happens when both on and off
            //scenes are specified
            previousScene = sceneName;
        }

        //has zone changed?
        if (previousZone != zone)
        {
            startTime = Time.realtimeSinceStartup;
            if (zone)
            {
                oldVolume = audio.volume;
                newVolume = masterVolume;
            }else
            {
                oldVolume = audio.volume;
                newVolume = 0f;
            }
            previousZone = zone;
        }
        Fade ();
    }

    bool Contains(string[] array, string key)
    {
        bool result = false;
        foreach (string x in array)
        {
            if (x == key)
            {
                result = true;
            }
        }
        return result;
    }

    void Fade()
    {
        if (((Time.realtimeSinceStartup - startTime) / fadeDuration) < 1f)
        {
            audio.volume = Mathf.Lerp(oldVolume, newVolume, (Time.realtimeSinceStartup - startTime) / fadeDuration);
        }
    }
}

I can specify the scenes I want the music on and off, and it’ll fade with the set duration. Works for my needs:)

Good news, thanks for sharing the code!

Gregzo