comparison src/audio/alsa/SDL_alsa_audio.c @ 4360:be2dff2f79c0 SDL-1.2

Getting as close as you can with periods is better than the default parameters.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 19 Oct 2009 03:18:15 +0000
parents 4b582c04ec1d
children 991e4bcfc7a8
comparison
equal deleted inserted replaced
4359:4b582c04ec1d 4360:be2dff2f79c0
378 fprintf(stderr, "ALSA: period size = %ld, periods = %u, buffer size = %lu\n", persize, periods, bufsize); 378 fprintf(stderr, "ALSA: period size = %ld, periods = %u, buffer size = %lu\n", persize, periods, bufsize);
379 } 379 }
380 return(0); 380 return(0);
381 } 381 }
382 382
383 static int ALSA_set_period_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params) 383 static int ALSA_set_period_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params, int override)
384 { 384 {
385 const char *env; 385 const char *env;
386 int status; 386 int status;
387 int override = 0;
388 snd_pcm_hw_params_t *hwparams; 387 snd_pcm_hw_params_t *hwparams;
389 snd_pcm_uframes_t frames; 388 snd_pcm_uframes_t frames;
390 unsigned int periods; 389 unsigned int periods;
391 390
392 /* Copy the hardware parameters for this setup */ 391 /* Copy the hardware parameters for this setup */
393 snd_pcm_hw_params_alloca(&hwparams); 392 snd_pcm_hw_params_alloca(&hwparams);
394 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params); 393 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params);
395 394
396 env = getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE"); 395 if ( !override ) {
397 if ( env ) { 396 env = getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE");
398 override = SDL_atoi(env); 397 if ( env ) {
399 if ( override == 0 ) { 398 override = SDL_atoi(env);
400 return(-1); 399 if ( override == 0 ) {
400 return(-1);
401 }
401 } 402 }
402 } 403 }
403 404
404 frames = spec->samples; 405 frames = spec->samples;
405 status = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, &frames, NULL); 406 status = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, &frames, NULL);
414 } 415 }
415 416
416 return ALSA_finalize_hardware(this, spec, hwparams, override); 417 return ALSA_finalize_hardware(this, spec, hwparams, override);
417 } 418 }
418 419
419 static int ALSA_set_buffer_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params) 420 static int ALSA_set_buffer_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params, int override)
420 { 421 {
421 const char *env; 422 const char *env;
422 int status; 423 int status;
423 int override = 0;
424 snd_pcm_hw_params_t *hwparams; 424 snd_pcm_hw_params_t *hwparams;
425 snd_pcm_uframes_t frames; 425 snd_pcm_uframes_t frames;
426 426
427 /* Copy the hardware parameters for this setup */ 427 /* Copy the hardware parameters for this setup */
428 snd_pcm_hw_params_alloca(&hwparams); 428 snd_pcm_hw_params_alloca(&hwparams);
429 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params); 429 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params);
430 430
431 env = getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE"); 431 if ( !override ) {
432 if ( env ) { 432 env = getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE");
433 override = SDL_atoi(env); 433 if ( env ) {
434 if ( override == 0 ) { 434 override = SDL_atoi(env);
435 return(-1); 435 if ( override == 0 ) {
436 return(-1);
437 }
436 } 438 }
437 } 439 }
438 440
439 frames = spec->samples * 2; 441 frames = spec->samples * 2;
440 status = SDL_NAME(snd_pcm_hw_params_set_buffer_size_near)(pcm_handle, hwparams, &frames); 442 status = SDL_NAME(snd_pcm_hw_params_set_buffer_size_near)(pcm_handle, hwparams, &frames);
546 return(-1); 548 return(-1);
547 } 549 }
548 spec->freq = rate; 550 spec->freq = rate;
549 551
550 /* Set the buffer size, in samples */ 552 /* Set the buffer size, in samples */
551 if ( ALSA_set_period_size(this, spec, hwparams) < 0 && 553 if ( ALSA_set_period_size(this, spec, hwparams, 0) < 0 &&
552 ALSA_set_buffer_size(this, spec, hwparams) < 0 ) { 554 ALSA_set_buffer_size(this, spec, hwparams, 0) < 0 ) {
553 /* Failed to set buffer size, try to just use the defaults */ 555 /* Failed to set desired buffer size, do the best you can... */
554 if ( ALSA_finalize_hardware(this, spec, hwparams, 1) < 0 ) { 556 if ( ALSA_set_period_size(this, spec, hwparams, 1) < 0 ) {
555 SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status)); 557 SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status));
556 ALSA_CloseAudio(this); 558 ALSA_CloseAudio(this);
557 return(-1); 559 return(-1);
558 } 560 }
559 } 561 }