Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() re…
Browse files Browse the repository at this point in the history
…moval.
  • Loading branch information
icculus committed Oct 10, 2009
1 parent 7b7aa70 commit c2530cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
77 changes: 40 additions & 37 deletions src/audio/alsa/SDL_alsa_audio.c
Expand Up @@ -25,7 +25,6 @@

#include <sys/types.h>
#include <signal.h> /* For kill() */
#include <dlfcn.h>
#include <errno.h>
#include <string.h>

Expand All @@ -35,6 +34,9 @@
#include "../SDL_audio_c.h"
#include "SDL_alsa_audio.h"

#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
#include "SDL_loadso.h"
#endif

/* The tag name used by ALSA audio */
#define DRIVER_NAME "alsa"
Expand All @@ -60,16 +62,16 @@ static int (*ALSA_snd_pcm_hw_params_set_format)
(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t);
static int (*ALSA_snd_pcm_hw_params_set_channels)
(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int);
static int (*ALSA_snd_pcm_hw_params_get_channels) (const snd_pcm_hw_params_t
*);
static unsigned int (*ALSA_snd_pcm_hw_params_set_rate_near)
(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int, int *);
static snd_pcm_uframes_t(*ALSA_snd_pcm_hw_params_set_period_size_near)
(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t, int *);
static int (*ALSA_snd_pcm_hw_params_set_rate_near)
(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
static int (*ALSA_snd_pcm_hw_params_set_period_size_near)
(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
static int (*ALSA_snd_pcm_hw_params_get_channels)
(const snd_pcm_hw_params_t *, unsigned int *);
static snd_pcm_sframes_t(*ALSA_snd_pcm_hw_params_get_period_size)
(const snd_pcm_hw_params_t *);
static unsigned int (*ALSA_snd_pcm_hw_params_set_periods_near)
(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int, int *);
static int (*ALSA_snd_pcm_hw_params_set_periods_near)
(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
static int (*ALSA_snd_pcm_hw_params_get_periods) (snd_pcm_hw_params_t *);
static int (*ALSA_snd_pcm_hw_params) (snd_pcm_t *, snd_pcm_hw_params_t *);
static int (*ALSA_snd_pcm_sw_params_current) (snd_pcm_t *,
Expand All @@ -92,22 +94,10 @@ static void *alsa_handle = NULL;
static int
load_alsa_sym(const char *fn, void **addr)
{
/*
* !!! FIXME:
* Eventually, this will deal with fallbacks, version changes, and
* missing symbols we can workaround. But for now, it doesn't.
*/

#if HAVE_DLVSYM
*addr = dlvsym(alsa_handle, fn, "ALSA_0.9");
if (*addr == NULL)
#endif
{
*addr = dlsym(alsa_handle, fn);
if (*addr == NULL) {
SDL_SetError("dlsym('%s') failed: %s", fn, strerror(errno));
return 0;
}
*addr = SDL_LoadFunction(alsa_handle, fn);
if (*addr == NULL) {
/* Don't call SDL_SetError(): SDL_LoadFunction already did. */
return 0;
}

return 1;
Expand Down Expand Up @@ -159,7 +149,7 @@ static void
UnloadALSALibrary(void)
{
if (alsa_handle != NULL) {
dlclose(alsa_handle);
SDL_UnloadObject(alsa_handle);
alsa_handle = NULL;
}
}
Expand All @@ -169,11 +159,10 @@ LoadALSALibrary(void)
{
int retval = 0;
if (alsa_handle == NULL) {
alsa_handle = dlopen(alsa_library, RTLD_NOW);
alsa_handle = SDL_LoadObject(alsa_library);
if (alsa_handle == NULL) {
retval = -1;
SDL_SetError("ALSA: dlopen('%s') failed: %s\n",
alsa_library, strerror(errno));
/* Don't call SDL_SetError(): SDL_LoadObject already did. */
} else {
retval = load_alsa_syms();
if (retval < 0) {
Expand Down Expand Up @@ -380,6 +369,9 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
snd_pcm_format_t format = 0;
snd_pcm_uframes_t frames = 0;
SDL_AudioFormat test_format = 0;
unsigned int rate = 0;
unsigned int periods = 0;
unsigned int channels = 0;

/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
Expand Down Expand Up @@ -483,33 +475,44 @@ ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
/* Set the number of channels */
status = ALSA_snd_pcm_hw_params_set_channels(pcm_handle, hwparams,
this->spec.channels);
channels = this->spec.channels;
if (status < 0) {
status = ALSA_snd_pcm_hw_params_get_channels(hwparams);
if ((status <= 0) || (status > 2)) {
status = ALSA_snd_pcm_hw_params_get_channels(hwparams, &channels);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't set audio channels");
return 0;
}
this->spec.channels = status;
this->spec.channels = channels;
}

/* Set the audio rate */
rate = this->spec.freq;
status = ALSA_snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams,
this->spec.freq, NULL);
&rate, NULL);
if (status < 0) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't set audio frequency: %s",
ALSA_snd_strerror(status));
return 0;
}
this->spec.freq = status;
this->spec.freq = rate;

/* Set the buffer size, in samples */
frames = this->spec.samples;
frames = ALSA_snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams,
frames, NULL);
status = ALSA_snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams,
&frames, NULL);
if ( status < 0 ) {
ALSA_CloseDevice(this);
SDL_SetError("ALSA: Couldn't set audio frequency: %s",
ALSA_snd_strerror(status));
return(-1);
}
this->spec.samples = frames;
ALSA_snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, 2, NULL);

periods = 2;
ALSA_snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams,
&periods, NULL);

/* "set" the hardware with the desired parameters */
status = ALSA_snd_pcm_hw_params(pcm_handle, hwparams);
Expand Down
2 changes: 0 additions & 2 deletions src/audio/alsa/SDL_alsa_audio.h
Expand Up @@ -24,8 +24,6 @@
#ifndef _ALSA_PCM_audio_h
#define _ALSA_PCM_audio_h

#define ALSA_PCM_OLD_HW_PARAMS_API
#define ALSA_PCM_OLD_SW_PARAMS_API
#include <alsa/asoundlib.h>

#include "../SDL_sysaudio.h"
Expand Down

0 comments on commit c2530cb

Please sign in to comment.