Editors Note: The original patch was modified to use SDL_Delay() instead of
authorSam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 15:40:50 +0000
changeset 7654c2ba6161939
parent 764 974c0fb74bf8
child 766 ed57c876700d
Editors Note: The original patch was modified to use SDL_Delay() instead of
nanosleep because nanosleep may not be portable to all systems
using SDL with the ALSA backend. This may be a moot point with
the switch to blocking writes anyway...

Date: Sat, 27 Dec 2003 21:47:36 +0100
From: Michel Daenzer
To: Debian Bug Tracking System
Subject: [SDL] Bug#225252: [PATCH] ALSA fixes

Package: libsdl1.2debian-all
Version: 1.2.6-2
Severity: normal
Tags: patch

For SDL 1.2.6, the ALSA backend was changed to call snd_pcm_open() with
SND_PCM_NONBLOCK. That's a good idea per se, however, it causes high CPU
usage, interrupted sound and stuttering in some games here. Taking a nanosleep
whenever snd_pcm_writei() returns -EAGAIN fixes this, but I think it's more
efficient to use blocking mode for the actual sound playback. Feedback from the
SDL and ALSA lists appreciated.

The patch also fixes the default ALSA device to be used.
src/audio/alsa/SDL_alsa_audio.c
     1.1 --- a/src/audio/alsa/SDL_alsa_audio.c	Sun Jan 04 15:18:08 2004 +0000
     1.2 +++ b/src/audio/alsa/SDL_alsa_audio.c	Sun Jan 04 15:40:50 2004 +0000
     1.3 @@ -45,7 +45,7 @@
     1.4  #define DRIVER_NAME         "alsa"
     1.5  
     1.6  /* The default ALSA audio driver */
     1.7 -#define DEFAULT_DEVICE	"plughw:0,0"
     1.8 +#define DEFAULT_DEVICE	"default"
     1.9  
    1.10  /* Audio driver functions */
    1.11  static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
    1.12 @@ -146,17 +146,19 @@
    1.13  	int           status;
    1.14  	int           sample_len;
    1.15  	signed short *sample_buf;
    1.16 -       
    1.17 +
    1.18  	sample_len = this->spec.samples;
    1.19  	sample_buf = (signed short *)mixbuf;
    1.20  	while ( sample_len > 0 ) {
    1.21  		status = snd_pcm_writei(pcm_handle, sample_buf, sample_len);
    1.22  		if ( status < 0 ) {
    1.23  			if ( status == -EAGAIN ) {
    1.24 +				SDL_Delay(1);
    1.25  				continue;
    1.26  			}
    1.27  			if ( status == -ESTRPIPE ) {
    1.28  				do {
    1.29 +					SDL_Delay(1);
    1.30  					status = snd_pcm_resume(pcm_handle);
    1.31  				} while ( status == -EAGAIN );
    1.32  			}
    1.33 @@ -316,6 +318,9 @@
    1.34  	/* Get the parent process id (we're the parent of the audio thread) */
    1.35  	parent = getpid();
    1.36  
    1.37 +	/* Switch to blocking mode for playback */
    1.38 +	snd_pcm_nonblock(pcm_handle, 0);
    1.39 +
    1.40  	/* We're ready to rock and roll. :-) */
    1.41  	return(0);
    1.42  }