add PCM.state() and associated enum values

in principle, the state is already available from info(), but that's a
rather heavy function for something one might want to query often.

a practical use case might be checking whether a playback stream is done
draining, for example.
This commit is contained in:
Oswald Buddenhagen
2023-02-19 18:53:15 +01:00
parent 574f78939d
commit 946694d263
2 changed files with 41 additions and 0 deletions

View File

@@ -840,6 +840,21 @@ alsa_asoundlib_version(PyObject * module, PyObject *args)
return PyUnicode_FromString(snd_asoundlib_version());
}
static PyObject *
alsapcm_state(alsapcm_t *self, PyObject *args)
{
if (!PyArg_ParseTuple(args,":state"))
return NULL;
if (!self->handle)
{
PyErr_SetString(ALSAAudioError, "PCM device is closed");
return NULL;
}
return PyLong_FromUnsignedLong((unsigned long) snd_pcm_state(self->handle));
}
static PyObject *
alsapcm_htimestamp(alsapcm_t *self, PyObject *args)
{
@@ -1605,6 +1620,7 @@ static PyMethodDef alsapcm_methods[] = {
{"get_tstamp_mode", (PyCFunction) alsapcm_get_tstamp_mode, METH_VARARGS},
{"dumpinfo", (PyCFunction)alsapcm_dumpinfo, METH_VARARGS},
{"info", (PyCFunction)alsapcm_info, METH_VARARGS},
{"state", (PyCFunction)alsapcm_state, METH_VARARGS},
{"getformats", (PyCFunction)alsapcm_getformats, METH_VARARGS},
{"getratebounds", (PyCFunction)alsapcm_getratemaxmin, METH_VARARGS},
{"getrates", (PyCFunction)alsapcm_getrates, METH_VARARGS},
@@ -3054,6 +3070,16 @@ PyObject *PyInit_alsaaudio(void)
_EXPORT_INT(m, "PCM_FORMAT_DSD_U32_BE", SND_PCM_FORMAT_DSD_U32_BE);
#endif
_EXPORT_INT(m, "PCM_STATE_OPEN", SND_PCM_STATE_OPEN);
_EXPORT_INT(m, "PCM_STATE_SETUP", SND_PCM_STATE_SETUP);
_EXPORT_INT(m, "PCM_STATE_PREPARED", SND_PCM_STATE_PREPARED);
_EXPORT_INT(m, "PCM_STATE_RUNNING", SND_PCM_STATE_RUNNING);
_EXPORT_INT(m, "PCM_STATE_XRUN", SND_PCM_STATE_XRUN);
_EXPORT_INT(m, "PCM_STATE_DRAINING", SND_PCM_STATE_DRAINING);
_EXPORT_INT(m, "PCM_STATE_PAUSED", SND_PCM_STATE_PAUSED);
_EXPORT_INT(m, "PCM_STATE_SUSPENDED", SND_PCM_STATE_SUSPENDED);
_EXPORT_INT(m, "PCM_STATE_DISCONNECTED", SND_PCM_STATE_DISCONNECTED);
/* Mixer stuff */
_EXPORT_INT(m, "MIXER_CHANNEL_ALL", MIXER_CHANNEL_ALL);

View File

@@ -297,6 +297,21 @@ PCM objects have the following methods:
Dumps the PCM object's configured parameters to stdout.
.. method:: PCM.state()
Returs the current state of the stream, which can be one of
:const:`PCM_STATE_OPEN` (this should not actually happen),
:const:`PCM_STATE_SETUP` (after :func:`drop` or :func:`drain`),
:const:`PCM_STATE_PREPARED` (after construction),
:const:`PCM_STATE_RUNNING`,
:const:`PCM_STATE_XRUN`,
:const:`PCM_STATE_DRAINING`,
:const:`PCM_STATE_PAUSED`,
:const:`PCM_STATE_SUSPENDED`, and
:const:`PCM_STATE_DISCONNECTED`.
*New in 0.10*
.. method:: PCM.read()
In :const:`PCM_NORMAL` mode, this function blocks until a full period is