All game audio distorts and desyncs after a few hours

We’re making an arcade music game and are experiencing the following issue: when the game has been running for a few hours, it will eventually sound like a speaker blew out (heavy distortion that sounds like clipping) and the timing will be off. I can’t tell if it happens all at once or gradually, since this happens over a period of 5+ hours. Our game timing is synced to audio samples.

Calling AudioSettings.SetDSPBufferSize with any buffer length or number of buffers causes the distortion to happen immediately in Linux (though timing appears to remain fine), but not in Windows. AudioSettings.GetDSPBufferSize reports that the game is using a buffer length of 1024 and size of 4. The Unity Editor’s audio settings have the same report and same effects if set to “Let Unity manage my audio settings” and on “Best performance.” “Best latency” and “Good latency” are untested, but would probably make this problem happen more frequently.

We’re working on a fix for this ourselves, but I figured I’d see if the community here has an idea of what’s going on so we can get this fixed more quickly.

I resolved this issue in part and thought I’d share how. There are actually 3 bugs involved:

AudioSettings.SetDSPBufferSize resets the AudioListener volume to maximum when called, regardless of what the AudioListener volume is set to. We had tried to call the buffersize before every game, but the volume would get so loud that it would clip, which made us think it was the distortion effect (the distortion effect is actually different, but sounds similar to clipping).

The audio distorting and going off-sync after a long period of time is likely due to floating-point errors since it happens at around the 7-hour mark. This only happens in the Linux standalone. Calling a SetDSPBufferSize periodically resets the sound system, which prevents this from happening. The distortions to the waveform are regular and happen every 1024 samples, as pictured:

Finally, the Linux standalone simply seems to have a memory leak related to garbage collection. Even with the audio fix, the memory usage in a 2GB system inflates from 14% at boot to 52% in a 12-hour period. The Unity team indicated in their response to me that this is a known issue, but it’s worth mentioning here.