Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sound and Input lag

Discussion in 'Android' started by dsthilaire, Nov 15, 2010.

  1. dsthilaire

    dsthilaire

    Joined:
    Dec 4, 2009
    Posts:
    17
    Hi all!

    I'm currently working on porting our iphone games (chop chop series) to android. I'm almost done we all of them, but I just notice something very annoying which was obvious in our sports game, but was crystal clear in our "runner" game. There seems to be about ~0.5-1.0 sec input and sound lag in the game. This completely kills the "runner" game's gameplay since it relies on fast paced single touch input.

    Am I the only one having similar issues? Is there anything to do about them?

    Apart from that, everything is going really smoothly, thanks for the great job unity :)

    --
    David
     
  2. Wozik

    Wozik

    Joined:
    Apr 10, 2009
    Posts:
    662
    There should not be any lags. Can you reproduce them in a small basic scene?
    Otherwise you way want to profile your game or may be check scripts, or try on another device.
     
  3. MrTToo

    MrTToo

    Joined:
    Sep 8, 2009
    Posts:
    3
    I have same problem with you.
    Did you solve this problem?
    If you solved, please let me know the way.
     
  4. BuzzJive_Legacy

    BuzzJive_Legacy

    Joined:
    Oct 11, 2010
    Posts:
    26
    I don't have input lag but the sound lag is terrible, in 3.0 and 3.1. Anybody have tips on ways to get the Android to play a sound when you ask it to instead of a half second later?
     
  5. G2X44

    G2X44

    Joined:
    Jun 15, 2011
    Posts:
    11
    Hi!

    Is there any progress with this issue?
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Go to project settings and set audio to "best latency" to fix the issue.
     
  7. G2X44

    G2X44

    Joined:
    Jun 15, 2011
    Posts:
    11
    Thanks for your reply.

    I've already tried it. :(
    Unfortunately it doesn't solve the problem on android. (As far as I know it's working on iphone only.)
    I couldn't find the solution yet. (I've been searching for a while on the internet.)

    Is there any idea for this issue?
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Choose Decompress on Load option for each sample and try again.

    Also post the code you use to play one sound, the method may be important.
     
  9. G2X44

    G2X44

    Joined:
    Jun 15, 2011
    Posts:
    11
    I've already tried all combinations. (Decompress on Load, Compressed in memory..., wave, mp3.... Best latency, Good latency....)

    Here is the code:

    public AudioClip beeep;

    void Update () {
    if (Input.GetMouseButtonDown (0)){
    audio.PlayOneShot(beeep);
    }
    }
     
  10. XY01

    XY01

    Joined:
    Jul 15, 2011
    Posts:
    24
    I too have the same issue and have also tried all of the above proposed solutions.
    Any one with the same issue or fixes?
     
  11. CliffCawley

    CliffCawley

    Joined:
    Mar 30, 2009
    Posts:
    21
    Digging up an old thread...but this still occurs in Unity 4.0.0f7 on my Samsung Galaxy Tab 2.

    Doesn't matter the combination it still lags.

    Works great on iPad 2, just not on the Android Tablet.
     
  12. MHD Salka

    MHD Salka

    Joined:
    Mar 19, 2013
    Posts:
    72
    any news about latency on android? did anyone actually managed to fix it?
     
  13. Cake

    Cake

    Joined:
    Jul 1, 2013
    Posts:
    15
    All android devices on the market today have at least around 100ms lag on touch input. Unfortunately, the way Android handles touch input through its IPC layers the latency can't be avoided.

    Unless you have input lag above 200ms there is nothing Unity3D can do about it.
     
  14. trzmiel

    trzmiel

    Joined:
    Nov 5, 2013
    Posts:
    28
    Tablet Xperia Z: I can not see any touch input lag, but audio latency is about 300-500ms. Any descibed sollutions (Decompress on Load, Compressed in memory..., wave, mp3.... Best latency, Good latency....) doesn't work at all. Who know the sollution?
     
  15. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    Cake is right. The sound latency on most Android devices is times higher than on iOS, and this comes from the hardware, drivers and operating system itself. This latency depends on the particular device model - can be better on for example Galaxy Nexus, and worse on others.

    Apart from the settings recommended above, there's not much space for improvement... With the variety of devices, it's nearly impossible to guarantee specific latency numbers. Sad but true.
     
  16. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    I also have audio latency problems on Android devices.

    I have changed DSPBuffer Size to Best Latency (in Unity's AudioManager settings), I have also tried to set it to default and changed it in code via AudioSettings.SetDSPBufferSize to various value combinations. I have sounds for several percussion instruments (they all have their peaks as soon as possible in the sound wave) which I want to play when I press something. They are all 16 bits .wav files, set to "Load in memory" and 2D. The sound clips are set on individual AudioSources, so I only have to call Play to call them. These sources have all "bypass" effects checkboxes turned on. I would say this is a pretty much perfect setup for sounds to play with minimal delay in Unity. (I have also tried mono, oggs, compressed and not, etc).

    Regarding input, I play sounds when a key/mouse/touch is pressed down (not up, as that would result in further lag). I've tried it inside of OnGUI and Update, in response to UI Button events, etc. I've also used an almost empty scene without extra processing of any kind.

    Everything works without lag using a mouse in desktop builds (as expected). But I STILL have latency that is much higher than native applications in my Android device. I can notice a small delay no matter the app, but can hear the sound almost immediately for example in this app: https://play.google.com/store/apps/details?id=br.com.rodrigokolb.realdrum. This delay would be good enough for my use case. In contrast, no matter what I do in Unity, I always hear the sound significantly after the touch. I've turned on Android developer settings to show my touch and the touch response of my device is pretty much instant. Unity's response to touch seems to lag a bit behind, and then the audio adds to that.

    We're almost in 2015 and Unity still provides no way to reduce audio latency to satisfactory levels? This is killing rhythm games on the engine.
     
    Last edited: Mar 13, 2015
  17. mdrotar

    mdrotar

    Joined:
    Aug 26, 2013
    Posts:
    377
    @CanisLupus

    What is your method for measuring touch lag and audio lag? What devices are you testing with?

    The lag I have isn't much worse than the expected hardware+OS lag. All mobile devices have a pretty significant amount of touch lag and audio lag and it will be different between devices. If you're getting lag that is a lot worse than can be expected from hardware, then you're probably doing something wrong.
     
  18. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    I wish I was doing something wrong and could improve the lag. :) I find that hard to believe, currently. If I compare the lag I get with the Drum application I linked with the lag I get from my test app, they are certainly different.

    My test "method" consists simply in tapping the floor at a certain speed (say, 120 bpm) with my foot and press the screen with my finger at the same time. In the Drum application I get the sound from the device almost immediately after my foot's (I can notice the lag, but it's the expected amount). With anything I could come up with in Unity, delay is much higher. If I tap at relatively fast speeds I get the sound response from the game almost off-beat (i.e. midway through two finger presses). Actually, 120 bpm is almost fast enough for that to happen, and the sound wave is not the problem, as desktop builds of the game don't have any lag.

    I've currently tested on an Samsung Galaxy S3 and a Nexus tablet. I've had other people test it on these devices and they could certainly notice the difference in lag. I would give up on this if native Android apps had similar lag, but this is not the case.

    It's certainly either Unity's problem or my problem using the engine. I've tried everything I know, and haven't read any topic/thread/webpage that could help me, hence why I'm inclined to believe the former case. I would love to be publicly proven wrong, though. It would mean I could solve the problem. :)
     
  19. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    @CanisLupus , your observations seem correct.

    Prior to 2013, there was almost no way to get an acceptable audio latency for a musician in Android.
    Then, Google presented High Performance Audio https://code.google.com/p/high-performance-audio/ . You might want to watch Glenn Kasten's video on YouTube (link available on that page). The bottom line is - for newer devices, there is a way to enable audio fast path, and get a decent latency.

    We experimented with OpenSL and fast path in Unity. The latency is improved on some devices comparing to the default Java driver, but some devices show latency regression, and some have glitches. We cannot release it as is, so we are collaborating with Glenn and other folks at Google to get a solution. It will find its way to the release when stable enough.

    Addition: Galaxy S3 is not the best audio device out there. Galaxy Nexus is good, not sure about Nexus 7 and 10.
     
  20. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    Thanks for the explanation, Yury. :) So the native app I linked might be using high performance audio, and Unity currently does this differently, having worse lag. Would this be correct?

    By creating a custom native Android plugin in Java just to play a sound, and calling it from Unity, would I potentially be able to improve latency? (before your work with OpenSL becomes part of Unity) If so, would this require Unity Pro? It would be my first time trying to create an Android plugin.
     
  21. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    Right.

    I think theoretically you can write a native plugin (in C/C++) that implements raw OpenSL with no effects etc., but you will have to route your sound there... Frankly speaking I never tried, so no guarantee here :) Java won't work.

    Based on http://unity3d.com/unity/licenses - native plugins do not require Unity Pro.
     
  22. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    OK, thanks for the help. If I have time I'll try that, but I probably can't in the recent future. Meanwhile, if somebody has already tried this and wants to share the plugin, I would appreciate it very much. :)
     
  23. Ghopper21

    Ghopper21

    Joined:
    Aug 24, 2012
    Posts:
    170
    Hi Yury, thanks for this helpful response (and thanks to CanisLupis for this thread). It doesn't seem like OpenSL/fast path made it into the Unity 5 release, based on my testing. Can you confirm that's the case, i.e. Unity 5 is still using the same (higher-latency) audio for Android as before?

    And if so, any update on how your experiments with OpenSL/fastpath are going, and how soon we'll be able to see this released?
     
    CanisLupus likes this.
  24. Ghopper21

    Ghopper21

    Joined:
    Aug 24, 2012
    Posts:
    170
    Yury -- another question based on your comment here. Is Unity 5's new Native Audio Plug-in SDK relevant to the theoretical native plug-in approach you note above?
     
    CanisLupus likes this.
  25. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    @Ghopper21, you are right - Unity 5 does not have fast path for Android enabled. Unfortunately, not much progress here at the moment :( I will probably get back to it when we cope with the 5.0 launch overhead.

    As for the native audio plugins, I don't think it is related, but I will check with the audio team and get back to you with the details.
     
    Ghopper21 likes this.
  26. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    Ghopper21 likes this.
  27. Ghopper21

    Ghopper21

    Joined:
    Aug 24, 2012
    Posts:
    170
  28. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    I'm glad (and a bit scared) to announce that 5.2b2 will be the first version where OpenSL is enabled by default for all the devices that report FEATURE_AUDIO_LOW_LATENCY.

    You can check whether your device is running OpenSL by looking up in logcat message like "
    AudioManager: Selecting OpenSL output..."

    Hope that helps anyone.
     
    skullthug, lloydg, CanisLupus and 2 others like this.
  29. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    Feel free to check it out and post bugs ;)
     
  30. lloydg

    lloydg

    Joined:
    Mar 23, 2015
    Posts:
    36
    This is very good news! I will let you know how it goes.
     
  31. brewthetea

    brewthetea

    Joined:
    Dec 24, 2019
    Posts:
    5
    Hi,

    Audio lag issue still persists. The version I use is 2020.1.9f1. Has no solution been found regarding this issue?

    I used Google Translate.