0
\$\begingroup\$

My AudioClip is lagging when I play it in Unity3d. The AudioClip is supposed to play when you left-click inside the game window.

Steps I've tried for fixing it:

  • Uncompressing the .wav sound file.
  • Setting the clip variable of an AudioSource to a clip and then playing the sound, but that's a different story.

How the issue occurs: When I start the game, on the first click it plays fine. But then after a few clicks, it starts slowing down and/or lagging.

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(AudioClip))]
[RequireComponent(typeof(AudioSource))]

public class GameProcess : MonoBehaviour {

    public Canvas Game;
    public AudioClip explosion;
    AudioSource beatBox;
    Transform btn;
    Vector3 canvasMin;
    Vector3 canvasMax;
    Vector3 pointerMin;
    Vector3 pointerMax;
    Vector3 pointerPos;

    void Start () {
        btn = Game.transform.FindChild("Pointer");
        canvasMin = Game.GetComponent<Collider>().bounds.min;
        canvasMax = Game.GetComponent<Collider>().bounds.max;
        pointerMin = btn.GetComponent<Collider>().bounds.min;
        pointerMax = btn.GetComponent<Collider>().bounds.max;
        beatBox = GetComponent<AudioSource>();
    }

    void Update () {
        if (Input.GetMouseButton(0))
        {
            beatBox.PlayOneShot(explosion);
        }
        if (!(pointerMin.x <= canvasMin.x) || !(pointerMin.y <= canvasMin.y))
        {
            if(!(pointerMax.x >= canvasMax.x) || !(pointerMax.y >= canvasMax.y))
            {
                btn.transform.position = Input.mousePosition;
            }else
            {
                btn.transform.position.Set(pointerPos.x - 1, pointerPos.y - 1, 0);
            }
        }else
        {
            btn.transform.position.Set(pointerPos.x + 1, pointerPos.y + 1, 0);
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

When you're using the uncompressed WAV file, check the option that says "Preload audio data". This will ensure the sound clip is loaded into RAM and ready to use at a moment's notice instead of being loaded every time (or the first time) it's needed.

Sounds like bullets, explosions etc. should always be loaded into memory. Sounds that have narration or music can be loaded later on for size related reasons.

The original problems seems to be an audio slow-motion effect rather than a lag.

Possible problems that may cause this:

Audio Sources' pitch setting is less than 1 (means slower rate audio).

Doppler Factor of your audio manager is not zero and the object that makes the sound is moving away from the Audio Listener. (Shouldn't be the case since all you do is make sounds on the screen itself so they shouldn't be moving.)

Your sampling rate might be too high and it might have to pick a lower sampling rate in-game (not likely with the current technology but if you have an older system, this may be the case).

\$\endgroup\$
  • \$\begingroup\$ That didn't work. \$\endgroup\$ – XBM Dec 25 '16 at 0:03
  • \$\begingroup\$ Does it play fine when you wait for explosion sound to end and then press it to play again? It might just be a simple issue of having too many sound sources at once. \$\endgroup\$ – John Hamilton Dec 25 '16 at 5:41
  • \$\begingroup\$ No. It only plays fine the first time only if "play on awake" is on. If I just play it, it sounds like it's at 50% speed. However, if I change an option it sounds fine ONCE after I change the option, then it goes back to sounding slow again. \$\endgroup\$ – XBM Dec 25 '16 at 17:41
  • \$\begingroup\$ On a totally unrelated note, check for !(p and q) instead of (!p or !q) inside the if statements. (That's if(!((pointerMin.x <= canvasMin.x) && (pointerMin.y <= canvasMin.y)))). Also, does the sound have actual lag or does it just play slower than usual? (actual lag means you have parts where there's no sound and that would usually sound like the audio is crackling) \$\endgroup\$ – John Hamilton Dec 25 '16 at 20:06
  • \$\begingroup\$ It's just slower than usual. \$\endgroup\$ – XBM Dec 25 '16 at 21:49
0
\$\begingroup\$

Your problem might came from 2 things : Unity adds audio latency and ALSO input latency. Your sound is a result of clicking a button. Audio latency is one thing but input latency also indirectly increase the perceived audio latency. (You can look at my research here : https://github.com/5argon/UnityiOSNativeAudio) Though, I think input latency is noticable only on mobile device.

To fix audio latency : Project Setting > Audio > DSP Buffer Size > set it to Best Latency (small buffer size). As of today with this settings, it make a glitched sound on Windows build while on macOS, Android, iOS is completely fine. You might want to have larger buffer size on Windows. (at the expense of more latency)

If that is not enough you can use native methods of each platform. I just made Native Audio asset store plugins which can make a native call to both iOS and Android's fastest native way from one central interface. https://www.assetstore.unity3d.com/#!/content/107067

There are various ways of playing audio at native side, here's my choice :

  • On iOS it uses OpenAL. It is faster than AVAudioPlayer, AudioToolbox, and SystemSound.
  • On Android it uses OpenSL ES (C), I confirmed it to be faster than SoundPool and AudioTrack (Java).

I have compiled all of my findings in here : http://exceed7.com/native-audio

PS. I have used FMOD for Unity before. The best settings that I could do. In addition to setting the best file format, requires editing FMOD Unity's source code to use very low number of buffer size. With that still the latency is just about equal to Unity's "Best Latency" (ant the sound cracks more too due to a low buffer size)

To fix input latency : This is much more difficult as the path that Unity receives touch from Xcode project is almost hardwired and is not meant to be replaced easily. (Unlike audio, we just left the Unity one and use our native method)

I made iOS Native Touch which can reduce this input latency. But you will lose many conveniences that Unity provides including finger ID tracking, stationary state, etc.

http://exceed7.com/ios-native-touch/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.