From 470d6140df6350c382a0eaebc9f6950dd29c9849 Mon Sep 17 00:00:00 2001 From: larsimmisch Date: Thu, 24 Jan 2008 11:31:57 +0000 Subject: [PATCH] Import from divmod. Original log message: r1322 | casper | 2005-03-30 22:44:55 +0200 (Wed, 30 Mar 2005) | 2 lines Fixed return values for set* functions git-svn-id: svn://svn.code.sf.net/p/pyalsaaudio/code/trunk@11 ec2f30ec-7544-0410-870e-f70ca00c83f0 --- alsaaudio.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/alsaaudio.c b/alsaaudio.c index 2dcf8f8..85e3509 100644 --- a/alsaaudio.c +++ b/alsaaudio.c @@ -74,6 +74,8 @@ static PyTypeObject ALSAPCMType; static int alsapcm_setup(alsapcm_t *self) { int res,dir; + unsigned int val; + snd_pcm_uframes_t frames; snd_pcm_hw_params_t *hwparams; if (self->handle) { @@ -83,12 +85,11 @@ static int alsapcm_setup(alsapcm_t *self) { res = snd_pcm_open(&(self->handle),self->cardname,self->pcmtype,self->pcmmode); if (res < 0) return res; + /* Allocate a hwparam structure, and fill it in with configuration space */ snd_pcm_hw_params_alloca(&hwparams); res = snd_pcm_hw_params_any(self->handle, hwparams); if (res < 0) return res; - snd_pcm_hw_params_alloca(&hwparams); - /* Fill it in with default values. */ snd_pcm_hw_params_any(self->handle, hwparams); snd_pcm_hw_params_set_access(self->handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); @@ -99,7 +100,18 @@ static int alsapcm_setup(alsapcm_t *self) { snd_pcm_hw_params_set_period_size(self->handle, hwparams, self->periodsize, dir); snd_pcm_hw_params_set_periods(self->handle,hwparams,4,0); + /* Write it to the device */ res = snd_pcm_hw_params(self->handle, hwparams); + if (res) return res; + + /* Query current settings. These may differ from the requested values, + which should therefore be sync'ed with actual values */ + snd_pcm_hw_params_current(self->handle,hwparams); + + snd_pcm_hw_params_get_format(hwparams,&val); self->format = val; + snd_pcm_hw_params_get_channels(hwparams,&val); self->channels = val; + snd_pcm_hw_params_get_rate(hwparams,&val,&dir); self->rate = val; + snd_pcm_hw_params_get_period_size(hwparams,&frames,&dir); self->periodsize = (int) frames; self->framesize = self->channels * snd_pcm_hw_params_get_sbits(hwparams)/8; return res; @@ -270,23 +282,14 @@ static PyObject * alsapcm_setchannels(alsapcm_t *self, PyObject *args) { int channels; int res; - unsigned int val; - snd_pcm_hw_params_t *hwparams; - snd_pcm_hw_params_alloca(&hwparams); - snd_pcm_hw_params_current(self->handle,hwparams); - if (!PyArg_ParseTuple(args,"i",&channels)) return NULL; + self->channels = channels; res = alsapcm_setup(self); if (res < 0) { PyErr_SetString(ALSAAudioError, snd_strerror(res)); return NULL; } - - snd_pcm_hw_params_get_channels(hwparams, &val); - - self->channels = val; - - return PyInt_FromLong(val); + return PyInt_FromLong(self->channels); } static PyObject * @@ -975,6 +978,7 @@ void initalsaaudio(void) { _EXPORT_INT(m,"PCM_PLAYBACK",SND_PCM_STREAM_PLAYBACK); _EXPORT_INT(m,"PCM_CAPTURE",SND_PCM_STREAM_CAPTURE); + _EXPORT_INT(m,"PCM_NORMAL",0); _EXPORT_INT(m,"PCM_NONBLOCK",SND_PCM_NONBLOCK); _EXPORT_INT(m,"PCM_ASYNC",SND_PCM_ASYNC);