Get rid of some warnings.
[python.git] / Modules / linuxaudiodev.c
blob769451af8507607e6182e8cf06181ba35ea13ca9
1 /* Hey Emacs, this is -*-C-*-
2 ******************************************************************************
3 * linuxaudiodev.c -- Linux audio device for python.
4 *
5 * Author : Peter Bosch
6 * Created On : Thu Mar 2 21:10:33 2000
7 * Status : Unknown, Use with caution!
8 *
9 * Unless other notices are present in any part of this file
10 * explicitly claiming copyrights for other people and/or
11 * organizations, the contents of this file is fully copyright
12 * (C) 2000 Peter Bosch, all rights reserved.
13 ******************************************************************************
16 #include "Python.h"
17 #include "structmember.h"
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #else
22 #define O_RDONLY 00
23 #define O_WRONLY 01
24 #endif
27 #include <sys/ioctl.h>
28 #if defined(linux)
29 #include <linux/soundcard.h>
31 typedef unsigned long uint32_t;
33 #elif defined(__FreeBSD__)
34 #include <machine/soundcard.h>
36 #ifndef SNDCTL_DSP_CHANNELS
37 #define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
38 #endif
40 #endif
42 typedef struct {
43 PyObject_HEAD
44 int x_fd; /* The open file */
45 int x_mode; /* file mode */
46 int x_icount; /* Input count */
47 int x_ocount; /* Output count */
48 uint32_t x_afmts; /* Audio formats supported by hardware*/
49 } lad_t;
51 /* XXX several format defined in soundcard.h are not supported,
52 including _NE (native endian) options and S32 options
55 static struct {
56 int a_bps;
57 uint32_t a_fmt;
58 char *a_name;
59 } audio_types[] = {
60 { 8, AFMT_MU_LAW, "logarithmic mu-law 8-bit audio" },
61 { 8, AFMT_A_LAW, "logarithmic A-law 8-bit audio" },
62 { 8, AFMT_U8, "linear unsigned 8-bit audio" },
63 { 8, AFMT_S8, "linear signed 8-bit audio" },
64 { 16, AFMT_U16_BE, "linear unsigned 16-bit big-endian audio" },
65 { 16, AFMT_U16_LE, "linear unsigned 16-bit little-endian audio" },
66 { 16, AFMT_S16_BE, "linear signed 16-bit big-endian audio" },
67 { 16, AFMT_S16_LE, "linear signed 16-bit little-endian audio" },
68 { 16, AFMT_S16_NE, "linear signed 16-bit native-endian audio" },
71 static int n_audio_types = sizeof(audio_types) / sizeof(audio_types[0]);
73 static PyTypeObject Ladtype;
75 static PyObject *LinuxAudioError;
77 static lad_t *
78 newladobject(PyObject *arg)
80 lad_t *xp;
81 int fd, afmts, imode;
82 char *basedev = NULL;
83 char *mode = NULL;
85 /* Two ways to call linuxaudiodev.open():
86 open(device, mode) (for consistency with builtin open())
87 open(mode) (for backwards compatibility)
88 because the *first* argument is optional, parsing args is
89 a wee bit tricky. */
90 if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode))
91 return NULL;
92 if (mode == NULL) { /* only one arg supplied */
93 mode = basedev;
94 basedev = NULL;
97 if (strcmp(mode, "r") == 0)
98 imode = O_RDONLY;
99 else if (strcmp(mode, "w") == 0)
100 imode = O_WRONLY;
101 else {
102 PyErr_SetString(LinuxAudioError, "mode should be 'r' or 'w'");
103 return NULL;
106 /* Open the correct device. The base device name comes from the
107 * AUDIODEV environment variable first, then /dev/dsp. The
108 * control device tacks "ctl" onto the base device name.
110 * Note that the only difference between /dev/audio and /dev/dsp
111 * is that the former uses logarithmic mu-law encoding and the
112 * latter uses 8-bit unsigned encoding.
115 if (basedev == NULL) { /* called with one arg */
116 basedev = getenv("AUDIODEV");
117 if (basedev == NULL) /* $AUDIODEV not set */
118 basedev = "/dev/dsp";
121 if ((fd = open(basedev, imode)) == -1) {
122 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
123 return NULL;
125 if (imode == O_WRONLY && ioctl(fd, SNDCTL_DSP_NONBLOCK, NULL) == -1) {
126 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
127 return NULL;
129 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
130 PyErr_SetFromErrnoWithFilename(LinuxAudioError, basedev);
131 return NULL;
133 /* Create and initialize the object */
134 if ((xp = PyObject_New(lad_t, &Ladtype)) == NULL) {
135 close(fd);
136 return NULL;
138 xp->x_fd = fd;
139 xp->x_mode = imode;
140 xp->x_icount = xp->x_ocount = 0;
141 xp->x_afmts = afmts;
142 return xp;
145 static void
146 lad_dealloc(lad_t *xp)
148 /* if already closed, don't reclose it */
149 if (xp->x_fd != -1)
150 close(xp->x_fd);
151 PyObject_Del(xp);
154 static PyObject *
155 lad_read(lad_t *self, PyObject *args)
157 int size, count;
158 char *cp;
159 PyObject *rv;
161 if (!PyArg_ParseTuple(args, "i:read", &size))
162 return NULL;
163 rv = PyString_FromStringAndSize(NULL, size);
164 if (rv == NULL)
165 return NULL;
166 cp = PyString_AS_STRING(rv);
167 if ((count = read(self->x_fd, cp, size)) < 0) {
168 PyErr_SetFromErrno(LinuxAudioError);
169 Py_DECREF(rv);
170 return NULL;
172 self->x_icount += count;
173 _PyString_Resize(&rv, count);
174 return rv;
177 static PyObject *
178 lad_write(lad_t *self, PyObject *args)
180 char *cp;
181 int rv, size;
182 fd_set write_set_fds;
183 struct timeval tv;
184 int select_retval;
186 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
187 return NULL;
189 /* use select to wait for audio device to be available */
190 FD_ZERO(&write_set_fds);
191 FD_SET(self->x_fd, &write_set_fds);
192 tv.tv_sec = 4; /* timeout values */
193 tv.tv_usec = 0;
195 while (size > 0) {
196 select_retval = select(self->x_fd+1, NULL, &write_set_fds, NULL, &tv);
197 tv.tv_sec = 1; tv.tv_usec = 0; /* willing to wait this long next time*/
198 if (select_retval) {
199 if ((rv = write(self->x_fd, cp, size)) == -1) {
200 if (errno != EAGAIN) {
201 PyErr_SetFromErrno(LinuxAudioError);
202 return NULL;
203 } else {
204 errno = 0; /* EAGAIN: buffer is full, try again */
206 } else {
207 self->x_ocount += rv;
208 size -= rv;
209 cp += rv;
211 } else {
212 /* printf("Not able to write to linux audio device within %ld seconds\n", tv.tv_sec); */
213 PyErr_SetFromErrno(LinuxAudioError);
214 return NULL;
217 Py_INCREF(Py_None);
218 return Py_None;
221 static PyObject *
222 lad_close(lad_t *self, PyObject *args)
224 if (!PyArg_ParseTuple(args, ":close"))
225 return NULL;
227 if (self->x_fd >= 0) {
228 close(self->x_fd);
229 self->x_fd = -1;
231 Py_INCREF(Py_None);
232 return Py_None;
235 static PyObject *
236 lad_fileno(lad_t *self, PyObject *args)
238 if (!PyArg_ParseTuple(args, ":fileno"))
239 return NULL;
240 return PyInt_FromLong(self->x_fd);
243 static PyObject *
244 lad_setparameters(lad_t *self, PyObject *args)
246 int rate, ssize, nchannels, n, fmt, emulate=0;
248 if (!PyArg_ParseTuple(args, "iiii|i:setparameters",
249 &rate, &ssize, &nchannels, &fmt, &emulate))
250 return NULL;
252 if (rate < 0) {
253 PyErr_Format(PyExc_ValueError, "expected rate >= 0, not %d",
254 rate);
255 return NULL;
257 if (ssize < 0) {
258 PyErr_Format(PyExc_ValueError, "expected sample size >= 0, not %d",
259 ssize);
260 return NULL;
262 if (nchannels != 1 && nchannels != 2) {
263 PyErr_Format(PyExc_ValueError, "nchannels must be 1 or 2, not %d",
264 nchannels);
265 return NULL;
268 for (n = 0; n < n_audio_types; n++)
269 if (fmt == audio_types[n].a_fmt)
270 break;
271 if (n == n_audio_types) {
272 PyErr_Format(PyExc_ValueError, "unknown audio encoding: %d", fmt);
273 return NULL;
275 if (audio_types[n].a_bps != ssize) {
276 PyErr_Format(PyExc_ValueError,
277 "for %s, expected sample size %d, not %d",
278 audio_types[n].a_name, audio_types[n].a_bps, ssize);
279 return NULL;
282 if (emulate == 0) {
283 if ((self->x_afmts & audio_types[n].a_fmt) == 0) {
284 PyErr_Format(PyExc_ValueError,
285 "%s format not supported by device",
286 audio_types[n].a_name);
287 return NULL;
290 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT,
291 &audio_types[n].a_fmt) == -1) {
292 PyErr_SetFromErrno(LinuxAudioError);
293 return NULL;
295 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, &nchannels) == -1) {
296 PyErr_SetFromErrno(LinuxAudioError);
297 return NULL;
299 if (ioctl(self->x_fd, SNDCTL_DSP_SPEED, &rate) == -1) {
300 PyErr_SetFromErrno(LinuxAudioError);
301 return NULL;
304 Py_INCREF(Py_None);
305 return Py_None;
308 static int
309 _ssize(lad_t *self, int *nchannels, int *ssize)
311 int fmt;
313 fmt = 0;
314 if (ioctl(self->x_fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
315 return -errno;
317 switch (fmt) {
318 case AFMT_MU_LAW:
319 case AFMT_A_LAW:
320 case AFMT_U8:
321 case AFMT_S8:
322 *ssize = sizeof(char);
323 break;
324 case AFMT_S16_LE:
325 case AFMT_S16_BE:
326 case AFMT_U16_LE:
327 case AFMT_U16_BE:
328 *ssize = sizeof(short);
329 break;
330 case AFMT_MPEG:
331 case AFMT_IMA_ADPCM:
332 default:
333 return -EOPNOTSUPP;
335 if (ioctl(self->x_fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
336 return -errno;
337 return 0;
341 /* bufsize returns the size of the hardware audio buffer in number
342 of samples */
343 static PyObject *
344 lad_bufsize(lad_t *self, PyObject *args)
346 audio_buf_info ai;
347 int nchannels=0, ssize=0;
349 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
351 if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) {
352 PyErr_SetFromErrno(LinuxAudioError);
353 return NULL;
355 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
356 PyErr_SetFromErrno(LinuxAudioError);
357 return NULL;
359 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
362 /* obufcount returns the number of samples that are available in the
363 hardware for playing */
364 static PyObject *
365 lad_obufcount(lad_t *self, PyObject *args)
367 audio_buf_info ai;
368 int nchannels=0, ssize=0;
370 if (!PyArg_ParseTuple(args, ":obufcount"))
371 return NULL;
373 if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) {
374 PyErr_SetFromErrno(LinuxAudioError);
375 return NULL;
377 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
378 PyErr_SetFromErrno(LinuxAudioError);
379 return NULL;
381 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
382 (ssize * nchannels));
385 /* obufcount returns the number of samples that can be played without
386 blocking */
387 static PyObject *
388 lad_obuffree(lad_t *self, PyObject *args)
390 audio_buf_info ai;
391 int nchannels=0, ssize=0;
393 if (!PyArg_ParseTuple(args, ":obuffree"))
394 return NULL;
396 if (_ssize(self, &nchannels, &ssize) < 0 || !ssize || !nchannels) {
397 PyErr_SetFromErrno(LinuxAudioError);
398 return NULL;
400 if (ioctl(self->x_fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
401 PyErr_SetFromErrno(LinuxAudioError);
402 return NULL;
404 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
407 /* Flush the device */
408 static PyObject *
409 lad_flush(lad_t *self, PyObject *args)
411 if (!PyArg_ParseTuple(args, ":flush")) return NULL;
413 if (ioctl(self->x_fd, SNDCTL_DSP_SYNC, NULL) == -1) {
414 PyErr_SetFromErrno(LinuxAudioError);
415 return NULL;
417 Py_INCREF(Py_None);
418 return Py_None;
421 static PyObject *
422 lad_getptr(lad_t *self, PyObject *args)
424 count_info info;
425 int req;
427 if (!PyArg_ParseTuple(args, ":getptr"))
428 return NULL;
430 if (self->x_mode == O_RDONLY)
431 req = SNDCTL_DSP_GETIPTR;
432 else
433 req = SNDCTL_DSP_GETOPTR;
434 if (ioctl(self->x_fd, req, &info) == -1) {
435 PyErr_SetFromErrno(LinuxAudioError);
436 return NULL;
438 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
441 static PyMethodDef lad_methods[] = {
442 { "read", (PyCFunction)lad_read, METH_VARARGS },
443 { "write", (PyCFunction)lad_write, METH_VARARGS },
444 { "setparameters", (PyCFunction)lad_setparameters, METH_VARARGS },
445 { "bufsize", (PyCFunction)lad_bufsize, METH_VARARGS },
446 { "obufcount", (PyCFunction)lad_obufcount, METH_VARARGS },
447 { "obuffree", (PyCFunction)lad_obuffree, METH_VARARGS },
448 { "flush", (PyCFunction)lad_flush, METH_VARARGS },
449 { "close", (PyCFunction)lad_close, METH_VARARGS },
450 { "fileno", (PyCFunction)lad_fileno, METH_VARARGS },
451 { "getptr", (PyCFunction)lad_getptr, METH_VARARGS },
452 { NULL, NULL} /* sentinel */
455 static PyObject *
456 lad_getattr(lad_t *xp, char *name)
458 return Py_FindMethod(lad_methods, (PyObject *)xp, name);
461 static PyTypeObject Ladtype = {
462 PyObject_HEAD_INIT(&PyType_Type)
463 0, /*ob_size*/
464 "linuxaudiodev.linux_audio_device", /*tp_name*/
465 sizeof(lad_t), /*tp_size*/
466 0, /*tp_itemsize*/
467 /* methods */
468 (destructor)lad_dealloc, /*tp_dealloc*/
469 0, /*tp_print*/
470 (getattrfunc)lad_getattr, /*tp_getattr*/
471 0, /*tp_setattr*/
472 0, /*tp_compare*/
473 0, /*tp_repr*/
476 static PyObject *
477 ladopen(PyObject *self, PyObject *args)
479 return (PyObject *)newladobject(args);
482 static PyMethodDef linuxaudiodev_methods[] = {
483 { "open", ladopen, METH_VARARGS },
484 { 0, 0 },
487 void
488 initlinuxaudiodev(void)
490 PyObject *m;
492 m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
493 if (m == NULL)
494 return;
496 LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
497 if (LinuxAudioError)
498 PyModule_AddObject(m, "error", LinuxAudioError);
500 if (PyModule_AddIntConstant(m, "AFMT_MU_LAW", (long)AFMT_MU_LAW) == -1)
501 return;
502 if (PyModule_AddIntConstant(m, "AFMT_A_LAW", (long)AFMT_A_LAW) == -1)
503 return;
504 if (PyModule_AddIntConstant(m, "AFMT_U8", (long)AFMT_U8) == -1)
505 return;
506 if (PyModule_AddIntConstant(m, "AFMT_S8", (long)AFMT_S8) == -1)
507 return;
508 if (PyModule_AddIntConstant(m, "AFMT_U16_BE", (long)AFMT_U16_BE) == -1)
509 return;
510 if (PyModule_AddIntConstant(m, "AFMT_U16_LE", (long)AFMT_U16_LE) == -1)
511 return;
512 if (PyModule_AddIntConstant(m, "AFMT_S16_BE", (long)AFMT_S16_BE) == -1)
513 return;
514 if (PyModule_AddIntConstant(m, "AFMT_S16_LE", (long)AFMT_S16_LE) == -1)
515 return;
516 if (PyModule_AddIntConstant(m, "AFMT_S16_NE", (long)AFMT_S16_NE) == -1)
517 return;
519 return;