comparison src/audio/alsa/SDL_alsa_audio.c @ 765:4c2ba6161939

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.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 04 Jan 2004 15:40:50 +0000
parents 5d07f9a47f17
children b8d311d90021
comparison
equal deleted inserted replaced
764:974c0fb74bf8 765:4c2ba6161939
43 43
44 /* The tag name used by ALSA audio */ 44 /* The tag name used by ALSA audio */
45 #define DRIVER_NAME "alsa" 45 #define DRIVER_NAME "alsa"
46 46
47 /* The default ALSA audio driver */ 47 /* The default ALSA audio driver */
48 #define DEFAULT_DEVICE "plughw:0,0" 48 #define DEFAULT_DEVICE "default"
49 49
50 /* Audio driver functions */ 50 /* Audio driver functions */
51 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec); 51 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
52 static void ALSA_WaitAudio(_THIS); 52 static void ALSA_WaitAudio(_THIS);
53 static void ALSA_PlayAudio(_THIS); 53 static void ALSA_PlayAudio(_THIS);
144 static void ALSA_PlayAudio(_THIS) 144 static void ALSA_PlayAudio(_THIS)
145 { 145 {
146 int status; 146 int status;
147 int sample_len; 147 int sample_len;
148 signed short *sample_buf; 148 signed short *sample_buf;
149 149
150 sample_len = this->spec.samples; 150 sample_len = this->spec.samples;
151 sample_buf = (signed short *)mixbuf; 151 sample_buf = (signed short *)mixbuf;
152 while ( sample_len > 0 ) { 152 while ( sample_len > 0 ) {
153 status = snd_pcm_writei(pcm_handle, sample_buf, sample_len); 153 status = snd_pcm_writei(pcm_handle, sample_buf, sample_len);
154 if ( status < 0 ) { 154 if ( status < 0 ) {
155 if ( status == -EAGAIN ) { 155 if ( status == -EAGAIN ) {
156 SDL_Delay(1);
156 continue; 157 continue;
157 } 158 }
158 if ( status == -ESTRPIPE ) { 159 if ( status == -ESTRPIPE ) {
159 do { 160 do {
161 SDL_Delay(1);
160 status = snd_pcm_resume(pcm_handle); 162 status = snd_pcm_resume(pcm_handle);
161 } while ( status == -EAGAIN ); 163 } while ( status == -EAGAIN );
162 } 164 }
163 if ( status < 0 ) { 165 if ( status < 0 ) {
164 status = snd_pcm_prepare(pcm_handle); 166 status = snd_pcm_prepare(pcm_handle);
314 memset(mixbuf, spec->silence, spec->size); 316 memset(mixbuf, spec->silence, spec->size);
315 317
316 /* Get the parent process id (we're the parent of the audio thread) */ 318 /* Get the parent process id (we're the parent of the audio thread) */
317 parent = getpid(); 319 parent = getpid();
318 320
321 /* Switch to blocking mode for playback */
322 snd_pcm_nonblock(pcm_handle, 0);
323
319 /* We're ready to rock and roll. :-) */ 324 /* We're ready to rock and roll. :-) */
320 return(0); 325 return(0);
321 } 326 }