Fix #1474677, non-keyword argument following keyword.
[python.git] / Modules / ossaudiodev.c
blob563620cbea3f993c11ce33f19d2ec8c2a3285e81
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 if (ioctl(self->fd, SNDCTL_DSP_CHANNELS, nchannels) < 0)
573 return -errno;
574 return 0;
578 /* bufsize returns the size of the hardware audio buffer in number
579 of samples */
580 static PyObject *
581 oss_bufsize(oss_audio_t *self, PyObject *args)
583 audio_buf_info ai;
584 int nchannels=0, ssize=0;
586 if (!PyArg_ParseTuple(args, ":bufsize")) return NULL;
588 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
589 PyErr_SetFromErrno(PyExc_IOError);
590 return NULL;
592 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
593 PyErr_SetFromErrno(PyExc_IOError);
594 return NULL;
596 return PyInt_FromLong((ai.fragstotal * ai.fragsize) / (nchannels * ssize));
599 /* obufcount returns the number of samples that are available in the
600 hardware for playing */
601 static PyObject *
602 oss_obufcount(oss_audio_t *self, PyObject *args)
604 audio_buf_info ai;
605 int nchannels=0, ssize=0;
607 if (!PyArg_ParseTuple(args, ":obufcount"))
608 return NULL;
610 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
611 PyErr_SetFromErrno(PyExc_IOError);
612 return NULL;
614 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
615 PyErr_SetFromErrno(PyExc_IOError);
616 return NULL;
618 return PyInt_FromLong((ai.fragstotal * ai.fragsize - ai.bytes) /
619 (ssize * nchannels));
622 /* obufcount returns the number of samples that can be played without
623 blocking */
624 static PyObject *
625 oss_obuffree(oss_audio_t *self, PyObject *args)
627 audio_buf_info ai;
628 int nchannels=0, ssize=0;
630 if (!PyArg_ParseTuple(args, ":obuffree"))
631 return NULL;
633 if (_ssize(self, &nchannels, &ssize) < 0 || !nchannels || !ssize) {
634 PyErr_SetFromErrno(PyExc_IOError);
635 return NULL;
637 if (ioctl(self->fd, SNDCTL_DSP_GETOSPACE, &ai) < 0) {
638 PyErr_SetFromErrno(PyExc_IOError);
639 return NULL;
641 return PyInt_FromLong(ai.bytes / (ssize * nchannels));
644 static PyObject *
645 oss_getptr(oss_audio_t *self, PyObject *args)
647 count_info info;
648 int req;
650 if (!PyArg_ParseTuple(args, ":getptr"))
651 return NULL;
653 if (self->mode == O_RDONLY)
654 req = SNDCTL_DSP_GETIPTR;
655 else
656 req = SNDCTL_DSP_GETOPTR;
657 if (ioctl(self->fd, req, &info) == -1) {
658 PyErr_SetFromErrno(PyExc_IOError);
659 return NULL;
661 return Py_BuildValue("iii", info.bytes, info.blocks, info.ptr);
665 /* ----------------------------------------------------------------------
666 * Methods of mixer objects (OSSMixerType)
669 static PyObject *
670 oss_mixer_close(oss_mixer_t *self, PyObject *args)
672 if (!PyArg_ParseTuple(args, ":close"))
673 return NULL;
675 if (self->fd >= 0) {
676 close(self->fd);
677 self->fd = -1;
679 Py_INCREF(Py_None);
680 return Py_None;
683 static PyObject *
684 oss_mixer_fileno(oss_mixer_t *self, PyObject *args)
686 if (!PyArg_ParseTuple(args, ":fileno"))
687 return NULL;
688 return PyInt_FromLong(self->fd);
691 /* Simple mixer interface methods */
693 static PyObject *
694 oss_mixer_controls(oss_mixer_t *self, PyObject *args)
696 return _do_ioctl_1_internal(self->fd, args, "controls",
697 SOUND_MIXER_READ_DEVMASK);
700 static PyObject *
701 oss_mixer_stereocontrols(oss_mixer_t *self, PyObject *args)
703 return _do_ioctl_1_internal(self->fd, args, "stereocontrols",
704 SOUND_MIXER_READ_STEREODEVS);
707 static PyObject *
708 oss_mixer_reccontrols(oss_mixer_t *self, PyObject *args)
710 return _do_ioctl_1_internal(self->fd, args, "reccontrols",
711 SOUND_MIXER_READ_RECMASK);
714 static PyObject *
715 oss_mixer_get(oss_mixer_t *self, PyObject *args)
717 int channel, volume;
719 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
720 if (!PyArg_ParseTuple(args, "i:get", &channel))
721 return NULL;
723 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
724 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
725 return NULL;
728 if (ioctl(self->fd, MIXER_READ(channel), &volume) == -1)
729 return PyErr_SetFromErrno(PyExc_IOError);
731 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
734 static PyObject *
735 oss_mixer_set(oss_mixer_t *self, PyObject *args)
737 int channel, volume, leftVol, rightVol;
739 /* Can't use _do_ioctl_1 because of encoded arg thingy. */
740 if (!PyArg_ParseTuple(args, "i(ii):set", &channel, &leftVol, &rightVol))
741 return NULL;
743 if (channel < 0 || channel > SOUND_MIXER_NRDEVICES) {
744 PyErr_SetString(OSSAudioError, "Invalid mixer channel specified.");
745 return NULL;
748 if (leftVol < 0 || rightVol < 0 || leftVol > 100 || rightVol > 100) {
749 PyErr_SetString(OSSAudioError, "Volumes must be between 0 and 100.");
750 return NULL;
753 volume = (rightVol << 8) | leftVol;
755 if (ioctl(self->fd, MIXER_WRITE(channel), &volume) == -1)
756 return PyErr_SetFromErrno(PyExc_IOError);
758 return Py_BuildValue("(ii)", volume & 0xff, (volume & 0xff00) >> 8);
761 static PyObject *
762 oss_mixer_get_recsrc(oss_mixer_t *self, PyObject *args)
764 return _do_ioctl_1_internal(self->fd, args, "get_recsrc",
765 SOUND_MIXER_READ_RECSRC);
768 static PyObject *
769 oss_mixer_set_recsrc(oss_mixer_t *self, PyObject *args)
771 return _do_ioctl_1(self->fd, args, "set_recsrc",
772 SOUND_MIXER_WRITE_RECSRC);
776 /* ----------------------------------------------------------------------
777 * Method tables and other bureaucracy
780 static PyMethodDef oss_methods[] = {
781 /* Regular file methods */
782 { "read", (PyCFunction)oss_read, METH_VARARGS },
783 { "write", (PyCFunction)oss_write, METH_VARARGS },
784 { "writeall", (PyCFunction)oss_writeall, METH_VARARGS },
785 { "close", (PyCFunction)oss_close, METH_VARARGS },
786 { "fileno", (PyCFunction)oss_fileno, METH_VARARGS },
788 /* Simple ioctl wrappers */
789 { "nonblock", (PyCFunction)oss_nonblock, METH_VARARGS },
790 { "setfmt", (PyCFunction)oss_setfmt, METH_VARARGS },
791 { "getfmts", (PyCFunction)oss_getfmts, METH_VARARGS },
792 { "channels", (PyCFunction)oss_channels, METH_VARARGS },
793 { "speed", (PyCFunction)oss_speed, METH_VARARGS },
794 { "sync", (PyCFunction)oss_sync, METH_VARARGS },
795 { "reset", (PyCFunction)oss_reset, METH_VARARGS },
796 { "post", (PyCFunction)oss_post, METH_VARARGS },
798 /* Convenience methods -- wrap a couple of ioctls together */
799 { "setparameters", (PyCFunction)oss_setparameters, METH_VARARGS },
800 { "bufsize", (PyCFunction)oss_bufsize, METH_VARARGS },
801 { "obufcount", (PyCFunction)oss_obufcount, METH_VARARGS },
802 { "obuffree", (PyCFunction)oss_obuffree, METH_VARARGS },
803 { "getptr", (PyCFunction)oss_getptr, METH_VARARGS },
805 /* Aliases for backwards compatibility */
806 { "flush", (PyCFunction)oss_sync, METH_VARARGS },
808 { NULL, NULL} /* sentinel */
811 static PyMethodDef oss_mixer_methods[] = {
812 /* Regular file method - OSS mixers are ioctl-only interface */
813 { "close", (PyCFunction)oss_mixer_close, METH_VARARGS },
814 { "fileno", (PyCFunction)oss_mixer_fileno, METH_VARARGS },
816 /* Simple ioctl wrappers */
817 { "controls", (PyCFunction)oss_mixer_controls, METH_VARARGS },
818 { "stereocontrols", (PyCFunction)oss_mixer_stereocontrols, METH_VARARGS},
819 { "reccontrols", (PyCFunction)oss_mixer_reccontrols, METH_VARARGS},
820 { "get", (PyCFunction)oss_mixer_get, METH_VARARGS },
821 { "set", (PyCFunction)oss_mixer_set, METH_VARARGS },
822 { "get_recsrc", (PyCFunction)oss_mixer_get_recsrc, METH_VARARGS },
823 { "set_recsrc", (PyCFunction)oss_mixer_set_recsrc, METH_VARARGS },
825 { NULL, NULL}
828 static PyObject *
829 oss_getattr(oss_audio_t *self, char *name)
831 PyObject * rval = NULL;
832 if (strcmp(name, "closed") == 0) {
833 rval = (self->fd == -1) ? Py_True : Py_False;
834 Py_INCREF(rval);
836 else if (strcmp(name, "name") == 0) {
837 rval = PyString_FromString(self->devicename);
839 else if (strcmp(name, "mode") == 0) {
840 /* No need for a "default" in this switch: from newossobject(),
841 self->mode can only be one of these three values. */
842 switch(self->mode) {
843 case O_RDONLY:
844 rval = PyString_FromString("r");
845 break;
846 case O_RDWR:
847 rval = PyString_FromString("rw");
848 break;
849 case O_WRONLY:
850 rval = PyString_FromString("w");
851 break;
854 else {
855 rval = Py_FindMethod(oss_methods, (PyObject *)self, name);
857 return rval;
860 static PyObject *
861 oss_mixer_getattr(oss_mixer_t *self, char *name)
863 return Py_FindMethod(oss_mixer_methods, (PyObject *)self, name);
866 static PyTypeObject OSSAudioType = {
867 PyObject_HEAD_INIT(&PyType_Type)
868 0, /*ob_size*/
869 "ossaudiodev.oss_audio_device", /*tp_name*/
870 sizeof(oss_audio_t), /*tp_size*/
871 0, /*tp_itemsize*/
872 /* methods */
873 (destructor)oss_dealloc, /*tp_dealloc*/
874 0, /*tp_print*/
875 (getattrfunc)oss_getattr, /*tp_getattr*/
876 0, /*tp_setattr*/
877 0, /*tp_compare*/
878 0, /*tp_repr*/
881 static PyTypeObject OSSMixerType = {
882 PyObject_HEAD_INIT(&PyType_Type)
883 0, /*ob_size*/
884 "ossaudiodev.oss_mixer_device", /*tp_name*/
885 sizeof(oss_mixer_t), /*tp_size*/
886 0, /*tp_itemsize*/
887 /* methods */
888 (destructor)oss_mixer_dealloc, /*tp_dealloc*/
889 0, /*tp_print*/
890 (getattrfunc)oss_mixer_getattr, /*tp_getattr*/
891 0, /*tp_setattr*/
892 0, /*tp_compare*/
893 0, /*tp_repr*/
897 static PyObject *
898 ossopen(PyObject *self, PyObject *args)
900 return (PyObject *)newossobject(args);
903 static PyObject *
904 ossopenmixer(PyObject *self, PyObject *args)
906 return (PyObject *)newossmixerobject(args);
909 static PyMethodDef ossaudiodev_methods[] = {
910 { "open", ossopen, METH_VARARGS },
911 { "openmixer", ossopenmixer, METH_VARARGS },
912 { 0, 0 },
916 #define _EXPORT_INT(mod, name) \
917 if (PyModule_AddIntConstant(mod, #name, (long) (name)) == -1) return;
920 static char *control_labels[] = SOUND_DEVICE_LABELS;
921 static char *control_names[] = SOUND_DEVICE_NAMES;
924 static int
925 build_namelists (PyObject *module)
927 PyObject *labels;
928 PyObject *names;
929 PyObject *s;
930 int num_controls;
931 int i;
933 num_controls = sizeof(control_labels) / sizeof(control_labels[0]);
934 assert(num_controls == sizeof(control_names) / sizeof(control_names[0]));
936 labels = PyList_New(num_controls);
937 names = PyList_New(num_controls);
938 if (labels == NULL || names == NULL)
939 goto error2;
940 for (i = 0; i < num_controls; i++) {
941 s = PyString_FromString(control_labels[i]);
942 if (s == NULL)
943 goto error2;
944 PyList_SET_ITEM(labels, i, s);
946 s = PyString_FromString(control_names[i]);
947 if (s == NULL)
948 goto error2;
949 PyList_SET_ITEM(names, i, s);
952 if (PyModule_AddObject(module, "control_labels", labels) == -1)
953 goto error2;
954 if (PyModule_AddObject(module, "control_names", names) == -1)
955 goto error1;
957 return 0;
959 error2:
960 Py_XDECREF(labels);
961 error1:
962 Py_XDECREF(names);
963 return -1;
967 void
968 initossaudiodev(void)
970 PyObject *m;
972 m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
973 if (m == NULL)
974 return;
976 OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
977 NULL, NULL);
978 if (OSSAudioError) {
979 /* Each call to PyModule_AddObject decrefs it; compensate: */
980 Py_INCREF(OSSAudioError);
981 Py_INCREF(OSSAudioError);
982 PyModule_AddObject(m, "error", OSSAudioError);
983 PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
986 /* Build 'control_labels' and 'control_names' lists and add them
987 to the module. */
988 if (build_namelists(m) == -1) /* XXX what to do here? */
989 return;
991 /* Expose the audio format numbers -- essential! */
992 _EXPORT_INT(m, AFMT_QUERY);
993 _EXPORT_INT(m, AFMT_MU_LAW);
994 _EXPORT_INT(m, AFMT_A_LAW);
995 _EXPORT_INT(m, AFMT_IMA_ADPCM);
996 _EXPORT_INT(m, AFMT_U8);
997 _EXPORT_INT(m, AFMT_S16_LE);
998 _EXPORT_INT(m, AFMT_S16_BE);
999 _EXPORT_INT(m, AFMT_S8);
1000 _EXPORT_INT(m, AFMT_U16_LE);
1001 _EXPORT_INT(m, AFMT_U16_BE);
1002 _EXPORT_INT(m, AFMT_MPEG);
1003 #ifdef AFMT_AC3
1004 _EXPORT_INT(m, AFMT_AC3);
1005 #endif
1006 #ifdef AFMT_S16_NE
1007 _EXPORT_INT(m, AFMT_S16_NE);
1008 #endif
1009 #ifdef AFMT_U16_NE
1010 _EXPORT_INT(m, AFMT_U16_NE);
1011 #endif
1012 #ifdef AFMT_S32_LE
1013 _EXPORT_INT(m, AFMT_S32_LE);
1014 #endif
1015 #ifdef AFMT_S32_BE
1016 _EXPORT_INT(m, AFMT_S32_BE);
1017 #endif
1018 #ifdef AFMT_MPEG
1019 _EXPORT_INT(m, AFMT_MPEG);
1020 #endif
1022 /* Expose the sound mixer device numbers. */
1023 _EXPORT_INT(m, SOUND_MIXER_NRDEVICES);
1024 _EXPORT_INT(m, SOUND_MIXER_VOLUME);
1025 _EXPORT_INT(m, SOUND_MIXER_BASS);
1026 _EXPORT_INT(m, SOUND_MIXER_TREBLE);
1027 _EXPORT_INT(m, SOUND_MIXER_SYNTH);
1028 _EXPORT_INT(m, SOUND_MIXER_PCM);
1029 _EXPORT_INT(m, SOUND_MIXER_SPEAKER);
1030 _EXPORT_INT(m, SOUND_MIXER_LINE);
1031 _EXPORT_INT(m, SOUND_MIXER_MIC);
1032 _EXPORT_INT(m, SOUND_MIXER_CD);
1033 _EXPORT_INT(m, SOUND_MIXER_IMIX);
1034 _EXPORT_INT(m, SOUND_MIXER_ALTPCM);
1035 _EXPORT_INT(m, SOUND_MIXER_RECLEV);
1036 _EXPORT_INT(m, SOUND_MIXER_IGAIN);
1037 _EXPORT_INT(m, SOUND_MIXER_OGAIN);
1038 _EXPORT_INT(m, SOUND_MIXER_LINE1);
1039 _EXPORT_INT(m, SOUND_MIXER_LINE2);
1040 _EXPORT_INT(m, SOUND_MIXER_LINE3);
1041 #ifdef SOUND_MIXER_DIGITAL1
1042 _EXPORT_INT(m, SOUND_MIXER_DIGITAL1);
1043 #endif
1044 #ifdef SOUND_MIXER_DIGITAL2
1045 _EXPORT_INT(m, SOUND_MIXER_DIGITAL2);
1046 #endif
1047 #ifdef SOUND_MIXER_DIGITAL3
1048 _EXPORT_INT(m, SOUND_MIXER_DIGITAL3);
1049 #endif
1050 #ifdef SOUND_MIXER_PHONEIN
1051 _EXPORT_INT(m, SOUND_MIXER_PHONEIN);
1052 #endif
1053 #ifdef SOUND_MIXER_PHONEOUT
1054 _EXPORT_INT(m, SOUND_MIXER_PHONEOUT);
1055 #endif
1056 #ifdef SOUND_MIXER_VIDEO
1057 _EXPORT_INT(m, SOUND_MIXER_VIDEO);
1058 #endif
1059 #ifdef SOUND_MIXER_RADIO
1060 _EXPORT_INT(m, SOUND_MIXER_RADIO);
1061 #endif
1062 #ifdef SOUND_MIXER_MONITOR
1063 _EXPORT_INT(m, SOUND_MIXER_MONITOR);
1064 #endif
1066 /* Expose all the ioctl numbers for masochists who like to do this
1067 stuff directly. */
1068 _EXPORT_INT(m, SNDCTL_COPR_HALT);
1069 _EXPORT_INT(m, SNDCTL_COPR_LOAD);
1070 _EXPORT_INT(m, SNDCTL_COPR_RCODE);
1071 _EXPORT_INT(m, SNDCTL_COPR_RCVMSG);
1072 _EXPORT_INT(m, SNDCTL_COPR_RDATA);
1073 _EXPORT_INT(m, SNDCTL_COPR_RESET);
1074 _EXPORT_INT(m, SNDCTL_COPR_RUN);
1075 _EXPORT_INT(m, SNDCTL_COPR_SENDMSG);
1076 _EXPORT_INT(m, SNDCTL_COPR_WCODE);
1077 _EXPORT_INT(m, SNDCTL_COPR_WDATA);
1078 #ifdef SNDCTL_DSP_BIND_CHANNEL
1079 _EXPORT_INT(m, SNDCTL_DSP_BIND_CHANNEL);
1080 #endif
1081 _EXPORT_INT(m, SNDCTL_DSP_CHANNELS);
1082 _EXPORT_INT(m, SNDCTL_DSP_GETBLKSIZE);
1083 _EXPORT_INT(m, SNDCTL_DSP_GETCAPS);
1084 #ifdef SNDCTL_DSP_GETCHANNELMASK
1085 _EXPORT_INT(m, SNDCTL_DSP_GETCHANNELMASK);
1086 #endif
1087 _EXPORT_INT(m, SNDCTL_DSP_GETFMTS);
1088 _EXPORT_INT(m, SNDCTL_DSP_GETIPTR);
1089 _EXPORT_INT(m, SNDCTL_DSP_GETISPACE);
1090 #ifdef SNDCTL_DSP_GETODELAY
1091 _EXPORT_INT(m, SNDCTL_DSP_GETODELAY);
1092 #endif
1093 _EXPORT_INT(m, SNDCTL_DSP_GETOPTR);
1094 _EXPORT_INT(m, SNDCTL_DSP_GETOSPACE);
1095 #ifdef SNDCTL_DSP_GETSPDIF
1096 _EXPORT_INT(m, SNDCTL_DSP_GETSPDIF);
1097 #endif
1098 _EXPORT_INT(m, SNDCTL_DSP_GETTRIGGER);
1099 _EXPORT_INT(m, SNDCTL_DSP_MAPINBUF);
1100 _EXPORT_INT(m, SNDCTL_DSP_MAPOUTBUF);
1101 _EXPORT_INT(m, SNDCTL_DSP_NONBLOCK);
1102 _EXPORT_INT(m, SNDCTL_DSP_POST);
1103 #ifdef SNDCTL_DSP_PROFILE
1104 _EXPORT_INT(m, SNDCTL_DSP_PROFILE);
1105 #endif
1106 _EXPORT_INT(m, SNDCTL_DSP_RESET);
1107 _EXPORT_INT(m, SNDCTL_DSP_SAMPLESIZE);
1108 _EXPORT_INT(m, SNDCTL_DSP_SETDUPLEX);
1109 _EXPORT_INT(m, SNDCTL_DSP_SETFMT);
1110 _EXPORT_INT(m, SNDCTL_DSP_SETFRAGMENT);
1111 #ifdef SNDCTL_DSP_SETSPDIF
1112 _EXPORT_INT(m, SNDCTL_DSP_SETSPDIF);
1113 #endif
1114 _EXPORT_INT(m, SNDCTL_DSP_SETSYNCRO);
1115 _EXPORT_INT(m, SNDCTL_DSP_SETTRIGGER);
1116 _EXPORT_INT(m, SNDCTL_DSP_SPEED);
1117 _EXPORT_INT(m, SNDCTL_DSP_STEREO);
1118 _EXPORT_INT(m, SNDCTL_DSP_SUBDIVIDE);
1119 _EXPORT_INT(m, SNDCTL_DSP_SYNC);
1120 _EXPORT_INT(m, SNDCTL_FM_4OP_ENABLE);
1121 _EXPORT_INT(m, SNDCTL_FM_LOAD_INSTR);
1122 _EXPORT_INT(m, SNDCTL_MIDI_INFO);
1123 _EXPORT_INT(m, SNDCTL_MIDI_MPUCMD);
1124 _EXPORT_INT(m, SNDCTL_MIDI_MPUMODE);
1125 _EXPORT_INT(m, SNDCTL_MIDI_PRETIME);
1126 _EXPORT_INT(m, SNDCTL_SEQ_CTRLRATE);
1127 _EXPORT_INT(m, SNDCTL_SEQ_GETINCOUNT);
1128 _EXPORT_INT(m, SNDCTL_SEQ_GETOUTCOUNT);
1129 #ifdef SNDCTL_SEQ_GETTIME
1130 _EXPORT_INT(m, SNDCTL_SEQ_GETTIME);
1131 #endif
1132 _EXPORT_INT(m, SNDCTL_SEQ_NRMIDIS);
1133 _EXPORT_INT(m, SNDCTL_SEQ_NRSYNTHS);
1134 _EXPORT_INT(m, SNDCTL_SEQ_OUTOFBAND);
1135 _EXPORT_INT(m, SNDCTL_SEQ_PANIC);
1136 _EXPORT_INT(m, SNDCTL_SEQ_PERCMODE);
1137 _EXPORT_INT(m, SNDCTL_SEQ_RESET);
1138 _EXPORT_INT(m, SNDCTL_SEQ_RESETSAMPLES);
1139 _EXPORT_INT(m, SNDCTL_SEQ_SYNC);
1140 _EXPORT_INT(m, SNDCTL_SEQ_TESTMIDI);
1141 _EXPORT_INT(m, SNDCTL_SEQ_THRESHOLD);
1142 #ifdef SNDCTL_SYNTH_CONTROL
1143 _EXPORT_INT(m, SNDCTL_SYNTH_CONTROL);
1144 #endif
1145 #ifdef SNDCTL_SYNTH_ID
1146 _EXPORT_INT(m, SNDCTL_SYNTH_ID);
1147 #endif
1148 _EXPORT_INT(m, SNDCTL_SYNTH_INFO);
1149 _EXPORT_INT(m, SNDCTL_SYNTH_MEMAVL);
1150 #ifdef SNDCTL_SYNTH_REMOVESAMPLE
1151 _EXPORT_INT(m, SNDCTL_SYNTH_REMOVESAMPLE);
1152 #endif
1153 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1154 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1155 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1156 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1157 _EXPORT_INT(m, SNDCTL_TMR_START);
1158 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1159 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1160 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);