Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Audio Measure Audio Latency

Discussion in 'Audio & Video' started by syhrmr, Mar 12, 2018.

  1. syhrmr

    syhrmr

    Joined:
    Feb 21, 2018
    Posts:
    10
    Hello. I currently work on a project to make an instrument app and build it in different platform of devices (mobile and desktop). I want to do a research and get some data of audio latency from the devices that my app installed by triggering a button in my app. So what method in Unity that I should use to measure the audio latency? Thank you.
     
  2. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    You can compute length of time of OnAudioFilterRead's data buffer based on its size, channels and current output sample rate
    That should give you rough estimate of time needed to process smallest chunk of audio of common AudioSource - at least that's what unity exposes to user
     
  3. syhrmr

    syhrmr

    Joined:
    Feb 21, 2018
    Posts:
    10
    using dspTimeSettings?
     
  4. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
  5. syhrmr

    syhrmr

    Joined:
    Feb 21, 2018
    Posts:
    10
  6. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    you implement AudioSettings.GetDSPBufferSize by calling it :) :
    Code (CSharp):
    1.             int bufferLength, numBuffers;
    2.             AudioSettings.GetDSPBufferSize(out bufferLength, out numBuffers);
    the computed latency is in seconds - you're dividing buffer size (number of samples) by sample rate (samples per 1 second)
     
  7. syhrmr

    syhrmr

    Joined:
    Feb 21, 2018
    Posts:
    10
    Is buffer size amount different in different devices?
     
  8. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    It might differ, yes
    In general you should determine its value by calling GetDSPBufferSize
     
  9. syhrmr

    syhrmr

    Joined:
    Feb 21, 2018
    Posts:
    10
    Okay I've tried it. BufferLength is value of buffer size from DSP Buffer Size settings. For example if you set DSP Buffer Size to "best latency" you will get 256 from every devices where the application installed. Otherwise, sample rate value is different for different devices.
     
  10. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Right, I should have been more precise:
    you should be always determining the sizes by calling the function since the user can change the settings.
     
  11. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,556
    More information :

    - Each device has its own "native buffer size" which if the buffer size used in Unity matches, it is better. On Android there is an API to ask for this. Some of the collected data https://superpowered.com/latency
    - The website mentioned also has open source the way they calculate the latency on-device. (not just the native buffer size) You can learn from their code and integrate it into your game. They use auto-calibration which play a sound and record back multiple times, or use a loop back cable. IMO asking information from Unity is far from accurate of what the device can really achieve.
    - Unity's Best Latency is still really bad on some device. Look at this video using Best Latency compared with manually creating the track to match the device's native sampling rate and buffer size. That's why I said in the last point what you hear from Unity is not the true potential of the device.
     
  12. yinhuanhuan

    yinhuanhuan

    Joined:
    Dec 27, 2018
    Posts:
    2
    on some models, when I set the DSP Buffer size to Best latency, the music turns to electric current
     
  13. yinhuanhuan

    yinhuanhuan

    Joined:
    Dec 27, 2018
    Posts:
    2
    someone can tell Why is that
     
  14. Deleted User

    Deleted User

    Guest

    Perhaps you should clarify a bit more what you mean with "turns to electric current"