UltraStar-Deluxe / Play

Free and open source singing game with song editor for desktop, mobile, and smart TV
https://ultrastar-play.com
MIT License
377 stars 69 forks source link

Music stutters on scene change #374

Open achimmihca opened 1 year ago

achimmihca commented 1 year ago

Actual behaviour

On my Android phone, there is an audible delay / stutter in the music on scene change, notably in the menu scenes. Furthermore, there is an audible clicking noise on scene change.

The issue occurs also when the scene transition sound is disabled (volume set to 0). Thus, it seems to be the background music.

This issue does not occur on PC and in the Unity editor, at least not as strong. Maybe because the PC has better hardware than the mobile device.

Expected behaviour

Scene change should be smooth, fast, and without audible effect to the background music.

Steps to reproduce

  1. Start UltraStar Play on the phone
  2. Listen carefully while changing between main menu and settings menu multiple times.

Details

Provide some additional information:

achimmihca commented 1 year ago

Scene change feels slow anyway. There is probably some performance bottleneck, needs profiling.

BTW: I am not very happy anymore with the overall setup of CommonSceneObjects. Some of them are created and reinitialized in every scene. Some others use DontDestroyOnLoad, which requires different handling and does not work well together with current injection. This could probably be unified, e.g. with a single DontDestroyOnLoadManager that handles all CommonSceneObjects and takes care of their injection on scene change.

achimmihca commented 1 year ago

I am not very happy anymore with the overall setup of CommonSceneObjects

I fixed this using a DontDestroyOnLoadManager as suggested. Thus, new DontDestroyOnLoad objects can simple be added to this manager as a child. And scene injection will handle these objects properly.

On my Android phone, there is an audible delay / stutter in the music on scene change The issue occurs also when the scene transition sound is disabled

Only because of bad speaker in the phone. There is no issue with headphones or external speakers.

With proper speakers, the background music keeps playing smoothly. But the scene transition sound was a little nasty when changing scenes quickly.

I changed this such that a new scene transition sound is played only if the last scene transition sound has finished. This prevents any stop/start clicking noise.

Scene change feels slow anyway. There is probably some performance bottleneck

Scene change takes only about 100-200ms (also on mobile). Most of this time is spent by Unity for loading the scene, setting up the UI, and garbage collection. So there is not that much room for improvement on our side.

achimmihca commented 1 year ago

about 100-200ms (also on mobile). Most of this time is spent by Unity for loading the scene

I created a repository to test this: https://github.com/achimmihca/UnitySceneChangePerformanceTest

The LTS version Unity 2021.3.4f1 only takes 20-30 ms to change between the scenes. But Unity 2022.2.2f1 (which was used to build UltraStar Play v0.8.2) takes 170-220 ms to change the scenes. Same for Unity 2022.2.3f1.

So, I blame it on Unity.

The issue is known and Unity plans to optimize UI Toolkit initialization performance in the 2023 releases (see Unity forum). Thus, I won't do anything now to improve scene change performance.

achimmihca commented 1 year ago

The issue is still present. There is a huge audible delay on a Macbook from 2013 running macOS Big Sur.

This is weird. Unity is supposed to play the audio continuously even if loading the next scene takes some time. The BackgroundMusicManager has DontDestroyOnLoad so its audio should not be affected by the scene load.