Saved and restored logging._handlerList at the same time as saving/restoring logging...
[python.git] / Modules / ossaudiodev.c
blob4c22b07fd866ecbdde707152467b23c1261e561d
1 /*
2 * ossaudiodev -- Python interface to the OSS (Open Sound System) API.
3 * This is the standard audio API for Linux and some
4 * flavours of BSD [XXX which ones?]; it is also available
5 * for a wide range of commercial Unices.
7 * Originally written by Peter Bosch, March 2000, as linuxaudiodev.
9 * Renamed to ossaudiodev and rearranged/revised/hacked up
10 * by Greg Ward <gward@python.net>, November 2002.
11 * Mixer interface by Nicholas FitzRoy-Dale <wzdd@lardcave.net>, Dec 2002.
13 * (c) 2000 Peter Bosch. All Rights Reserved.
14 * (c) 2002 Gregory P. Ward. All Rights Reserved.
15 * (c) 2002 Python Software Foundation. All Rights Reserved.
17 * XXX need a license statement
19 * $Id$
22 #include "Python.h"
23 #include "structmember.h"
25 #ifdef HAVE_FCNTL_H
26 #include <fcntl.h>
27 #else
28 #define O_RDONLY 00
29 #define O_WRONLY 01
30 #endif
32 #include <sys/ioctl.h>
33 #include <sys/soundcard.h>
35 #if defined(linux)
37 typedef unsigned long uint32_t;
39 #elif defined(__FreeBSD__)
41 # ifndef SNDCTL_DSP_CHANNELS
42 # define SNDCTL_DSP_CHANNELS SOUND_PCM_WRITE_CHANNELS
43 # endif
45 #endif
47 typedef struct {
48 PyObject_HEAD
49 char *devicename; /* name of the device file */
50 int fd; /* file descriptor */
51 int mode; /* file mode (O_RDONLY, etc.) */
52 int icount; /* input count */
53 int ocount; /* output count */
54 uint32_t afmts; /* audio formats supported by hardware */
55 } oss_audio_t;
57 typedef struct {
58 PyObject_HEAD
59 int fd; /* The open mixer device */
60 } oss_mixer_t;
63 static PyTypeObject OSSAudioType;
64 static PyTypeObject OSSMixerType;
66 static PyObject *OSSAudioError;
69 /* ----------------------------------------------------------------------
70 * DSP object initialization/deallocation
73 static oss_audio_t *
74 newossobject(PyObject *arg)
76 oss_audio_t *self;
77 int fd, afmts, imode;
78 char *devicename = NULL;
79 char *mode = NULL;
81 /* Two ways to call open():
82 open(device, mode) (for consistency with builtin open())
83 open(mode) (for backwards compatibility)
84 because the *first* argument is optional, parsing args is
85 a wee bit tricky. */
86 if (!PyArg_ParseTuple(arg, "s|s:open", &devicename, &mode))
87 return NULL;
88 if (mode == NULL) { /* only one arg supplied */
89 mode = devicename;
90 devicename = NULL;
93 if (strcmp(mode, "r") == 0)
94 imode = O_RDONLY;
95 else if (strcmp(mode, "w") == 0)
96 imode = O_WRONLY;
97 else if (strcmp(mode, "rw") == 0)
98 imode = O_RDWR;
99 else {
100 PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
101 return NULL;
104 /* Open the correct device: either the 'device' argument,
105 or the AUDIODEV environment variable, or "/dev/dsp". */
106 if (devicename == NULL) { /* called with one arg */
107 devicename = getenv("AUDIODEV");
108 if (devicename == NULL) /* $AUDIODEV not set */
109 devicename = "/dev/dsp";
112 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
113 one open at a time. This does *not* affect later I/O; OSS
114 provides a special ioctl() for non-blocking read/write, which is
115 exposed via oss_nonblock() below. */
116 if ((fd = open(devicename, imode|O_NONBLOCK)) == -1) {
117 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
118 return NULL;
121 /* And (try to) put it back in blocking mode so we get the
122 expected write() semantics. */
123 if (fcntl(fd, F_SETFL, 0) == -1) {
124 close(fd);
125 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
126 return NULL;
129 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
130 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
131 return NULL;
133 /* Create and initialize the object */
134 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
135 close(fd);
136 return NULL;
138 self->devicename = devicename;
139 self->fd = fd;
140 self->mode = imode;
141 self->icount = self->ocount = 0;
142 self->afmts = afmts;
143 return self;
146 static void
147 oss_dealloc(oss_audio_t *self)
149 /* if already closed, don't reclose it */
150 if (self->fd != -1)
151 close(self->fd);
152 PyObject_Del(self);
156 /* ----------------------------------------------------------------------
157 * Mixer object initialization/deallocation
160 static oss_mixer_t *
161 newossmixerobject(PyObject *arg)
163 char *devicename = NULL;
164 int fd;
165 oss_mixer_t *self;
167 if (!PyArg_ParseTuple(arg, "|s", &devicename)) {
168 return NULL;
171 if (devicename == NULL) {
172 devicename = getenv("MIXERDEV");
173 if (devicename == NULL) /* MIXERDEV not set */
174 devicename = "/dev/mixer";
177 if ((fd = open(devicename, O_RDWR)) == -1) {
178 PyErr_SetFromErrnoWithFilename(PyExc_IOError, devicename);
179 return NULL;
182 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
183 close(fd);
184 return NULL;
187 self->fd = fd;
189 return self;
192 static void
193 oss_mixer_dealloc(oss_mixer_t *self)
195 /* if already closed, don't reclose it */
196 if (self->fd != -1)
197 close(self->fd);
198 PyObject_Del(self);
202 /* Methods to wrap the OSS ioctls. The calling convention is pretty
203 simple:
204 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
205 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
206 etc.
210 /* ----------------------------------------------------------------------
211 * Helper functions
214 /* _do_ioctl_1() is a private helper function used for the OSS ioctls --
215 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C
216 like this:
217 ioctl(fd, SNDCTL_DSP_cmd, &arg)
219 where arg is the value to set, and on return the driver sets arg to
220 the value that was actually set. Mapping this to Python is obvious:
221 arg = dsp.xxx(arg)
223 static PyObject *
224 _do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
226 char argfmt[33] = "i:";
227 int arg;
229 assert(strlen(fname) <= 30);
230 strcat(argfmt, fname);
231 if (!PyArg_ParseTuple(args, argfmt, &arg))
232 return NULL;
234 if (ioctl(fd, cmd, &arg) == -1)
235 return PyErr_SetFromErrno(PyExc_IOError);
236 return PyInt_FromLong(arg);
240 /* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
241 but return an output -- ie. we need to pass a pointer to a local C
242 variable so the driver can write its output there, but from Python
243 all we see is the return value. For example,
244 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
245 devices, but does not use the value of the parameter passed-in in any
246 way.
248 static PyObject *
249 _do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
251 char argfmt[32] = ":";
252 int arg = 0;
254 assert(strlen(fname) <= 30);
255 strcat(argfmt, fname);
256 if (!PyArg_ParseTuple(args, argfmt, &arg))
257 return NULL;
259 if (ioctl(fd, cmd, &arg) == -1)
260 return PyErr_SetFromErrno(PyExc_IOError);
261 return PyInt_FromLong(arg);
266 /* _do_ioctl_0() is a private helper for the no-argument ioctls:
267 SNDCTL_DSP_{SYNC,RESET,POST}. */
268 static PyObject *
269 _do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
271 char argfmt[32] = ":";
272 int rv;
274 assert(strlen(fname) <= 30);
275 strcat(argfmt, fname);
276 if (!PyArg_ParseTuple(args, argfmt))
277 return NULL;
279 /* According to hannu@opensound.com, all three of the ioctls that
280 use this function can block, so release the GIL. This is
281 especially important for SYNC, which can block for several
282 seconds. */
283 Py_BEGIN_ALLOW_THREADS
284 rv = ioctl(fd, cmd, 0);
285 Py_END_ALLOW_THREADS
287 if (rv == -1)
288 return PyErr_SetFromErrno(PyExc_IOError);
289 Py_INCREF(Py_None);
290 return Py_None;
294 /* ----------------------------------------------------------------------
295 * Methods of DSP objects (OSSAudioType)
298 static PyObject *
299 oss_nonblock(oss_audio_t *self, PyObject *args)
301 /* Hmmm: it doesn't appear to be possible to return to blocking
302 mode once we're in non-blocking mode! */
303 if (!PyArg_ParseTuple(args, ":nonblock"))
304 return NULL;
305 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
306 return PyErr_SetFromErrno(PyExc_IOError);
307 Py_INCREF(Py_None);
308 return Py_None;
311 static PyObject *
312 oss_setfmt(oss_audio_t *self, PyObject *args)
314 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
317 static PyObject *
318 oss_getfmts(oss_audio_t *self, PyObject *args)
320 int mask;
321 if (!PyArg_ParseTuple(args, ":getfmts"))
322 return NULL;
323 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
324 return PyErr_SetFromErrno(PyExc_IOError);
325 return PyInt_FromLong(mask);
328 static PyObject *
329 oss_channels(oss_audio_t *self, PyObject *args)
331 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
334 static PyObject *
335 oss_speed(oss_audio_t *self, PyObject *args)
337 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
340 static PyObject *
341 oss_sync(oss_audio_t *self, PyObject *args)
343 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
346 static PyObject *
347 oss_reset(oss_audio_t *self, PyObject *args)
349 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
352 static PyObject *
353 oss_post(oss_audio_t *self, PyObject *args)
355 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
359 /* Regular file methods: read(), write(), close(), etc. as well
360 as one convenience method, writeall(). */
362 static PyObject *
363 oss_read(oss_audio_t *self, PyObject *args)
365 int size, count;
366 char *cp;
367 PyObject *rv;
369 if (!PyArg_ParseTuple(args, "i:read", &size))
370 return NULL;
371 rv = PyString_FromStringAndSize(NULL, size);
372 if (rv == NULL)
373 return NULL;
374 cp = PyString_AS_STRING(rv);
376 Py_BEGIN_ALLOW_THREADS
377 count = read(self->fd, cp, size);
378 Py_END_ALLOW_THREADS
380 if (count < 0) {
381 PyErr_SetFromErrno(PyExc_IOError);
382 Py_DECREF(rv);
383 return NULL;
385 self->icount += count;
386 _PyString_Resize(&rv, count);
387 return rv;
390 static PyObject *
391 oss_write(oss_audio_t *self, PyObject *args)
393 char *cp;
394 int rv, size;
396 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
397 return NULL;
400 Py_BEGIN_ALLOW_THREADS
401 rv = write(self->fd, cp, size);
402 Py_END_ALLOW_THREADS
404 if (rv == -1) {
405 return PyErr_SetFromErrno(PyExc_IOError);
406 } else {
407 self->ocount += rv;
409 return PyInt_FromLong(rv);
412 static PyObject *
413 oss_writeall(oss_audio_t *self, PyObject *args)
415 char *cp;
416 int rv, size;
417 fd_set write_set_fds;
418 int select_rv;
420 /* NB. writeall() is only useful in non-blocking mode: according to
421 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
422 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
423 write() in blocking mode consumes the whole buffer. In blocking
424 mode, the behaviour of write() and writeall() from Python is
425 indistinguishable. */
427 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
428 return NULL;
430 /* use select to wait for audio device to be available */
431 FD_ZERO(&write_set_fds);
432 FD_SET(self->fd, &write_set_fds);
434 while (size > 0) {
435 Py_BEGIN_ALLOW_THREADS
436 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
437 Py_END_ALLOW_THREADS
438 assert(select_rv != 0); /* no timeout, can't expire */
439 if (select_rv == -1)
440 return PyErr_SetFromErrno(PyExc_IOError);
442 Py_BEGIN_ALLOW_THREADS
443 rv = write(self->fd, cp, size);
444 Py_END_ALLOW_THREADS
445 if (rv == -1) {
446 if (errno == EAGAIN) { /* buffer is full, try again */
447 errno = 0;
448 continue;
449 } else /* it's a real error */
450 return PyErr_SetFromErrno(PyExc_IOError);
451 } else { /* wrote rv bytes */
452 self->ocount += rv;
453 size -= rv;
454 cp += rv;
457 Py_INCREF(Py_None);
458 return Py_None;
461 static PyObject *
462 oss_close(oss_audio_t *self, PyObject *args)
464 if (!PyArg_ParseTuple(args, ":close"))
465 return NULL;
467 if (self->fd >= 0) {
468 Py_BEGIN_ALLOW_THREADS
469 close(self->fd);
470 Py_END_ALLOW_THREADS
471 self->fd = -1;
473 Py_INCREF(Py_None);
474 return Py_None;
477 static PyObject *
478 oss_fileno(oss_audio_t *self, PyObject *args)
480 if (!PyArg_ParseTuple(args, ":fileno"))
481 return NULL;
482 return PyInt_FromLong(self->fd);
486 /* Convenience methods: these generally wrap a couple of ioctls into one
487 common task. */
489 static PyObject *
490 oss_setparameters(oss_audio_t *self, PyObject *args)
492 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
493 int fmt, channels, rate;
494 PyObject * rv; /* return tuple (fmt, channels, rate) */
496 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
497 &wanted_fmt, &wanted_channels, &wanted_rate,
498 &strict))
499 return NULL;
501 fmt = wanted_fmt;
502 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
503 return PyErr_SetFromErrno(PyExc_IOError);
505 if (strict && fmt != wanted_fmt) {
506 return PyErr_Format
507 (OSSAudioError,
508 "unable to set requested format (wanted %d, got %d)",
509 wanted_fmt, fmt);
512 channels = wanted_channels;
513 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
514 return PyErr_SetFromErrno(PyExc_IOError);
516 if (strict && channels != wanted_channels) {
517 return PyErr_Format
518 (OSSAudioError,
519 "unable to set requested channels (wanted %d, got %d)",
520 wanted_channels, channels);
523 rate = wanted_rate;
524 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
525 return PyErr_SetFromErrno(PyExc_IOError);
527 if (strict && rate != wanted_rate) {
528 return PyErr_Format
529 (OSSAudioError,
530 "unable to set requested rate (wanted %d, got %d)",
531 wanted_rate, rate);
534 /* Construct the return value: a (fmt, channels, rate) tuple that
535 tells what the audio hardware was actually set to. */
536 rv = PyTuple_New(3);
537 if (rv == NULL)
538 return NULL;
539 PyTuple_SET_ITEM(rv, 0, PyInt_FromLong(fmt));
540 PyTuple_SET_ITEM(rv, 1, PyInt_FromLong(channels));
541 PyTuple_SET_ITEM(rv, 2, PyInt_FromLong(rate));
542 return rv;
545 static int
546 _ssize(oss_audio_t *self, int *nchannels, int *ssize)
548 int fmt;
550 fmt = 0;
551 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
552 return -errno;
554 switch (fmt) {
555 case AFMT_MU_LAW:
556 case AFMT_A_LAW:
557 case AFMT_U8:
558 case AFMT_S8:
559 *ssize = 1; /* 8 bit formats: 1 byte */
560 break;
561 case AFMT_S16_LE:
562 case AFMT_S16_BE:
563 case AFMT_U16_LE:
564 case AFMT_U16_BE:
565 *ssize = 2; /* 16 bit formats: 2 byte */
566 break;
567 case AFMT_MPEG:
568 case AFMT_IMA_ADPCM:
569 default:
570 return -EOPNOTSUPP;
572 *nchannels = 0;
573 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
574 return -errno;
575 return 0;
579 /* bufsize returns the size of the hardware audio buffer in number
580 of samples */
581 static PyObject *
582 oss_bufsize(oss_audio_t *self, PyObject *args)
584 audio_buf_info ai;
585 int nchannels, ssize;
587 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
589 if (_ssize(self, &nchannels, &ssize) < 0) {
590 PyErr_SetFromErrno(PyExc_IOError);
591 return NULL;
593 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
594 PyErr_SetFromErrno(PyExc_IOError);
595 return NULL;
597 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
600 /* obufcount returns the number of samples that are available in the
601 hardware for playing */
602 static PyObject *
603 oss_obufcount(oss_audio_t *self, PyObject *args)
605 audio_buf_info ai;
606 int nchannels, ssize;
608 if (!PyArg_ParseTuple(args, ":obufcount"))
609 return NULL;
611 if (_ssize(self, &nchannels, &ssize) < 0) {
612 PyErr_SetFromErrno(PyExc_IOError);
613 return NULL;
615 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
616 PyErr_SetFromErrno(PyExc_IOError);
617 return NULL;
619 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
620 (ssize * nchannels));
623 /* obufcount returns the number of samples that can be played without
624 blocking */
625 static PyObject *
626 oss_obuffree(oss_audio_t *self, PyObject *args)
628 audio_buf_info ai;
629 int nchannels, ssize;
631 if (!PyArg_ParseTuple(args, ":obuffree"))
632 return NULL;
634 if (_ssize(self, &nchannels, &ssize) < 0) {
635 PyErr_SetFromErrno(PyExc_IOError);
636 return NULL;
638 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
639 PyErr_SetFromErrno(PyExc_IOError);
640 return NULL;
642 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
645 static PyObject *
646 oss_getptr(oss_audio_t *self, PyObject *args)
648 count_info info;
649 int req;
651 if (!PyArg_ParseTuple(args, ":getptr"))
652 return NULL;
654 if (self->mode == O_RDONLY)
655 req = SNDCTL_DSP_GETIPTR;
656 else
657 req = SNDCTL_DSP_GETOPTR;
658 if (ioctl(self->fd, req, &info) == -1) {
659 PyErr_SetFromErrno(PyExc_IOError);
660 return NULL;
662 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
666 /* ----------------------------------------------------------------------
667 * Methods of mixer objects (OSSMixerType)
670 static PyObject *
671 oss_mixer_close(oss_mixer_t *self, PyObject *args)
673 if (!PyArg_ParseTuple(args, ":close"))
674 return NULL;
676 if (self->fd >= 0) {
677 close(self->fd);
678 self->fd = -1;
680 Py_INCREF(Py_None);
681 return Py_None;
684 static PyObject *
685 oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
687 if (!PyArg_ParseTuple(args, ":fileno"))
688 return NULL;
689 return PyInt_FromLong(self->fd);
692 /* Simple mixer interface methods */
694 static PyObject *
695 oss_mixer_controls(oss_mixer_t *self, PyObject *args)
697 return _do_ioctl_1_internal(self->fd, args, "controls",
698 SOUND_MIXER_READ_DEVMASK);
701 static PyObject *
702 oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
704 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
705 SOUND_MIXER_READ_STEREODEVS);
708 static PyObject *
709 oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
711 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
712 SOUND_MIXER_READ_RECMASK);
715 static PyObject *
716 oss_mixer_get(oss_mixer_t *self, PyObject *args)
718 int channel, volume;
720 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
721 if (!PyArg_ParseTuple(args, "i:get", &channel))
722 return NULL;
724 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
725 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
726 return NULL;
729 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
730 return PyErr_SetFromErrno(PyExc_IOError);
732 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
735 static PyObject *
736 oss_mixer_set(oss_mixer_t *self, PyObject *args)
738 int channel, volume, leftVol, rightVol;
740 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
741 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
742 return NULL;
744 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
745 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
746 return NULL;
749 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
750 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
751 return NULL;
754 volume = (rightVol << 8) | leftVol;
756 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
757 return PyErr_SetFromErrno(PyExc_IOError);
759 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
762 static PyObject *
763 oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
765 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
766 SOUND_MIXER_READ_RECSRC);
769 static PyObject *
770 oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
772 return _do_ioctl_1(self->fd, args, "set_recsrc",
773 SOUND_MIXER_WRITE_RECSRC);
777 /* ----------------------------------------------------------------------
778 * Method tables and other bureaucracy
781 static PyMethodDef oss_methods[] = {
782 /* Regular file methods */
783 { "read", (PyCFunction)oss_read, METH_VARARGS },
784 { "write", (PyCFunction)oss_write, METH_VARARGS },
785 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
786 { "close", (PyCFunction)oss_close, METH_VARARGS },
787 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
789 /* Simple ioctl wrappers */
790 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
791 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
792 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
793 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
794 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
795 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
796 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
797 { "post", (PyCFunction)oss_post, METH_VARARGS },
799 /* Convenience methods -- wrap a couple of ioctls together */
800 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
801 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
802 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
803 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
804 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
806 /* Aliases for backwards compatibility */
807 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
809 { NULL, NULL} /* sentinel */
812 static PyMethodDef oss_mixer_methods[] = {
813 /* Regular file method - OSS mixers are ioctl-only interface */
814 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
815 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
817 /* Simple ioctl wrappers */
818 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
819 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
820 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
821 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
822 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
823 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
824 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
826 { NULL, NULL}
829 static PyObject *
830 oss_getattr(oss_audio_t *self, char *name)
832 PyObject * rval = NULL;
833 if (strcmp(name, "closed") == 0) {
834 rval = (self->fd == -1) ? Py_True : Py_False;
835 Py_INCREF(rval);
837 else if (strcmp(name, "name") == 0) {
838 rval = PyString_FromString(self->devicename);
840 else if (strcmp(name, "mode") == 0) {
841 /* No need for a "default" in this switch: from newossobject(),
842 self->mode can only be one of these three values. */
843 switch(self->mode) {
844 case O_RDONLY:
845 rval = PyString_FromString("r");
846 break;
847 case O_RDWR:
848 rval = PyString_FromString("rw");
849 break;
850 case O_WRONLY:
851 rval = PyString_FromString("w");
852 break;
855 else {
856 rval = Py_FindMethod(oss_methods, (PyObject *)self, name);
858 return rval;
861 static PyObject *
862 oss_mixer_getattr(oss_mixer_t *self, char *name)
864 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
867 static PyTypeObject OSSAudioType = {
868 PyObject_HEAD_INIT(&PyType_Type)
869 0, /*ob_size*/
870 "ossaudiodev.oss_audio_device", /*tp_name*/
871 sizeof(oss_audio_t), /*tp_size*/
872 0, /*tp_itemsize*/
873 /* methods */
874 (destructor)oss_dealloc, /*tp_dealloc*/
875 0, /*tp_print*/
876 (getattrfunc)oss_getattr, /*tp_getattr*/
877 0, /*tp_setattr*/
878 0, /*tp_compare*/
879 0, /*tp_repr*/
882 static PyTypeObject OSSMixerType = {
883 PyObject_HEAD_INIT(&PyType_Type)
884 0, /*ob_size*/
885 "ossaudiodev.oss_mixer_device", /*tp_name*/
886 sizeof(oss_mixer_t), /*tp_size*/
887 0, /*tp_itemsize*/
888 /* methods */
889 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
890 0, /*tp_print*/
891 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
892 0, /*tp_setattr*/
893 0, /*tp_compare*/
894 0, /*tp_repr*/
898 static PyObject *
899 ossopen(PyObject *self, PyObject *args)
901 return (PyObject *)newossobject(args);
904 static PyObject *
905 ossopenmixer(PyObject *self, PyObject *args)
907 return (PyObject *)newossmixerobject(args);
910 static PyMethodDef ossaudiodev_methods[] = {
911 { "open", ossopen, METH_VARARGS },
912 { "openmixer", ossopenmixer, METH_VARARGS },
913 { 0, 0 },
917 #define _EXPORT_INT(mod, name) \
918 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
921 static char *control_labels[] = SOUND_DEVICE_LABELS;
922 static char *control_names[] = SOUND_DEVICE_NAMES;
925 static int
926 build_namelists (PyObject *module)
928 PyObject *labels;
929 PyObject *names;
930 PyObject *s;
931 int num_controls;
932 int i;
934 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
935 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
937 labels = PyList_New(num_controls);
938 names = PyList_New(num_controls);
939 for (i = 0; i < num_controls; i++) {
940 s = PyString_FromString(control_labels[i]);
941 if (s == NULL)
942 return -1;
943 PyList_SET_ITEM(labels, i, s);
945 s = PyString_FromString(control_names[i]);
946 if (s == NULL)
947 return -1;
948 PyList_SET_ITEM(names, i, s);
951 if (PyModule_AddObject(module, "control_labels", labels) == -1)
952 return -1;
953 if (PyModule_AddObject(module, "control_names", names) == -1)
954 return -1;
956 return 0;
960 void
961 initossaudiodev(void)
963 PyObject *m;
965 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
966 if (m == NULL)
967 return;
969 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
970 NULL, NULL);
971 if (OSSAudioError) {
972 /* Each call to PyModule_AddObject decrefs it; compensate: */
973 Py_INCREF(OSSAudioError);
974 Py_INCREF(OSSAudioError);
975 PyModule_AddObject(m, "error", OSSAudioError);
976 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
979 /* Build 'control_labels' and 'control_names' lists and add them
980 to the module. */
981 if (build_namelists(m) == -1) /* XXX what to do here? */
982 return;
984 /* Expose the audio format numbers -- essential! */
985 _EXPORT_INT(m, AFMT_QUERY);
986 _EXPORT_INT(m, AFMT_MU_LAW);
987 _EXPORT_INT(m, AFMT_A_LAW);
988 _EXPORT_INT(m, AFMT_IMA_ADPCM);
989 _EXPORT_INT(m, AFMT_U8);
990 _EXPORT_INT(m, AFMT_S16_LE);
991 _EXPORT_INT(m, AFMT_S16_BE);
992 _EXPORT_INT(m, AFMT_S8);
993 _EXPORT_INT(m, AFMT_U16_LE);
994 _EXPORT_INT(m, AFMT_U16_BE);
995 _EXPORT_INT(m, AFMT_MPEG);
996 #ifdef AFMT_AC3
997 _EXPORT_INT(m, AFMT_AC3);
998 #endif
999 #ifdef AFMT_S16_NE
1000 _EXPORT_INT(m, AFMT_S16_NE);
1001 #endif
1002 #ifdef AFMT_U16_NE
1003 _EXPORT_INT(m, AFMT_U16_NE);
1004 #endif
1005 #ifdef AFMT_S32_LE
1006 _EXPORT_INT(m, AFMT_S32_LE);
1007 #endif
1008 #ifdef AFMT_S32_BE
1009 _EXPORT_INT(m, AFMT_S32_BE);
1010 #endif
1011 #ifdef AFMT_MPEG
1012 _EXPORT_INT(m, AFMT_MPEG);
1013 #endif
1015 /* Expose the sound mixer device numbers. */
1016 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
1017 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1018 _EXPORT_INT(m, SOUND_MIXER_BASS);
1019 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1020 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1021 _EXPORT_INT(m, SOUND_MIXER_PCM);
1022 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1023 _EXPORT_INT(m, SOUND_MIXER_LINE);
1024 _EXPORT_INT(m, SOUND_MIXER_MIC);
1025 _EXPORT_INT(m, SOUND_MIXER_CD);
1026 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1027 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1028 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1029 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1030 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1031 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1032 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1033 _EXPORT_INT(m, SOUND_MIXER_LINE3);
1034 #ifdef SOUND_MIXER_DIGITAL1
1035 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
1036 #endif
1037 #ifdef SOUND_MIXER_DIGITAL2
1038 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
1039 #endif
1040 #ifdef SOUND_MIXER_DIGITAL3
1041 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
1042 #endif
1043 #ifdef SOUND_MIXER_PHONEIN
1044 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
1045 #endif
1046 #ifdef SOUND_MIXER_PHONEOUT
1047 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
1048 #endif
1049 #ifdef SOUND_MIXER_VIDEO
1050 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
1051 #endif
1052 #ifdef SOUND_MIXER_RADIO
1053 _EXPORT_INT(m, SOUND_MIXER_RADIO);
1054 #endif
1055 #ifdef SOUND_MIXER_MONITOR
1056 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
1057 #endif
1059 /* Expose all the ioctl numbers for masochists who like to do this
1060 stuff directly. */
1061 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1062 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1063 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1064 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1065 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1066 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1067 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1068 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1069 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1070 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
1071 #ifdef SNDCTL_DSP_BIND_CHANNEL
1072 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
1073 #endif
1074 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1075 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1076 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
1077 #ifdef SNDCTL_DSP_GETCHANNELMASK
1078 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
1079 #endif
1080 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1081 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1082 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1083 #ifdef SNDCTL_DSP_GETODELAY
1084 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1085 #endif
1086 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1087 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
1088 #ifdef SNDCTL_DSP_GETSPDIF
1089 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
1090 #endif
1091 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1092 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1093 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1094 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1095 _EXPORT_INT(m, SNDCTL_DSP_POST);
1096 #ifdef SNDCTL_DSP_PROFILE
1097 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
1098 #endif
1099 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1100 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1101 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1102 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1103 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
1104 #ifdef SNDCTL_DSP_SETSPDIF
1105 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
1106 #endif
1107 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1108 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1109 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1110 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1111 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1112 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1113 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1114 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1115 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1116 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1117 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1118 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1119 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1120 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1121 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
1122 #ifdef SNDCTL_SEQ_GETTIME
1123 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
1124 #endif
1125 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1126 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1127 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1128 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1129 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1130 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1131 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1132 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1133 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1134 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
1135 #ifdef SNDCTL_SYNTH_CONTROL
1136 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
1137 #endif
1138 #ifdef SNDCTL_SYNTH_ID
1139 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
1140 #endif
1141 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1142 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
1143 #ifdef SNDCTL_SYNTH_REMOVESAMPLE
1144 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
1145 #endif
1146 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1147 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1148 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1149 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1150 _EXPORT_INT(m, SNDCTL_TMR_START);
1151 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1152 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1153 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);