When walking between scenes on the Switch I'm getting a lot of music stuttering. This happens eeven if its the same music that keeps on playing across both scenes.
The music is played using the music storage soundtrack. Any ideas?
Thanks!
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Can't think of anything on the AC side of things - ultimately it's still a regular AudioSource placed in the DontDestroyOnLoad scene, which is Unity's standard practice for cross-scene music playback.
Try tinkering with the AudioClip's properties, as well as running a Deep Profile to see if anything's slowing things down.
I made a new discovery that makes me think this might be an AC issue after all:
The music only starts stuttering on scene loads AFTER a player speech has been played. Before that, scene transitions are 100% smooth.
A little info which might help you recreate:
As in, speech played in the previous scene? Has it finished playback by the time the stuttering occurs?
I suspect the issue will be affected (at the very least, compounded) by the size of the project / assets involved. How many speech / music tracks are you using?
What are the Preload Audio Data and Load Type values for your music clips, and is there any difference if you enable the former and set the latter to Streaming?
OK, so I tried A LOT of stuff to get to the bottom of this problem
Some learnings for context
1. Android (and probably every other low end device) suffers from exactly the same problem
2. The number/size of the assets doesn't affect the problem
3. The problem remains after trying every possible permutation of Preload Audio Data/Load Type/Streaming
4. The problem remains no matter if the speech has ended or not when switching scene
5. The problem gets "reset" if you give another app focus on the phone/device and then come back to the game - scene loads are then back to being totally silent (until after a speech has been played)
This helps a lot
The biggest part of the problem turns out to be ACs calls to Resources.UnloadUnusedAssets() on AssetLoader.UnloadAssets().
These calls are probably a good idea when using Resources for stuff like save game references or scenes, but that method is also known to cause audio stuttering on low end devices.
Also, I'm using Addressables for both these scenarios, so perhaps it would be a good idea to skip that call if that is the case. Or play it safe and add a checkbox in the Settings manager for it (or just have property that can be set programmatically to skip these calls).
This helps some
Stuttering can be further minimized by setting Project Settings->Audio->DSP Buffer Size to "Best performance"
Almost gone..
Together, those fixes takes care of (ish) 90% of the stuttering, but there's still enough left to sometimes break the flow when moving from scene to scene.
Any ideas?
Point 5 on my list makes me think that perhaps the speech audio isn't disposed/unloaded after it has been played? Or is something audio related done once the app regains focus?
Thank you for your efforts. I'll look into the use of UnloadUnusedAssets further.
What happens when audio is played, and focus is regained, however, is handled by Unity. Have you been through AC's "Performance and optimisation" chapter?
You're welcome - happy to provide some love to my AC brethren 👍
I have been through that chapter, yes. Here's the reason why I think the last audio crackle has to do with how AC handles speech specifically:
Playing other sounds (like walk animation sounds and UI sounds) does not cause the next scene transition to crackle - only speech sounds do that. And if a speech is shown that doesn't hava any audio - not a crackle...
If you have any theories as to some code you want me to try or uncomment to see if it gets rid of it, don't hesitate to ping me. But you should be able to reproduce this using any Android phone (even high end ones) given the info on this thread.
I've looked into the use of UnloadUnusedAssets, and I believe it can be bypassed automatically (no need for a separate option) so long as Save asset references with Addressables? is checked.
Does this occur when speech is played "By Direct Reference"?
I'm pretty sure you're wrong there. I just did a debug session with breakpoints on all ACs call to UnloadUnusedAssets - they were all hit. As I indicated though, it's only the call made on AssetLoader.UnloadAssets() that is causing the problems while changing scenes. That one is hit 3 times the first scene switch and then 4 times from then on. Here are the stacktraces for a scene switch that causes 4 calls:
Yep, still there I'm afraid.
I wasn't talking about the current release - but a potential update.
What were the results of a Deep Profile at the time the stuttering occurs?
Ah, sorry
In a word - overwhelming... Since a whole new scene is loaded, it's very hard to pinpoint what might be causing the audio glitch...
A heads up: I'm not sure that you ONLY want to skip those UnloadUnusedAssets calls when not using Addressables. I kept nothing dynamically created in my previous game but I would still like to get rid of the stuttering.
I also heard back from a user who doesn't use Addressables yet comment out those calls when it's time to do ports.
To be honest, I'm not 100% sure that putting NPCs in addressables ALWAYS is beneficial to the perf in all situations, so if it was up to me, I would at least also allow for a programmatic way to opt out of the calls 👍
Point made, I'll look into it.