Whitespace normalization.
[python/dscho.git] / Modules / ossaudiodev.c
blob047355fb784d5c724c5cf0afa658871b95832d00
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 int fd; /* The open file */
50 int mode; /* file mode */
51 int icount; /* Input count */
52 int ocount; /* Output count */
53 uint32_t afmts; /* Audio formats supported by hardware */
54 } oss_audio_t;
56 typedef struct {
57 PyObject_HEAD;
58 int fd; /* The open mixer device */
59 } oss_mixer_t;
62 static PyTypeObject OSSAudioType;
63 static PyTypeObject OSSMixerType;
65 static PyObject *OSSAudioError;
68 /* ----------------------------------------------------------------------
69 * DSP object initialization/deallocation
72 static oss_audio_t *
73 newossobject(PyObject *arg)
75 oss_audio_t *self;
76 int fd, afmts, imode;
77 char *basedev = NULL;
78 char *mode = NULL;
80 /* Two ways to call open():
81 open(device, mode) (for consistency with builtin open())
82 open(mode) (for backwards compatibility)
83 because the *first* argument is optional, parsing args is
84 a wee bit tricky. */
85 if (!PyArg_ParseTuple(arg, "s|s:open", &basedev, &mode))
86 return NULL;
87 if (mode == NULL) { /* only one arg supplied */
88 mode = basedev;
89 basedev = NULL;
92 if (strcmp(mode, "r") == 0)
93 imode = O_RDONLY;
94 else if (strcmp(mode, "w") == 0)
95 imode = O_WRONLY;
96 else if (strcmp(mode, "rw") == 0)
97 imode = O_RDWR;
98 else {
99 PyErr_SetString(OSSAudioError, "mode must be 'r', 'w', or 'rw'");
100 return NULL;
103 /* Open the correct device: either the 'device' argument,
104 or the AUDIODEV environment variable, or "/dev/dsp". */
105 if (basedev == NULL) { /* called with one arg */
106 basedev = getenv("AUDIODEV");
107 if (basedev == NULL) /* $AUDIODEV not set */
108 basedev = "/dev/dsp";
111 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
112 one open at a time. This does *not* affect later I/O; OSS
113 provides a special ioctl() for non-blocking read/write, which is
114 exposed via oss_nonblock() below. */
115 if ((fd = open(basedev, imode|O_NONBLOCK)) == -1) {
116 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
117 return NULL;
120 /* And (try to) put it back in blocking mode so we get the
121 expected write() semantics. */
122 if (fcntl(fd, F_SETFL, 0) == -1) {
123 close(fd);
124 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
125 return NULL;
128 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &afmts) == -1) {
129 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
130 return NULL;
132 /* Create and initialize the object */
133 if ((self = PyObject_New(oss_audio_t, &OSSAudioType)) == NULL) {
134 close(fd);
135 return NULL;
137 self->fd = fd;
138 self->mode = imode;
139 self->icount = self->ocount = 0;
140 self->afmts = afmts;
141 return self;
144 static void
145 oss_dealloc(oss_audio_t *self)
147 /* if already closed, don't reclose it */
148 if (self->fd != -1)
149 close(self->fd);
150 PyObject_Del(self);
154 /* ----------------------------------------------------------------------
155 * Mixer object initialization/deallocation
158 static oss_mixer_t *
159 newossmixerobject(PyObject *arg)
161 char *basedev = NULL;
162 int fd;
163 oss_mixer_t *self;
165 if (!PyArg_ParseTuple(arg, "|s", &basedev)) {
166 return NULL;
169 if (basedev == NULL) {
170 basedev = getenv("MIXERDEV");
171 if (basedev == NULL) /* MIXERDEV not set */
172 basedev = "/dev/mixer";
175 if ((fd = open(basedev, O_RDWR)) == -1) {
176 PyErr_SetFromErrnoWithFilename(PyExc_IOError, basedev);
177 return NULL;
180 if ((self = PyObject_New(oss_mixer_t, &OSSMixerType)) == NULL) {
181 close(fd);
182 return NULL;
185 self->fd = fd;
187 return self;
190 static void
191 oss_mixer_dealloc(oss_mixer_t *self)
193 /* if already closed, don't reclose it */
194 if (self->fd != -1)
195 close(self->fd);
196 PyObject_Del(self);
200 /* Methods to wrap the OSS ioctls. The calling convention is pretty
201 simple:
202 nonblock() -> ioctl(fd, SNDCTL_DSP_NONBLOCK)
203 fmt = setfmt(fmt) -> ioctl(fd, SNDCTL_DSP_SETFMT, &fmt)
204 etc.
208 /* ----------------------------------------------------------------------
209 * Helper functions
212 /* _do_ioctl_1() is a private helper function used for the OSS ioctls --
213 SNDCTL_DSP_{SETFMT,CHANNELS,SPEED} -- that that are called from C
214 like this:
215 ioctl(fd, SNDCTL_DSP_cmd, &arg)
217 where arg is the value to set, and on return the driver sets arg to
218 the value that was actually set. Mapping this to Python is obvious:
219 arg = dsp.xxx(arg)
221 static PyObject *
222 _do_ioctl_1(int fd, PyObject *args, char *fname, int cmd)
224 char argfmt[33] = "i:";
225 int arg;
227 assert(strlen(fname) <= 30);
228 strcat(argfmt, fname);
229 if (!PyArg_ParseTuple(args, argfmt, &arg))
230 return NULL;
232 if (ioctl(fd, cmd, &arg) == -1)
233 return PyErr_SetFromErrno(PyExc_IOError);
234 return PyInt_FromLong(arg);
238 /* _do_ioctl_1_internal() is a wrapper for ioctls that take no inputs
239 but return an output -- ie. we need to pass a pointer to a local C
240 variable so the driver can write its output there, but from Python
241 all we see is the return value. For example,
242 SOUND_MIXER_READ_DEVMASK returns a bitmask of available mixer
243 devices, but does not use the value of the parameter passed-in in any
244 way.
246 static PyObject *
247 _do_ioctl_1_internal(int fd, PyObject *args, char *fname, int cmd)
249 char argfmt[32] = ":";
250 int arg = 0;
252 assert(strlen(fname) <= 30);
253 strcat(argfmt, fname);
254 if (!PyArg_ParseTuple(args, argfmt, &arg))
255 return NULL;
257 if (ioctl(fd, cmd, &arg) == -1)
258 return PyErr_SetFromErrno(PyExc_IOError);
259 return PyInt_FromLong(arg);
264 /* _do_ioctl_0() is a private helper for the no-argument ioctls:
265 SNDCTL_DSP_{SYNC,RESET,POST}. */
266 static PyObject *
267 _do_ioctl_0(int fd, PyObject *args, char *fname, int cmd)
269 char argfmt[32] = ":";
270 int rv;
272 assert(strlen(fname) <= 30);
273 strcat(argfmt, fname);
274 if (!PyArg_ParseTuple(args, argfmt))
275 return NULL;
277 /* According to hannu@opensound.com, all three of the ioctls that
278 use this function can block, so release the GIL. This is
279 especially important for SYNC, which can block for several
280 seconds. */
281 Py_BEGIN_ALLOW_THREADS
282 rv = ioctl(fd, cmd, 0);
283 Py_END_ALLOW_THREADS
285 if (rv == -1)
286 return PyErr_SetFromErrno(PyExc_IOError);
287 Py_INCREF(Py_None);
288 return Py_None;
292 /* ----------------------------------------------------------------------
293 * Methods of DSP objects (OSSAudioType)
296 static PyObject *
297 oss_nonblock(oss_audio_t *self, PyObject *args)
299 /* Hmmm: it doesn't appear to be possible to return to blocking
300 mode once we're in non-blocking mode! */
301 if (!PyArg_ParseTuple(args, ":nonblock"))
302 return NULL;
303 if (ioctl(self->fd, SNDCTL_DSP_NONBLOCK, NULL) == -1)
304 return PyErr_SetFromErrno(PyExc_IOError);
305 Py_INCREF(Py_None);
306 return Py_None;
309 static PyObject *
310 oss_setfmt(oss_audio_t *self, PyObject *args)
312 return _do_ioctl_1(self->fd, args, "setfmt", SNDCTL_DSP_SETFMT);
315 static PyObject *
316 oss_getfmts(oss_audio_t *self, PyObject *args)
318 int mask;
319 if (!PyArg_ParseTuple(args, ":getfmts"))
320 return NULL;
321 if (ioctl(self->fd, SNDCTL_DSP_GETFMTS, &mask) == -1)
322 return PyErr_SetFromErrno(PyExc_IOError);
323 return PyInt_FromLong(mask);
326 static PyObject *
327 oss_channels(oss_audio_t *self, PyObject *args)
329 return _do_ioctl_1(self->fd, args, "channels", SNDCTL_DSP_CHANNELS);
332 static PyObject *
333 oss_speed(oss_audio_t *self, PyObject *args)
335 return _do_ioctl_1(self->fd, args, "speed", SNDCTL_DSP_SPEED);
338 static PyObject *
339 oss_sync(oss_audio_t *self, PyObject *args)
341 return _do_ioctl_0(self->fd, args, "sync", SNDCTL_DSP_SYNC);
344 static PyObject *
345 oss_reset(oss_audio_t *self, PyObject *args)
347 return _do_ioctl_0(self->fd, args, "reset", SNDCTL_DSP_RESET);
350 static PyObject *
351 oss_post(oss_audio_t *self, PyObject *args)
353 return _do_ioctl_0(self->fd, args, "post", SNDCTL_DSP_POST);
357 /* Regular file methods: read(), write(), close(), etc. as well
358 as one convenience method, writeall(). */
360 static PyObject *
361 oss_read(oss_audio_t *self, PyObject *args)
363 int size, count;
364 char *cp;
365 PyObject *rv;
367 if (!PyArg_ParseTuple(args, "i:read", &size))
368 return NULL;
369 rv = PyString_FromStringAndSize(NULL, size);
370 if (rv == NULL)
371 return NULL;
372 cp = PyString_AS_STRING(rv);
374 Py_BEGIN_ALLOW_THREADS
375 count = read(self->fd, cp, size);
376 Py_END_ALLOW_THREADS
378 if (count < 0) {
379 PyErr_SetFromErrno(PyExc_IOError);
380 Py_DECREF(rv);
381 return NULL;
383 self->icount += count;
384 _PyString_Resize(&rv, count);
385 return rv;
388 static PyObject *
389 oss_write(oss_audio_t *self, PyObject *args)
391 char *cp;
392 int rv, size;
394 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size)) {
395 return NULL;
398 Py_BEGIN_ALLOW_THREADS
399 rv = write(self->fd, cp, size);
400 Py_END_ALLOW_THREADS
402 if (rv == -1) {
403 return PyErr_SetFromErrno(PyExc_IOError);
404 } else {
405 self->ocount += rv;
407 return PyInt_FromLong(rv);
410 static PyObject *
411 oss_writeall(oss_audio_t *self, PyObject *args)
413 char *cp;
414 int rv, size;
415 fd_set write_set_fds;
416 int select_rv;
418 /* NB. writeall() is only useful in non-blocking mode: according to
419 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
420 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
421 write() in blocking mode consumes the whole buffer. In blocking
422 mode, the behaviour of write() and writeall() from Python is
423 indistinguishable. */
425 if (!PyArg_ParseTuple(args, "s#:write", &cp, &size))
426 return NULL;
428 /* use select to wait for audio device to be available */
429 FD_ZERO(&write_set_fds);
430 FD_SET(self->fd, &write_set_fds);
432 while (size > 0) {
433 Py_BEGIN_ALLOW_THREADS
434 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
435 Py_END_ALLOW_THREADS
436 assert(select_rv != 0); /* no timeout, can't expire */
437 if (select_rv == -1)
438 return PyErr_SetFromErrno(PyExc_IOError);
440 Py_BEGIN_ALLOW_THREADS
441 rv = write(self->fd, cp, size);
442 Py_END_ALLOW_THREADS
443 if (rv == -1) {
444 if (errno == EAGAIN) { /* buffer is full, try again */
445 errno = 0;
446 continue;
447 } else /* it's a real error */
448 return PyErr_SetFromErrno(PyExc_IOError);
449 } else { /* wrote rv bytes */
450 self->ocount += rv;
451 size -= rv;
452 cp += rv;
455 Py_INCREF(Py_None);
456 return Py_None;
459 static PyObject *
460 oss_close(oss_audio_t *self, PyObject *args)
462 if (!PyArg_ParseTuple(args, ":close"))
463 return NULL;
465 if (self->fd >= 0) {
466 Py_BEGIN_ALLOW_THREADS
467 close(self->fd);
468 Py_END_ALLOW_THREADS
469 self->fd = -1;
471 Py_INCREF(Py_None);
472 return Py_None;
475 static PyObject *
476 oss_fileno(oss_audio_t *self, PyObject *args)
478 if (!PyArg_ParseTuple(args, ":fileno"))
479 return NULL;
480 return PyInt_FromLong(self->fd);
484 /* Convenience methods: these generally wrap a couple of ioctls into one
485 common task. */
487 static PyObject *
488 oss_setparameters(oss_audio_t *self, PyObject *args)
490 int wanted_fmt, wanted_channels, wanted_rate, strict=0;
491 int fmt, channels, rate;
492 PyObject * rv; /* return tuple (fmt, channels, rate) */
494 if (!PyArg_ParseTuple(args, "iii|i:setparameters",
495 &wanted_fmt, &wanted_channels, &wanted_rate,
496 &strict))
497 return NULL;
499 fmt = wanted_fmt;
500 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) == -1) {
501 return PyErr_SetFromErrno(PyExc_IOError);
503 if (strict && fmt != wanted_fmt) {
504 return PyErr_Format
505 (OSSAudioError,
506 "unable to set requested format (wanted %d, got %d)",
507 wanted_fmt, fmt);
510 channels = wanted_channels;
511 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
512 return PyErr_SetFromErrno(PyExc_IOError);
514 if (strict && channels != wanted_channels) {
515 return PyErr_Format
516 (OSSAudioError,
517 "unable to set requested channels (wanted %d, got %d)",
518 wanted_channels, channels);
521 rate = wanted_rate;
522 if (ioctl(self->fd, SNDCTL_DSP_SPEED, &rate) == -1) {
523 return PyErr_SetFromErrno(PyExc_IOError);
525 if (strict && rate != wanted_rate) {
526 return PyErr_Format
527 (OSSAudioError,
528 "unable to set requested rate (wanted %d, got %d)",
529 wanted_rate, rate);
532 /* Construct the return value: a (fmt, channels, rate) tuple that
533 tells what the audio hardware was actually set to. */
534 rv = PyTuple_New(3);
535 if (rv == NULL)
536 return NULL;
537 PyTuple_SET_ITEM(rv, 0, PyInt_FromLong(fmt));
538 PyTuple_SET_ITEM(rv, 1, PyInt_FromLong(channels));
539 PyTuple_SET_ITEM(rv, 2, PyInt_FromLong(rate));
540 return rv;
543 static int
544 _ssize(oss_audio_t *self, int *nchannels, int *ssize)
546 int fmt;
548 fmt = 0;
549 if (ioctl(self->fd, SNDCTL_DSP_SETFMT, &fmt) < 0)
550 return -errno;
552 switch (fmt) {
553 case AFMT_MU_LAW:
554 case AFMT_A_LAW:
555 case AFMT_U8:
556 case AFMT_S8:
557 *ssize = 1; /* 8 bit formats: 1 byte */
558 break;
559 case AFMT_S16_LE:
560 case AFMT_S16_BE:
561 case AFMT_U16_LE:
562 case AFMT_U16_BE:
563 *ssize = 2; /* 16 bit formats: 2 byte */
564 break;
565 case AFMT_MPEG:
566 case AFMT_IMA_ADPCM:
567 default:
568 return -EOPNOTSUPP;
570 *nchannels = 0;
571 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
572 return -errno;
573 return 0;
577 /* bufsize returns the size of the hardware audio buffer in number
578 of samples */
579 static PyObject *
580 oss_bufsize(oss_audio_t *self, PyObject *args)
582 audio_buf_info ai;
583 int nchannels, ssize;
585 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
587 if (_ssize(self, &nchannels, &ssize) < 0) {
588 PyErr_SetFromErrno(PyExc_IOError);
589 return NULL;
591 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
592 PyErr_SetFromErrno(PyExc_IOError);
593 return NULL;
595 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
598 /* obufcount returns the number of samples that are available in the
599 hardware for playing */
600 static PyObject *
601 oss_obufcount(oss_audio_t *self, PyObject *args)
603 audio_buf_info ai;
604 int nchannels, ssize;
606 if (!PyArg_ParseTuple(args, ":obufcount"))
607 return NULL;
609 if (_ssize(self, &nchannels, &ssize) < 0) {
610 PyErr_SetFromErrno(PyExc_IOError);
611 return NULL;
613 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
614 PyErr_SetFromErrno(PyExc_IOError);
615 return NULL;
617 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
618 (ssize * nchannels));
621 /* obufcount returns the number of samples that can be played without
622 blocking */
623 static PyObject *
624 oss_obuffree(oss_audio_t *self, PyObject *args)
626 audio_buf_info ai;
627 int nchannels, ssize;
629 if (!PyArg_ParseTuple(args, ":obuffree"))
630 return NULL;
632 if (_ssize(self, &nchannels, &ssize) < 0) {
633 PyErr_SetFromErrno(PyExc_IOError);
634 return NULL;
636 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
637 PyErr_SetFromErrno(PyExc_IOError);
638 return NULL;
640 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
643 static PyObject *
644 oss_getptr(oss_audio_t *self, PyObject *args)
646 count_info info;
647 int req;
649 if (!PyArg_ParseTuple(args, ":getptr"))
650 return NULL;
652 if (self->mode == O_RDONLY)
653 req = SNDCTL_DSP_GETIPTR;
654 else
655 req = SNDCTL_DSP_GETOPTR;
656 if (ioctl(self->fd, req, &info) == -1) {
657 PyErr_SetFromErrno(PyExc_IOError);
658 return NULL;
660 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
664 /* ----------------------------------------------------------------------
665 * Methods of mixer objects (OSSMixerType)
668 static PyObject *
669 oss_mixer_close(oss_mixer_t *self, PyObject *args)
671 if (!PyArg_ParseTuple(args, ":close"))
672 return NULL;
674 if (self->fd >= 0) {
675 close(self->fd);
676 self->fd = -1;
678 Py_INCREF(Py_None);
679 return Py_None;
682 static PyObject *
683 oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
685 if (!PyArg_ParseTuple(args, ":fileno"))
686 return NULL;
687 return PyInt_FromLong(self->fd);
690 /* Simple mixer interface methods */
692 static PyObject *
693 oss_mixer_controls(oss_mixer_t *self, PyObject *args)
695 return _do_ioctl_1_internal(self->fd, args, "controls",
696 SOUND_MIXER_READ_DEVMASK);
699 static PyObject *
700 oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
702 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
703 SOUND_MIXER_READ_STEREODEVS);
706 static PyObject *
707 oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
709 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
710 SOUND_MIXER_READ_RECMASK);
713 static PyObject *
714 oss_mixer_get(oss_mixer_t *self, PyObject *args)
716 int channel, volume;
718 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
719 if (!PyArg_ParseTuple(args, "i:get", &channel))
720 return NULL;
722 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
723 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
724 return NULL;
727 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
728 return PyErr_SetFromErrno(PyExc_IOError);
730 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
733 static PyObject *
734 oss_mixer_set(oss_mixer_t *self, PyObject *args)
736 int channel, volume, leftVol, rightVol;
738 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
739 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
740 return NULL;
742 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
743 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
744 return NULL;
747 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
748 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
749 return NULL;
752 volume = (rightVol << 8) | leftVol;
754 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
755 return PyErr_SetFromErrno(PyExc_IOError);
757 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
760 static PyObject *
761 oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
763 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
764 SOUND_MIXER_READ_RECSRC);
767 static PyObject *
768 oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
770 return _do_ioctl_1(self->fd, args, "set_recsrc",
771 SOUND_MIXER_WRITE_RECSRC);
775 /* ----------------------------------------------------------------------
776 * Method tables and other bureaucracy
779 static PyMethodDef oss_methods[] = {
780 /* Regular file methods */
781 { "read", (PyCFunction)oss_read, METH_VARARGS },
782 { "write", (PyCFunction)oss_write, METH_VARARGS },
783 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
784 { "close", (PyCFunction)oss_close, METH_VARARGS },
785 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
787 /* Simple ioctl wrappers */
788 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
789 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
790 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
791 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
792 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
793 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
794 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
795 { "post", (PyCFunction)oss_post, METH_VARARGS },
797 /* Convenience methods -- wrap a couple of ioctls together */
798 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
799 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
800 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
801 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
802 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
804 /* Aliases for backwards compatibility */
805 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
807 { NULL, NULL} /* sentinel */
810 static PyMethodDef oss_mixer_methods[] = {
811 /* Regular file method - OSS mixers are ioctl-only interface */
812 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
813 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
815 /* Simple ioctl wrappers */
816 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
817 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
818 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
819 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
820 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
821 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
822 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
824 { NULL, NULL}
827 static PyObject *
828 oss_getattr(oss_audio_t *self, char *name)
830 return Py_FindMethod(oss_methods, (PyObject *)self, name);
833 static PyObject *
834 oss_mixer_getattr(oss_mixer_t *self, char *name)
836 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
839 static PyTypeObject OSSAudioType = {
840 PyObject_HEAD_INIT(&PyType_Type)
841 0, /*ob_size*/
842 "ossaudiodev.oss_audio_device", /*tp_name*/
843 sizeof(oss_audio_t), /*tp_size*/
844 0, /*tp_itemsize*/
845 /* methods */
846 (destructor)oss_dealloc, /*tp_dealloc*/
847 0, /*tp_print*/
848 (getattrfunc)oss_getattr, /*tp_getattr*/
849 0, /*tp_setattr*/
850 0, /*tp_compare*/
851 0, /*tp_repr*/
854 static PyTypeObject OSSMixerType = {
855 PyObject_HEAD_INIT(&PyType_Type)
856 0, /*ob_size*/
857 "ossaudiodev.oss_mixer_device", /*tp_name*/
858 sizeof(oss_mixer_t), /*tp_size*/
859 0, /*tp_itemsize*/
860 /* methods */
861 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
862 0, /*tp_print*/
863 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
864 0, /*tp_setattr*/
865 0, /*tp_compare*/
866 0, /*tp_repr*/
870 static PyObject *
871 ossopen(PyObject *self, PyObject *args)
873 return (PyObject *)newossobject(args);
876 static PyObject *
877 ossopenmixer(PyObject *self, PyObject *args)
879 return (PyObject *)newossmixerobject(args);
882 static PyMethodDef ossaudiodev_methods[] = {
883 { "open", ossopen, METH_VARARGS },
884 { "openmixer", ossopenmixer, METH_VARARGS },
885 { 0, 0 },
889 #define _EXPORT_INT(mod, name) \
890 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
893 static char *control_labels[] = SOUND_DEVICE_LABELS;
894 static char *control_names[] = SOUND_DEVICE_NAMES;
897 static int
898 build_namelists (PyObject *module)
900 PyObject *labels;
901 PyObject *names;
902 PyObject *s;
903 int num_controls;
904 int i;
906 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
907 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
909 labels = PyList_New(num_controls);
910 names = PyList_New(num_controls);
911 for (i = 0; i < num_controls; i++) {
912 s = PyString_FromString(control_labels[i]);
913 if (s == NULL)
914 return -1;
915 PyList_SET_ITEM(labels, i, s);
917 s = PyString_FromString(control_names[i]);
918 if (s == NULL)
919 return -1;
920 PyList_SET_ITEM(names, i, s);
923 if (PyModule_AddObject(module, "control_labels", labels) == -1)
924 return -1;
925 if (PyModule_AddObject(module, "control_names", names) == -1)
926 return -1;
928 return 0;
932 void
933 initossaudiodev(void)
935 PyObject *m;
937 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
939 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
940 NULL, NULL);
941 if (OSSAudioError) {
942 /* Each call to PyModule_AddObject decrefs it; compensate: */
943 Py_INCREF(OSSAudioError);
944 Py_INCREF(OSSAudioError);
945 PyModule_AddObject(m, "error", OSSAudioError);
946 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
949 /* Build 'control_labels' and 'control_names' lists and add them
950 to the module. */
951 if (build_namelists(m) == -1) /* XXX what to do here? */
952 return;
954 /* Expose the audio format numbers -- essential! */
955 _EXPORT_INT(m, AFMT_QUERY);
956 _EXPORT_INT(m, AFMT_MU_LAW);
957 _EXPORT_INT(m, AFMT_A_LAW);
958 _EXPORT_INT(m, AFMT_IMA_ADPCM);
959 _EXPORT_INT(m, AFMT_U8);
960 _EXPORT_INT(m, AFMT_S16_LE);
961 _EXPORT_INT(m, AFMT_S16_BE);
962 _EXPORT_INT(m, AFMT_S8);
963 _EXPORT_INT(m, AFMT_U16_LE);
964 _EXPORT_INT(m, AFMT_U16_BE);
965 _EXPORT_INT(m, AFMT_MPEG);
966 #ifdef AFMT_AC3
967 _EXPORT_INT(m, AFMT_AC3);
968 #endif
969 #ifdef AFMT_S16_NE
970 _EXPORT_INT(m, AFMT_S16_NE);
971 #endif
973 /* Expose the sound mixer device numbers. */
974 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
975 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
976 _EXPORT_INT(m, SOUND_MIXER_BASS);
977 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
978 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
979 _EXPORT_INT(m, SOUND_MIXER_PCM);
980 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
981 _EXPORT_INT(m, SOUND_MIXER_LINE);
982 _EXPORT_INT(m, SOUND_MIXER_MIC);
983 _EXPORT_INT(m, SOUND_MIXER_CD);
984 _EXPORT_INT(m, SOUND_MIXER_IMIX);
985 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
986 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
987 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
988 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
989 _EXPORT_INT(m, SOUND_MIXER_LINE1);
990 _EXPORT_INT(m, SOUND_MIXER_LINE2);
991 _EXPORT_INT(m, SOUND_MIXER_LINE3);
992 #ifdef SOUND_MIXER_DIGITAL1
993 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
994 #endif
995 #ifdef SOUND_MIXER_DIGITAL2
996 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
997 #endif
998 #ifdef SOUND_MIXER_DIGITAL3
999 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
1000 #endif
1001 #ifdef SOUND_MIXER_PHONEIN
1002 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
1003 #endif
1004 #ifdef SOUND_MIXER_PHONEOUT
1005 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
1006 #endif
1007 #ifdef SOUND_MIXER_VIDEO
1008 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
1009 #endif
1010 #ifdef SOUND_MIXER_RADIO
1011 _EXPORT_INT(m, SOUND_MIXER_RADIO);
1012 #endif
1013 #ifdef SOUND_MIXER_MONITOR
1014 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
1015 #endif
1017 /* Expose all the ioctl numbers for masochists who like to do this
1018 stuff directly. */
1019 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1020 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1021 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1022 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1023 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1024 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1025 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1026 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1027 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1028 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
1029 #ifdef SNDCTL_DSP_BIND_CHANNEL
1030 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
1031 #endif
1032 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1033 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1034 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
1035 #ifdef SNDCTL_DSP_GETCHANNELMASK
1036 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
1037 #endif
1038 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1039 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1040 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1041 #ifdef SNDCTL_DSP_GETODELAY
1042 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1043 #endif
1044 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1045 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
1046 #ifdef SNDCTL_DSP_GETSPDIF
1047 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
1048 #endif
1049 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1050 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1051 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1052 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1053 _EXPORT_INT(m, SNDCTL_DSP_POST);
1054 #ifdef SNDCTL_DSP_PROFILE
1055 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
1056 #endif
1057 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1058 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1059 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1060 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1061 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
1062 #ifdef SNDCTL_DSP_SETSPDIF
1063 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
1064 #endif
1065 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1066 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1067 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1068 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1069 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1070 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1071 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1072 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1073 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1074 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1075 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1076 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1077 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1078 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1079 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
1080 #ifdef SNDCTL_SEQ_GETTIME
1081 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
1082 #endif
1083 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1084 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1085 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1086 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1087 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1088 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1089 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1090 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1091 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1092 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
1093 #ifdef SNDCTL_SYNTH_CONTROL
1094 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
1095 #endif
1096 #ifdef SNDCTL_SYNTH_ID
1097 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
1098 #endif
1099 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1100 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
1101 #ifdef SNDCTL_SYNTH_REMOVESAMPLE
1102 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
1103 #endif
1104 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1105 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1106 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1107 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1108 _EXPORT_INT(m, SNDCTL_TMR_START);
1109 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1110 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1111 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);