comparison src/audio/alsa/SDL_alsa_audio.c @ 2060:866052b01ee5

indent is evil
author Sam Lantinga <slouken@libsdl.org>
date Sat, 28 Oct 2006 16:48:03 +0000
parents 5f6550e5184f
children e1da92da346c
comparison
equal deleted inserted replaced
2059:4685ccd33d0e 2060:866052b01ee5
41 41
42 /* The default ALSA audio driver */ 42 /* The default ALSA audio driver */
43 #define DEFAULT_DEVICE "default" 43 #define DEFAULT_DEVICE "default"
44 44
45 static int (*ALSA_snd_pcm_open) 45 static int (*ALSA_snd_pcm_open)
46 (snd_pcm_t **, const char *, snd_pcm_stream_t, int); 46 (snd_pcm_t **, const char *, snd_pcm_stream_t, int);
47 static int (*ALSA_snd_pcm_close)(snd_pcm_t * pcm); 47 static int (*ALSA_snd_pcm_close) (snd_pcm_t * pcm);
48 static snd_pcm_sframes_t(*ALSA_snd_pcm_writei) 48 static snd_pcm_sframes_t(*ALSA_snd_pcm_writei)
49 (snd_pcm_t *,const void *, snd_pcm_uframes_t); 49 (snd_pcm_t *, const void *, snd_pcm_uframes_t);
50 static int (*ALSA_snd_pcm_resume)(snd_pcm_t *); 50 static int (*ALSA_snd_pcm_resume) (snd_pcm_t *);
51 static int (*ALSA_snd_pcm_prepare)(snd_pcm_t *); 51 static int (*ALSA_snd_pcm_prepare) (snd_pcm_t *);
52 static int (*ALSA_snd_pcm_drain)(snd_pcm_t *); 52 static int (*ALSA_snd_pcm_drain) (snd_pcm_t *);
53 static const char *(*ALSA_snd_strerror)(int); 53 static const char *(*ALSA_snd_strerror) (int);
54 static size_t(*ALSA_snd_pcm_hw_params_sizeof)(void); 54 static size_t(*ALSA_snd_pcm_hw_params_sizeof) (void);
55 static size_t(*ALSA_snd_pcm_sw_params_sizeof)(void); 55 static size_t(*ALSA_snd_pcm_sw_params_sizeof) (void);
56 static int (*ALSA_snd_pcm_hw_params_any)(snd_pcm_t *, snd_pcm_hw_params_t *); 56 static int (*ALSA_snd_pcm_hw_params_any) (snd_pcm_t *, snd_pcm_hw_params_t *);
57 static int (*ALSA_snd_pcm_hw_params_set_access) 57 static int (*ALSA_snd_pcm_hw_params_set_access)
58 (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t); 58 (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t);
59 static int (*ALSA_snd_pcm_hw_params_set_format) 59 static int (*ALSA_snd_pcm_hw_params_set_format)
60 (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t); 60 (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t);
61 static int (*ALSA_snd_pcm_hw_params_set_channels) 61 static int (*ALSA_snd_pcm_hw_params_set_channels)
62 (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int); 62 (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int);
63 static int (*ALSA_snd_pcm_hw_params_get_channels)(const snd_pcm_hw_params_t *); 63 static int (*ALSA_snd_pcm_hw_params_get_channels) (const snd_pcm_hw_params_t
64 *);
64 static unsigned int (*ALSA_snd_pcm_hw_params_set_rate_near) 65 static unsigned int (*ALSA_snd_pcm_hw_params_set_rate_near)
65 (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int, int *); 66 (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int, int *);
66 static snd_pcm_uframes_t (*ALSA_snd_pcm_hw_params_set_period_size_near) 67 static snd_pcm_uframes_t(*ALSA_snd_pcm_hw_params_set_period_size_near)
67 (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t, int *); 68 (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t, int *);
68 static snd_pcm_sframes_t (*ALSA_snd_pcm_hw_params_get_period_size) 69 static snd_pcm_sframes_t(*ALSA_snd_pcm_hw_params_get_period_size)
69 (const snd_pcm_hw_params_t *); 70 (const snd_pcm_hw_params_t *);
70 static unsigned int (*ALSA_snd_pcm_hw_params_set_periods_near) 71 static unsigned int (*ALSA_snd_pcm_hw_params_set_periods_near)
71 (snd_pcm_t *,snd_pcm_hw_params_t *, unsigned int, int *); 72 (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int, int *);
72 static int (*ALSA_snd_pcm_hw_params_get_periods)(snd_pcm_hw_params_t *); 73 static int (*ALSA_snd_pcm_hw_params_get_periods) (snd_pcm_hw_params_t *);
73 static int (*ALSA_snd_pcm_hw_params)(snd_pcm_t *, snd_pcm_hw_params_t *); 74 static int (*ALSA_snd_pcm_hw_params) (snd_pcm_t *, snd_pcm_hw_params_t *);
74 static int (*ALSA_snd_pcm_sw_params_current)(snd_pcm_t*, snd_pcm_sw_params_t*); 75 static int (*ALSA_snd_pcm_sw_params_current) (snd_pcm_t *,
76 snd_pcm_sw_params_t *);
75 static int (*ALSA_snd_pcm_sw_params_set_start_threshold) 77 static int (*ALSA_snd_pcm_sw_params_set_start_threshold)
76 (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t); 78 (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
77 static int (*ALSA_snd_pcm_sw_params_set_avail_min) 79 static int (*ALSA_snd_pcm_sw_params_set_avail_min)
78 (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t); 80 (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
79 static int (*ALSA_snd_pcm_sw_params)(snd_pcm_t *, snd_pcm_sw_params_t *); 81 static int (*ALSA_snd_pcm_sw_params) (snd_pcm_t *, snd_pcm_sw_params_t *);
80 static int (*ALSA_snd_pcm_nonblock)(snd_pcm_t *, int); 82 static int (*ALSA_snd_pcm_nonblock) (snd_pcm_t *, int);
81 #define snd_pcm_hw_params_sizeof ALSA_snd_pcm_hw_params_sizeof 83 #define snd_pcm_hw_params_sizeof ALSA_snd_pcm_hw_params_sizeof
82 #define snd_pcm_sw_params_sizeof ALSA_snd_pcm_sw_params_sizeof 84 #define snd_pcm_sw_params_sizeof ALSA_snd_pcm_sw_params_sizeof
83 85
84 86
85 #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC 87 #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
116 if (!load_alsa_sym(#x, (void **) (char *) &ALSA_##x)) return -1 118 if (!load_alsa_sym(#x, (void **) (char *) &ALSA_##x)) return -1
117 #else 119 #else
118 #define SDL_ALSA_SYM(x) ALSA_##x = x 120 #define SDL_ALSA_SYM(x) ALSA_##x = x
119 #endif 121 #endif
120 122
121 static int load_alsa_syms(void) 123 static int
124 load_alsa_syms(void)
122 { 125 {
123 SDL_ALSA_SYM(snd_pcm_open); 126 SDL_ALSA_SYM(snd_pcm_open);
124 SDL_ALSA_SYM(snd_pcm_close); 127 SDL_ALSA_SYM(snd_pcm_close);
125 SDL_ALSA_SYM(snd_pcm_writei); 128 SDL_ALSA_SYM(snd_pcm_writei);
126 SDL_ALSA_SYM(snd_pcm_resume); 129 SDL_ALSA_SYM(snd_pcm_resume);
145 SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min); 148 SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min);
146 SDL_ALSA_SYM(snd_pcm_sw_params); 149 SDL_ALSA_SYM(snd_pcm_sw_params);
147 SDL_ALSA_SYM(snd_pcm_nonblock); 150 SDL_ALSA_SYM(snd_pcm_nonblock);
148 return 0; 151 return 0;
149 } 152 }
153
150 #undef SDL_ALSA_SYM 154 #undef SDL_ALSA_SYM
151 155
152 #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC 156 #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
153 157
154 static void 158 static void
167 if (alsa_handle == NULL) { 171 if (alsa_handle == NULL) {
168 alsa_handle = dlopen(alsa_library, RTLD_NOW); 172 alsa_handle = dlopen(alsa_library, RTLD_NOW);
169 if (alsa_handle == NULL) { 173 if (alsa_handle == NULL) {
170 retval = -1; 174 retval = -1;
171 SDL_SetError("ALSA: dlopen('%s') failed: %s\n", 175 SDL_SetError("ALSA: dlopen('%s') failed: %s\n",
172 alsa_library, strerror(errno)); 176 alsa_library, strerror(errno));
173 } else { 177 } else {
174 retval = load_alsa_syms(); 178 retval = load_alsa_syms();
175 if (retval < 0) { 179 if (retval < 0) {
176 UnloadALSALibrary(); 180 UnloadALSALibrary();
177 } 181 }
374 snd_pcm_uframes_t frames = 0; 378 snd_pcm_uframes_t frames = 0;
375 SDL_AudioFormat test_format = 0; 379 SDL_AudioFormat test_format = 0;
376 380
377 /* Initialize all variables that we clean on shutdown */ 381 /* Initialize all variables that we clean on shutdown */
378 this->hidden = (struct SDL_PrivateAudioData *) 382 this->hidden = (struct SDL_PrivateAudioData *)
379 SDL_malloc((sizeof *this->hidden)); 383 SDL_malloc((sizeof *this->hidden));
380 if (this->hidden == NULL) { 384 if (this->hidden == NULL) {
381 SDL_OutOfMemory(); 385 SDL_OutOfMemory();
382 return 0; 386 return 0;
383 } 387 }
384 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 388 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
420 424
421 /* Try for a closest match on audio format */ 425 /* Try for a closest match on audio format */
422 status = -1; 426 status = -1;
423 for (test_format = SDL_FirstAudioFormat(this->spec.format); 427 for (test_format = SDL_FirstAudioFormat(this->spec.format);
424 test_format && (status < 0);) { 428 test_format && (status < 0);) {
425 status = 0; /* if we can't support a format, it'll become -1. */ 429 status = 0; /* if we can't support a format, it'll become -1. */
426 switch (test_format) { 430 switch (test_format) {
427 case AUDIO_U8: 431 case AUDIO_U8:
428 format = SND_PCM_FORMAT_U8; 432 format = SND_PCM_FORMAT_U8;
429 break; 433 break;
430 case AUDIO_S8: 434 case AUDIO_S8:
510 ALSA_CloseDevice(this); 514 ALSA_CloseDevice(this);
511 SDL_SetError("ALSA: Couldn't set hardware audio parameters: %s", 515 SDL_SetError("ALSA: Couldn't set hardware audio parameters: %s",
512 ALSA_snd_strerror(status)); 516 ALSA_snd_strerror(status));
513 return 0; 517 return 0;
514 } 518 }
515
516 #if AUDIO_DEBUG 519 #if AUDIO_DEBUG
517 { 520 {
518 snd_pcm_sframes_t bufsize; 521 snd_pcm_sframes_t bufsize;
519 int fragments; 522 int fragments;
520 bufsize = ALSA_snd_pcm_hw_params_get_period_size(hwparams); 523 bufsize = ALSA_snd_pcm_hw_params_get_period_size(hwparams);
521 fragments = ALSA_snd_pcm_hw_params_get_periods(hwparams); 524 fragments = ALSA_snd_pcm_hw_params_get_periods(hwparams);
522 fprintf(stderr,"ALSA: bufsize = %ld, fragments = %d\n",bufsize,fragments); 525 fprintf(stderr, "ALSA: bufsize = %ld, fragments = %d\n", bufsize,
523 } 526 fragments);
527 }
524 #endif 528 #endif
525 529
526 /* Set the software parameters */ 530 /* Set the software parameters */
527 snd_pcm_sw_params_alloca(&swparams); 531 snd_pcm_sw_params_alloca(&swparams);
528 status = ALSA_snd_pcm_sw_params_current(pcm_handle, swparams); 532 status = ALSA_snd_pcm_sw_params_current(pcm_handle, swparams);
530 ALSA_CloseDevice(this); 534 ALSA_CloseDevice(this);
531 SDL_SetError("ALSA: Couldn't get software config: %s", 535 SDL_SetError("ALSA: Couldn't get software config: %s",
532 ALSA_snd_strerror(status)); 536 ALSA_snd_strerror(status));
533 return 0; 537 return 0;
534 } 538 }
535 status = ALSA_snd_pcm_sw_params_set_start_threshold(pcm_handle,swparams,0); 539 status =
540 ALSA_snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 0);
536 if (status < 0) { 541 if (status < 0) {
537 ALSA_CloseDevice(this); 542 ALSA_CloseDevice(this);
538 SDL_SetError("ALSA: Couldn't set start threshold: %s", 543 SDL_SetError("ALSA: Couldn't set start threshold: %s",
539 ALSA_snd_strerror(status)); 544 ALSA_snd_strerror(status));
540 return 0; 545 return 0;
541 } 546 }
542 status = ALSA_snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, frames); 547 status =
548 ALSA_snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, frames);
543 if (status < 0) { 549 if (status < 0) {
544 ALSA_CloseDevice(this); 550 ALSA_CloseDevice(this);
545 SDL_SetError("Couldn't set avail min: %s", ALSA_snd_strerror(status)); 551 SDL_SetError("Couldn't set avail min: %s", ALSA_snd_strerror(status));
546 return 0; 552 return 0;
547 } 553 }
581 { 587 {
582 UnloadALSALibrary(); 588 UnloadALSALibrary();
583 } 589 }
584 590
585 static int 591 static int
586 ALSA_Init(SDL_AudioDriverImpl *impl) 592 ALSA_Init(SDL_AudioDriverImpl * impl)
587 { 593 {
588 if (LoadALSALibrary() < 0) { 594 if (LoadALSALibrary() < 0) {
589 return 0; 595 return 0;
590 } 596 }
591 597
594 impl->WaitDevice = ALSA_WaitDevice; 600 impl->WaitDevice = ALSA_WaitDevice;
595 impl->GetDeviceBuf = ALSA_GetDeviceBuf; 601 impl->GetDeviceBuf = ALSA_GetDeviceBuf;
596 impl->PlayDevice = ALSA_PlayDevice; 602 impl->PlayDevice = ALSA_PlayDevice;
597 impl->CloseDevice = ALSA_CloseDevice; 603 impl->CloseDevice = ALSA_CloseDevice;
598 impl->Deinitialize = ALSA_Deinitialize; 604 impl->Deinitialize = ALSA_Deinitialize;
599 impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME: Add device enum! */ 605 impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME: Add device enum! */
600 606
601 return 1; 607 return 1;
602 } 608 }
603 609
604 610