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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Very low latency audio output on iOS ?

Discussion in 'iOS and tvOS' started by jonlab, Sep 26, 2013.

  1. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    Hi there,

    I'm working on a musical app (a virtual instrument) and for this app I'd like (obviously) a very low audio output latency.

    In theory, I should be able to control the audio output buffer size and count with AudioSettings.SetDSPBufferSize(), but unfortunately, it doesn't seems to work. The sound continues to work, but the buffers configuration doesn't change. I tried several values without any success...
    (I tested on an iPad 3 under iOS 6.1.3 - Unity 4.2.1)

    The lowest latency I can achieve is 256 x 4 buffers (with "Best latency" in Audio Projects Settings in Editor). I'd like something better, maybe 128 x 2...
    Is there any way to achieve this without having to roll my own native Audio Unit (plugin) ?

    Thanks !
     
    Last edited: Sep 30, 2013
  2. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    I did a test on an Android device (ACER A700 tablet), and we have the same behaviour : actually the AudioSettings.SetDSPBufferSize() seems to work only in the Editor.
    Any thoughts about that ?
     
  3. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    Well, I implemented a plugin using iOS low level Audio Units. Latency is now minimal (I still have something like 1/60 s of latency due to the app logic and input running at 60 Hz).
    But of course I cannot use Unity Audio engine. I now have to run my own Audio Engine in parallel with an API to load samples, play samples, position sources, ...
     
  4. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    up ! anybody has any ideas about that ?
     
  5. klvo

    klvo

    Joined:
    Mar 24, 2013
    Posts:
    31
    Bump

    I'm trying to do something similar on iOS as well.

    No one has found a workaround for low latency using only Unity?

    @jonlab: did you find a way to lower the latency using Unity or did you end up using the separate Audio engine?
     
  6. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    I'm using a totally reparate Audio engine for now. And latency is much better.
     
  7. dchen05

    dchen05

    Joined:
    Aug 28, 2012
    Posts:
    45
    Hey @jonlab what engine did you end up going with? I'm trying to do something similar.
     
  8. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    My own one, with an iOS AudioUnit and a C sample based synthesizer.
     
  9. dchen05

    dchen05

    Joined:
    Aug 28, 2012
    Posts:
    45
    @jonlab Interesting, are you still using Unity at all for the visuals? If so is audio to visual communication hard? Or are you doing everything strictly in objective-c?
     
  10. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    I wrote a C# plugin, so everything is driven from Unity, but all the loadings and heavy audio is done in C.
     
  11. dchen05

    dchen05

    Joined:
    Aug 28, 2012
    Posts:
    45
    Sounds awesome, I'm sure there would be some interest in that plugin as an asset. I would pay for it.
     
  12. klvo

    klvo

    Joined:
    Mar 24, 2013
    Posts:
    31
    +1
    I would also pay for that type of solution on the asset store.
    @jonlab Please consider it!
     
  13. jonlab

    jonlab

    Joined:
    Apr 6, 2010
    Posts:
    182
    Well, actually this is really a very specific one for now, but I will think about this.
     
  14. hello1111

    hello1111

    Joined:
    Apr 14, 2014
    Posts:
    13
    bonkers that Unity doesn't have a take on low-latency audio itself.
     
  15. F.Salka

    F.Salka

    Joined:
    Dec 13, 2013
    Posts:
    43
    I had the same problem, the solution is mind blowing:

    Set DSPBuffer size in project settings to "Default" , not low latency , not good latency, "Default" is the secret sauce, only then you will be able to adjust dsp buffer size from script, i was able to go as low as 64 buffer size on my ipad mini with retina display for my music app, and the result is extremely low latency, almost the same as the one you get in garage band, enjoy :)
     
  16. fizzd

    fizzd

    Joined:
    Jul 30, 2013
    Posts:
    21
    Hi, I am trying to get very low latency working on iOS too. Just for the record, I tried F. salka's app on my iPad 2 in the wild hope that I would finally see a Unity app with very low latency - but sadly despite what you say it is still far away from the results I can get implementing Audio Units via the Amazing Audio Engine.

    Devs, I noticed Unity is getting worked on on the audio front for the upcoming unity 5. Any plans to allow very low latency for iOS? I really think this is the one thing preventing proper music apps from being built on Unity,, something I and many others in this thread are looking for.
     
  17. fizzd

    fizzd

    Joined:
    Jul 30, 2013
    Posts:
    21
    @jonlab, were you able to mix audio in unity with audio from your custom engine, or did you have to route everything through the new plugin and disable audio in unity completely?
     
  18. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,555
    What is "Amazing Audio Engine"? Is that a plugin?
     
  19. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    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. Speaking of AudioUnit, the lowst audio technology of iOS that jonlab talked about, OpenAL is a cross platform library that happen to maps to AudioUnit on iOS. It means its latency must be equal to or more than AudioUnit.
    - On Android it uses AudioTrack, I confirmed it to be faster than SoundPool and no meaningful difference from C++ side OpneSL ES of NDK.

    I have compiled all of my findings in here : http://exceed7.com/native-audio
     
    Last edited: Apr 16, 2018
  20. dchen05

    dchen05

    Joined:
    Aug 28, 2012
    Posts:
    45
    wow thanks for making this! I had been pretty discouraged from making mobile audio projects in unity after this. I will definitely check it out, looks very thorough, well done!
     
    5argon likes this.