Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Unscaled equivalent of Audiosettings.dsptime?

Discussion in 'Audio & Video' started by pertsaboii, Nov 25, 2023.

  1. pertsaboii

    pertsaboii

    Joined:
    Jan 25, 2022
    Posts:
    8
    Hello,

    I have a following problem: I have a progressive music system in Unity that tracks the point where loops should change with Audiosettings.dsptime. It works great. However, I implemented a feature which keeps the music playing when the game is paused (with lower volume and a lowpass filter), but this obviously messes the timing system up.

    I tried to track time with a unscaled deltatime -based timer on Update, and it works for some time, but after some dozen seconds or minutes it gets messed up. The point in which it messes up seems random, and I haven't been able to point out anything specific that would cause it.

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (unscaledTimer > goalTime - .2f) PlayTimedProgressiveLevelClip();
    4.         unscaledTimer += Time.unscaledDeltaTime;
    5.     }
    Code (CSharp):
    1.     void PlayTimedProgressiveLevelClip()
    2.     {
    3.         AudioClip clipToPlay = currentLoopClips[0];
    4.         if (currentLoopClips.Count != 1) while (earlierClip == clipToPlay) clipToPlay = currentLoopClips[Random.Range(0, currentLoopClips.Count)];
    5.         earlierClip = clipToPlay;
    6.  
    7.         if (audioSource2Next == true)
    8.         {
    9.             SoundManager.Instance.musicSounds2.clip = clipToPlay;
    10.             SoundManager.Instance.musicSounds2.PlayScheduled(goalTime);
    11.         }
    12.         else
    13.         {
    14.             SoundManager.Instance.musicSounds.clip = clipToPlay;
    15.             SoundManager.Instance.musicSounds.PlayScheduled(goalTime);
    16.         }
    17.         clipDuration = (double)clipToPlay.samples / clipToPlay.frequency;
    18.         goalTime += clipDuration - lvlMusicAudioTailTime;
    19.         audioSource2Next = !audioSource2Next;
    20.     }
    Any ideas?
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    379
    I believe the slippage you're experiencing is due to the precision of `Time.unscaledDeltaTime`. Although it is conceptually right to use that, you should probably use `AudioSettings.dspTime` for all your time sensitive calculations.

    In your code, I would suggest you get rid of `unscaledTimer` and simply compare `goalTime` to `AudioSettings.dspTime` everywhere. That should make it more consistent by keeping the highest precision in that context, and might fix your issue.
     
  3. pertsaboii

    pertsaboii

    Joined:
    Jan 25, 2022
    Posts:
    8
    Thanks for the reply! However in my case that is not the solution, since I have audiolistener on pause during the pauses. But I managed to find an ok workaround for it. I just start a coroutine which waits for the duration of the clip currently playing, after which it starts playing the next one. It's not as precise but works well enough.
     
    SeventhString likes this.