comparison src/audio/dsp/SDL_dspaudio.c @ 1037:c5dedfdb4e42

Date: Tue, 01 Feb 2005 17:53:07 -0800 From: Bill May Subject: [SDL] Diffs for dsp audio in case of failures. The rewrite recently done for 1.2.8 forgot to handle error cases by closing the audio. Here is a patch that does.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 12 Feb 2005 19:39:08 +0000
parents 4675910b0b7b
children 644b39bf7253
comparison
equal deleted inserted replaced
1036:50f4e7e4d117 1037:c5dedfdb4e42
185 { long flags; 185 { long flags;
186 flags = fcntl(audio_fd, F_GETFL); 186 flags = fcntl(audio_fd, F_GETFL);
187 flags &= ~O_NONBLOCK; 187 flags &= ~O_NONBLOCK;
188 if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) { 188 if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) {
189 SDL_SetError("Couldn't set audio blocking mode"); 189 SDL_SetError("Couldn't set audio blocking mode");
190 DSP_CloseAudio(this);
190 return(-1); 191 return(-1);
191 } 192 }
192 } 193 }
193 194
194 /* Get a list of supported hardware formats */ 195 /* Get a list of supported hardware formats */
195 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { 196 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
196 perror("SNDCTL_DSP_GETFMTS"); 197 perror("SNDCTL_DSP_GETFMTS");
197 SDL_SetError("Couldn't get audio format list"); 198 SDL_SetError("Couldn't get audio format list");
199 DSP_CloseAudio(this);
198 return(-1); 200 return(-1);
199 } 201 }
200 202
201 /* Try for a closest match on audio format */ 203 /* Try for a closest match on audio format */
202 format = 0; 204 format = 0;
250 test_format = SDL_NextAudioFormat(); 252 test_format = SDL_NextAudioFormat();
251 } 253 }
252 } 254 }
253 if ( format == 0 ) { 255 if ( format == 0 ) {
254 SDL_SetError("Couldn't find any hardware audio formats"); 256 SDL_SetError("Couldn't find any hardware audio formats");
257 DSP_CloseAudio(this);
255 return(-1); 258 return(-1);
256 } 259 }
257 spec->format = test_format; 260 spec->format = test_format;
258 261
259 /* Set the audio format */ 262 /* Set the audio format */
260 value = format; 263 value = format;
261 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || 264 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
262 (value != format) ) { 265 (value != format) ) {
263 perror("SNDCTL_DSP_SETFMT"); 266 perror("SNDCTL_DSP_SETFMT");
264 SDL_SetError("Couldn't set audio format"); 267 SDL_SetError("Couldn't set audio format");
268 DSP_CloseAudio(this);
265 return(-1); 269 return(-1);
266 } 270 }
267 271
268 /* Set the number of channels of output */ 272 /* Set the number of channels of output */
269 value = spec->channels; 273 value = spec->channels;
270 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) { 274 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) {
271 perror("SNDCTL_DSP_CHANNELS"); 275 perror("SNDCTL_DSP_CHANNELS");
272 SDL_SetError("Cannot set the number of channels"); 276 SDL_SetError("Cannot set the number of channels");
277 DSP_CloseAudio(this);
273 return(-1); 278 return(-1);
274 } 279 }
275 spec->channels = value; 280 spec->channels = value;
276 281
277 /* Set the DSP frequency */ 282 /* Set the DSP frequency */
278 value = spec->freq; 283 value = spec->freq;
279 if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) { 284 if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) {
280 perror("SNDCTL_DSP_SPEED"); 285 perror("SNDCTL_DSP_SPEED");
281 SDL_SetError("Couldn't set audio frequency"); 286 SDL_SetError("Couldn't set audio frequency");
287 DSP_CloseAudio(this);
282 return(-1); 288 return(-1);
283 } 289 }
284 spec->freq = value; 290 spec->freq = value;
285 291
286 /* Calculate the final parameters for this audio specification */ 292 /* Calculate the final parameters for this audio specification */
288 294
289 /* Determine the power of two of the fragment size */ 295 /* Determine the power of two of the fragment size */
290 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); 296 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
291 if ( (0x01<<frag_spec) != spec->size ) { 297 if ( (0x01<<frag_spec) != spec->size ) {
292 SDL_SetError("Fragment size must be a power of two"); 298 SDL_SetError("Fragment size must be a power of two");
299 DSP_CloseAudio(this);
293 return(-1); 300 return(-1);
294 } 301 }
295 frag_spec |= 0x00020000; /* two fragments, for low latency */ 302 frag_spec |= 0x00020000; /* two fragments, for low latency */
296 303
297 /* Set the audio buffering parameters */ 304 /* Set the audio buffering parameters */
315 322
316 /* Allocate mixing buffer */ 323 /* Allocate mixing buffer */
317 mixlen = spec->size; 324 mixlen = spec->size;
318 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); 325 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
319 if ( mixbuf == NULL ) { 326 if ( mixbuf == NULL ) {
327 DSP_CloseAudio(this);
320 return(-1); 328 return(-1);
321 } 329 }
322 memset(mixbuf, spec->silence, spec->size); 330 memset(mixbuf, spec->silence, spec->size);
323 331
324 /* Get the parent process id (we're the parent of the audio thread) */ 332 /* Get the parent process id (we're the parent of the audio thread) */