We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 650 - Sound artefacts (crackling) with SDL + snd-emu10k1 ALSA driver on Linux
Summary: Sound artefacts (crackling) with SDL + snd-emu10k1 ALSA driver on Linux
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: audio (show other bugs)
Version: 1.2.13
Hardware: x86 Linux
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL: http://forums.gentoo.org/viewtopic.ph...
Keywords: target-1.2.14
Depends on:
Blocks:
 
Reported: 2008-12-05 10:11 UTC by Gian Paolo Mureddu
Modified: 2009-10-19 06:21 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gian Paolo Mureddu 2008-12-05 10:11:37 UTC
After a short while of running an application that uses SDL for sound, like games (such as Neverwinter Nights, using the system's SDL-1.2.13 library, the Prey-Demo, etc), sound gets "corrupted" and eventually crackling artefacts flood the audio. It would seem as if this was a buffer-underrun condition (or so I think from the limited knowledge I have of ALSA), and even though it doesn't prevent the application from running, it can be quite annoying. I have been trying to get a recording of the sound but for some reason all I get when I try to record using arecord is silence (though I can record sound from Neverwinter Nights just fine, it would seem that running NWN on a cold-start shows the problem and subsequent loads does not, while prey-demo always shows the problem... But I cannot capture it).
Comment 1 Karol Nowak 2009-07-06 13:53:27 UTC
We have the same problem with Wesnoth @ 64-bit Gentoo. Both me and Ivanovic use the emu10k1 driver. I've managed to reproduce this problem with vanilla SDL (no SDL_mixer) with a simple test program.
Comment 2 Karol Nowak 2009-07-07 15:01:35 UTC
I'm currently testing it with a simplistic pure SDL player. Having a manually compiled latest version of the 1.2 branch actually solves the problem. Now it's time to find the reason of crackling with the system lib.

I've added a timer display to the audio callback in my test app. It displays the number of milliseconds that have passed since the callback has been previously invoked. On a healthy run with manually built SDL:

25
23
23
23
23

System-wide SDL with crackling (reproducible by ctrl+z suspending the process and resuming it):

0
46
0
47
0
46
0
47

Ouch. Looks like it now takes twice as long for it to get called, which probably causes buffer underruns. On the other hand, zeroes indicates that the callback gets called right after it has returned, but in that case it won't have much to write...
Comment 3 Karol Nowak 2009-07-07 16:01:02 UTC
I've narrowed down the differences between system build of SDL and my own. The reason why my build worked is because I haven't disabled OSS (with --disable-oss). The "good" timings in my previous comment are produced with SDL_AUDIODRIVER=dsp. The bad ones with SDL_AUDIODRIVER=alsa.

Note that I only have OSS emulation in ALSA, no "real" OSS in my kernel.

I'm back where I started, but at least I have a work-around.
Comment 4 James Le Cuirot 2009-08-31 02:02:50 UTC
There's a bunch of Gentoo users, including myself, who have been experiencing this. I've seen it with xrick and dxx-rebirth with the -nosdlmixer option. Check out the forum thread.

http://forums.gentoo.org/viewtopic.php?p=5952048
Comment 5 Ryan C. Gordon 2009-09-13 16:33:25 UTC
Tagging this bug with "target-1.2.14" so we can try to resolve it for SDL 1.2.14.

Please note that we may choose to resolve it as WONTFIX. This tag is largely so we have a comprehensive wishlist of bugs to examine for 1.2.14 (and so we can close bugs that we'll never fix, rather than have them live forever in Bugzilla).

--ryan.
Comment 6 Sam Lantinga 2009-09-27 15:06:03 UTC
The SDL audio code may have this problem fixed, can you retest?
http://www.libsdl.org/tmp/SDL-1.2.zip
Comment 7 Karol Nowak 2009-09-27 16:25:58 UTC
Tested with the attached sources, the problem is still there. 
Make sure to export SDL_AUDIO_DRIVER=alsa in case you have SDL compiled with both ALSA and OSS as the latter has not been affected.
Comment 8 Ryan C. Gordon 2009-09-27 21:57:39 UTC
How much do you want to bet that these are systems that are converting between non-power-of-two sample rates?

--ryan.
Comment 9 Sam Lantinga 2009-09-27 23:11:45 UTC
No bet.  Suggestions? :)
Comment 10 James Le Cuirot 2009-09-27 23:35:34 UTC
Thought I should try it too. No change here.
Comment 11 Sam Lantinga 2009-10-08 02:15:50 UTC
We don't have enough information to track this down yet.

Can you try configuring SDL with --disable-assembly and completely rebuild and reinstall and see if that fixes it?

Are any of you available for an IRC chat to help track this down?

Thanks!
Comment 12 James Le Cuirot 2009-10-08 02:44:25 UTC
Aaah, Karol Nowak confused me for a bit there, I thought it was working now but it's SDL_AUDIODRIVER, not SDL_AUDIO_DRIVER. Tried --disable-assembly but that didn't help, I'm afraid. I'm a bit busy now but I'll try and get on IRC later.
Comment 13 Sam Lantinga 2009-10-10 19:06:46 UTC
Can you guys try the very latest SDL snapshot?  There have been a couple tweaks to the ALSA code that may fix this:
http://www.libsdl.org/tmp/SDL-1.2.zip
Comment 14 Karol Nowak 2009-10-10 20:21:58 UTC
It's still there in my case. :(
Comment 15 James Le Cuirot 2009-10-11 02:20:50 UTC
And me.
Comment 16 Sam Lantinga 2009-10-11 02:58:06 UTC
Hey Ryan, do you happen to have one of these sound cards sitting in a system you can test with?
Comment 17 Sam Lantinga 2009-10-11 02:59:31 UTC
Hey guys, can you comment with exactly your sound card, distribution, kernel version, ALSA package version, test program, and anything else you think might help?

I definitely want to see if we can track this down before release.

Thanks!
Comment 18 James Le Cuirot 2009-10-11 03:46:07 UTC
Sound Blaster Audigy Platinum (emu10k1)
Gentoo Linux (recently updated)
2.6.30-gentoo-r6 (have experienced it on earlier kernels)
alsa-lib-1.0.21a (have experienced it on earlier versions)

I always test with xrick. Much smaller than those games mentioned in the first post and it doesn't support SDL_mixer like some games. I'm on IRC right now if you need.
Comment 19 Karol Nowak 2009-10-11 08:32:43 UTC
2.6.29-gentoo-r5 #8 SMP x86_64 AMD Phenom(tm) II X3 710 Processor AuthenticAMD GNU/Linux (Experienced with earlier versions as well)

ALSA: 1.0.20-r1 (Experienced with earlier versions as well)

Test program: anything that uses ALSA? Battle for Wesnoth for instance.

Other than that, an ugly test program that uses SDL only is here: http://nopaste.com/p/aneeb1mmJ

Testing with a file with following specs: audiodump.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 44100 Hz
Comment 20 Karol Nowak 2009-10-11 08:34:04 UTC
2.6.29-gentoo-r5 #8 SMP x86_64 AMD Phenom(tm) II X3 710 Processor AuthenticAMD GNU/Linux (Experienced with earlier versions as well)

ALSA: 1.0.20-r1 (Experienced with earlier versions as well)

03:07.0 Multimedia audio controller: Creative Labs SB Live! EMU10k1 (rev 08)
        Subsystem: Creative Labs CT4832 SBLive! Value
        Flags: bus master, medium devsel, latency 32, IRQ 21
        I/O ports at cf00 [size=32]
        Capabilities: [dc] Power Management version 1
        Kernel driver in use: EMU10K1_Audigy

Test program: anything that uses ALSA? Battle for Wesnoth for instance.

Other than that, an ugly test program that uses SDL only is here: http://nopaste.com/p/aneeb1mmJ

Testing with a file with following specs: audiodump.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 44100 Hz
Comment 21 James Le Cuirot 2009-10-11 08:38:38 UTC
I should add that I'm running a 64-bit system with an Intel Core 2 Quad.
Comment 22 Sam Lantinga 2009-10-11 09:42:16 UTC
I was googling and found this:
"I found the problem: I had all alsamixer settings set to 100% I went back and 
changed them all to about 75% and it sounds perfect."
Comment 24 Sam Lantinga 2009-10-11 11:47:01 UTC
Okay, I found a SBLive card sitting in my closet, and I'm hooking it up to my old 1800+ AMD system and going to try to install Gentoo. :)
Comment 25 Karol Nowak 2009-10-11 14:02:36 UTC
Note that my friend reported the very same problem on a 32-bit Debian system.
Comment 26 Ryan C. Gordon 2009-10-11 14:41:23 UTC
(In reply to comment #16)
> Hey Ryan, do you happen to have one of these sound cards sitting in a system
> you can test with?

I'm on an Audigy 2 (emu10k1 chip, like several other users here).

--ryan.
Comment 27 Sam Lantinga 2009-10-11 14:45:16 UTC
And I assume you're not seeing any issues?
Comment 28 James Le Cuirot 2009-10-11 14:52:40 UTC
Here's a recording which might give you a clue. If you listen carefully, you can hear a little sound from the game. What may be interesting is that the last few seconds should be silent (nothing is happening in the game) but there is still noise.

http://www.aura-online.co.uk/~chewi/xrick.flac
Comment 29 Karol Nowak 2009-10-11 15:14:50 UTC
Here's what it sounds like in my case: http://www.speedyshare.com/421977151.html
Comment 30 James Le Cuirot 2009-10-11 15:18:20 UTC
Clearly mine is a lot worse but there's definitely a problem with yours too. I wonder if these are even the same issue.
Comment 31 James Le Cuirot 2009-10-11 15:26:21 UTC
I just tried Karol's test program and that works fine with any sounds I try it with. How annoying!
Comment 32 Sam Lantinga 2009-10-11 22:35:14 UTC
cat /proc/cpuinfo | fgrep "model name" 
model name	: AMD Athlon(tm) XP 1800+

uname -a
Linux mint 2.6.28-11-generic #42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009 i686 GNU/Linux

ALSA: 1.0.18-1ubuntu11

lspci -v
00:09.1 Input device controller: Creative Labs SB Live! Game Port (rev 05)
        Subsystem: Creative Labs Device 0020
        Flags: bus master, medium devsel, latency 32
        I/O ports at a400 [size=8]
        Capabilities: [dc] Power Management version 1
        Kernel driver in use: Emu10k1_gameport
        Kernel modules: emu10k1-gp

Tested loopwave and xrick with the alsa SDL audio driver...

Result: No audio issues
Comment 33 Sam Lantinga 2009-10-11 22:37:41 UTC
It works better if I show the correct device:

00:09.0 Multimedia audio controller: Creative Labs SB Live! EMU10k1 (rev 05)
        Subsystem: Creative Labs Device 0021
        Flags: bus master, medium devsel, latency 32, IRQ 10
        I/O ports at a000 [size=32]
        Capabilities: [dc] Power Management version 1
        Kernel driver in use: EMU10K1_Audigy
        Kernel modules: snd-emu10k1
Comment 34 James Le Cuirot 2009-10-11 23:15:56 UTC
I just found out how you change the audio device using AUDIODEV. I have a 5.1 surround card and speakers. If I use "default" (which I guess is hw:0,0) then I get noise. I also get noise if I use "hw:0,0" or "front". It works if I use "plug:dmix" and it works with slightly slower sounds if I use "surround50". I get no sound if I use "surround51" but that's probably normal.
Comment 35 James Le Cuirot 2009-10-11 23:18:31 UTC
Forgot to give my device info.

4:06.0 Multimedia audio controller: Creative Labs SB Audigy (rev 03)
	Subsystem: Creative Labs SB0090 Audigy Player
	Flags: bus master, medium devsel, latency 32, IRQ 16
	I/O ports at cf00 [size=32]
	Capabilities: [dc] Power Management version 2
	Kernel driver in use: EMU10K1_Audigy
	Kernel modules: snd-emu10k1
Comment 36 Ryan C. Gordon 2009-10-12 01:10:22 UTC
I have a possible fix in svn revision #5068.

Can people try the latest in the SDL-1.2 branch and see if it fixes the problem?

--ryan.
Comment 37 James Le Cuirot 2009-10-12 06:12:13 UTC
Looks like you've cracked it, Ryan. The latest 1.2.14 works for me. Having said that, the patch you mentioned applied against 1.2.13 makes no difference. Either another very recent patch actually fixed it instead or your change alone isn't sufficient. Strange. I suppose it doesn't matter now.
Comment 38 Sam Lantinga 2009-10-12 09:16:44 UTC
If you drop the SDL_alsa_audio.* from SDL 1.2.13 in SDL-1.2.14/src/audio/alsa and rebuild and reinstall, does the problem come back?

Thanks!
Comment 39 James Le Cuirot 2009-10-12 09:43:47 UTC
Yes and I also tried the converse situation by dropping the new ALSA code into the old SDL. That works so whatever fixed it is contained within that directory.
Comment 40 Sam Lantinga 2009-10-12 09:49:50 UTC
Okay, thanks!  Can others confirm that their ALSA crackling issues are fixed?
Comment 41 Karol Nowak 2009-10-12 10:24:27 UTC
Revision: 5074

It's become much more "interesting" in my case: http://www.speedyshare.com/485329694.html
Comment 42 Ryan C. Gordon 2009-10-12 16:47:36 UTC
(In reply to comment #41)
> It's become much more "interesting" in my case:
> http://www.speedyshare.com/485329694.html

What is that from? The test program? Can you try loopwave?

--ryan.
Comment 43 Sam Lantinga 2009-10-13 03:07:58 UTC
Okay, I've put in a bunch of changes to the ALSA driver, as well as made the PulseAudio driver the default.

Can you all try the latest snapshot?
http://www.libsdl.org/tmp/SDL-1.2.zip

Remember to set the SDL_AUDIODRIVER environment variable to "alsa" or "pulse" or "dsp", depending on what driver you want to test.

Thanks!
Comment 44 James Le Cuirot 2009-10-13 07:43:39 UTC
Just tried that. All good with ALSA here.
Comment 45 Karol Nowak 2009-10-13 12:21:43 UTC
ryan: it is from Battle for Wesnoth (so effectively: SDL_mixer).
Comment 46 James Le Cuirot 2009-10-13 12:26:12 UTC
Oh dear! Because I've been so busy with work, I didn't bother trying any other games. It seems like all SDL_mixer games now sound quite bad. Sorry for not realising this sooner.
Comment 47 Sam Lantinga 2009-10-13 13:34:12 UTC
Do they all sound bad with the latest snapshot, or was that a general statement about the currently released code?
Comment 48 James Le Cuirot 2009-10-13 13:56:25 UTC
I mean SVN. xrick is the only non SDL_mixer game on my system that I can think of. With the latest code from SVN, that's still working fine, but other games that use SDL_mixer like Neverball, D1X-Rebirth, Hedgewars and Frets On Fire all sound terrible.
Comment 49 James Le Cuirot 2009-10-13 13:57:41 UTC
Sorry, still need to clarify that, I think. The SDL_mixer games worked fine for me before. It was only xrick that was causing a problem. The "fix" has broken the others.
Comment 50 Sam Lantinga 2009-10-13 17:46:32 UTC
Can you edit src/audio/alsa/SDL_alsa_audio.c and uncomment the two PERIOD defines at the top and retest?

Thanks!
Comment 51 Sam Lantinga 2009-10-13 20:45:47 UTC
Is anyone available to sit on IRC and work through this with me tonight?

See ya!
Comment 52 Sam Lantinga 2009-10-14 00:49:07 UTC
Okay, I found someone on the ioquake3 channel that was having issues, and I think we have them resolved:
http://www.libsdl.org/tmp/SDL-1.2.zip (or just update from subversion)

Can you all try this version and see if it works for you?

If it doesn't, can you edit src/audio/alsa/SDL_alsa_audio.c and enable the DEBUG define at the top, and post that here?

Then, enable the SET_PERIOD_SIZE define and see if that improves things, and also post the debug output for that as well.

Thanks!
Comment 53 James Le Cuirot 2009-10-14 10:31:45 UTC
That's sorted it for all the games I mentioned. You certainly got some interesting feedback on the ALSA list. Shows that it can still be something of a black art even for someone as experienced as yourself.
Comment 54 Karol Nowak 2009-10-14 11:51:32 UTC
Fixed both of my test cases (SDL_mixer and plain SDL) with SDL_AUDIODRIVER=alsa. Good job!
Comment 55 Sam Lantinga 2009-10-14 16:20:22 UTC
Great!  Thank you guys very much! :)
Comment 56 Sam Lantinga 2009-10-18 19:52:23 UTC
Hey guys, I made one last pass on ALSA sound support. Can you grab SDL from http://www.libsdl.org/tmp/download-1.2.php and try it out with SDL_AUDIODRIVER=alsa SDL_AUDIO_ALSA_DEBUG=1 and let me know if it works?
Thanks!
Comment 57 James Le Cuirot 2009-10-19 04:01:43 UTC
Yep, all good here.
Comment 58 Sam Lantinga 2009-10-19 06:21:02 UTC
Great, thanks!