Skip to content
Advertisement

SDL audio randomly becomes distorted on Linux

I have a strange issue with SDL audio. It will randomly become distorted. It’s completely random, I can’t reliably reproduce it at all. Sometime it starts being distorted seconds after the program starts, other times I’ve played with the software, let it run over night and it’ll still be working in the morning.

It could have to do with SDL, because I can close the mixer with Mix_CloseAudio() and then re-open it with Mix_OpenAudio() and it’ll fix the sound… for a while, sometimes a long while, other times it’ll mess up soon after. (As Brad pointed out in the comments, this doesn’t necessarily mean it’s an SDL issue).

More about the system:

  • stripped down linux system with kernel 2.6.27.5-117.fc10.i686.
  • libSDL_mixer-1.2.so.0.2.6.

This is a legacy system that always worked in the past, and I’ve made no changes at all to the audio system part of the software. The system used to run off a CD-rom and all the game’s assets were loaded into ram on a filesystem overlay, but I’ve switched it to running off of a CF card without all the sounds in ram all the time. I’ve made no changes to the sound drivers either.

Here’s an example of the sound distortion https://www.youtube.com/watch?v=zEneMi5mtt0
Here’s an example of the same sounds functioning properly https://www.youtube.com/watch?v=nyPZLJ-9gsI

Advertisement

Answer

Some observations based on your sound that we can use to debug the issue:

  • The distortion you’re hearing is based on your sound, and is a series of regular clicks.
  • If the audio is quiet, the distortion/clicks are quiet. Likewise, the problem gets worse as the audio gets louder.
  • Pitch is correct, so it isn’t a sample rate issue.
  • Once the problem begins, it is consistent.

Basically, what’s happening is that the sound card is reading from the circular buffer at a different place than where the software writing to it thinks it is. At some point, there was a buffer underrun causing the play head to get in-front of the read head.

What’s supposed to happen is that the playback should reset, causing a momentary drop-out of audio until the buffer is full again. What seems to have happened is that playback continued. Therefore, the play head keeps running over a line between old buffer data and new buffer data, causing the click. This is also why when the sound is quieter, the click/distortion is quieter… the difference between the two PCM samples is less.

I’ve seen this happen with buggy sound card drivers, and also buggy sound cards. To work around the problem, find a way to increase your buffer size. This increases latency but may help prevent the problem from occurring in the first place.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement