Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio Audiosource.time innaccurate in Android?

Discussion in 'Audio & Video' started by SovietSpartan, May 20, 2017.

  1. SovietSpartan

    SovietSpartan

    Joined:
    May 2, 2014
    Posts:
    17
    Hello! I'm working on a Rythym game for Android where players can import their own songs with their own self made beatmaps.

    The game is about tapping some circles that approach a position as their "tap time" approaches. These circles have a tap time, which is the time where they are supposed to be tapped, and a spawn time, which is used to spawn the circles a few seconds before their tap time is reached so that they have time to move torwards their position.

    These circles use the following code to move:

    Code (CSharp):
    1.  initialTimeDiff = Mathf.Abs(currentSongTime - spawnTime); <---This is set on Start()
    2. currentTimeDiff = spawnTime - currentSongTime;                    
    3. transform.position = Vector3.Lerp(targetPos,spawnPos,currentTimeDiff/initialTimeDiff);
    This works perfectly on the Unity Editor, however, on Android is where i'm finding issues.

    Both doing Audiosource.time and Audiosource.clip.timesamples/Audiosource.clip.frequency give innacurate values.

    By "innaccurate" i mean that the values seem to not update as fast as they should. I made a test to see whether it was a performance issue or something by adding a Time.deltaTime timer and a Audiosource.clip.timesamples/Audiosource.clip.frequency timer next to each other and seeing which of the two updated faster.

    Time.deltaTime was updating as fast as expected, while the Audiosource timer was lagging behind a bit. Eventually the Time.deltaTime timer got out of sync with the song, so i had to discard using it for the game.

    This issue causes the circles in the game to move as if they were "lagging" while everything else runs smoothly. This affects both the timing where the player is supposed to tap them too.

    Anyone knows what is going on? I also found out that the issue got better by increasing the System Sample Rate to 96000 in the Audio settings, but i can't make it go farther than that, so the issue is still there. I made a post before, but didn't get any answers, so i decided to give it a shot and see if i could get help this time.

    Any helps is appreciated! >_<
     
  2. SovietSpartan

    SovietSpartan

    Joined:
    May 2, 2014
    Posts:
    17
    Bumping! No one else experiences this? Right now, i'm using the average value between Time.deltaTime and Audiosource.Time, however this is not 100% accurate to the song still.
    Is there a way to get the accurate audio time on Android?
     
  3. Jacowaco

    Jacowaco

    Joined:
    Aug 23, 2013
    Posts:
    8
    Hi Soviet, i'm experiencing the exact same issue. I know this post is old but did you find a way to solve it?
     
  4. atalkingfish

    atalkingfish

    Joined:
    Sep 17, 2018
    Posts:
    47
    I have been really struggling with this, having the same issue. I looked at a lot of threads looking for a solution to this, and nobody seems to have any ideas. After looking at a lot of ideas, I started looking at the project audio settings. I think I may have found a solution.

    By going to the Audio Settings and changing the "DSP Buffer Size" from "Best Performance" to "Best Latency", according to the inspector, it seems to have resolved the issue. That is, I'm no longer getting doubled values on adjacent frames.

    For what it's worth, I was having two issues: (1) .time and .timeSamples were not updating every frame and, (2) when the project was built onto a device, the audio was notably behind for the type of game (a music rhythm video game).

    After building it to my phone just now, it looks like turning the DSP Buffer Size to "Best Latency" has resolved both issues on my build!
     
    PellySharp and nhanc18 like this.
  5. johnathonjcramer

    johnathonjcramer

    Joined:
    May 23, 2019
    Posts:
    1
    Changing the DSP Buffer Size setting to "Best Latency" resolved an issue I was having with syncing audio to visual effects in my game, but absolutely destroyed audio qualilty. Using the "Good Latency" setting seems to be a nice middle-ground.

    It took me a long time to find this little gem, so I'm going to add some keywords to this post for any other googlers out there trying to resolve this issue:

    Unity sync audio
    Unity audio sync stutter
    Unity timesamples stutter
    Unity timesamples repeat
    Unity audio timesamples off
    Unity rhythm game
     
    Last edited: Jan 13, 2022
    jf065d likes this.
  6. cihadturhan_unity

    cihadturhan_unity

    Joined:
    Apr 19, 2021
    Posts:
    62
    Just another beat/rhythm game dev and the same problem.
    I naively thought `audioSource.time` would be accurate enough so I could use them to update position of an object and I can just follow the camera with it.

    It stutters like hell!

    I've spent some good amount of hours because I thought I did something wrong and searched in cinemachine posts. No, it was unity giving me innaccurate time when I use `audioSource.time`

    I think the only good way to do it is to keep track of time manually and not relying on `audioSource.time`
    Any other ideas?
     
    jf065d likes this.
  7. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    380
    You could try to maintain a global "game time" that is updated every frame in a manager (aka persistent!) object. With your AudioClips properly documented and triggered. By calculating your deltas against that stable anchor might result in cleaner movement of your objects.
     
    cihadturhan_unity likes this.