diff options
Diffstat (limited to 'Modules/linuxaudiodev.c')
-rw-r--r-- | Modules/linuxaudiodev.c | 215 |
1 files changed, 129 insertions, 86 deletions
diff --git a/Modules/linuxaudiodev.c b/Modules/linuxaudiodev.c index 5b18f16..0661fb7 100644 --- a/Modules/linuxaudiodev.c +++ b/Modules/linuxaudiodev.c @@ -24,8 +24,12 @@ #ifdef HAVE_FCNTL_H #include <fcntl.h> +#else +#define O_RDONLY 00 +#define O_WRONLY 01 #endif + #include <sys/ioctl.h> #if defined(linux) #include <linux/soundcard.h> @@ -44,24 +48,32 @@ typedef unsigned long uint32_t; typedef struct { PyObject_HEAD; int x_fd; /* The open file */ + int x_mode; /* file mode */ int x_icount; /* Input count */ int x_ocount; /* Output count */ - uint32_t x_afmts; /* Supported audio formats */ + uint32_t x_afmts; /* Audio formats supported by hardware*/ } lad_t; +/* XXX several format defined in soundcard.h are not supported, + including _NE (native endian) options and S32 options +*/ + static struct { int a_bps; uint32_t a_fmt; + char *a_name; } audio_types[] = { - { 8, AFMT_MU_LAW }, - { 8, AFMT_U8 }, - { 8, AFMT_S8 }, - { 16, AFMT_U16_BE }, - { 16, AFMT_U16_LE }, - { 16, AFMT_S16_BE }, - { 16, AFMT_S16_LE }, + { 8, AFMT_MU_LAW, "Logarithmic mu-law audio" }, + { 8, AFMT_A_LAW, "Logarithmic A-law audio" }, + { 8, AFMT_U8, "Standard unsigned 8-bit audio" }, + { 8, AFMT_S8, "Standard signed 8-bit audio" }, + { 16, AFMT_U16_BE, "Big-endian 16-bit unsigned format" }, + { 16, AFMT_U16_LE, "Little-endian 16-bit unsigned format" }, + { 16, AFMT_S16_BE, "Big-endian 16-bit signed format" }, + { 16, AFMT_S16_LE, "Little-endian 16-bit signed format" }, }; +static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]); staticforward PyTypeObject Ladtype; @@ -78,31 +90,36 @@ newladobject(PyObject *arg) /* Check arg for r/w/rw */ if (!PyArg_ParseTuple(arg, "s:open", &mode)) return NULL; if (strcmp(mode, "r") == 0) - imode = 0; + imode = O_RDONLY; else if (strcmp(mode, "w") == 0) - imode = 1; + imode = O_WRONLY; else { PyErr_SetString(LinuxAudioError, "Mode should be one of 'r', or 'w'"); return NULL; } /* Open the correct device. The base device name comes from the - * AUDIODEV environment variable first, then /dev/audio. The + * AUDIODEV environment variable first, then /dev/dsp. The * control device tacks "ctl" onto the base device name. + * + * Note that the only difference between /dev/audio and /dev/dsp + * is that the former uses logarithmic mu-law encoding and the + * latter uses 8-bit unsigned encoding. */ + basedev = getenv("AUDIODEV"); if (!basedev) basedev = "/dev/dsp"; - if ((fd = open(basedev, imode)) < 0) { + if ((fd = open(basedev, imode)) == -1) { PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); return NULL; } - if (imode && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) < 0) { + if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) { PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); return NULL; } - if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) < 0) { + if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) { PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev); return NULL; } @@ -111,7 +128,8 @@ newladobject(PyObject *arg) close(fd); return NULL; } - xp->x_fd = fd; + xp->x_fd = fd; + xp->x_mode = imode; xp->x_icount = xp->x_ocount = 0; xp->x_afmts = afmts; return xp; @@ -138,17 +156,15 @@ lad_read(lad_t *self, PyObject *args) rv = PyString_FromStringAndSize(NULL, size); if (rv == NULL) return NULL; - - if (!(cp = PyString_AsString(rv))) { - Py_DECREF(rv); - return NULL; - } + cp = PyString_AS_STRING(rv); if ((count = read(self->x_fd, cp, size)) < 0) { PyErr_SetFromErrno(LinuxAudioError); Py_DECREF(rv); return NULL; } self->x_icount += count; + if (_PyString_Resize(&rv, count) == -1) + return NULL; return rv; } @@ -158,16 +174,17 @@ lad_write(lad_t *self, PyObject *args) char *cp; int rv, size; - if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) return NULL; + if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) + return NULL; while (size > 0) { - if ((rv = write(self->x_fd, cp, size)) < 0) { + if ((rv = write(self->x_fd, cp, size)) == -1) { PyErr_SetFromErrno(LinuxAudioError); return NULL; } self->x_ocount += rv; - size -= rv; - cp += rv; + size -= rv; + cp += rv; } Py_INCREF(Py_None); return Py_None; @@ -176,7 +193,9 @@ lad_write(lad_t *self, PyObject *args) static PyObject * lad_close(lad_t *self, PyObject *args) { - if (!PyArg_ParseTuple(args, ":close")) return NULL; + if (!PyArg_ParseTuple(args, ":close")) + return NULL; + if (self->x_fd >= 0) { close(self->x_fd); self->x_fd = -1; @@ -188,50 +207,73 @@ lad_close(lad_t *self, PyObject *args) static PyObject * lad_fileno(lad_t *self, PyObject *args) { - if (!PyArg_ParseTuple(args, ":fileno")) return NULL; + if (!PyArg_ParseTuple(args, ":fileno")) + return NULL; return PyInt_FromLong(self->x_fd); } static PyObject * lad_setparameters(lad_t *self, PyObject *args) { - int rate, ssize, nchannels, stereo, n, fmt; + int rate, ssize, nchannels, n, fmt, emulate=0; - if (!PyArg_ParseTuple(args, "iiii:setparameters", - &rate, &ssize, &nchannels, &fmt)) + if (!PyArg_ParseTuple(args, "iiii|i:setparameters", + &rate, &ssize, &nchannels, &fmt, &emulate)) return NULL; - if (rate < 0 || ssize < 0 || (nchannels != 1 && nchannels != 2)) { - PyErr_SetFromErrno(LinuxAudioError); - return NULL; + if (rate < 0) { + PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d", + rate); + return NULL; } - if (ioctl(self->x_fd, SOUND_PCM_WRITE_RATE, &rate) < 0) { - PyErr_SetFromErrno(LinuxAudioError); - return NULL; + if (ssize < 0) { + PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d", + ssize); + return NULL; + } + if (nchannels != 1 && nchannels != 2) { + PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d", + nchannels); + return NULL; } - if (ioctl(self->x_fd, SNDCTL_DSP_SAMPLESIZE, &ssize) < 0) { + + if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) { PyErr_SetFromErrno(LinuxAudioError); return NULL; } - stereo = (nchannels == 1)? 0: (nchannels == 2)? 1: -1; - if (ioctl(self->x_fd, SNDCTL_DSP_STEREO, &stereo) < 0) { + if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) { PyErr_SetFromErrno(LinuxAudioError); return NULL; } - for (n = 0; n != sizeof(audio_types) / sizeof(audio_types[0]); n++) + + for (n = 0; n < n_audio_types; n++) if (fmt == audio_types[n].a_fmt) break; + if (n == n_audio_types) { + PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt); + return NULL; + } + if (audio_types[n].a_bps != ssize) { + PyErr_Format(PyExc_ValueError, + "sample size %d expected for %s: %d received", + audio_types[n].a_bps, audio_types[n].a_name, ssize); + return NULL; + } - if (n == sizeof(audio_types) / sizeof(audio_types[0]) || - audio_types[n].a_bps != ssize || - (self->x_afmts & audio_types[n].a_fmt) == 0) { - PyErr_SetFromErrno(LinuxAudioError); - return NULL; + if (emulate == 0) { + if ((self->x_afmts & audio_types[n].a_fmt) == 0) { + PyErr_Format(PyExc_ValueError, + "format not supported by device: %s", + audio_types[n].a_name); + return NULL; + } } - if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &audio_types[n].a_fmt) < 0) { + if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, + &audio_types[n].a_fmt) == -1) { PyErr_SetFromErrno(LinuxAudioError); return NULL; } + Py_INCREF(Py_None); return Py_None; } @@ -342,7 +384,7 @@ lad_flush(lad_t *self, PyObject *args) { if (!PyArg_ParseTuple(args, ":flush")) return NULL; - if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) < 0) { + if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) { PyErr_SetFromErrno(LinuxAudioError); return NULL; } @@ -350,6 +392,26 @@ lad_flush(lad_t *self, PyObject *args) return Py_None; } +static PyObject * +lad_getptr(lad_t *self, PyObject *args) +{ + count_info info; + int req; + + if (!PyArg_ParseTuple(args, ":getptr")) + return NULL; + + if (self->x_mode == O_RDONLY) + req = SNDCTL_DSP_GETIPTR; + else + req = SNDCTL_DSP_GETOPTR; + if (ioctl(self->x_fd, req, &info) == -1) { + PyErr_SetFromErrno(LinuxAudioError); + return NULL; + } + return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr); +} + static PyMethodDef lad_methods[] = { { "read", (PyCFunction)lad_read, METH_VARARGS }, { "write", (PyCFunction)lad_write, METH_VARARGS }, @@ -360,6 +422,7 @@ static PyMethodDef lad_methods[] = { { "flush", (PyCFunction)lad_flush, METH_VARARGS }, { "close", (PyCFunction)lad_close, METH_VARARGS }, { "fileno", (PyCFunction)lad_fileno, METH_VARARGS }, + { "getptr", (PyCFunction)lad_getptr, METH_VARARGS }, { NULL, NULL} /* sentinel */ }; @@ -398,50 +461,30 @@ static PyMethodDef linuxaudiodev_methods[] = { void initlinuxaudiodev(void) { - PyObject *m, *d, *x; + PyObject *m; m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods); - d = PyModule_GetDict(m); LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL); if (LinuxAudioError) - PyDict_SetItemString(d, "error", LinuxAudioError); - - x = PyInt_FromLong((long) AFMT_MU_LAW); - if (x == NULL || PyDict_SetItemString(d, "AFMT_MU_LAW", x) < 0) - goto error; - Py_DECREF(x); - - x = PyInt_FromLong((long) AFMT_U8); - if (x == NULL || PyDict_SetItemString(d, "AFMT_U8", x) < 0) - goto error; - Py_DECREF(x); - - x = PyInt_FromLong((long) AFMT_S8); - if (x == NULL || PyDict_SetItemString(d, "AFMT_S8", x) < 0) - goto error; - Py_DECREF(x); - - x = PyInt_FromLong((long) AFMT_U16_BE); - if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_BE", x) < 0) - goto error; - Py_DECREF(x); - - x = PyInt_FromLong((long) AFMT_U16_LE); - if (x == NULL || PyDict_SetItemString(d, "AFMT_U16_LE", x) < 0) - goto error; - Py_DECREF(x); - - x = PyInt_FromLong((long) AFMT_S16_BE); - if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_BE", x) < 0) - goto error; - Py_DECREF(x); - - x = PyInt_FromLong((long) AFMT_S16_LE); - if (x == NULL || PyDict_SetItemString(d, "AFMT_S16_LE", x) < 0) - goto error; - - error: - Py_DECREF(x); + PyModule_AddObject(m, "error", LinuxAudioError); + + if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1) + return; + if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1) + return; + if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1) + return; + if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1) + return; + if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1) + return; + if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1) + return; + if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1) + return; + if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1) + return; + return; } |