comparison src/audio/SDL_audio.c @ 1794:5605a9820134

Fixed bug #137 If SDL_OpenAudio() is passed zero for the desired format fields, the following environment variables will be used to fill them in: SDL_AUDIO_FREQUENCY SDL_AUDIO_FORMAT SDL_AUDIO_CHANNELS SDL_AUDIO_SAMPLES If an environment variable is not specified, it will be set to a reasonable default value.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 09 May 2006 08:52:54 +0000
parents 290b5baf2fca
children c121d94672cb c1b6bb5205f1
comparison
equal deleted inserted replaced
1793:4d66375c2012 1794:5605a9820134
281 { 281 {
282 if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) { 282 if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
283 return; 283 return;
284 } 284 }
285 SDL_mutexV(audio->mixer_lock); 285 SDL_mutexV(audio->mixer_lock);
286 }
287
288 static Uint16 SDL_ParseAudioFormat(const char *string)
289 {
290 Uint16 format = 0;
291
292 switch (*string) {
293 case 'U':
294 ++string;
295 format |= 0x0000;
296 break;
297 case 'S':
298 ++string;
299 format |= 0x8000;
300 break;
301 default:
302 return 0;
303 }
304 switch (SDL_atoi(string)) {
305 case 8:
306 string += 1;
307 format |= 8;
308 break;
309 case 16:
310 string += 2;
311 format |= 16;
312 if ( SDL_strcmp(string, "LSB") == 0
313 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
314 || SDL_strcmp(string, "SYS") == 0
315 #endif
316 ) {
317 format |= 0x0000;
318 }
319 if ( SDL_strcmp(string, "MSB") == 0
320 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
321 || SDL_strcmp(string, "SYS") == 0
322 #endif
323 ) {
324 format |= 0x1000;
325 }
326 break;
327 default:
328 return 0;
329 }
330 return format;
286 } 331 }
287 332
288 int SDL_AudioInit(const char *driver_name) 333 int SDL_AudioInit(const char *driver_name)
289 { 334 {
290 SDL_AudioDevice *audio; 335 SDL_AudioDevice *audio;
384 } 429 }
385 430
386 int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained) 431 int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
387 { 432 {
388 SDL_AudioDevice *audio; 433 SDL_AudioDevice *audio;
434 const char *env;
389 435
390 /* Start up the audio driver, if necessary */ 436 /* Start up the audio driver, if necessary */
391 if ( ! current_audio ) { 437 if ( ! current_audio ) {
392 if ( (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) || 438 if ( (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) ||
393 (current_audio == NULL) ) { 439 (current_audio == NULL) ) {
400 SDL_SetError("Audio device is already opened"); 446 SDL_SetError("Audio device is already opened");
401 return(-1); 447 return(-1);
402 } 448 }
403 449
404 /* Verify some parameters */ 450 /* Verify some parameters */
405 if ( desired->callback == NULL ) { 451 if ( desired->freq == 0 ) {
406 SDL_SetError("SDL_OpenAudio() passed a NULL callback"); 452 env = SDL_getenv("SDL_AUDIO_FREQUENCY");
407 return(-1); 453 if ( env ) {
454 desired->freq = SDL_atoi(env);
455 }
456 }
457 if ( desired->freq == 0 ) {
458 /* Pick some default audio frequency */
459 desired->freq = 22050;
460 }
461 if ( desired->format == 0 ) {
462 env = SDL_getenv("SDL_AUDIO_FORMAT");
463 if ( env ) {
464 desired->format = SDL_ParseAudioFormat(env);
465 }
466 }
467 if ( desired->format == 0 ) {
468 /* Pick some default audio format */
469 desired->format = AUDIO_S16;
470 }
471 if ( desired->channels == 0 ) {
472 env = SDL_getenv("SDL_AUDIO_CHANNELS");
473 if ( env ) {
474 desired->channels = SDL_atoi(env);
475 }
476 }
477 if ( desired->channels == 0 ) {
478 /* Pick a default number of channels */
479 desired->channels = 2;
408 } 480 }
409 switch ( desired->channels ) { 481 switch ( desired->channels ) {
410 case 1: /* Mono */ 482 case 1: /* Mono */
411 case 2: /* Stereo */ 483 case 2: /* Stereo */
412 case 4: /* surround */ 484 case 4: /* surround */
413 case 6: /* surround with center and lfe */ 485 case 6: /* surround with center and lfe */
414 break; 486 break;
415 default: 487 default:
416 SDL_SetError("1 (mono) and 2 (stereo) channels supported"); 488 SDL_SetError("1 (mono) and 2 (stereo) channels supported");
489 return(-1);
490 }
491 if ( desired->samples == 0 ) {
492 env = SDL_getenv("SDL_AUDIO_SAMPLES");
493 if ( env ) {
494 desired->samples = SDL_atoi(env);
495 }
496 }
497 if ( desired->samples == 0 ) {
498 /* Pick a default of ~46 ms at desired frequency */
499 int samples = (desired->freq / 1000) * 46;
500 int power2 = 1;
501 while ( power2 < samples ) {
502 power2 *= 2;
503 }
504 desired->samples = power2;
505 }
506 if ( desired->callback == NULL ) {
507 SDL_SetError("SDL_OpenAudio() passed a NULL callback");
417 return(-1); 508 return(-1);
418 } 509 }
419 510
420 #if defined(__MACOS__) || (defined(__RISCOS__) && SDL_THREADS_DISABLED) 511 #if defined(__MACOS__) || (defined(__RISCOS__) && SDL_THREADS_DISABLED)
421 /* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */ 512 /* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */