Why does async loading cause audio glitches in currently playing audio? (DEF-3032) (DEF-3618)

I use Monarch for screens. If I switch screens with a reasonable amount of assets while a music track is playing there is sometimes a noticeable audio stutter / crackling / clicking in any playing music.

There are also sometimes other random audio stuttering which is annoying because it makes the game seem unpolished.

Tested on Windows 10

3 Likes

It also really bothers me that audio still crackles when closing apps on Windows. :sob:

1 Like

I too, experience this problem a lot. On switching scenes the audio does crackle sometimes, and sometimes stops at all for a brief period. And this problem happens without using monarch too

I believe it’s because audio is lockstep with the sim. It’s all on the same thread. So when the game has even the most tiny amount of lag the audio glitches. And something to do with async loading collections is not actually do async, otherwise it wouldn’t produce lag.

FMOD extension is in its own thread? That could be an option, but would be really annoying to switch to just for this.

I believe that async loading of a collection will split loading of required content over multiple frames but each piece of content that is loaded will be loaded in a single frame. This means that a large 16k atlas will be loaded in a single frame. Perhaps that’s what’s causing the stutter? Some large asset in the collection that you’re loading?

It happens with every screen, and we try to keep all atlases below 2048x2048. And sometimes it doesn’t happen at all when switching screens for whatever reason, but most of the time it does. It will also sometimes stutter randomly for whatever reason when the game is idling but music is playing. And when the game closes it’s like nails grating on a chalkboard. I try to set the master gain to 0 but it seems like changes to gain are also disabled when the game closes and the glitch sounds play at max gain. We’ve thought about fading out gain quickly between screens but it’s somewhat obtrusive because popups have the same effect.

Async loading is not done in its own thread? :cry:

@inactive-vilse probably knows the details of audio playback on windows and might shed some more light on this.

Nope. @sven, correct me if I’m wrong here!

That is a huge bummer. If it didn’t impact audio playing it could be forgivable but as is it really hurts.

1 Like

We currently drive the audio in the frame loop trying to be ahead enough to not cause stutters but we also do resource loading on each frame.

There is a separate thread that loads the raw data in the background but actually converting the binary data to something useful for the engine (such as image data to texture) is done on the main thread. The audio rendering each frame is competing with everything else we do in a frame and if we have low enough frame rate we will start to lag behind with the audio rendering.

This work is time-capped but due to how it is implemented it can use more time than intended and cause stutters. We are aware of this and have ideas on how to improve it but no firm time frame yet.

As for the crackle on windows exit I was not aware of it and I’ll make a ticket if there is not one already.

7 Likes

Created:

DEF-3032 - Audio crackles at exit on Windows 10
DEF-3618 - Loading resources may cause audio to crackle

7 Likes

@Pkeod Do you have a project that you can share with me that shows these problems? I have a few fixes I’d like to try out.

1 Like

AudioTester.zip (5.6 MB)

Stand alone example where issues are present. Press 1 and 2 on keyboard to toggle loading collections as audio plays.

2 Likes

Thanks, I just tried it on my MacBook and no issues with crackling sound. I’ll need to try it on a windows machine to see if I can reproduce.

1 Like

If you have any slower computers might be worth testing, or may only be a Windows issue. If you can make a custom build I can test somehow with your fixes you can send to me and I can test on my computer.

2 Likes

I just tried it on my windows 7 machine, no crackling or glitching when loading the collections. I wouldn’t say my computer is slow though. Do you get the same crackling if you use “load”? If I change it to “load” instead of “async_load” it stops for a beat, but no crackling.

On exit I don’t get crackling either, but I do get a short pause and then it repeats the last fraction of a second of music. Like a record skipping. If I stop the music 0.45sec before exiting there’s no problems, but anything less than that and it skips.

1 Like

I’m not sure what the bottleneck is on the computer I’m testing on that is making it take longer than a frame to load / cause the audio glitches, my test computer is still somewhat average according to Steam hardware survey so probably will happen for many users which I don’t like. Did you try the example I uploaded and spam asyncload/unload buttons? You may need to rebuild a few times to get it to happen too as once it is running for a while it seems to not happen as much.

Audio glitches on closing happen sometimes, sometimes it sounds like it’s hanging. Maybe it’s to do with how much was loaded in a frame, dunno.

I also cannot mute the audio when closing the app. Does that work for you? I tried to set the gain to zero / stop audio, but it doesn’t work on the same frame as closing, so you still get the audio glitch/repeated sound when closing.

With the FMOD extension I get none of these problems probably largely in part because it’s running on its own thread?

1 Like

Yeah, I tried your example a bunch of times. Tried rebuilding too. Couldn’t reproduce any sound problems with loading though. Ugh.

Hmm, I just tried animating an object on screen. It pauses a bit when I async_load one of the collections, more if both together, but still the audio is smooth! I guess whatever Defold is doing to prevent stutters is working on my machine.

Yeah, same here with the exiting issue. Stopping the sound or setting the gain to zero anytime less than half a second before exiting does nothing to help.

3 Likes

Async loading is done on separate thread but the main thread does some processing of the stuff preloaded.

One cause for preloading hitches that I found is that the preloader queue gets full which results in synced loading anyway since not all resources are queued to load in the background.

7 Likes

Fixed in 1.2.149

3 Likes